Skip to content

Commit

Permalink
test filtering user list
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Oct 22, 2023
1 parent 2afbbae commit 1a89c87
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 4 additions & 1 deletion codeforlife/tests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pyotp import TOTP
from django.urls import reverse
from django.utils import timezone
from django.utils.http import urlencode
from django.db.models import Model
from django.db.models.query import QuerySet
from rest_framework.test import APITestCase as _APITestCase
Expand All @@ -20,6 +21,7 @@

class APIClient(_APIClient):
StatusCodeAssertion = t.Optional[t.Union[int, t.Callable[[int], bool]]]
ListFilters = t.Optional[t.Dict[str, str]]

@staticmethod
def status_code_is_ok(status_code: int):
Expand Down Expand Up @@ -114,6 +116,7 @@ def list(
models: t.Iterable[AnyModel],
model_serializer_class: t.Type[AnyModelSerializer],
status_code_assertion: StatusCodeAssertion = None,
filters: ListFilters = None,
**kwargs,
):
model_class: t.Type[AnyModel] = model_serializer_class.Meta.model
Expand All @@ -122,7 +125,7 @@ def list(
).exists(), "List must exclude some models for a valid test."

response: Response = self.get(
reverse(f"{basename}-list"),
f"{reverse(f'{basename}-list')}?{urlencode(filters or {})}",
status_code_assertion=status_code_assertion,
**kwargs,
)
Expand Down
4 changes: 2 additions & 2 deletions codeforlife/user/filters/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@


class UserFilterSet(filters.FilterSet):
student__klass = filters.CharFilter(
students_in_class = filters.CharFilter(
"new_student__class_field__access_code", "exact"
)

class Meta:
model = User
fields = ["student__klass"]
fields = ["students_in_class"]
23 changes: 22 additions & 1 deletion codeforlife/user/tests/views/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,24 +328,28 @@ def test_retrieve__indy_student__student(self):

"""
List naming convention:
test_list__{user_type}
test_list__{user_type}__{filters}
user_type: The type of user that is making the request. Options:
- teacher: A teacher.
- student: A school student.
- indy_student: A non-school student.
filters: Any search params used to dynamically filter the list.
"""

def _list_users(
self,
users: t.Iterable[User],
status_code_assertion: APIClient.StatusCodeAssertion = None,
filters: APIClient.ListFilters = None,
):
return self.client.list(
"user",
users,
UserSerializer,
status_code_assertion,
filters,
)

def test_list__teacher(self):
Expand All @@ -362,6 +366,23 @@ def test_list__teacher(self):
)
)

def test_list__teacher__students_in_class(self):
"""
Teacher can list all the users in the same school.
"""

user = self._login_teacher()

access_code = user.teacher.class_teacher.first().access_code
assert access_code

self._list_users(
User.objects.filter(
new_student__class_field__access_code=access_code
),
filters={"students_in_class": access_code},
)

def test_list__student(self):
"""
Student can list only themself.
Expand Down

0 comments on commit 1a89c87

Please sign in to comment.