Skip to content

Commit 3eba01d

Browse files
committed
PPHA-475: Protect all pages behind login
1 parent 09263e5 commit 3eba01d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+580
-49
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

lung_cancer_screening/questions/tests/factories/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import factory
2+
3+
from ...models.user import User
4+
5+
6+
class UserFactory(factory.django.DjangoModelFactory):
7+
class Meta:
8+
model = User
9+
10+
nhs_number = factory.Sequence(lambda n: f"9{str(n).zfill(9)}")
11+
password = factory.django.Password(None)

lung_cancer_screening/questions/tests/unit/models/test_user.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def test_has_updated_at_as_a_datetime(self):
2828
datetime
2929
)
3030

31+
3132
def test_nhs_number_has_a_max_length_of_10(self):
3233
with self.assertRaises(ValidationError) as context:
3334
User.objects.create_user("1"*11)

lung_cancer_screening/questions/tests/unit/views/helpers/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from ....factories.user_factory import UserFactory
2+
3+
def login_user(client):
4+
current_user = UserFactory()
5+
client.force_login(current_user)
6+
7+
return current_user

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
from django.test import TestCase
22
from django.urls import reverse
33

4+
from .helpers.authentication import login_user
5+
46
from lung_cancer_screening.questions.models.participant import Participant
57

68
class TestPostAgeRangeExit(TestCase):
79
def setUp(self):
10+
login_user(self.client)
11+
812
participant = Participant.objects.create(unique_id="12345")
913

1014
session = self.client.session
1115
session['participant_id'] = participant.unique_id
1216
session.save()
1317

18+
def test_get_redirects_if_the_user_is_not_logged_in(self):
19+
self.client.logout()
20+
21+
participant = Participant.objects.create(unique_id="abcdef")
22+
23+
session = self.client.session
24+
session['participant_id'] = participant.unique_id
25+
session.save()
26+
27+
response = self.client.get(
28+
reverse("questions:age_range_exit")
29+
)
30+
31+
self.assertRedirects(response, "/oidc/authenticate/?next=/age-range-exit", fetch_redirect_response=False)
32+
1433
def test_get_redirects_if_the_particpant_does_not_exist(self):
1534
session = self.client.session
1635
session['participant_id'] = "somebody none existant participant"

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
from django.test import TestCase
22
from django.urls import reverse
3+
from django.conf import settings
34

5+
from .helpers.authentication import login_user
46
from lung_cancer_screening.questions.models.participant import Participant
57

68

79
class TestAsbestosExposure(TestCase):
810
def setUp(self):
11+
login_user(self.client)
12+
913
self.participant = Participant.objects.create(unique_id="12345")
1014
self.participant.responseset_set.create()
1115
self.valid_params = {"asbestos_exposure": True}
@@ -14,6 +18,19 @@ def setUp(self):
1418
session['participant_id'] = self.participant.unique_id
1519
session.save()
1620

21+
def test_get_redirects_if_the_user_is_not_logged_in(self):
22+
participant = Participant.objects.create(unique_id="abcdef")
23+
self.client.logout()
24+
session = self.client.session
25+
session['participant_id'] = participant.unique_id
26+
session.save()
27+
28+
response = self.client.get(
29+
reverse("questions:asbestos_exposure")
30+
)
31+
32+
self.assertRedirects(response, "/oidc/authenticate/?next=/asbestos-exposure", fetch_redirect_response=False)
33+
1734
def test_get_redirects_if_the_participant_does_not_exist(self):
1835
session = self.client.session
1936
session['participant_id'] = "somebody none existant participant"
@@ -33,6 +50,21 @@ def test_get_contains_the_correct_form_fields(self):
3350
response = self.client.get(reverse("questions:asbestos_exposure"))
3451
self.assertContains(response, "Have you ever worked in a job where you might have been exposed to asbestos?")
3552

53+
def test_post_redirects_if_the_user_is_not_logged_in(self):
54+
self.client.logout()
55+
participant = Participant.objects.create(unique_id="abcdef")
56+
57+
session = self.client.session
58+
session['participant_id'] = participant.unique_id
59+
session.save()
60+
61+
response = self.client.post(
62+
reverse("questions:asbestos_exposure"),
63+
self.valid_params
64+
)
65+
66+
self.assertRedirects(response, "/oidc/authenticate/?next=/asbestos-exposure", fetch_redirect_response=False)
67+
3668
def test_post_redirects_if_the_participant_does_not_exist(self):
3769
session = self.client.session
3870
session['participant_id'] = "somebody none existant participant"

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
from datetime import date
44
from dateutil.relativedelta import relativedelta
55

