Skip to content

Commit

Permalink
✨(backend) allow to filter enrollments by state on admin api
Browse files Browse the repository at this point in the history
The enrollment admin api should allow to filter resources through
their state.
  • Loading branch information
jbpenrath committed Mar 25, 2024
1 parent 5346523 commit 62aa626
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/backend/joanie/core/filters/admin/enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
32 changes: 32 additions & 0 deletions src/backend/joanie/tests/core/test_api_admin_enrollments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/backend/joanie/tests/swagger/admin-swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 62aa626

Please sign in to comment.