Skip to content

Commit 45a50b2

Browse files
committed
Fixed more sonarqube errors
1 parent be49805 commit 45a50b2

4 files changed

Lines changed: 20 additions & 32 deletions

File tree

tests/api/test_middleware_auth.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,30 @@ def test_token_not_found(self):
5959
self.assertEqual(res.status_code, 401)
6060

6161
def test_wrong_hash(self):
62-
plaintext, _ = self.get_token(self.user)
62+
plaintext, token = self.get_token(self.user)
6363
wrong_token = token.token_prefix + 'A' * \
6464
(len(plaintext) - len(token.token_prefix))
6565
res = self.client.get('/api/v1/system/queue',
6666
headers={'Authorization': f'Bearer {wrong_token}'})
6767
self.assertEqual(res.status_code, 401)
6868

6969
def test_revoked_token(self):
70-
plaintext, _ = self.get_token(self.user)
70+
plaintext, token = self.get_token(self.user)
7171
token.revoke()
7272
g.db.commit()
7373
res = self.client.get('/api/v1/system/queue',
7474
headers={'Authorization': f'Bearer {plaintext}'})
7575
self.assertEqual(res.status_code, 401)
7676

7777
def test_expired_token(self):
78-
plaintext, _ = self.get_token(self.user, expires_in_days=-1)
78+
plaintext, token = self.get_token(self.user, expires_in_days=-1)
7979
res = self.client.get('/api/v1/system/queue',
8080
headers={'Authorization': f'Bearer {plaintext}'})
8181
self.assertEqual(res.status_code, 401)
8282

8383
def test_valid_token_missing_scope(self):
8484
# /api/v1/system/queue requires 'system:read'
85-
plaintext, _ = self.get_token(self.user, scopes=['runs:read'])
85+
plaintext, token = self.get_token(self.user, scopes=['runs:read'])
8686
res = self.client.get('/api/v1/system/queue',
8787
headers={'Authorization': f'Bearer {plaintext}'})
8888
self.assertEqual(res.status_code, 403)
@@ -91,29 +91,29 @@ def test_valid_token_missing_scope(self):
9191
self.assertIn('missing_scopes', res.json['details'])
9292

9393
def test_valid_token_with_scope(self):
94-
plaintext, _ = self.get_token(self.user, scopes=['system:read'])
94+
plaintext, token = self.get_token(self.user, scopes=['system:read'])
9595
res = self.client.get('/api/v1/system/queue',
9696
headers={'Authorization': f'Bearer {plaintext}'})
9797
self.assertEqual(res.status_code, 200)
9898