6+
from .helpers.authentication import login_user
67
from ....models.participant import Participant
78

89
class TestPostDateOfBirth(TestCase):
910
def setUp(self):
11+
login_user(self.client)
12+
1013
self.participant = Participant.objects.create(unique_id="12345")
1114
self.participant.responseset_set.create()
1215
self.valid_age = date.today() - relativedelta(years=55)
@@ -27,6 +30,19 @@ def setUp(self):
2730
session['participant_id'] = self.participant.unique_id
2831
session.save()
2932

33+
def test_get_redirects_if_the_user_is_not_logged_in(self):
34+
participant = Participant.objects.create(unique_id="abcdef")
35+
self.client.logout()
36+
session = self.client.session
37+
session['participant_id'] = participant.unique_id
38+
session.save()
39+
40+
response = self.client.get(
41+
reverse("questions:date_of_birth")
42+
)
43+
44+
self.assertRedirects(response, "/oidc/authenticate/?next=/date-of-birth", fetch_redirect_response=False)
45+
3046
def test_get_redirects_if_the_particpant_does_not_exist(self):
3147
session = self.client.session
3248
session['participant_id'] = "somebody none existant participant"
@@ -43,6 +59,21 @@ def test_get_responds_successfully(self):
4359

4460
self.assertEqual(response.status_code, 200)
4561

62+
def test_post_redirects_if_the_user_is_not_logged_in(self):
63+
self.client.logout()
64+
participant = Participant.objects.create(unique_id="abcdef")
65+
66+
session = self.client.session
67+
session['participant_id'] = participant.unique_id
68+
session.save()
69+
70+
response = self.client.post(
71+
reverse("questions:date_of_birth"),
72+
self.valid_params
73+
)
74+
75+
self.assertRedirects(response, "/oidc/authenticate/?next=/date-of-birth", fetch_redirect_response=False)
76+
4677
def test_post_redirects_if_the_particpant_does_not_exist(self):
4778
session = self.client.session
4879
session['participant_id'] = "somebody none existant participant"

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from django.test import TestCase
22
from django.urls import reverse
33

4+
from .helpers.authentication import login_user
5+
46
from lung_cancer_screening.questions.models.participant import Participant
57
from lung_cancer_screening.questions.models.response_set import EthnicityValues
68

7-
89
class TestEthnicity(TestCase):
910
def setUp(self):
11+
login_user(self.client)
12+
1013
self.participant = Participant.objects.create(unique_id="12345")
1114
self.participant.responseset_set.create()
1215
self.valid_params = { "ethnicity": EthnicityValues.WHITE }
@@ -16,6 +19,18 @@ def setUp(self):
1619
session.save()
1720

1821
### Test GET request
22+
def test_get_redirects_if_the_user_is_not_logged_in(self):
23+
participant = Participant.objects.create(unique_id="abcdef")
24+
self.client.logout()
25+
session = self.client.session
26+
session['participant_id'] = participant.unique_id
27+
session.save()
28+
29+
response = self.client.get(
30+
reverse("questions:ethnicity")
31+
)
32+
33+
self.assertRedirects(response, "/oidc/authenticate/?next=/ethnicity", fetch_redirect_response=False)
1934

2035
def test_get_redirects_if_the_participant_does_not_exist(self):
2136
session = self.client.session
@@ -40,6 +55,22 @@ def test_get_contains_the_correct_form_fields(self):
4055

4156
### Test POST request
4257

58+
def test_post_redirects_if_the_user_is_not_logged_in(self):
59+
self.client.logout()
60+
participant = Participant.objects.create(unique_id="abcdef")
61+
62+
session = self.client.session
63+
session['participant_id'] = participant.unique_id
64+
session.save()
65+
66+
response = self.client.post(
67+
reverse("questions:ethnicity"),
68+
self.valid_params
69+
)
70+
71+
self.assertRedirects(response, "/oidc/authenticate/?next=/ethnicity", fetch_redirect_response=False)
72+
73+
4374
def test_post_redirects_if_the_participant_does_not_exist(self):
4475
session = self.client.session
4576
session['participant_id'] = "somebody none existant participant"

0 commit comments

Comments
 (0)