diff --git a/app/models/user.py b/app/models/user.py
index aa92a3f5d..119a95073 100644
--- a/app/models/user.py
+++ b/app/models/user.py
@@ -151,8 +151,11 @@ class User(UserMixin):
def has_permission_for_service(self, service_id, permission):
return permission in self._permissions.get(service_id, [])
+ def belongs_to_service(self, service_id):
+ return str(service_id) in self.services
+
def belongs_to_service_or_403(self, service_id):
- if str(service_id) not in self.services:
+ if not self.belongs_to_service(service_id):
abort(403)
def is_locked(self):
diff --git a/app/templates/withoutnav_template.html b/app/templates/withoutnav_template.html
index d883559d5..fa464ce7c 100644
--- a/app/templates/withoutnav_template.html
+++ b/app/templates/withoutnav_template.html
@@ -2,7 +2,7 @@
{% block fullwidth_content %}
- {% if current_service and current_user.is_authenticated %}
+ {% if current_service and current_user.is_authenticated and current_user.belongs_to_service(current_service.id) %}
diff --git a/tests/app/main/views/accounts/test_choose_accounts.py b/tests/app/main/views/accounts/test_choose_accounts.py
index a82e9bc36..bd0f0352f 100644
--- a/tests/app/main/views/accounts/test_choose_accounts.py
+++ b/tests/app/main/views/accounts/test_choose_accounts.py
@@ -2,7 +2,12 @@ import pytest
from bs4 import BeautifulSoup
from flask import url_for
-from tests.conftest import SERVICE_ONE_ID, normalize_spaces
+from tests.conftest import (
+ SERVICE_ONE_ID,
+ normalize_spaces,
+ service_one,
+ service_two,
+)
SAMPLE_DATA = {
'organisations': [
@@ -137,3 +142,44 @@ def test_choose_account_should_not_show_back_to_service_link_if_not_signed_in(
assert page.select_one('h1').text == 'Sign in' # We’re not signed in
assert page.select_one('.navigation-service a') is None
+
+
+@pytest.mark.parametrize('service, expected_status, page_text', (
+ (service_one, 200, (
+ 'Test Service Switch service '
+ ''
+ 'Dashboard '
+ 'Templates '
+ 'Team members'
+ )),
+ (service_two, 403, (
+ # Page has no ‘back to’ link
+ '403 '
+ 'You do not have permission to view this page.'
+ )),
+))
+def test_should_not_show_back_to_service_if_user_doesnt_belong_to_service(
+ client_request,
+ api_user_active,
+ fake_uuid,
+ mock_get_service,
+ mock_get_service_template,
+ mock_get_template_folders,
+ service,
+ expected_status,
+ page_text,
+):
+ mock_get_service.return_value = service(api_user_active)
+
+ page = client_request.get(
+ 'main.view_template',
+ service_id=mock_get_service.return_value['id'],
+ template_id=fake_uuid,
+ _expected_status=expected_status,
+ )
+
+ assert normalize_spaces(
+ page.select_one('#content').text
+ ).startswith(
+ normalize_spaces(page_text)
+ )
diff --git a/tests/conftest.py b/tests/conftest.py
index d05fcf349..580a28396 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -55,6 +55,11 @@ def service_one(api_user_active):
return service_json(SERVICE_ONE_ID, 'service one', [api_user_active.id])
+@pytest.fixture(scope='function')
+def service_two(api_user_active):
+ return service_json(SERVICE_TWO_ID, 'service two', [api_user_active.id])
+
+
@pytest.fixture(scope='function')
def multiple_reply_to_email_addresses(mocker):
def _get(service_id):