Skip to content

Commit 8ab4a9f

Browse files
committed
Ensure CheckNeedAppointment requires DOB to be answered
1 parent 2800e87 commit 8ab4a9f

3 files changed

Lines changed: 78 additions & 65 deletions

File tree

lung_cancer_screening/questions/tests/unit/views/test_check_need_appointment.py

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
from django.urls import reverse
33

44
from .helpers.authentication import login_user
5-
from lung_cancer_screening.questions.models.check_need_appointment_response import CheckNeedAppointmentResponse
65
from ...factories.response_set_factory import ResponseSetFactory
6+
from ...factories.date_of_birth_response_factory import DateOfBirthResponseFactory
77

88
@tag("CheckNeedAppointment")
99
class TestGetCheckNeedAppointment(TestCase):
1010
def setUp(self):
1111
self.user = login_user(self.client)
1212

13+
self.response_set = ResponseSetFactory.create(user=self.user)
1314

14-
def test_get_redirects_if_the_user_is_not_logged_in(self):
15+
16+
def test_redirects_if_the_user_is_not_logged_in(self):
1517
self.client.logout()
1618

1719
response = self.client.get(
@@ -21,7 +23,8 @@ def test_get_redirects_if_the_user_is_not_logged_in(self):
2123
self.assertRedirects(response, "/oidc/authenticate/?next=/check-if-you-need-an-appointment", fetch_redirect_response=False)
2224

2325

24-
def test_get_redirects_when_an_submitted_response_set_exists_within_the_last_year(self):
26+
def test_redirects_when_an_submitted_response_set_exists_within_the_last_year(self):
27+
self.response_set.delete()
2528
ResponseSetFactory.create(
2629
user=self.user,
2730
recently_submitted=True
@@ -34,26 +37,43 @@ def test_get_redirects_when_an_submitted_response_set_exists_within_the_last_yea
3437
self.assertRedirects(response, reverse("questions:confirmation"))
3538

3639

37-
def test_get_responds_successfully(self):
40+
def test_redirects_if_hasnt_answered_date_of_birth(self):
3841
response = self.client.get(reverse("questions:check_need_appointment"))
3942

40-
self.assertEqual(response.status_code, 200)
43+
self.assertRedirects(response, reverse("questions:date_of_birth"), fetch_redirect_response=False)
44+
4145

46+
def test_redirects_if_has_answered_date_of_birth_with_ineligible_response(self):
47+
DateOfBirthResponseFactory.create(
48+
response_set=self.response_set,
49+
ineligible=True
50+
)
4251

43-
def test_get_contains_the_correct_form_fields(self):
4452
response = self.client.get(reverse("questions:check_need_appointment"))
4553

46-
self.assertContains(response, "Do you need to leave the online service and ask for a face-to-face appointment?")
54+
self.assertRedirects(response, reverse("questions:date_of_birth"), fetch_redirect_response=False)
55+
56+
57+
def test_responds_successfully(self):
58+
DateOfBirthResponseFactory.create(
59+
response_set=self.response_set, eligible=True
60+
)
61+
62+
response = self.client.get(reverse("questions:check_need_appointment"))
63+
64+
self.assertEqual(response.status_code, 200)
65+
4766

4867
@tag("CheckNeedAppointment")
4968
class TestPostCheckNeedAppointment(TestCase):
5069
def setUp(self):
5170
self.user = login_user(self.client)
71+
self.response_set = ResponseSetFactory.create(user=self.user)
5272

5373
self.valid_params = {"value": False}
5474

5575

56-
def test_post_redirects_if_the_user_is_not_logged_in(self):
76+
def test_redirects_if_the_user_is_not_logged_in(self):
5777
self.client.logout()
5878

5979
response = self.client.post(
@@ -64,69 +84,48 @@ def test_post_redirects_if_the_user_is_not_logged_in(self):
6484
self.assertRedirects(response, "/oidc/authenticate/?next=/check-if-you-need-an-appointment", fetch_redirect_response=False)
6585

6686

67-
def test_post_creates_an_unsubmitted_response_set_for_the_user_when_no_response_set_exists(self):
68-
self.client.post(
69-
reverse("questions:check_need_appointment"),
70-
self.valid_params
87+
def test_redirects_when_an_submitted_response_set_exists_within_the_last_year(self):
88+
self.response_set.delete()
89+
ResponseSetFactory.create(
90+
user=self.user,
91+
recently_submitted=True
7192
)
7293

73-
response_set = self.user.responseset_set.first()
74-
self.assertEqual(self.user.responseset_set.count(), 1)
75-
self.assertEqual(response_set.submitted_at, None)
76-
self.assertEqual(CheckNeedAppointmentResponse.objects.get(response_set=response_set).value, self.valid_params["value"])
77-
self.assertEqual(response_set.user, self.user)
78-
79-
80-
def test_post_updates_an_unsubmitted_response_set_for_the_user_when_an_unsubmitted_response_set_existso(self):
81-
response_set = self.user.responseset_set.create()
82-
83-
self.client.post(
94+
response = self.client.post(
8495
reverse("questions:check_need_appointment"),
8596
self.valid_params
8697
)
8798

88-
response_set.refresh_from_db()
89-
self.assertEqual(self.user.responseset_set.count(), 1)
90-
self.assertEqual(response_set.submitted_at, None)
91-
self.assertEqual(CheckNeedAppointmentResponse.objects.get(response_set=response_set).value, self.valid_params["value"])
92-
self.assertEqual(response_set.user, self.user)
93-
99+
self.assertRedirects(response, reverse("questions:confirmation"))
94100

95-
def test_post_creates_an_new_unsubmitted_response_set_for_the_user_when_a_non_recently_submitted_response_set_exists(self):
96-
ResponseSetFactory.create(
97-
user=self.user,
98-
not_recently_submitted=True
99-
)
100101

101-
self.client.post(
102+
def test_redirects_if_hasnt_answered_date_of_birth(self):
103+
response = self.client.post(
102104
reverse("questions:check_need_appointment"),
103105
self.valid_params
104106
)
105107

106-
self.assertEqual(self.user.responseset_set.count(), 2)
107-
self.assertEqual(self.user.responseset_set.unsubmitted().count(), 1)
108+
self.assertRedirects(response, reverse("questions:date_of_birth"), fetch_redirect_response=False)
108109

109-
response_set = self.user.responseset_set.last()
110-
self.assertEqual(response_set.submitted_at, None)
111-
self.assertEqual(CheckNeedAppointmentResponse.objects.get(response_set=response_set).value, self.valid_params["value"])
112-
self.assertEqual(response_set.user, self.user)
113110

114-
115-
def test_post_redirects_when_an_submitted_response_set_exists_within_the_last_year(self):
116-
ResponseSetFactory.create(
117-
user=self.user,
118-
recently_submitted=True
111+
def test_redirects_if_has_answered_date_of_birth_with_ineligible_response(self):
112+
DateOfBirthResponseFactory.create(
113+
response_set=self.response_set, ineligible=True
119114
)
120115

121116
response = self.client.post(
122117
reverse("questions:check_need_appointment"),
123118
self.valid_params
124119
)
125120

126-
self.assertRedirects(response, reverse("questions:confirmation"))
121+
self.assertRedirects(response, reverse("questions:date_of_birth"), fetch_redirect_response=False)
127122

128123

129-
def test_post_redirects_to_the_next_page(self):
124+
def test_redirects_to_the_next_page(self):
125+
DateOfBirthResponseFactory.create(
126+
response_set=self.response_set, eligible=True
127+
)
128+
130129
response = self.client.post(
131130
reverse("questions:check_need_appointment"),
132131
self.valid_params
@@ -135,28 +134,26 @@ def test_post_redirects_to_the_next_page(self):
135134
self.assertRedirects(response, reverse("questions:height"), fetch_redirect_response=False)
136135

137136

138-
def test_post_redirects_to_book_an_appointment_page(self):
137+
def test_redirects_to_book_an_appointment_page(self):
138+
DateOfBirthResponseFactory.create(
139+
response_set=self.response_set, eligible=True
140+
)
141+
139142
response = self.client.post(
140143
reverse("questions:check_need_appointment"),
141144
{"value": True}
142145
)
143146

144147
self.assertRedirects(response, reverse("questions:book_an_appointment"))
145148

146-
def test_post_responds_with_422_if_the_response_fails_to_create(self):
149+
def test_responds_with_422_if_the_response_fails_to_create(self):
150+
DateOfBirthResponseFactory.create(
151+
response_set=self.response_set, eligible=True
152+
)
153+
147154
response = self.client.post(
148155
reverse("questions:check_need_appointment"),
149156
{"value": "something not in list"}
150157
)
151158

152159
self.assertEqual(response.status_code, 422)
153-
154-
def test_post_does_not_update_response_set_on_invalid_data(self):
155-
self.client.post(
156-
reverse("questions:check_need_appointment"),
157-
{"value": "invalid"}
158-
)
159-
160-
response_set = self.user.responseset_set.first()
161-
if response_set:
162-
self.assertFalse(CheckNeedAppointmentResponse.objects.filter(response_set=response_set).exists())

lung_cancer_screening/questions/views/check_need_appointment.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.urls import reverse, reverse_lazy
22
from django.contrib.auth.mixins import LoginRequiredMixin
3+
from django.shortcuts import redirect
34

45
from .mixins.ensure_response_set import EnsureResponseSet
56
from .question_base_view import QuestionBaseView
@@ -9,7 +10,18 @@
910
)
1011

1112

12-
class CheckNeedAppointmentView(LoginRequiredMixin, EnsureResponseSet, QuestionBaseView):
13+
class EnsureAgeEligible:
14+
def dispatch(self, request, *args, **kwargs):
15+
if (
16+
not hasattr(request.response_set, "date_of_birth_response")
17+
or not request.response_set.date_of_birth_response.is_eligible()
18+
):
19+
return redirect(reverse("questions:date_of_birth"))
20+
else:
21+
return super().dispatch(request, *args, **kwargs)
22+
23+
24+
class CheckNeedAppointmentView(LoginRequiredMixin, EnsureResponseSet, EnsureAgeEligible, QuestionBaseView):
1325
template_name = "check_need_appointment.jinja"
1426
form_class = CheckNeedAppointmentForm
1527
model = CheckNeedAppointmentResponse

lung_cancer_screening/questions/views/date_of_birth.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
from django.urls import reverse, reverse_lazy
2-
from django.shortcuts import redirect
32
from django.contrib.auth.mixins import LoginRequiredMixin
3+
from django.shortcuts import redirect
44

55
from .mixins.ensure_response_set import EnsureResponseSet
66
from .question_base_view import QuestionBaseView
77
from ..forms.date_of_birth_form import DateOfBirthForm
88
from ..models.date_of_birth_response import DateOfBirthResponse
99

10-
class HaveYouEverSmokedRequiredMixin:
10+
class EnsureSmokedEligible:
1111
def dispatch(self, request, *args, **kwargs):
12-
if not hasattr(request.response_set, "have_you_ever_smoked_response") or not request.response_set.have_you_ever_smoked_response.is_eligible():
12+
if (
13+
not hasattr(request.response_set, "have_you_ever_smoked_response")
14+
or not request.response_set.have_you_ever_smoked_response.is_eligible()
15+
):
1316
return redirect(reverse("questions:have_you_ever_smoked"))
1417
else:
1518
return super().dispatch(request, *args, **kwargs)
1619

17-
class DateOfBirthView(LoginRequiredMixin, EnsureResponseSet, HaveYouEverSmokedRequiredMixin, QuestionBaseView):
20+
21+
class DateOfBirthView(LoginRequiredMixin, EnsureResponseSet, EnsureSmokedEligible, QuestionBaseView):
1822
template_name = "question_form.jinja"
1923
form_class = DateOfBirthForm
2024
model = DateOfBirthResponse

0 commit comments

Comments
 (0)