import pytest
from django.contrib.auth.models import Permission
from django.test import RequestFactory, TestCase
from django.urls import reverse
from ctrack.core.views import home_page
from ctrack.register.models import SingleDateTimeEvent
from ctrack.users.models import User
from ctrack.users.views import UserDetailView, UserRedirectView, UserUpdateView
pytestmark = pytest.mark.django_db
test_case = TestCase("run")
class TestUserProfilePage:
def test_their_full_name_in_h3(self, user: User, client):
client.force_login(user)
response = client.get(reverse("users:detail", args=[user.username]))
assert response.status_code == 200
html = response.content.decode("utf-8")
test_string = f'
{user.name}
'
assert test_string in html
def test_view_has_all_events_related_to_user(self, user, client):
SingleDateTimeEvent.objects.create(
type_descriptor="PHONE_CALL",
short_description="Important event",
url="http://fake.url.com",
requested_response_date="2021-01-24",
response_received_date=None,
date="2020-10-10T15:00",
comments="Comments on important event",
# location is optional
user=user,
)
client.force_login(user)
response = client.get(reverse("users:detail", args=[user.username]))
assert response.status_code == 200
html = response.content.decode("utf-8")
test_case.assertInHTML("Comments on important event", html)
class TestUserUpdateView:
"""
TODO:
extracting view initialization code as class-scoped fixture
would be great if only pytest-django supported non-function-scoped
fixture db access -- this is a work-in-progress for now:
https://github.com/pytest-dev/pytest-django/pull/258
"""
def test_get_success_url(self, user: User, request_factory: RequestFactory):
view = UserUpdateView()
request = request_factory.get("/fake-url/")
request.user = user
view.request = request
assert view.get_success_url() == f"/users/{user.username}/"
def test_get_object(self, user: User, request_factory: RequestFactory):
view = UserUpdateView()
request = request_factory.get("/fake-url/")
request.user = user
view.request = request
assert view.get_object() == user
class TestUserRedirectView:
def test_get_redirect_url(self, user: User, request_factory: RequestFactory):
view = UserRedirectView()
request = request_factory.get("/fake-url")
request.user = user
view.request = request
assert view.get_redirect_url() == "/"
def test_profile_view_contains_organisation_information(
person, request_factory, stakeholder_user
):
"""
This tests the context_data - not the rendered page... We'll do that in the
next test.
"""
org_name = person.organisation.name
request = request_factory.get(f"/users/{stakeholder_user.username}")
# we have to do the following to simulate logged-in user
# Django Advanced Testing Topics
request.user = stakeholder_user
# We pass 'username' rather than 'slug' here because we are setting 'slug_url_kwarg' in our CBV.
response = UserDetailView.as_view()(request, username=stakeholder_user.username)
assert response.status_code == 200
assert response.context_data["user"].username == stakeholder_user.username
assert response.context_data["user"].is_stakeholder is True
assert response.context_data["user"].stakeholder.person.first_name == "Toss"
# Two ways of getting the organisaton name
assert (
response.context_data["user"].stakeholder.person.get_organisation_name()
== org_name
)
assert response.context_data["user"].get_organisation_name() == org_name
assert response.context_data["user"].stakeholder.person.first_name == "Toss"
def test_home_page_h1_tag_with_client(client, django_user_model):
"""
Basic test of HTML from the home page.
"""
django_user_model.objects.create_user(username="toss", password="knob")
client.login(username="toss", password="knob")
response = client.get("/")
assert response.status_code == 200
assert b"ctrack - NIS Tracker" in response.content
assert b"