Skip to content

Commit de32a56

Browse files
committed
Formatting
1 parent 167800f commit de32a56

File tree

3 files changed

+57
-57
lines changed

3 files changed

+57
-57
lines changed

server/src/testflinger/api/v1.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,29 @@ def job_post(json_data: dict):
102102
@v1.input(schemas.Job, location="json")
103103
@v1.output(schemas.JobId)
104104
def agent_job_post(json_data: dict):
105-
"""Add a child job to the queue with inherited credentials from parent job."""
105+
"""
106+
Add a child job to the queue with inherited credentials
107+
from parent job.
108+
"""
106109
try:
107110
job_queue = json_data.get("job_queue")
108111
parent_job_id = json_data.get("parent_job_id")
109112
except (AttributeError, BadRequest):
110113
job_queue = ""
111114
parent_job_id = None
112-
115+
113116
if not job_queue:
114117
abort(422, message="Invalid data or no job_queue specified")
115-
118+
116119
if not parent_job_id:
117120
abort(422, message="parent_job_id required for agent job submission")
118-
121+
119122
if not check_valid_uuid(parent_job_id):
120123
abort(400, message="Invalid parent_job_id specified")
121-
124+
122125
# Retrieve parent job permissions for credential inheritance
123126
inherited_permissions = database.retrieve_parent_permissions(parent_job_id)
124-
127+
125128
try:
126129
job = job_builder(json_data, inherited_permissions)
127130
except ValueError:
@@ -147,9 +150,10 @@ def has_attachments(data: dict) -> bool:
147150

