diff --git a/src/backend/joanie/core/filters/admin/enrollment.py b/src/backend/joanie/core/filters/admin/enrollment.py index b33252e4f..313db1c77 100644 --- a/src/backend/joanie/core/filters/admin/enrollment.py +++ b/src/backend/joanie/core/filters/admin/enrollment.py @@ -37,6 +37,7 @@ class Meta: ) ids = MultipleValueFilter(field_class=fields.UUIDField, field_name="id") is_active = filters.BooleanFilter() + state = filters.ChoiceFilter(choices=enums.ENROLLMENT_STATE_CHOICES) def filter_by_query(self, queryset, _name, value): """ diff --git a/src/backend/joanie/tests/core/test_api_admin_enrollments.py b/src/backend/joanie/tests/core/test_api_admin_enrollments.py index 935d3f7c3..23ace7967 100644 --- a/src/backend/joanie/tests/core/test_api_admin_enrollments.py +++ b/src/backend/joanie/tests/core/test_api_admin_enrollments.py @@ -340,6 +340,38 @@ def test_api_admin_enrollments_filter_by_is_active(self): self.assertEqual(content["count"], 1) self.assertEqual(content["results"][0]["id"], str(active_enrollment.id)) + def test_api_admin_enrollments_filter_by_state(self): + """ + Authenticated admin user should be able to filter all existing enrollments by + their state. + """ + + set_enrollment = factories.EnrollmentFactory.create( + state=enums.ENROLLMENT_STATE_SET + ) + failed_enrollment = factories.EnrollmentFactory.create( + state=enums.ENROLLMENT_STATE_FAILED + ) + + admin = factories.UserFactory(is_staff=True, is_superuser=True) + self.client.login(username=admin.username, password="password") + + response = self.client.get("/api/v1.0/admin/enrollments/") + self.assertEqual(response.status_code, HTTPStatus.OK) + self.assertEqual(response.json()["count"], 2) + + response = self.client.get("/api/v1.0/admin/enrollments/?state=set") + self.assertEqual(response.status_code, HTTPStatus.OK) + content = response.json() + self.assertEqual(content["count"], 1) + self.assertEqual(content["results"][0]["id"], str(set_enrollment.id)) + + response = self.client.get("/api/v1.0/admin/enrollments/?state=failed") + self.assertEqual(response.status_code, HTTPStatus.OK) + content = response.json() + self.assertEqual(content["count"], 1) + self.assertEqual(content["results"][0]["id"], str(failed_enrollment.id)) + def test_api_admin_enrollments_create(self): """Create an enrollment should be not allowed.""" # Create an admin user diff --git a/src/backend/joanie/tests/swagger/admin-swagger.json b/src/backend/joanie/tests/swagger/admin-swagger.json index c4446c5e7..d5fc51245 100644 --- a/src/backend/joanie/tests/swagger/admin-swagger.json +++ b/src/backend/joanie/tests/swagger/admin-swagger.json @@ -2476,6 +2476,18 @@ "type": "string" } }, + { + "in": "query", + "name": "state", + "schema": { + "type": "string", + "enum": [ + "failed", + "set" + ] + }, + "description": "* `set` - Set\n* `failed` - Failed" + }, { "in": "query", "name": "user_ids",