|
| 1 | +from django.urls import reverse |
| 2 | +from django.utils.translation import gettext_lazy as _ |
| 3 | + |
| 4 | +from django_webtest import WebTest |
| 5 | +from mozilla_django_oidc_db.models import OpenIDConnectConfig |
| 6 | + |
| 7 | +from open_inwoner.accounts.tests.factories import UserFactory |
| 8 | +from open_inwoner.configurations.models import SiteConfiguration |
| 9 | +from open_inwoner.utils.test import ClearCachesMixin |
| 10 | + |
| 11 | +from ..choices import OpenIDDisplayChoices |
| 12 | + |
| 13 | + |
| 14 | +class OIDCConfigTest(ClearCachesMixin, WebTest): |
| 15 | + csrf_checks = False |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def setUpTestData(cls): |
| 19 | + super().setUpTestData() |
| 20 | + |
| 21 | + openid_config = OpenIDConnectConfig.get_solo() |
| 22 | + openid_config.enabled = True |
| 23 | + openid_config.save() |
| 24 | + |
| 25 | + def test_admin_only_enabled(self): |
| 26 | + """Assert that the OIDC login option is only displayed for login via admin""" |
| 27 | + |
| 28 | + config = SiteConfiguration.get_solo() |
| 29 | + config.openid_display = OpenIDDisplayChoices.admin |
| 30 | + config.save() |
| 31 | + |
| 32 | + # regular site |
| 33 | + response = self.app.get(reverse("login")) |
| 34 | + |
| 35 | + self.assertNotContains(response, _("Login with Azure AD")) |
| 36 | + |
| 37 | + # admin login |
| 38 | + response = self.app.get(reverse("admin:login")) |
| 39 | + |
| 40 | + oidc_login_option = response.pyquery.find(".admin-login-option") |
| 41 | + |
| 42 | + self.assertEqual(oidc_login_option.text(), _("Login with organization account")) |
| 43 | + |
| 44 | + def test_admin_only_disabled(self): |
| 45 | + """Assert that the OIDC login option is only displayed for regular users""" |
| 46 | + |
| 47 | + config = SiteConfiguration.get_solo() |
| 48 | + config.openid_display = OpenIDDisplayChoices.regular |
| 49 | + config.save() |
| 50 | + |
| 51 | + # regular site |
| 52 | + response = self.app.get(reverse("login")) |
| 53 | + |
| 54 | + link = response.pyquery.find("[title='Login with Azure AD']") |
| 55 | + link_text = link.find(".link__text").text() |
| 56 | + |
| 57 | + self.assertEqual(link_text, _("Login with Azure AD")) |
| 58 | + |
| 59 | + # admin login |
| 60 | + response = self.client.get(reverse("admin:login")) |
| 61 | + |
| 62 | + self.assertNotContains(response, _("Login with organization account")) |
| 63 | + |
| 64 | + def test_oidc_config_validation(self): |
| 65 | + """ |
| 66 | + Assert that error is displayed on attempt to select OIDC login for regular users |
| 67 | + if and only if `make_users_staff` is enabled in `OpenIDConnectConfig`. |
| 68 | + """ |
| 69 | + |
| 70 | + self.user = UserFactory(is_superuser=True, is_staff=True) |
| 71 | + self.client.force_login(self.user) |
| 72 | + |
| 73 | + # case 1: `make_users_staff` is True |
| 74 | + openid_config = OpenIDConnectConfig.get_solo() |
| 75 | + openid_config.enabled = True |
| 76 | + openid_config.make_users_staff = True |
| 77 | + openid_config.save() |
| 78 | + |
| 79 | + url = reverse("admin:configurations_siteconfiguration_change") |
| 80 | + response = self.app.post( |
| 81 | + url, user=self.user, params={"openid_display": OpenIDDisplayChoices.regular} |
| 82 | + ) |
| 83 | + |
| 84 | + field = response.pyquery(".field-openid_display") |
| 85 | + error_text = field.find(".errorlist").text() |
| 86 | + expected_error_text = _( |
| 87 | + "You cannot select this option if 'Make users staff' " |
| 88 | + "is selected in the OpenID Connect configuration." |
| 89 | + ) |
| 90 | + |
| 91 | + self.assertEqual(expected_error_text, error_text) |
| 92 | + |
| 93 | + # case 2: `make_users_staff` is False |
| 94 | + openid_config.make_users_staff = False |
| 95 | + openid_config.save() |
| 96 | + |
| 97 | + response = self.client.post( |
| 98 | + url, user=self.user, data={"openid_display": "regular"} |
| 99 | + ) |
| 100 | + |
| 101 | + self.assertNotContains(response, expected_error_text) |
0 commit comments