From fab21cad7fa2e85633e993c87cc11db52eae4f13 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Sun, 31 May 2020 20:09:27 +0100 Subject: working on test for cct_user to view person list --- .../templates/organisations/person_list.html | 39 ++++++++++++++++++++ ctrack/organisations/tests/test_views.py | 42 ++++++++++++++++++++-- ctrack/organisations/urls.py | 4 ++- ctrack/organisations/views.py | 14 ++++---- ctrack/templates/base.html | 1 + 5 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 ctrack/organisations/templates/organisations/person_list.html diff --git a/ctrack/organisations/templates/organisations/person_list.html b/ctrack/organisations/templates/organisations/person_list.html new file mode 100644 index 0000000..2e9d333 --- /dev/null +++ b/ctrack/organisations/templates/organisations/person_list.html @@ -0,0 +1,39 @@ +{% extends "base.html" %} + +{% block title %} + People +{% endblock %} + +{% block content %} + +
+
+
+
+
+

People

+
+
+
+
+ + + + + + + + {% for p in object_list %} + + + + + {% endfor %} +
PersonOrganisation
{{ p.first_name }} {{ p.last_name }}{{ p.organisation.name }}
+
+
+
+
+
+ +{% endblock content %} diff --git a/ctrack/organisations/tests/test_views.py b/ctrack/organisations/tests/test_views.py index 7f9da0a..84116de 100644 --- a/ctrack/organisations/tests/test_views.py +++ b/ctrack/organisations/tests/test_views.py @@ -2,9 +2,12 @@ import pytest from django.contrib.auth import get_user_model from django.contrib.auth.models import Group, Permission from django.test import RequestFactory +from django.urls import reverse -from ctrack.organisations.tests.factories import OrganisationFactory -from ctrack.organisations.views import IncidentReportCreateView +from ctrack.caf.tests.factories import PersonFactory +from ctrack.organisations.models import Mode, Submode +from ctrack.organisations.tests.factories import OrganisationFactory, RoleFactory +from ctrack.organisations.views import IncidentReportCreateView, OrganisationDetailView from ..views import OrganisationListView @@ -61,3 +64,38 @@ def test_incident_report_create_view(stakeholder_user): request.user = stakeholder_user response = IncidentReportCreateView.as_view()(request, org.slug) assert response.status_code == 200 + + +def test_only_member_of_cct_user_group_can_view_a_single_person( + stakeholder_user, org_with_people, client +): + role = RoleFactory.create() + submode = Submode.objects.create( + descriptor="Light Rail", mode=Mode.objects.create(descriptor="Rail") + ) + PersonFactory.create( + role=role, + predecessor=None, + organisation__submode=submode, + organisation=org_with_people, + ) + PersonFactory.create( + role=role, + predecessor=None, + organisation__submode=submode, + organisation=org_with_people, + ) + group = Group.objects.create(name="cct_user") + + stakeholder_user.groups.add(group) + + person_list_permission = Permission.objects.get(name="Can view person") + group.permissions.add(person_list_permission) + + client.force_login(stakeholder_user) + + response = client.get(reverse("organisations:people")) + + # They get this permisson via the cct_user group + assert stakeholder_user.has_perm("organisations.view_person") + assert response.status_code == 200 diff --git a/ctrack/organisations/urls.py b/ctrack/organisations/urls.py index 5ccfca0..95967c6 100644 --- a/ctrack/organisations/urls.py +++ b/ctrack/organisations/urls.py @@ -5,11 +5,13 @@ from ctrack.organisations.views import ( OrganisationCreate, OrganisationDetailView, OrganisationListView, + PersonListView, ) app_name = "organisations" urlpatterns = [ + path("people/", view=PersonListView.as_view(), name="people"), path("/", view=OrganisationDetailView.as_view(), name="detail"), path( "/create-incident-report/", @@ -17,6 +19,6 @@ urlpatterns = [ name="create_incident_report", ), path("", view=OrganisationListView.as_view(), name="list"), - path("create", view=OrganisationCreate.as_view(), name="create") + path("create", view=OrganisationCreate.as_view(), name="create"), # path("create", view=OrganisationCreate.as_view(), name="create") ] diff --git a/ctrack/organisations/views.py b/ctrack/organisations/views.py index 30ce5a0..d918c71 100644 --- a/ctrack/organisations/views.py +++ b/ctrack/organisations/views.py @@ -1,17 +1,19 @@ from typing import Any, Dict -from django.contrib.auth.mixins import ( - LoginRequiredMixin, - PermissionRequiredMixin, - UserPassesTestMixin, -) +from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin from django.db import transaction from django.http import HttpResponseRedirect from django.urls import reverse, reverse_lazy from django.views.generic import CreateView, DetailView, FormView, ListView from .forms import AddressInlineFormSet, IncidentReportForm, OrganisationCreateForm -from .models import IncidentReport, Organisation +from .models import IncidentReport, Organisation, Person + + +class PersonListView(LoginRequiredMixin, PermissionRequiredMixin, ListView): + model = Person + template_name = "organisations/person_list.html" + permission_required = "organisations.view_person" class OrganisationCreate(LoginRequiredMixin, CreateView): diff --git a/ctrack/templates/base.html b/ctrack/templates/base.html index dfaaa17..06f3d44 100644 --- a/ctrack/templates/base.html +++ b/ctrack/templates/base.html @@ -57,6 +57,7 @@