aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/register/tests/test_views.py
blob: 306cec5ebc5b2368bfadee495777980824e1bda4 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import pytest
from django.test import TestCase
from django.urls import reverse

from ctrack.register.views import SingleDateTimeEventCreate

# Doing this allows us to use TestCase assertions (assertIn, etc)
test_case = TestCase("run")

pytestmark = pytest.mark.django_db


class TestSingleDateTimeEvent:
    url = reverse("register:event_create_simple_event")

    def test_add_single_datetime_event_form(self, client):
        response = client.get(self.url)
        assert response.status_code == 200

        form = response.context_data["form"]
        assert not form.is_bound
        expected_fields = [
            "type_descriptor",
            "short_description",
            "datetime",
            "comments",
            "location",
        ]
        for field in expected_fields:
            assert field in form.fields
        # We're keeping the use field out of the form
        assert "user" not in form.fields

    @pytest.mark.parametrize(
        "bad_date,expected_error",
        [
            ("NOT A DATE", "Enter a valid date/time."),
            ("202002-10-12", "Enter a valid date/time."),
            ("32 May 2020", "Enter a valid date/time."),
            ("May 2020", "Enter a valid date/time."),
        ],
    )
    def test_bad_date(self, bad_date, cct_user, expected_error, client):
        data = {
            "type_descriptor": "MEETING",
            "short_description": "Test Short Description",
            "datetime": bad_date,
            "comments": "Blah...",
            "location": "The Moon",
        }
        # we need to use the cct_user fixture here who has permissions
        # on the redirect page
        client.force_login(cct_user)
        response = client.post(self.url, data)
        assert response.status_code == 200
        html = response.content.decode("utf-8")
        test_case.assertIn(expected_error, html)

    @pytest.mark.parametrize("good_date", ["2010-10-10"])
    def test_good_date(self, good_date, cct_user, client):
        data = {
            "type_descriptor": "PHONE_CALL",
            "short_description": "Test Short Description",
            "datetime": good_date,
            "comments": "Blah...",
            "location": "The Moon",
        }
        client.force_login(cct_user)
        response = client.post(self.url, data, follow=True)
        test_case.assertRedirects(
            response,
            reverse("organisations:list"),
        )

    @pytest.mark.parametrize(
        "bad_type,expected_error",
        [
            (
                "Meeting X",
                "Select a valid choice. Meeting X is not one of the available choices.",
            ),
            (
                "Meeting Bunting Radgehead",
                "Select a valid choice. Meeting Bunting Radgehead is not one of the available choices.",
            ),
        ],
    )
    def test_add_incorrect_form_data_single_datetime(
        self, bad_type, expected_error, client
    ):
        data = {
            "type_descriptor": bad_type,
            "short_description": "Test Short Description",
            "datetime": "2010-10-10",
            "comments": "Blah...",
            "location": "The Moon",
        }
        response = client.post(self.url, data)
        assert response.status_code == 200
        html = response.content.decode("utf-8")
        test_case.assertIn(expected_error, html)

    def test_user_passed_as_kwarg(self, user, request_factory):
        view = SingleDateTimeEventCreate()
        request = request_factory.get("/register/event/create-simple-event/")
        request.user = user
        view.request = request
        view.setup(request)
        assert "user" in view.get_form_kwargs()

    def test_create_simple_event_without_org_means_no_participants(self, user, client):
        client.force_login(user)
        url = reverse("register:event_create_simple_event")
        response = client.get(url)
        html = response.content.decode("utf-8")
        assert '<input type="hidden" name="participants" id="id_participants">' in html

    def test_can_create_simple_event_with_org_slug(self, user, org, client):
        slug = org.slug
        url = reverse("register:event_create_simple_event_from_org", args=[slug])
        client.force_login(user)
        response = client.get(url)
        html = response.content.decode("utf-8")
        assert response.status_code == 200
        test_case.assertInHTML(f"Create a new simple event involving {org.name}", html)

    def test_org_passed_as_kwarg(self, user, org, request_factory):
        slug = org.slug
        view = SingleDateTimeEventCreate()
        url = reverse("register:event_create_simple_event_from_org", args=[slug])
        request = request_factory.get(url)
        request.user = user
        view.request = request
        view.setup(request)
        assert "org_slug" in view.get_form_kwargs()

    def test_meeting_type_and_org_passed_as_kwarg(
        self, user, org, request_factory
    ):
        event_type = "PHONE_CALL"
        slug = org.slug
        view = SingleDateTimeEventCreate()
        url = reverse("register:event_create_simple_event_from_org_with_type", args=[slug, event_type])
        request = request_factory.get(url)
        request.user = user
        view.request = request
        view.setup(request)
        assert "event_type" in view.get_form_kwargs()


class TestSingleDateCAFEventViews:
    def test_initial_caf_received(self, client):
        url = reverse("register:event_create_simple_event")
        response = client.get(url)
        assert response.status_code == 200