Skip to content

Commit f904af4

Browse files
mihowclaude
andcommitted
refactor(test): consolidate role ML job tests into parameterized subTest
Address CodeRabbit review feedback: merge three near-identical role authorization tests into a single table-driven test using subTest. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0e2bb26 commit f904af4

1 file changed

Lines changed: 12 additions & 19 deletions

File tree

ami/main/tests.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,25 +2025,18 @@ def setUp(self):
20252025
def _create_ml_job(self):
20262026
return Job.objects.create(name="Test ML Job", project=self.project, job_type_key="ml")
20272027

2028-
def test_ml_data_manager_can_run_ml_job(self):
2029-
self.client.force_authenticate(self.ml_user)
2030-
job = self._create_ml_job()
2031-
response = self.client.post(f"/api/v2/jobs/{job.pk}/run/", format="json")
2032-
self.assertEqual(response.status_code, status.HTTP_200_OK, "MLDataManager should be able to run ML jobs")
2033-
2034-
def test_project_manager_can_run_ml_job(self):
2035-
self.client.force_authenticate(self.pm_user)
2036-
job = self._create_ml_job()
2037-
response = self.client.post(f"/api/v2/jobs/{job.pk}/run/", format="json")
2038-
self.assertEqual(response.status_code, status.HTTP_200_OK, "ProjectManager should be able to run ML jobs")
2039-
2040-
def test_basic_member_cannot_run_ml_job(self):
2041-
self.client.force_authenticate(self.basic_user)
2042-
job = self._create_ml_job()
2043-
response = self.client.post(f"/api/v2/jobs/{job.pk}/run/", format="json")
2044-
self.assertEqual(
2045-
response.status_code, status.HTTP_403_FORBIDDEN, "BasicMember should NOT be able to run ML jobs"
2046-
)
2028+
def test_role_based_ml_job_run_permissions(self):
2029+
role_matrix = [
2030+
("MLDataManager", self.ml_user, status.HTTP_200_OK),
2031+
("ProjectManager", self.pm_user, status.HTTP_200_OK),
2032+
("BasicMember", self.basic_user, status.HTTP_403_FORBIDDEN),
2033+
]
2034+
for role_name, user, expected_status in role_matrix:
2035+
with self.subTest(role=role_name):
2036+
self.client.force_authenticate(user)
2037+
job = self._create_ml_job()
2038+
response = self.client.post(f"/api/v2/jobs/{job.pk}/run/", format="json")
2039+
self.assertEqual(response.status_code, expected_status, f"{role_name} got unexpected status")
20472040

20482041
def test_ml_data_manager_run_perm_reflected_in_job_detail(self):
20492042
self.client.force_authenticate(self.ml_user)

0 commit comments

Comments
 (0)