aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/users/tests/test_views.py
blob: 6859215847b65c9a628fd804d415815b86d47419 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import pytest
from django.contrib.auth import get_user_model
from django.http import HttpRequest
from django.test import RequestFactory
from django.urls import resolve

from ctrack.core.views import home_page
from ctrack.organisations.models import Stakeholder
from ctrack.users.models import User
from ctrack.users.views import UserDetailView, UserRedirectView, UserUpdateView

pytestmark = pytest.mark.django_db


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() == f"/users/{user.username}/"


def test_profile_view_contains_organisation_information(person, user):
    """
    This tests the context_data - not the rendered page... We'll do that in the
    next test.
    """
    org_name = person.organisation.name
    stakeholder = Stakeholder.objects.create(person=person)
    user.stakeholder = stakeholder
    user.save()
    factory = RequestFactory()
    request = factory.get(f"/users/{user.username}")

    # we have to do the following to simulate logged-in user
    # Django Advanced Testing Topics
    request.user = user

    # We pass 'username' rather than 'slug' here because we are setting 'slug_url_kwarg' in our CBV.
    response = UserDetailView.as_view()(request, username=user.username)

    assert response.status_code == 200
    assert response.context_data["user"].username == 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 response.content[:15] == b"<!DOCTYPE html>"
    assert b"<title>ctrack - Department for Transport</title>" in response.content
    assert b"<h1>Welcome to ctrack - Department for Transport</h1>" in response.content
    assert b"</html>" in response.content


def test_regular_user_gets_regular_user_template(django_user_model):
    """
    When a user logs in without a stakeholder mapping, they get sent to the regular user
    template.
    """
    user = django_user_model.objects.create_user(username="toss", password="knob")
    factory = RequestFactory()
    request = factory.get("/")
    request.user = user
    response = home_page(request)
    assert response.status_code == 200
    assert b"<p>THIS IS A TEMPLATE FOR A REGULAR USER</p>" in response.content