Commit 2b98268c authored by Marten Kenbeek's avatar Marten Kenbeek Committed by Cornee Traas
Browse files

Added additional comments to the tests.

parent dd308932
# contents of runpytest.py
#!/usr/bin/env python
import sys
import pytest
......@@ -10,7 +10,7 @@ class MyPlugin:
def pytest_collection_modifyitems(self, items):
for item in items:
self.collected.append(item.nodeid)
self.collected.append((item.nodeid, item._obj.__doc__))
my_plugin = MyPlugin()
......@@ -20,7 +20,7 @@ file = open("test-backend.tex", "w+")
currentname = ""
file.write("\\documentclass[preview]{standalone}\n \\begin{document}\n")
for nodeid in my_plugin.collected:
for nodeid, docstring in my_plugin.collected:
id = nodeid.split("::")
filename = id[0].replace("_", "\\_")
......@@ -31,5 +31,20 @@ for nodeid in my_plugin.collected:
currentname = filename
file.write("\\subsection{" + filename + "}\n\\begin{itemize}\n")
file.write("\\item " + functionname + "\n")
itemize = False
for line in docstring.splitlines(keepends=True):
if line.startswith(" -") and not itemize:
itemize = True
file.write(" \\begin{itemize}\n")
elif not line.startswith(" -") and itemize:
itemize = False
file.write(" \\end{itemize}\n")
if line.startswith(" -"):
line = line.replace(" - ", " \\item ")
file.write(line.replace("_", "\\_"))
if itemize:
file.write(" \\end{itemize}\n")
file.write("\n")
print(nodeid)
print(docstring)
file.write("\\end{itemize}\n\\end{document}\n")
......@@ -7,6 +7,9 @@ from courses.models import Course, RegisteredAction
@pytest.mark.django_db
def test_can_create_course():
"""
Test that a course can be created through the ORM.
"""
course = Course.objects.create(
course_id="bmHtyVrIEee3CwoIJ_9DVg",
course_slug="capstone-recommender-systems",
......@@ -19,11 +22,17 @@ def test_can_create_course():
@pytest.mark.django_db
@pytest.mark.parametrize("field", ["course_id"])
def test_unique_fields(field):
"""
Test that the Course.course_id field is unique.
"""
assert Course._meta.get_field(field).unique, f"Course.{field} is not unique"
@pytest.mark.django_db
def test_course_str(course):
"""
Test that the string representation of a Course is the course's name.
"""
assert (
str(course) == "Capstone Recommender Systems"
), "incorrect string representation"
......@@ -31,6 +40,9 @@ def test_course_str(course):
@pytest.mark.django_db
def test_can_create_registered_action(course):
"""
Test that a registered action can be created through the ORM.
"""
action = RegisteredAction.objects.create(
course=course,
title="Updated quiz 2",
......@@ -43,4 +55,7 @@ def test_can_create_registered_action(course):
@pytest.mark.django_db
def test_registered_action_str(registered_action):
"""
Test that the string representation of a RegisteredAction is the action's title.
"""
assert str(registered_action) == "Updated quiz 1", "incorrect string representation"
......@@ -5,6 +5,9 @@ from courses.serializers import CourseSerializer, RegisteredActionSerializer
@pytest.mark.django_db
def test_can_serialize_course(course):
"""
Test that a course is serialized correctly.
"""
assert CourseSerializer(course).data == {
"pk": course.pk,
"course_id": course.course_id,
......@@ -15,6 +18,10 @@ def test_can_serialize_course(course):
@pytest.mark.django_db
def test_can_serialize_registered_action(teacher, registered_action):
"""
Test that a registered action is serialized correctly.
"""
class Request:
pass
......
......@@ -4,6 +4,9 @@ from django.urls import reverse
@pytest.mark.django_db
def test_can_view_course(admin_api_client, course):
"""
Test that an admin can see the course details.
"""
response = admin_api_client.get(
reverse("courses-api:course-detail", kwargs={"pk": course.pk})
)
......@@ -18,6 +21,9 @@ def test_can_view_course(admin_api_client, course):
@pytest.mark.django_db
def test_can_view_registered_action(teacher, teacher_api_client, registered_action):
"""
Test that a teacher can see the registered action details.
"""
response = teacher_api_client.get(
reverse(
"courses-api:registeredaction-detail", kwargs={"pk": registered_action.pk}
......@@ -35,6 +41,9 @@ def test_can_view_registered_action(teacher, teacher_api_client, registered_acti
@pytest.mark.django_db
def test_can_filter_action_list(teacher, teacher_api_client, registered_action):
"""
Test that a teacher can see a list of registered actions for a specific course.
"""
response = teacher_api_client.get(
reverse(
"courses-api:registeredaction-list",
......@@ -46,6 +55,9 @@ def test_can_filter_action_list(teacher, teacher_api_client, registered_action):
@pytest.mark.django_db
def test_can_create_registered_action(teacher_api_client, teacher, course):
"""
Test that a teacher can create a registered action.
"""
response = teacher_api_client.post(
reverse("courses-api:registeredaction-list"),
{
......
......@@ -5,6 +5,9 @@ from users.models import User
def test_email_is_username():
"""
Test that the custom user model has 'email' set as the username field.
"""
assert User.USERNAME_FIELD == "email", "USERNAME_FIELD is not correct"
......
......@@ -14,6 +14,9 @@ USER_FIELDS = {"pk", "email", "display_name", "role", "organization", "courses"}
@pytest.mark.django_db
def test_test_view(user_api_client):
"""
Test that an authenticated user can access the test view.
"""
response = user_api_client.get(reverse("users-api:test-view"))
assert response.status_code == 200, response.content
assert json.loads(response.content) == {
......@@ -23,12 +26,18 @@ def test_test_view(user_api_client):
@pytest.mark.django_db
def test_test_view_no_access(api_client):
"""
Test that an unauthenticated user cannot access the test view.
"""
response = api_client.get(reverse("users-api:test-view"))
assert response.status_code == 403, "unauthenticated user could reach test view"
@pytest.mark.django_db
def test_login_template(client):
"""
Test that the login page uses the correct template.
"""
response = client.get(reverse("users:login"))
assert "registration/login.html" in [
t.name for t in response.templates if t.name is not None
......@@ -37,6 +46,9 @@ def test_login_template(client):
@pytest.mark.django_db
def test_user_viewset_me(user_api_client, user):
"""
Test that a user can view its own user details.
"""
response = user_api_client.get(reverse("users-api:user-me"))
assert response.status_code == 200, "authenticated user could not get data"
assert response.data["pk"] == user.pk, "data returned to user is not its data"
......@@ -44,12 +56,18 @@ def test_user_viewset_me(user_api_client, user):
@pytest.mark.django_db
def test_user_viewset_must_be_admin(user_api_client):
"""
Test that only admins can view all user's details.
"""
response = user_api_client.get(reverse("users-api:user-list"))
assert len(response.data) <= 1, "regular user has permission"
@pytest.mark.django_db
def test_user_viewset_can_get_own_data(user_api_client, user):
"""
Test that a regular user can view its own user details in the list view.
"""
response = user_api_client.get(reverse("users-api:user-list"))
assert len(response.data) == 1, "regular user cannot access own data"
assert response.data[0]["pk"] == user.pk, "data returned to user is not its data"
......@@ -57,12 +75,18 @@ def test_user_viewset_can_get_own_data(user_api_client, user):
@pytest.mark.django_db
def test_admin_can_access_user_viewset(admin_api_client):
"""
Test that an admin can view the list of user details.
"""
response = admin_api_client.get(reverse("users-api:user-list"))
assert response.status_code == 200, response.content
@pytest.mark.django_db
def test_user_viewset_detail(admin_api_client, user):
"""
Test that an admin can view the details of a specific user.
"""
response = admin_api_client.get(
reverse("users-api:user-detail", kwargs={"pk": user.pk})
)
......@@ -79,6 +103,9 @@ def test_user_viewset_detail(admin_api_client, user):
@pytest.mark.django_db
def test_user_viewset_detail_cannot_get_other_data(user_api_client, user):
"""
Test that a regular user cannot access a different user's details.
"""
response = user_api_client.get(
reverse("users-api:user-detail", kwargs={"pk": user.pk + 1})
)
......@@ -88,6 +115,9 @@ def test_user_viewset_detail_cannot_get_other_data(user_api_client, user):
@pytest.mark.django_db
@pytest.mark.parametrize("role", [User.TEACHER, User.QDT])
def test_user_viewset_create(admin_api_client, course, role):
"""
Test that an admin can create a new user.
"""
response = admin_api_client.post(
reverse("users-api:user-list"),
{
......@@ -111,6 +141,9 @@ def test_user_viewset_create(admin_api_client, course, role):
@pytest.mark.django_db
@pytest.mark.parametrize("role", [User.TEACHER, User.QDT])
def test_user_viewset_user_is_not_a_superuser(admin_api_client, role):
"""
Test that a new user with a non-admin role is not a superuser.
"""
response = admin_api_client.post(
reverse("users-api:user-list"), {"email": "new@example.com", "role": role}
)
......@@ -123,6 +156,9 @@ def test_user_viewset_user_is_not_a_superuser(admin_api_client, role):
@pytest.mark.django_db
def test_user_viewset_create_admin(admin_api_client):
"""
Test that a new user with an admin role is a superuser.
"""
response = admin_api_client.post(
reverse("users-api:user-list"),
{"email": "admin2@example.com", "role": User.ADMIN},
......@@ -136,6 +172,9 @@ def test_user_viewset_create_admin(admin_api_client):
@pytest.mark.django_db
def test_user_viewset_set_admin_status(admin_api_client, teacher):
"""
Test that a user's role can be changed to admin.
"""
assert teacher.role == User.TEACHER, f"user is not a teacher"
assert not teacher.is_staff, "user is a staff member"
assert not teacher.is_superuser, "user is a superuser"
......@@ -154,6 +193,9 @@ def test_user_viewset_set_admin_status(admin_api_client, teacher):
@pytest.mark.django_db
@pytest.mark.parametrize("role", [User.TEACHER, User.QDT])
def test_user_viewset_remove_admin_status(admin_api_client, admin, role):
"""
Test that a user's admin role can be removed.
"""
assert admin.role == User.ADMIN, "user is not an admin"
assert admin.is_staff, "user is not a staff member"
assert admin.is_superuser, "user is not a superuser"
......@@ -170,6 +212,9 @@ def test_user_viewset_remove_admin_status(admin_api_client, admin, role):
@pytest.mark.django_db
def test_user_viewset_update_email(admin_api_client, teacher):
"""
Test that an admin can update a user's email.
"""
assert teacher.email != "new@example.com", "email was already set"
response = admin_api_client.patch(
......@@ -183,6 +228,9 @@ def test_user_viewset_update_email(admin_api_client, teacher):
@pytest.mark.django_db
def test_user_viewset_full_update(admin_api_client, teacher, course):
"""
Test that an admin can update a user's details.
"""
assert teacher.role == User.TEACHER, f"user is not a teacher"
assert not teacher.is_staff, "user is a staff member"
assert not teacher.is_superuser, "user is a superuser"
......@@ -211,6 +259,9 @@ def test_user_viewset_full_update(admin_api_client, teacher, course):
@pytest.mark.django_db
def test_create_user_send_email(admin_api_client, mailoutbox):
"""
Test that creating a user sends an activation email.
"""
admin_api_client.post(reverse("users-api:user-list"), {"email": "new@example.com"})
assert len(mailoutbox) == 1, "no mails sent"
m = mailoutbox[0]
......@@ -220,6 +271,9 @@ def test_create_user_send_email(admin_api_client, mailoutbox):
@pytest.mark.django_db
def test_reset_password(user, api_client):
"""
Test that password reset works.
"""
token = default_token_generator.make_token(user)
response = api_client.put(
reverse("users-api:user-password-reset", kwargs={"pk": user.pk}),
......@@ -234,6 +288,9 @@ def test_reset_password(user, api_client):
@pytest.mark.django_db
def test_reset_password_low_quality_password(user, api_client):
"""
Test that a low quality password is rejected.
"""
token = default_token_generator.make_token(user)
response = api_client.put(
reverse("users-api:user-password-reset", kwargs={"pk": user.pk}),
......@@ -247,6 +304,9 @@ def test_reset_password_low_quality_password(user, api_client):
@pytest.mark.django_db
def test_reset_password_invalid_token(user, api_client):
"""
Test that an invalid password reset token is rejected.
"""
response = api_client.put(
reverse("users-api:user-password-reset", kwargs={"pk": user.pk}),
{"token": "invalid_token", "password": "new_password"},
......@@ -259,6 +319,9 @@ def test_reset_password_invalid_token(user, api_client):
@pytest.mark.django_db
def test_forgot_password_request(user, api_client, mailoutbox):
"""
Test that the forgot password view sends a password reset email.
"""
response = api_client.put(
reverse("users-api:user-forgot-password"), {"email": "john.doe@example.com"}
)
......@@ -268,6 +331,10 @@ def test_forgot_password_request(user, api_client, mailoutbox):
@pytest.mark.django_db
def test_forgot_password_unknown_email(user, api_client, mailoutbox):
"""
Test that the forgot password view does not send a password reset email to an
unknown email address.
"""
response = api_client.put(
reverse("users-api:user-forgot-password"), {"email": "jeff.wrong@example.com"}
)
......@@ -277,6 +344,10 @@ def test_forgot_password_unknown_email(user, api_client, mailoutbox):
@pytest.mark.django_db
def test_authorize(api_client, user):
"""
Test that the front-end application can authorize a user and generate an access
token.
"""
application = Application.objects.get(name="DASH-IT Frontend")
response = api_client.post(
......@@ -298,6 +369,10 @@ def test_authorize(api_client, user):
@pytest.mark.django_db
def test_introspection_endpoint(introspection_client, teacher_access_token):
"""
Test that a OAuth2 client with introspection capabilities can introspect
a user's access token.
"""
response = introspection_client.post(
reverse("users:introspect"),
{"token": teacher_access_token.token, "platform": "coursera"},
......@@ -311,6 +386,9 @@ def test_introspection_endpoint(introspection_client, teacher_access_token):
@pytest.mark.django_db
def test_introspection_non_existent_token(introspection_client):
"""
Test that a non-existent token cannot be introspected.
"""
response = introspection_client.post(
reverse("users:introspect"),
{"token": "non-existent-token", "platform": "coursera"},
......@@ -320,6 +398,9 @@ def test_introspection_non_existent_token(introspection_client):
@pytest.mark.django_db
def test_introspection_invalid_token(introspection_client, invalid_token):
"""
Test that an invalid token can be introspected.
"""
response = introspection_client.post(
reverse("users:introspect"),
{"token": invalid_token.token, "platform": "coursera"},
......@@ -333,6 +414,9 @@ def test_introspection_invalid_token(introspection_client, invalid_token):
def test_introspect_application_token(
introspection_client, introspection_access_token, coursera_application
):
"""
Test that the introspection access token can be introspected.
"""
response = introspection_client.post(
reverse("users:introspect"),
{"token": introspection_access_token.token, "platform": "coursera"},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment