Skip to content

Commit 295462c

Browse files
committed
Ensure participants can only submit one ResponseSet per year
1 parent 5c14382 commit 295462c

23 files changed

+343
-35
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
3+
from playwright.sync_api import sync_playwright, expect
4+
from datetime import datetime
5+
from dateutil.relativedelta import relativedelta
6+
7+
from .helpers.user_interaction_helpers import (
8+
fill_in_and_submit_participant_id,
9+
fill_in_and_submit_smoking_elligibility,
10+
fill_in_and_submit_date_of_birth
11+
)
12+
13+
14+
class TestQuestionnaire(StaticLiveServerTestCase):
15+
16+
@classmethod
17+
def setUpClass(cls):
18+
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
19+
super().setUpClass()
20+
cls.playwright = sync_playwright().start()
21+
cls.browser = cls.playwright.chromium.launch()
22+
23+
@classmethod
24+
def tearDownClass(cls):
25+
super().tearDownClass()
26+
cls.browser.close()
27+
cls.playwright.stop()
28+
29+
def test_cannot_change_responses_once_checked_and_submitted(self):
30+
participant_id = '123'
31+
smoking_status = 'Yes, I used to smoke regularly'
32+
age = datetime.now() - relativedelta(years=55)
33+
34+
page = self.browser.new_page()
35+
page.goto(f"{self.live_server_url}/start")
36+
37+
fill_in_and_submit_participant_id(page, participant_id)
38+
fill_in_and_submit_smoking_elligibility(page, smoking_status)
39+
fill_in_and_submit_date_of_birth(page, age)
40+
41+
page.click("text=Submit")
42+
43+
page.goto(f"{self.live_server_url}/start")
44+
45+
fill_in_and_submit_participant_id(page, participant_id)
46+
47+
expect(page.locator('#maincontent')).to_contain_text(
48+
"Responses have already been submitted for this participant"
49+
)

lung_cancer_screening/core/tests/acceptance/test_questionnaire.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ def test_full_questionaire_user_journey(self):
4949
expect(page.locator(".responses")).to_contain_text(
5050
age.strftime("Have you ever smoked? Yes, I used to smoke regularly"))
5151
expect(page.locator(".responses")).to_contain_text(age.strftime("What is your date of birth? %Y-%m-%d"))
52+
53+
page.click("text=Submit")
54+
55+
expect(page).to_have_url(f"{self.live_server_url}/your-results")

lung_cancer_screening/core/tests/acceptance/test_questionnaire_validation_errors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
from .helpers.user_interaction_helpers import (
66
fill_in_and_submit_participant_id,
7-
fill_in_and_submit_smoking_elligibility,
8-
fill_in_and_submit_date_of_birth
7+
fill_in_and_submit_smoking_elligibility
98
)
109

1110
class TestQuestionnaireValidationErrors(StaticLiveServerTestCase):

lung_cancer_screening/questions/forms/date_of_birth_form.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
class DateOfBirthForm(forms.ModelForm):
88
def __init__(self, *args, **kwargs):
9+
self.participant = kwargs.pop('participant')
910
super().__init__(*args, **kwargs)
11+
self.instance.participant = self.participant
1012

1113
self.fields["date_of_birth"] = SplitDateField(
1214
max_value=date.today(),

lung_cancer_screening/questions/forms/have_you_ever_smoked_form.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
class HaveYouEverSmokedForm(forms.ModelForm):
77

88
def __init__(self, *args, **kwargs):
9+
self.participant = kwargs.pop('participant')
910
super().__init__(*args, **kwargs)
11+
self.instance.participant = self.participant
1012

1113
self.fields["have_you_ever_smoked"] = TypedChoiceField(
1214
choices=HaveYouEverSmokedValues.choices,

lung_cancer_screening/questions/jinja2/responses.jinja

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends 'layout.jinja' %}
2-
2+
{% from 'button/macro.jinja' import button %}
33

44
{% block content %}
55
<div class="nhsuk-grid-row">
@@ -9,6 +9,14 @@
99
<li>Have you ever smoked? {{ response_set.get_have_you_ever_smoked_display() }}</li>
1010
<li>What is your date of birth? {{ response_set.date_of_birth }}</li>
1111
</ul>
12+
13+
<form action="{{ request.path }}" method="post">
14+
{{ csrf_input }}
15+
16+
{{ button({
17+
"text": "Submit"
18+
}) }}
19+
</form>
1220
</div>
1321
</div>
1422
{% endblock %}

lung_cancer_screening/questions/jinja2/start.jinja

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
{% extends 'layout.jinja' %}
2+
{% from 'error-summary/macro.jinja' import errorSummary %}
23
{% from 'button/macro.jinja' import button %}
34
{% from 'input/macro.jinja' import input %}
45

56
{% block content %}
7+
8+
{% if error_messages %}
9+
{{ errorSummary({
10+
"titleText": "There is a problem",
11+
"errorList": error_messages
12+
}) }}
13+
{% endif %}
14+
615
<div class="nhsuk-grid-row">
716
<div class="nhsuk-grid-column-two-thirds">
817
<form action="{{ request.path }}" method="POST">
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends 'layout.jinja' %}
2+
3+
{% block content %}
4+
<div class="nhsuk-grid-row">
5+
<div class="nhsuk-grid-column-two-thirds">
6+
Thank you for submitting your responses
7+
</div>
8+
</div>
9+
{% endblock %}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generated by Django 5.2.6 on 2025-09-08 15:57
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('questions', '0006_remove_dateresponse_participant_and_more'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='responseset',
15+
name='submitted',
16+
field=models.BooleanField(default=False),
17+
),
18+
migrations.AlterField(
19+
model_name='responseset',
20+
name='date_of_birth',
21+
field=models.DateField(blank=True, null=True),
22+
),
23+
migrations.AlterField(
24+
model_name='responseset',
25+
name='have_you_ever_smoked',
26+
field=models.IntegerField(blank=True, choices=[(0, 'Yes, I currently smoke'), (1, 'Yes, I used to smoke regularly'), (2, 'Yes, but only a few times'), (3, 'No, I have never smoked')], null=True),
27+
),
28+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 5.2.6 on 2025-09-09 09:34
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('questions', '0007_responseset_submitted_and_more'),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name='responseset',
15+
name='submitted',
16+
),
17+
migrations.AddField(
18+
model_name='responseset',
19+
name='submitted_at',
20+
field=models.DateTimeField(blank=True, null=True),
21+
),
22+
]

0 commit comments

Comments
 (0)