Skip to content

Commit 9c5c0b8

Browse files
committed
extract constants for permission names
1 parent e0bd2e4 commit 9c5c0b8

5 files changed

Lines changed: 25 additions & 16 deletions

File tree

manage_breast_screening/auth/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ class Role(StrEnum):
88
SUPERUSER = "Superuser"
99

1010

11+
class Permission(StrEnum):
12+
VIEW_PARTICIPANT_DATA = "participants.view_participant_data"
13+
PERFORM_MAMMOGRAM_APPOINTMENT = "mammograms.perform_mammogram_appointment"
14+
15+
1116
@dataclass
1217
class Persona:
1318
first_name: str

manage_breast_screening/auth/rules.py

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

77
import rules
88

9-
from .models import Role
9+
from .models import Permission, Role
1010

1111

1212
def user_has_any_role(role, *other_roles):
@@ -22,7 +22,7 @@ def check(user):
2222

2323
# fmt: off
2424

25-
rules.add_perm("participants.view_participant_data", user_has_any_role(Role.CLINICAL, Role.ADMINISTRATIVE))
26-
rules.add_perm("mammograms.perform_mammogram_appointment", user_has_any_role(Role.CLINICAL))
25+
rules.add_perm(Permission.VIEW_PARTICIPANT_DATA, user_has_any_role(Role.CLINICAL, Role.ADMINISTRATIVE))
26+
rules.add_perm(Permission.PERFORM_MAMMOGRAM_APPOINTMENT, user_has_any_role(Role.CLINICAL))
2727

2828
# fmt: on
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22

3+
from manage_breast_screening.auth.models import Permission
4+
35

46
@pytest.mark.django_db
57
class TestRules:
@@ -8,19 +10,19 @@ def test_rule_requiring_clinical_role(
810
):
911
user.groups.add()
1012

11-
assert not user.has_perm("mammograms.perform_mammogram_appointment")
13+
assert not user.has_perm(Permission.PERFORM_MAMMOGRAM_APPOINTMENT)
1214
assert not administrative_user.has_perm(
13-
"mammograms.perform_mammogram_appointment"
15+
Permission.PERFORM_MAMMOGRAM_APPOINTMENT
1416
)
15-
assert clinical_user.has_perm("mammograms.perform_mammogram_appointment")
16-
assert superuser.has_perm("mammograms.perform_mammogram_appointment")
17+
assert clinical_user.has_perm(Permission.PERFORM_MAMMOGRAM_APPOINTMENT)
18+
assert superuser.has_perm(Permission.PERFORM_MAMMOGRAM_APPOINTMENT)
1719

1820
def test_rule_requiring_clinical_or_administrative_role(
1921
self, user, administrative_user, clinical_user, superuser
2022
):
2123
user.groups.add()
2224

23-
assert not user.has_perm("participants.view_participant_data")
24-
assert administrative_user.has_perm("participants.view_participant_data")
25-
assert clinical_user.has_perm("participants.view_participant_data")
26-
assert superuser.has_perm("participants.view_participant_data")
25+
assert not user.has_perm(Permission.VIEW_PARTICIPANT_DATA)
26+
assert administrative_user.has_perm(Permission.VIEW_PARTICIPANT_DATA)
27+
assert clinical_user.has_perm(Permission.VIEW_PARTICIPANT_DATA)
28+
assert superuser.has_perm(Permission.VIEW_PARTICIPANT_DATA)

manage_breast_screening/mammograms/views/appointment_flow.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.views.decorators.http import require_http_methods
99
from django.views.generic import FormView, TemplateView
1010

11+
from manage_breast_screening.auth.models import Permission
1112
from manage_breast_screening.core.services.auditor import Auditor
1213
from manage_breast_screening.participants.models import (
1314
Appointment,
@@ -60,7 +61,7 @@ class InProgressAppointmentMixin(PermissionRequiredMixin, AppointmentMixin):
6061
If the appointment is not in progress, redirect to the appointment show page.
6162
"""
6263

63-
permission_required = "mammograms.perform_mammogram_appointment"
64+
permission_required = Permission.PERFORM_MAMMOGRAM_APPOINTMENT
6465

6566
def dispatch(self, request, *args, **kwargs):
6667
appointment = self.appointment # type: ignore
@@ -83,7 +84,7 @@ class ShowAppointment(AppointmentMixin, View):
8384
def get(self, request, *args, **kwargs):
8485
appointment = self.appointment
8586
if (
86-
request.user.has_perm("mammograms.perform_mammogram_appointment")
87+
request.user.has_perm(Permission.PERFORM_MAMMOGRAM_APPOINTMENT)
8788
and appointment.current_status.in_progress
8889
):
8990
return redirect("mammograms:start_screening", pk=self.appointment.pk)

manage_breast_screening/participants/views.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.shortcuts import get_object_or_404, redirect, render
66
from django.urls import reverse
77

8+
from manage_breast_screening.auth.models import Permission
89
from manage_breast_screening.mammograms.presenters import LastKnownMammogramPresenter
910
from manage_breast_screening.participants.services import fetch_current_provider
1011

@@ -32,7 +33,7 @@ def parse_return_url(request, default: str) -> str:
3233
return return_url
3334

3435

35-
@permission_required("participants.view_participant_data")
36+
@permission_required(Permission.VIEW_PARTICIPANT_DATA)
3637
def show(request, pk):
3738
participant = get_object_or_404(Participant, pk=pk)
3839
presented_participant = ParticipantPresenter(participant)
@@ -75,7 +76,7 @@ def show(request, pk):
7576
)
7677

7778

78-
@permission_required("participants.view_participant_data")
79+
@permission_required(Permission.VIEW_PARTICIPANT_DATA)
7980
def edit_ethnicity(request, pk):
8081
participant = get_object_or_404(Participant, pk=pk)
8182

@@ -109,7 +110,7 @@ def edit_ethnicity(request, pk):
109110
)
110111

111112

112-
@permission_required("participants.view_participant_data")
113+
@permission_required(Permission.VIEW_PARTICIPANT_DATA)
113114
def add_previous_mammogram(request, pk):
114115
participant = get_object_or_404(Participant, pk=pk)
115116
current_provider = fetch_current_provider(pk)

0 commit comments

Comments
 (0)