@@ -18,6 +18,7 @@ def personas():
1818 nhs_uid = persona .username ,
1919 first_name = persona .first_name ,
2020 last_name = persona .last_name ,
21+ is_superuser = persona .is_superuser ,
2122 )
2223 UserAssignmentFactory (
2324 user = user ,
@@ -51,6 +52,36 @@ def test_post_persona_login(client):
5152 assert response .headers ["location" ] == "/current-provider/select/?next=%2Fsome-url"
5253
5354
55+ @pytest .mark .django_db
56+ def test_post_persona_login_superuser_redirects_to_admin (client ):
57+ response = client .post (
58+ reverse ("auth:persona_login" ),
59+ {"username" : "priya_bains" },
60+ )
61+ assert response .status_code == 302
62+ assert response .headers ["location" ] == "/admin/"
63+
64+
65+ @pytest .mark .django_db
66+ def test_post_persona_login_superuser_with_root_next_redirects_to_admin (client ):
67+ response = client .post (
68+ reverse ("auth:persona_login" ),
69+ {"username" : "priya_bains" , "next" : "/" },
70+ )
71+ assert response .status_code == 302
72+ assert response .headers ["location" ] == "/admin/"
73+
74+
75+ @pytest .mark .django_db
76+ def test_post_persona_login_superuser_with_next_redirects_to_select_provider (client ):
77+ response = client .post (
78+ reverse ("auth:persona_login" ),
79+ {"username" : "priya_bains" , "next" : "/some-url" },
80+ )
81+ assert response .status_code == 302
82+ assert response .headers ["location" ] == "/current-provider/select/?next=%2Fsome-url"
83+
84+
5485@pytest .mark .django_db
5586@override_settings (CIS2_ACR_VALUES = "some-test-acr-value" )
5687def test_cis2_login_uses_configured_acr_values (client , monkeypatch ):
@@ -187,6 +218,7 @@ def test_accepts_valid_assurance_levels(
187218
188219 mock_user = Mock ()
189220 mock_user .nhs_uid = "user-123"
221+ mock_user .is_superuser = False
190222 mock_authenticate = Mock (return_value = mock_user )
191223 mock_login = Mock ()
192224
@@ -208,3 +240,24 @@ def test_accepts_valid_assurance_levels(
208240 ANY , cis2_sub = "user-123" , cis2_userinfo = {"sub" : "user-123" }
209241 )
210242 mock_login .assert_called_once_with (ANY , mock_user )
243+
244+ def test_superuser_redirects_to_admin (
245+ self , client , monkeypatch , mock_cis2_client_factory
246+ ):
247+ """Superusers should always be sent to the admin site after login."""
248+ mock_cis2_client_factory ()
249+
250+ mock_user = Mock ()
251+ mock_user .nhs_uid = "user-123"
252+ mock_user .is_superuser = True
253+
254+ monkeypatch .setattr (
255+ "manage_breast_screening.auth.views.authenticate" ,
256+ Mock (return_value = mock_user ),
257+ )
258+ monkeypatch .setattr ("manage_breast_screening.auth.views.auth_login" , Mock ())
259+
260+ response = client .get (reverse ("auth:cis2_callback" ))
261+
262+ assert response .status_code == 302
263+ assert response .headers ["location" ] == "/admin/"
0 commit comments