9999
def test_role_decorator_missing_role(self):
100100
# GET /api/v1/auth/tokens requires 'tokens:manage' and roles ['admin', 'contributor', 'tester']
101-
plaintext, _ = self.get_token(
101+
plaintext, token = self.get_token(
102102
self.user, scopes=['tokens:manage']) # role is user
103103
res = self.client.get('/api/v1/auth/tokens',
104104
headers={'Authorization': f'Bearer {plaintext}'})
105105
self.assertEqual(res.status_code, 403)
106106
self.assertEqual(res.json['code'], 'forbidden')
107107

108108
def test_role_decorator_with_role(self):
109-
plaintext, _ = self.get_token(
109+
plaintext, token = self.get_token(
110110
self.admin, scopes=['tokens:manage']) # role is admin
111111
res = self.client.get('/api/v1/auth/tokens',
112112
headers={'Authorization': f'Bearer {plaintext}'})
113113
self.assertEqual(res.status_code, 200)
114114

115115
def test_scope_boundary_write_endpoints_fail_on_read_only_scopes(self):
116-
plaintext, _ = self.get_token(
116+
plaintext, token = self.get_token(
117117
self.user, scopes=['runs:read', 'results:read'])
118118

119119
# 1. POST /runs
@@ -164,7 +164,7 @@ def test_multiple_candidates_same_prefix(self):
164164
self.assertEqual(res3.status_code, 401)
165165

166166
def test_auth_sets_g_api_user_and_token(self):
167-
plaintext, _ = self.get_token(self.user, scopes=['system:read'])
167+
plaintext, token = self.get_token(self.user, scopes=['system:read'])
168168
expected_user_id = self.user.id
169169
expected_token_id = token.id
170170
with self.app.test_request_context('/api/v1/system/queue', headers={'Authorization': f'Bearer {plaintext}'}):

tests/api/test_services_error_service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import datetime
2-
from unittest.mock import MagicMock
2+
from unittest.mock import MagicMock, PropertyMock
33

44
from flask import g
55

@@ -167,6 +167,7 @@ def test_get_sample_id(self):
167167
self.assertEqual(_get_sample_id(tr), 42)
168168

169169
# Test exception catching
170-
tr.regression_test = property(lambda self: (
171-
_ for _ in ()).throw(RuntimeError('Mock exception')))
170+
mock_reg = MagicMock()
171+
type(mock_reg).sample_id = PropertyMock(side_effect=RuntimeError('Mock exception'))
172+
tr.regression_test = mock_reg
172173
self.assertIsNone(_get_sample_id(tr))

tests/api/test_services_log_service.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_read_log_lines_basic(self, mock_get_path):
3535
path = self.create_log_file(content)
3636
mock_get_path.return_value = path
3737

38-
lines, _ = read_log_lines(1)
38+
lines, next_cursor = read_log_lines(1)
3939
self.assertEqual(len(lines), 3)
4040
self.assertIsNone(next_cursor)
4141
self.assertEqual(lines[0]['level'], 'info')
@@ -50,13 +50,13 @@ def test_read_log_lines_pagination(self, mock_get_path):
5050
path = self.create_log_file(content)
5151
mock_get_path.return_value = path
5252

53-
lines, _ = read_log_lines(1, limit=2)
53+
lines, next_cursor = read_log_lines(1, limit=2)
5454
self.assertEqual(len(lines), 2)
5555
self.assertEqual(next_cursor, '2')
5656
self.assertEqual(lines[0]['message'], "Line 1")
5757
self.assertEqual(lines[1]['message'], "Line 2")
5858

59-
lines, _ = read_log_lines(1, cursor=next_cursor, limit=2)
59+
lines, next_cursor = read_log_lines(1, cursor=next_cursor, limit=2)
6060
self.assertEqual(len(lines), 2)
6161
self.assertIsNone(next_cursor)
6262
self.assertEqual(lines[0]['message'], "Line 3")
@@ -68,7 +68,7 @@ def test_read_log_lines_limit_clamped(self, mock_get_path):
6868
path = self.create_log_file(content)
6969
mock_get_path.return_value = path
7070

71-
lines, _ = read_log_lines(1, limit=2000)
71+
lines, next_cursor = read_log_lines(1, limit=2000)
7272
# Should be clamped to 500
7373
self.assertEqual(len(lines), 500)
7474

@@ -79,16 +79,16 @@ def test_read_log_lines_filters(self, mock_get_path):
7979
mock_get_path.return_value = path
8080

8181
# Filter by level
82-
lines, _ = read_log_lines(1, level='error')
82+
lines, next_cursor = read_log_lines(1, level='error')
8383
self.assertEqual(len(lines), 1)
8484
self.assertEqual(lines[0]['message'], "ERROR build: Failed")
8585

8686
# Filter by source
87-
lines, _ = read_log_lines(1, source='build')
87+
lines, next_cursor = read_log_lines(1, source='build')
8888
self.assertEqual(len(lines), 2)
8989

9090
# Filter by contains
91-
lines, _ = read_log_lines(1, contains='STARTING')
91+
lines, next_cursor = read_log_lines(1, contains='STARTING')
9292
self.assertEqual(len(lines), 1)
9393

9494
@patch('mod_api.services.log_service.get_log_file_path')

tests/api/verify_schemathesis.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,6 @@
7272
# ---------------------------------------------------------------------------
7373
# Helpers
7474
# ---------------------------------------------------------------------------
75-
76-
77-
URL_AUTH_TOKENS = URL_AUTH_TOKENS
78-
ADMIN_EMAIL = ADMIN_EMAIL
79-
SCOPE_RUNS_READ = SCOPE_RUNS_READ
80-
URL_SYSTEM_QUEUE = URL_SYSTEM_QUEUE
81-
URL_SAMPLES = URL_SAMPLES
82-
URL_RUNS = URL_RUNS
83-
URL_SYSTEM_HEALTH = URL_SYSTEM_HEALTH
84-
APP_JSON = APP_JSON
85-
# noqa: E402
86-
87-
8875
def _suppress_known_failures(exc):
8976
"""Return True if *exc* is a FailureGroup containing only known suppressible types."""
9077
failure_group_cls = getattr(

0 commit comments

Comments
 (0)