|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +from importlib import reload |
| 6 | +from unittest import mock |
| 7 | + |
| 8 | +from django.contrib.auth.models import User |
| 9 | +from django.test import TestCase, override_settings |
| 10 | +from django.urls import reverse |
| 11 | + |
| 12 | + |
| 13 | +class LoginTestBase(TestCase): |
| 14 | + TEST_ADMIN_PASSWORD = "admin12345" |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + self.wagtail_login_url = reverse("wagtailadmin_login") |
| 18 | + self.django_admin_login_url = reverse("admin:login") |
| 19 | + |
| 20 | + def _create_admin(self): |
| 21 | + # create an admin user |
| 22 | + admin = User.objects.create_superuser( |
| 23 | + username="admin", |
| 24 | + |
| 25 | + password=self.TEST_ADMIN_PASSWORD, |
| 26 | + ) |
| 27 | + assert admin.is_active is True |
| 28 | + assert admin.has_usable_password() is True |
| 29 | + assert admin.check_password(self.TEST_ADMIN_PASSWORD) is True |
| 30 | + assert admin.is_staff is True |
| 31 | + assert admin.is_superuser is True |
| 32 | + |
| 33 | + return admin |
| 34 | + |
| 35 | + |
| 36 | +class ConventionalLoginDeniedTest(LoginTestBase): |
| 37 | + """Tests to show that the standard way to sign in to Wagtail and the Django |
| 38 | + Admin just do not work (which is good, because everyone should use SSO |
| 39 | + in production)""" |
| 40 | + |
| 41 | + @override_settings( |
| 42 | + WAGTAIL_ENABLE_ADMIN=True, |
| 43 | + USE_SSO_AUTH=True, |
| 44 | + AUTHENTICATION_BACKENDS=("mozilla_django_oidc.auth.OIDCAuthenticationBackend",), |
| 45 | + ) |
| 46 | + def test_login_page_contains_no_form(self): |
| 47 | + for url in (self.wagtail_login_url, self.django_admin_login_url): |
| 48 | + with self.subTest(url=url): |
| 49 | + response = self.client.get(url) |
| 50 | + assert response.status_code == 200 |
| 51 | + # Check for the form field attrs that would normally be present |
| 52 | + self.assertNotContains(response, b'name="username"') |
| 53 | + self.assertNotContains(response, b'name="password"') |
| 54 | + # No CSRF token == no go, anyway |
| 55 | + self.assertNotContains(response, b"csrfmiddlewaretoken") |
| 56 | + # Confirm SSO link |
| 57 | + self.assertContains(response, b"Sign in with Mozilla SSO") |
| 58 | + |
| 59 | + @override_settings( |
| 60 | + WAGTAIL_ENABLE_ADMIN=True, |
| 61 | + USE_SSO_AUTH=True, |
| 62 | + AUTHENTICATION_BACKENDS=("mozilla_django_oidc.auth.OIDCAuthenticationBackend",), |
| 63 | + ) |
| 64 | + def test_posting_to_login_denied(self): |
| 65 | + admin = self._create_admin() |
| 66 | + |
| 67 | + for url, error_message, expected_template in ( |
| 68 | + ( |
| 69 | + self.wagtail_login_url, |
| 70 | + b"Your username and password didn't match.", |
| 71 | + "wagtailadmin/login.html", |
| 72 | + ), |
| 73 | + ( |
| 74 | + self.django_admin_login_url, |
| 75 | + b"Please enter the correct username and password for a staff account.", |
| 76 | + "admin/login.html", |
| 77 | + ), |
| 78 | + ): |
| 79 | + payload = { |
| 80 | + "username": admin.username, |
| 81 | + "password": self.TEST_ADMIN_PASSWORD, |
| 82 | + } |
| 83 | + with self.subTest( |
| 84 | + url=url, |
| 85 | + error_message=error_message, |
| 86 | + expected_template=expected_template, |
| 87 | + ): |
| 88 | + response = self.client.post(url, data=payload, follow=True) |
| 89 | + self.assertEqual( |
| 90 | + response.status_code, |
| 91 | + 200, # 200 is what comes back after the redirect |
| 92 | + ) |
| 93 | + # Show that while we provided valid credentials, we still get |
| 94 | + # treated as if they are not the correct ones. |
| 95 | + self.assertContains(response, error_message) |
| 96 | + self.assertContains(response, b"Sign in with Mozilla SSO") |
| 97 | + self.assertTemplateUsed(response, expected_template) |
| 98 | + |
| 99 | + |
| 100 | +class AuthenticationBackendSelectionTests(TestCase): |
| 101 | + # We have to force the USE_SSO_AUTH to True at the environment level |
| 102 | + # then import the settings to trigger the appropriate if/else branch |
| 103 | + # that sets the right auth backend. |
| 104 | + |
| 105 | + @mock.patch.dict("os.environ", {"USE_SSO_AUTH": "True"}) |
| 106 | + def test_only_sso_backend_enabled_if_USE_SSO_AUTH_is_True(self): |
| 107 | + from bedrock.settings import base as base_settings |
| 108 | + |
| 109 | + reloaded_settings = reload(base_settings) |
| 110 | + |
| 111 | + self.assertEqual( |
| 112 | + reloaded_settings.AUTHENTICATION_BACKENDS, |
| 113 | + ("mozilla_django_oidc.auth.OIDCAuthenticationBackend",), |
| 114 | + ) |
| 115 | + |
| 116 | + @mock.patch.dict("os.environ", {"USE_SSO_AUTH": "False"}) |
| 117 | + def test_only_model_backend_enabled_if_USE_SSO_AUTH_is_False(self): |
| 118 | + from bedrock.settings import base as base_settings |
| 119 | + |
| 120 | + reloaded_settings = reload(base_settings) |
| 121 | + |
| 122 | + self.assertEqual( |
| 123 | + reloaded_settings.AUTHENTICATION_BACKENDS, |
| 124 | + ("django.contrib.auth.backends.ModelBackend",), |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +class ConventionalLoginAllowedTest(LoginTestBase): |
| 129 | + """If certain settings are set in settings.local, regular |
| 130 | + username + password sign-in functionality is restored |
| 131 | + """ |
| 132 | + |
| 133 | + @override_settings(WAGTAIL_ENABLE_ADMIN=True, USE_SSO_AUTH=False) |
| 134 | + def test_login_page_contains_form(self): |
| 135 | + for url in (self.wagtail_login_url, self.django_admin_login_url): |
| 136 | + with self.subTest(url=url): |
| 137 | + response = self.client.get(url) |
| 138 | + assert response.status_code == 200 |
| 139 | + # Check for the form field attrs that would normally be present |
| 140 | + self.assertContains(response, b'name="username"', 1) |
| 141 | + self.assertContains(response, b'name="password"', 1) |
| 142 | + self.assertContains(response, b"csrfmiddlewaretoken", 1) |
| 143 | + self.assertNotContains(response, b"Sign in with Mozilla SSO") |
| 144 | + |
| 145 | + @override_settings( |
| 146 | + AUTHENTICATION_BACKENDS=("django.contrib.auth.backends.ModelBackend",), |
| 147 | + WAGTAIL_ENABLE_ADMIN=True, |
| 148 | + USE_SSO_AUTH=False, |
| 149 | + ) |
| 150 | + def test_posting_to_login_works_if_the_modelbackend_is_configured(self): |
| 151 | + # Only relevant to local usage, but good to confirm |
| 152 | + admin = self._create_admin() |
| 153 | + for url, expected_template in ( |
| 154 | + (self.wagtail_login_url, "wagtailadmin/home.html"), |
| 155 | + ( |
| 156 | + self.django_admin_login_url, |
| 157 | + "wagtailadmin/home.html", |
| 158 | + # That expected template is correct. Signing in to Django Admin |
| 159 | + # redirects to Wagtail's Admin, because that's what |
| 160 | + # LOGIN_REDIRECT_URL points to |
| 161 | + ), |
| 162 | + ): |
| 163 | + payload = { |
| 164 | + "username": admin.username, |
| 165 | + "password": self.TEST_ADMIN_PASSWORD, |
| 166 | + } |
| 167 | + with self.subTest(url=url, expected_template=expected_template): |
| 168 | + response = self.client.post(url, data=payload, follow=True) |
| 169 | + self.assertEqual(response.status_code, 200) |
| 170 | + self.assertNotContains(response, b"Sign in") |
| 171 | + self.assertTemplateUsed(response, expected_template) |
0 commit comments