Skip to content

Commit 1a36c5e

Browse files
authored
Merge pull request #1175 from NHSDigital/12341-administrative-users-start-button
Guard start appointment button by permission
2 parents 43cef0e + 5eb6345 commit 1a36c5e

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

manage_breast_screening/assets/js/check-in.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ describe('Check in', () => {
114114
expect(startAppointmentContainer).not.toHaveAttribute('hidden')
115115
})
116116

117+
it('succeeds when start-appointment container is absent (admin user)', async () => {
118+
// Simulate admin user's page — no start-appointment container in DOM
119+
startAppointmentContainer.remove()
120+
121+
jest.mocked(fetch).mockResolvedValue(
122+
/** @type {Response} */ ({
123+
ok: true,
124+
status: 200
125+
})
126+
)
127+
128+
createAll(CheckIn)
129+
130+
await user.click(button)
131+
132+
expect(console.error).not.toHaveBeenCalled()
133+
expect(currentStatus).toHaveAttribute('hidden')
134+
expect(checkedInStatus).not.toHaveAttribute('hidden')
135+
})
136+
117137
it('does not change the DOM if the request fails', async () => {
118138
jest.mocked(fetch).mockResolvedValue(
119139
/** @type {Response} */ ({

manage_breast_screening/core/jinja2/components/start-appointment/template.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
{{ raise('start_appointment_url is required') }}
33
{% endif %}
44

5-
<div data-module="app-start-appointment" data-appointment-id="{{ presented_appointment.pk }}" {% if presented_appointment.can_be_checked_in or not presented_appointment.can_be_started_by(user) %}hidden{% endif %}>
5+
{% if user.has_perm('mammograms.do_mammogram_appointment') %}
6+
<div data-module="app-start-appointment" data-appointment-id="{{ presented_appointment.pk }}" {% if presented_appointment.can_be_checked_in %}hidden{% endif %}>
67
<form action="{{ start_appointment_url }}" method="post" novalidate>
78
<p>
89
<button class="app-button app-button--link">Start appointment<span class="nhsuk-u-visually-hidden"> {{ presented_appointment.participant.full_name }}</span></button>
910
</p>
1011
{{ csrf_input }}
1112
</form>
1213
</div>
14+
{% endif %}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from unittest.mock import MagicMock
2+
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def template(jinja_env):
8+
return jinja_env.get_template("components/start-appointment/template.jinja")
9+
10+
11+
@pytest.fixture
12+
def presented_appointment():
13+
mock = MagicMock()
14+
mock.pk = "abc-123"
15+
mock.participant.full_name = "Jane Smith"
16+
mock.can_be_checked_in = False
17+
return mock
18+
19+
20+
def render(template, user_has_perm, presented_appointment):
21+
user = MagicMock()
22+
user.has_perm.return_value = user_has_perm
23+
return template.render(
24+
{
25+
"user": user,
26+
"presented_appointment": presented_appointment,
27+
"start_appointment_url": "/mammograms/abc-123/start-appointment/",
28+
"csrf_input": "",
29+
}
30+
)
31+
32+
33+
def test_renders_start_button_for_clinical_user(template, presented_appointment):
34+
html = render(
35+
template, user_has_perm=True, presented_appointment=presented_appointment
36+
)
37+
assert 'data-module="app-start-appointment"' in html
38+
39+
40+
def test_does_not_render_start_button_for_admin_user(template, presented_appointment):
41+
html = render(
42+
template, user_has_perm=False, presented_appointment=presented_appointment
43+
)
44+
assert 'data-module="app-start-appointment"' not in html
45+
46+
47+
def test_renders_button_hidden_before_checkin(template, presented_appointment):
48+
presented_appointment.can_be_checked_in = True
49+
html = render(
50+
template, user_has_perm=True, presented_appointment=presented_appointment
51+
)
52+
assert 'data-module="app-start-appointment"' in html
53+
assert 'data-appointment-id="abc-123" hidden' in html
54+
55+
56+
def test_renders_button_visible_after_checkin(template, presented_appointment):
57+
presented_appointment.can_be_checked_in = False
58+
html = render(
59+
template, user_has_perm=True, presented_appointment=presented_appointment
60+
)
61+
assert 'data-module="app-start-appointment"' in html
62+
assert 'data-appointment-id="abc-123" hidden' not in html

0 commit comments

Comments
 (0)