Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
faucomte97 committed Feb 22, 2024
1 parent d1db279 commit 2517d8b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"model": "common.student",
"pk": 18,
"fields": {
"user": 28,
"new_user": 28,
"pending_class_request": 7
}
}
]
7 changes: 1 addition & 6 deletions backend/api/tests/views/test_klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
© Ocado Group
Created on 05/02/2024 at 16:13:46(+00:00).
"""

from datetime import timedelta

from codeforlife.permissions import OR, AllowNone
Expand Down Expand Up @@ -78,10 +77,7 @@ def test_get_permissions__retrieve(self):
)

def test_create__self(self):
"""
Teacher can create a class with themself as the class owner.
"""

"""Teacher can create a class with themselves as the class owner."""
user = self.client.login_school_teacher(
email="[email protected]",
password="password",
Expand All @@ -100,7 +96,6 @@ def test_create__other(self):
"""
Teacher can create a class with another teacher as the class owner.
"""

user = self.client.login_admin_school_teacher(
email="[email protected]",
password="password",
Expand Down
136 changes: 49 additions & 87 deletions backend/api/tests/views/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@
class TestUserViewSet(ModelViewSetTestCase[User]):
basename = "user"
model_view_set_class = UserViewSet
fixtures = ["independent", "non_school_teacher", "school_1", "school_2"]
fixtures = [
"independent",
"independent_school_1_class_1_join_request",
"non_school_teacher",
"school_1",
"school_2",
]

def setUp(self):
self.non_school_teacher_user = NonSchoolTeacherUser.objects.get(
Expand All @@ -53,9 +59,6 @@ def setUp(self):
email="[email protected]"
)
self.indy_user = IndependentUser.objects.get(email="[email protected]")
self.indy_with_join_request = IndependentUser.objects.get(
email="[email protected]"
)
self.class_2 = Class.objects.get(name="Class 2 @ School 1")

def _get_pk_and_token_for_user(self, email: str):
Expand Down Expand Up @@ -229,8 +232,7 @@ def test_handle_join_class_request__invalid_school(self):
self.client.login_as(self.admin_school2_teacher_user)

viewname = self.reverse_action(
"handle-join-class-request",
kwargs={"pk": self.indy_with_join_request.pk},
"handle-join-class-request", kwargs={"pk": self.indy_user.pk}
)

response = self.client.patch(
Expand All @@ -244,19 +246,15 @@ def test_handle_join_class_request__invalid_school(self):
"This class join request is not in your school."
]

self.indy_with_join_request.refresh_from_db()
assert (
self.indy_with_join_request.new_student.pending_class_request
== self.class_2
)
self.indy_user.refresh_from_db()
assert self.indy_user.new_student.pending_class_request == self.class_2

def test_handle_join_class_request__invalid_class(self):
"""Non-admin teacher cannot reject a join request outside their class"""
self.client.login_as(self.non_admin_school_teacher_user)

viewname = self.reverse_action(
"handle-join-class-request",
kwargs={"pk": self.indy_with_join_request.pk},
"handle-join-class-request", kwargs={"pk": self.indy_user.pk}
)

response = self.client.patch(
Expand All @@ -270,19 +268,15 @@ def test_handle_join_class_request__invalid_class(self):
"This class join request is not for one for your classes."
]

self.indy_with_join_request.refresh_from_db()
assert (
self.indy_with_join_request.new_student.pending_class_request
== self.class_2
)
self.indy_user.refresh_from_db()
assert self.indy_user.new_student.pending_class_request == self.class_2

def test_handle_join_class_request__invalid_accept(self):
"""Teacher cannot handle join class request with wrong accept type"""
self.client.login_as(self.admin_school_teacher_user)

viewname = self.reverse_action(
"handle-join-class-request",
kwargs={"pk": self.indy_with_join_request.pk},
"handle-join-class-request", kwargs={"pk": self.indy_user.pk}
)

response = self.client.patch(
Expand All @@ -296,19 +290,15 @@ def test_handle_join_class_request__invalid_accept(self):
"Invalid type for accept - must be True or False."
]

self.indy_with_join_request.refresh_from_db()
assert (
self.indy_with_join_request.new_student.pending_class_request
== self.class_2
)
self.indy_user.refresh_from_db()
assert self.indy_user.new_student.pending_class_request == self.class_2

def test_handle_join_class_request__missing_accept(self):
"""Teacher cannot handle join class request with missing accept"""
self.client.login_as(self.admin_school_teacher_user)

viewname = self.reverse_action(
"handle-join-class-request",
kwargs={"pk": self.indy_with_join_request.pk},
"handle-join-class-request", kwargs={"pk": self.indy_user.pk}
)

response = self.client.patch(
Expand All @@ -322,19 +312,15 @@ def test_handle_join_class_request__missing_accept(self):
"Accept field is required."
]

self.indy_with_join_request.refresh_from_db()
assert (
self.indy_with_join_request.new_student.pending_class_request
== self.class_2
)
self.indy_user.refresh_from_db()
assert self.indy_user.new_student.pending_class_request == self.class_2

def test_handle_join_class_request__reject(self):
"""Teacher can successfully reject a join class request."""
self.client.login_as(self.admin_school_teacher_user)

viewname = self.reverse_action(
"handle-join-class-request",
kwargs={"pk": self.indy_with_join_request.pk},
"handle-join-class-request", kwargs={"pk": self.indy_user.pk}
)

self.client.patch(
Expand All @@ -344,19 +330,15 @@ def test_handle_join_class_request__reject(self):
format="json",
)

self.indy_with_join_request.refresh_from_db()
assert (
self.indy_with_join_request.new_student.pending_class_request
is None
)
self.indy_user.refresh_from_db()
assert self.indy_user.new_student.pending_class_request is None

def test_handle_join_class_request__accept__invalid_first_name(self):
"""Teacher cannot accept a join class request with invalid name"""
self.client.login_as(self.admin_school_teacher_user)

viewname = self.reverse_action(
"handle-join-class-request",
kwargs={"pk": self.indy_with_join_request.pk},
"handle-join-class-request", kwargs={"pk": self.indy_user.pk}
)

response = self.client.patch(
Expand All @@ -368,19 +350,15 @@ def test_handle_join_class_request__accept__invalid_first_name(self):

assert response.data["first_name"] == ["First name must be a string."]

self.indy_with_join_request.refresh_from_db()
assert (
self.indy_with_join_request.new_student.pending_class_request
== self.class_2
)
self.indy_user.refresh_from_db()
assert self.indy_user.new_student.pending_class_request == self.class_2

def test_handle_join_class_request__accept__missing_first_name(self):
"""Teacher cannot accept a join class request with missing name"""
self.client.login_as(self.admin_school_teacher_user)

viewname = self.reverse_action(
"handle-join-class-request",
kwargs={"pk": self.indy_with_join_request.pk},
"handle-join-class-request", kwargs={"pk": self.indy_user.pk}
)

response = self.client.patch(
Expand All @@ -392,19 +370,15 @@ def test_handle_join_class_request__accept__missing_first_name(self):

assert response.data["first_name"] == ["This field is required."]

self.indy_with_join_request.refresh_from_db()
assert (
self.indy_with_join_request.new_student.pending_class_request
== self.class_2
)
self.indy_user.refresh_from_db()
assert self.indy_user.new_student.pending_class_request == self.class_2

def test_handle_join_class_request__accept__duplicate_first_name(self):
"""Teacher cannot accept a join class request with duplicate name"""
self.client.login_as(self.admin_school_teacher_user)

viewname = self.reverse_action(
"handle-join-class-request",
kwargs={"pk": self.indy_with_join_request.pk},
"handle-join-class-request", kwargs={"pk": self.indy_user.pk}
)

response = self.client.patch(
Expand All @@ -422,45 +396,36 @@ def test_handle_join_class_request__accept__duplicate_first_name(self):
"Please choose a different name."
]

self.indy_with_join_request.refresh_from_db()
assert (
self.indy_with_join_request.new_student.pending_class_request
== self.class_2
)
self.indy_user.refresh_from_db()
assert self.indy_user.new_student.pending_class_request == self.class_2

def test_handle_join_class_request__accept(self):
"""Teacher can successfully accept a join class request."""
self.client.login_as(self.admin_school_teacher_user)

indy_email = self.indy_with_join_request.email
indy_email = self.indy_user.email

viewname = self.reverse_action(
"handle-join-class-request",
kwargs={"pk": self.indy_with_join_request.pk},
"handle-join-class-request", kwargs={"pk": self.indy_user.pk}
)

self.client.patch(
viewname,
data={
"accept": True,
"first_name": self.indy_with_join_request.first_name,
"first_name": self.indy_user.first_name,
},
status_code_assertion=status.HTTP_200_OK,
format="json",
)

self.indy_with_join_request.refresh_from_db()
self.indy_user.refresh_from_db()

assert (
self.indy_with_join_request.new_student.class_field == self.class_2
)
assert (
self.indy_with_join_request.new_student.pending_class_request
is None
)
assert self.indy_with_join_request.last_name == ""
assert self.indy_with_join_request.email == ""
assert self.indy_with_join_request.username != indy_email
assert self.indy_user.new_student.class_field == self.class_2
assert self.indy_user.new_student.pending_class_request is None
assert self.indy_user.last_name == ""
assert self.indy_user.email == ""
assert self.indy_user.username != indy_email

# test: reset password actions

Expand Down Expand Up @@ -571,8 +536,7 @@ def test_reset_password__patch__indy(self):
pk, token = self._get_pk_and_token_for_user(self.indy_user.email)

viewname = self.reverse_action(
"reset-password",
kwargs={"pk": pk, "token": token},
"reset-password", kwargs={"pk": pk, "token": token}
)

self.client.patch(viewname, data={"password": "N3wPassword"})
Expand Down Expand Up @@ -630,8 +594,7 @@ def test_partial_update__teacher(self):

other_school_teacher_user = (
SchoolTeacherUser.objects.filter(
new_teacher__school=self.admin_school_teacher_user.teacher
.school
new_teacher__school=self.admin_school_teacher_user.teacher.school
)
.exclude(pk=self.admin_school_teacher_user.pk)
.first()
Expand Down Expand Up @@ -659,11 +622,10 @@ def test_partial_update__indy__send_join_request(self):

def test_partial_update__indy__revoke_join_request(self):
"""Independent user can revoke their request to join a class."""
self.client.login_as(self.indy_with_join_request)
self.client.login_as(self.indy_user)

self.client.partial_update(
self.indy_with_join_request,
{"requesting_to_join_class": ""},
self.indy_user, {"requesting_to_join_class": ""}
)

def assert_user_is_anonymized(self, user: User):
Expand Down Expand Up @@ -717,7 +679,7 @@ def _test_destroy(
)

def test_destroy__class_teacher(self):
"""Class-teacher-users can anonymize themself and their classes."""
"""Class-teacher-users can anonymize themselves and their classes."""
user = self.non_admin_school_teacher_user
assert user.teacher.class_teacher.exists()
class_names = list(
Expand All @@ -731,8 +693,8 @@ def test_destroy__class_teacher(self):

def test_destroy__school_teacher__last_teacher(self):
"""
School-teacher-users can anonymize themself and their school if they are
the last teacher.
School-teacher-users can anonymize themselves and their school if they
are the last teacher.
"""
user = self.admin_school_teacher_user
assert user.teacher.class_teacher.exists()
Expand All @@ -754,7 +716,7 @@ def test_destroy__school_teacher__last_teacher(self):

def test_destroy__school_teacher__last_admin_teacher(self):
"""
School-teacher-users cannot anonymize themself if they are the last
School-teacher-users cannot anonymize themselves if they are the last
admin teachers.
"""
self._test_destroy(
Expand All @@ -763,7 +725,7 @@ def test_destroy__school_teacher__last_admin_teacher(self):
)

def test_destroy__independent(self):
"""Independent-users can anonymize themself."""
"""Independent-users can anonymize themselves."""
user = self.indy_user
self._test_destroy(user)
user.refresh_from_db()
Expand Down

0 comments on commit 2517d8b

Please sign in to comment.