148151
def job_builder(data: dict, inherited_permissions: dict = None):
149152
"""Build a job from a dictionary of data.
150-
153+
151154
:param data: Job data dictionary
152-
:param inherited_permissions: Optional permissions inherited from parent job
155+
:param inherited_permissions: Optional permissions inherited from
156+
parent job
153157
"""
154158
job = {
155159
"created_at": datetime.now(timezone.utc),
@@ -173,9 +177,13 @@ def job_builder(data: dict, inherited_permissions: dict = None):
173177
data["attachments_status"] = "waiting"
174178

175179
priority_level = data.get("job_priority", 0)
176-
177-
# Use inherited permissions if provided, otherwise use current user's permissions
178-
permissions_to_check = inherited_permissions if inherited_permissions else g.permissions
180+
181+
# Use inherited permissions if provided, otherwise use current
182+
# user's permissions
183+
if inherited_permissions:
184+
permissions_to_check = inherited_permissions
185+
else:
186+
permissions_to_check = g.permissions
179187
auth.check_permissions(
180188
permissions_to_check,
181189
data,

server/src/testflinger/database.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def check_client_exists(client_id: str) -> bool:
441441

442442
def add_client_permissions(data: dict) -> None:
443443
"""Create a new client_id along with its permissions.
444-
:param data: client_id along with its permissions
444+
:param data: client_id along with its permissions.
445445
"""
446446
mongo.db.client_permissions.insert_one(data)
447447

@@ -540,18 +540,19 @@ def delete_refresh_token(token: str) -> None:
540540
"""
541541
mongo.db.refresh_tokens.delete_one({"refresh_token": token})
542542

543+
543544
def retrieve_parent_permissions(parent_job_id: str) -> dict:
544545
"""Retrieve auth permissions from parent job for credential inheritance.
545-
546+
546547
:param parent_job_id: UUID of the parent job to inherit permissions from.
547-
:return: Dictionary with parent job's auth permissions, or empty dict if none.
548+
:return: Dictionary with parent job's auth permissions,
549+
or empty dict if none.
548550
"""
549551
parent_job = mongo.db.jobs.find_one(
550-
{"job_id": parent_job_id},
551-
{"auth_permissions": True, "_id": False}
552+
{"job_id": parent_job_id}, {"auth_permissions": True, "_id": False}
552553
)
553-
554+
554555
if not parent_job:
555556
return {}
556-
557+
557558
return parent_job.get("auth_permissions", {})

server/tests/test_v1_authorization.py

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ def test_role_hierachy_create_permissions(mongo_app_with_permissions):
946946
# Cleanup
947947
mongo.client_permissions.delete_one({"client_id": "test_manager"})
948948

949+
949950
def test_refresh_access_token(mongo_app_with_permissions):
950951
"""Test refreshing an access token with a valid refresh token."""
951952
app, _, client_id, client_key, max_priority = mongo_app_with_permissions
@@ -1232,26 +1233,27 @@ def test_refresh_token_last_accessed_update(mongo_app_with_permissions):
12321233

12331234
assert last_accessed_after > last_accessed_before
12341235

1236+
12351237
def test_retrieve_parent_permissions_valid(mongo_app):
12361238
"""Test retrieving auth permissions from valid parent job."""
12371239
from testflinger import database
1238-
1240+
12391241
app, mongo = mongo_app
12401242
parent_permissions = {
12411243
"client_id": "test_client",
12421244
"max_priority": {"queue1": 100},
12431245
"allowed_queues": ["restricted_queue"],
1244-
"max_reservation_time": {"queue1": 7200}
1246+
"max_reservation_time": {"queue1": 7200},
12451247
}
1246-
1248+
12471249
# Insert parent job with permissions
12481250
parent_job = {
12491251
"job_id": "parent-123",
12501252
"auth_permissions": parent_permissions,
1251-
"job_data": {"job_queue": "queue1"}
1253+
"job_data": {"job_queue": "queue1"},
12521254
}
12531255
mongo.jobs.insert_one(parent_job)
1254-
1256+
12551257
# Test retrieval
12561258
retrieved_permissions = database.retrieve_parent_permissions("parent-123")
12571259
assert retrieved_permissions == parent_permissions
@@ -1260,16 +1262,13 @@ def test_retrieve_parent_permissions_valid(mongo_app):
12601262
def test_retrieve_parent_permissions_no_permissions(mongo_app):
12611263
"""Test retrieving permissions from parent job with no auth_permissions."""
12621264
from testflinger import database
1263-
1265+
12641266
app, mongo = mongo_app
1265-
1267+
12661268
# Insert parent job without permissions
1267-
parent_job = {
1268-
"job_id": "parent-456",
1269-
"job_data": {"job_queue": "queue1"}
1270-
}
1269+
parent_job = {"job_id": "parent-456", "job_data": {"job_queue": "queue1"}}
12711270
mongo.jobs.insert_one(parent_job)
1272-
1271+
12731272
# Should return empty dict, not error
12741273
retrieved_permissions = database.retrieve_parent_permissions("parent-456")
12751274
assert retrieved_permissions == {}
@@ -1278,42 +1277,40 @@ def test_retrieve_parent_permissions_no_permissions(mongo_app):
12781277
def test_retrieve_parent_permissions_nonexistent(mongo_app):
12791278
"""Test retrieving permissions from non-existent parent job."""
12801279
from testflinger import database
1281-
1280+
12821281
app, mongo = mongo_app
1283-
1282+
12841283
# Should return empty dict, not error
1285-
retrieved_permissions = database.retrieve_parent_permissions("nonexistent-job")
1284+
retrieved_permissions = database.retrieve_parent_permissions(
1285+
"nonexistent-job"
1286+
)
12861287
assert retrieved_permissions == {}
12871288

12881289

12891290
def test_agent_jobs_endpoint_with_credentials(mongo_app_with_permissions):
12901291
"""Test agent endpoint submits child job with inherited credentials."""
12911292
app, mongo, client_id, client_key, _ = mongo_app_with_permissions
1292-
1293+
12931294
# Create parent job with permissions
1294-
authenticate_output = app.post(
1295-
"/v1/oauth2/token",
1296-
headers=create_auth_header(client_id, client_key),
1297-
)
1298-
token = authenticate_output.data.decode("utf-8")
1295+
token = get_access_token(app, client_id, client_key)
12991296
parent_job = {"job_queue": "myqueue2", "job_priority": 200}
13001297
parent_response = app.post(
13011298
"/v1/job", json=parent_job, headers={"Authorization": token}
13021299
)
13031300
parent_job_id = parent_response.json.get("job_id")
1304-
1301+
13051302
# Submit child job via agent endpoint
13061303
child_job = {
1307-
"job_queue": "myqueue2",
1304+
"job_queue": "myqueue2",
13081305
"job_priority": 200,
1309-
"parent_job_id": parent_job_id
1306+
"parent_job_id": parent_job_id,
13101307
}
13111308
child_response = app.post("/v1/agent/jobs", json=child_job)
13121309
assert child_response.status_code == 200
1313-
1310+
13141311
child_job_id = child_response.json.get("job_id")
13151312
assert child_job_id is not None
1316-
1313+
13171314
# Verify child job inherited parent permissions
13181315
child_job_data = mongo.jobs.find_one({"job_id": child_job_id})
13191316
assert child_job_data["parent_job_id"] == parent_job_id
@@ -1323,7 +1320,7 @@ def test_agent_jobs_endpoint_with_credentials(mongo_app_with_permissions):
13231320
def test_agent_jobs_endpoint_missing_parent_job_id(mongo_app):
13241321
"""Test agent endpoint rejects jobs without parent_job_id."""
13251322
app, _ = mongo_app
1326-
1323+
13271324
child_job = {"job_queue": "myqueue"}
13281325
response = app.post("/v1/agent/jobs", json=child_job)
13291326
assert response.status_code == 422
@@ -1333,11 +1330,8 @@ def test_agent_jobs_endpoint_missing_parent_job_id(mongo_app):
13331330
def test_agent_jobs_endpoint_invalid_parent_job_id(mongo_app):
13341331
"""Test agent endpoint rejects jobs with invalid parent_job_id."""
13351332
app, _ = mongo_app
1336-
1337-
child_job = {
1338-
"job_queue": "myqueue",
1339-
"parent_job_id": "invalid-uuid"
1340-
}
1333+
1334+
child_job = {"job_queue": "myqueue", "parent_job_id": "invalid-uuid"}
13411335
response = app.post("/v1/agent/jobs", json=child_job)
13421336
assert response.status_code == 400
13431337
assert "Invalid parent_job_id" in response.get_json()["message"]
@@ -1346,20 +1340,17 @@ def test_agent_jobs_endpoint_invalid_parent_job_id(mongo_app):
13461340
def test_agent_jobs_endpoint_no_parent_permissions(mongo_app):
13471341
"""Test agent endpoint works when parent has no permissions."""
13481342
app, mongo = mongo_app
1349-
1343+
13501344
# Create parent job without authentication (no permissions)
13511345
parent_job = {"job_queue": "myqueue"}
13521346
parent_response = app.post("/v1/job", json=parent_job)
13531347
parent_job_id = parent_response.json.get("job_id")
1354-
1348+
13551349
# Submit child job via agent endpoint
1356-
child_job = {
1357-
"job_queue": "myqueue",
1358-
"parent_job_id": parent_job_id
1359-
}
1350+
child_job = {"job_queue": "myqueue", "parent_job_id": parent_job_id}
13601351
child_response = app.post("/v1/agent/jobs", json=child_job)
13611352
assert child_response.status_code == 200
1362-
1353+
13631354
# Verify child job was created successfully
13641355
child_job_id = child_response.json.get("job_id")
13651356
child_job_data = mongo.jobs.find_one({"job_id": child_job_id})

0 commit comments

Comments
 (0)