Skip to content

Commit

Permalink
feedback: added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 31, 2024
1 parent 5396175 commit 5404604
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 12 deletions.
2 changes: 1 addition & 1 deletion backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ google-cloud-logging = "==1.*"
google-auth = "==2.*"
google-cloud-container = "==2.3.0"
# "django-anymail[amazon_ses]" = "==7.0.*"
codeforlife = {ref = "v0.10.1", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
codeforlife = {ref = "v0.11.2", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
django = "==3.2.20"
djangorestframework = "==3.13.1"
django-cors-headers = "==4.1.0"
Expand Down
10 changes: 5 additions & 5 deletions backend/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions backend/api/fixtures/non_school_teacher.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"model": "auth.user",
"pk": 22,
"fields": {
"first_name": "John",
"last_name": "Doe",
"username": "[email protected]",
"email": "[email protected]",
"password": "pbkdf2_sha256$720000$Jp50WPBA6WZImUIpj3UcVm$OJWB8+UoW5lLaUkHLYo0cKgMkyRI6qnqVOWxYEsi9T0="
}
},
{
"model": "common.userprofile",
"pk": 22,
"fields": {
"user": 22,
"is_verified": true
}
},
{
"model": "common.teacher",
"pk": 5,
"fields": {
"user": 22,
"new_user": 22
}
}
]
112 changes: 112 additions & 0 deletions backend/api/fixtures/school_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
[
{
"model": "common.school",
"pk": 2,
"fields": {
"name": "School 1",
"country": "GB",
"county": "Hertfordshire"
}
},
{
"model": "auth.user",
"pk": 23,
"fields": {
"first_name": "John",
"last_name": "Doe",
"username": "[email protected]",
"email": "[email protected]",
"password": "pbkdf2_sha256$720000$Jp50WPBA6WZImUIpj3UcVm$OJWB8+UoW5lLaUkHLYo0cKgMkyRI6qnqVOWxYEsi9T0="
}
},
{
"model": "common.userprofile",
"pk": 23,
"fields": {
"user": 23,
"is_verified": true
}
},
{
"model": "common.teacher",
"pk": 6,
"fields": {
"user": 23,
"new_user": 23,
"school": 2
}
},
{
"model": "common.class",
"pk": 6,
"fields": {
"name": "Class 1 @ School 1",
"access_code": "ZZ111",
"teacher": 6
}
},
{
"model": "auth.user",
"pk": 27,
"fields": {
"first_name": "Student1",
"username": "111111111111111111111111111111",
"password": "pbkdf2_sha256$720000$Jp50WPBA6WZImUIpj3UcVm$OJWB8+UoW5lLaUkHLYo0cKgMkyRI6qnqVOWxYEsi9T0="
}
},
{
"model": "common.userprofile",
"pk": 27,
"fields": {
"user": 27,
"is_verified": true
}
},
{
"model": "common.student",
"pk": 17,
"fields": {
"user": 27,
"new_user": 27,
"class_field": 6
}
},
{
"model": "auth.user",
"pk": 24,
"fields": {
"first_name": "Jane",
"last_name": "Doe",
"username": "[email protected]",
"email": "[email protected]",
"password": "pbkdf2_sha256$720000$Jp50WPBA6WZImUIpj3UcVm$OJWB8+UoW5lLaUkHLYo0cKgMkyRI6qnqVOWxYEsi9T0="
}
},
{
"model": "common.userprofile",
"pk": 24,
"fields": {
"user": 24,
"is_verified": true
}
},
{
"model": "common.teacher",
"pk": 7,
"fields": {
"user": 24,
"new_user": 24,
"school": 2,
"is_admin": true
}
},
{
"model": "common.class",
"pk": 7,
"fields": {
"name": "Class 2 @ School 1",
"access_code": "ZZ222",
"teacher": 7
}
}
]
86 changes: 86 additions & 0 deletions backend/api/fixtures/school_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
[
{
"model": "common.school",
"pk": 3,
"fields": {
"name": "School 2",
"country": "GB",
"county": "Hertfordshire"
}
},
{
"model": "auth.user",
"pk": 25,
"fields": {
"first_name": "John",
"last_name": "Doe",
"username": "[email protected]",
"email": "[email protected]",
"password": "pbkdf2_sha256$720000$Jp50WPBA6WZImUIpj3UcVm$OJWB8+UoW5lLaUkHLYo0cKgMkyRI6qnqVOWxYEsi9T0="
}
},
{
"model": "common.userprofile",
"pk": 25,
"fields": {
"user": 25,
"is_verified": true
}
},
{
"model": "common.teacher",
"pk": 8,
"fields": {
"user": 25,
"new_user": 25,
"school": 3
}
},
{
"model": "common.class",
"pk": 8,
"fields": {
"name": "Class 1 @ School 2",
"access_code": "XX111",
"teacher": 8
}
},
{
"model": "auth.user",
"pk": 26,
"fields": {
"first_name": "Jane",
"last_name": "Doe",
"username": "[email protected]",
"email": "[email protected]",
"password": "pbkdf2_sha256$720000$Jp50WPBA6WZImUIpj3UcVm$OJWB8+UoW5lLaUkHLYo0cKgMkyRI6qnqVOWxYEsi9T0="
}
},
{
"model": "common.userprofile",
"pk": 26,
"fields": {
"user": 26,
"is_verified": true
}
},
{
"model": "common.teacher",
"pk": 9,
"fields": {
"user": 26,
"new_user": 26,
"school": 3,
"is_admin": true
}
},
{
"model": "common.class",
"pk": 9,
"fields": {
"name": "Class 2 @ School 2",
"access_code": "XX222",
"teacher": 9
}
}
]
14 changes: 10 additions & 4 deletions backend/api/serializers/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@ def validate_klass(self, value: str):

if teacher.school is None:
raise serializers.ValidationError(
"The requesting teacher must be in a school."
"The requesting teacher must be in a school.",
code="teacher_not_in_school",
)

try:
klass = Class.objects.get(access_code=value)
except Class.DoesNotExist as ex:
raise serializers.ValidationError("Class does not exist.") from ex
raise serializers.ValidationError(
"Class does not exist.",
code="does_not_exist",
) from ex

if klass.teacher.school_id != teacher.school_id:
raise serializers.ValidationError(
"Class must belong to the same school as requesting teacher."
"Class must belong to the same school as requesting teacher.",
code="teacher_not_in_same_school",
)
if not teacher.is_admin and klass.teacher != teacher:
raise serializers.ValidationError(
"The requesting teacher must be an admin or own the class."
"The requesting teacher must be an admin or own the class.",
code="teacher_not_admin_or_class_owner",
)

return value
6 changes: 4 additions & 2 deletions backend/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def get_first_name(user_fields: t.Dict[str, t.Any]):
if len(list(group)) > 1:
raise serializers.ValidationError(
f'First name "{first_name}" is specified more than once'
f" in data for class {access_code}."
f" in data for class {access_code}.",
code="first_name_not_unique_per_class_in_data",
)

# Validate first names are not already taken in class.
Expand All @@ -97,7 +98,8 @@ def get_first_name(user_fields: t.Dict[str, t.Any]):
).exists():
raise serializers.ValidationError(
"One or more first names is already taken in class"
f" {access_code}."
f" {access_code}.",
code="first_name_not_unique_per_class_in_db",
)

return attrs
Expand Down
4 changes: 4 additions & 0 deletions backend/api/tests/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
© Ocado Group
Created on 30/01/2024 at 19:03:37(+00:00).
"""
Loading

0 comments on commit 5404604

Please sign in to comment.