|
7 | 7 | from django.conf import settings |
8 | 8 | from django.contrib.auth import SESSION_KEY, get_user_model |
9 | 9 | from django.core import mail |
| 10 | +from django.core.validators import ValidationError |
10 | 11 | from django.test import TestCase, override_settings |
11 | 12 | from django.urls import reverse, reverse_lazy |
12 | 13 | from djangosaml2.tests import auth_response, conf |
@@ -61,12 +62,12 @@ class TestAssertionConsumerServiceView(TestSamlMixin, TestCase): |
61 | 62 | def _get_relay_state(self, redirect_url, org_slug): |
62 | 63 | return f'{redirect_url}?org={org_slug}' |
63 | 64 |
|
64 | | - def _get_saml_response_for_acs_view(self, relay_state): |
| 65 | + def _get_saml_response_for_acs_view( self, relay_state, uid='[email protected]'): |
65 | 66 | response = self.client.get(self.login_url, {'RelayState': relay_state}) |
66 | 67 | saml2_req = saml2_from_httpredirect_request(response.url) |
67 | 68 | session_id = get_session_id_from_saml2(saml2_req) |
68 | 69 | self.add_outstanding_query(session_id, relay_state) |
69 | | - return auth_response( session_id, '[email protected]'), relay_state |
| 70 | + return auth_response(session_id, uid), relay_state |
70 | 71 |
|
71 | 72 | def _post_successful_auth_assertions(self, query_params, org_slug): |
72 | 73 | self.assertEqual(User.objects.count(), 1) |
@@ -108,6 +109,24 @@ def test_organization_slug_present(self): |
108 | 109 | query_params = parse_qs(urlparse(response.url).query) |
109 | 110 | self._post_successful_auth_assertions(query_params, org_slug) |
110 | 111 |
|
| 112 | + @capture_any_output() |
| 113 | + def test_invalid_email_raise_validation_error(self): |
| 114 | + invalid_email = 'invalid_email@example' |
| 115 | + relay_state = self._get_relay_state( |
| 116 | + redirect_url='https://captive-portal.example.com', org_slug='default' |
| 117 | + ) |
| 118 | + saml_response, relay_state = self._get_saml_response_for_acs_view( |
| 119 | + relay_state, uid=invalid_email |
| 120 | + ) |
| 121 | + with self.assertRaises(ValidationError): |
| 122 | + self.client.post( |
| 123 | + reverse('radius:saml2_acs'), |
| 124 | + { |
| 125 | + 'SAMLResponse': self.b64_for_post(saml_response), |
| 126 | + 'RelayState': relay_state, |
| 127 | + }, |
| 128 | + ) |
| 129 | + |
111 | 130 | @capture_any_output() |
112 | 131 | def test_relay_state_relative_path(self): |
113 | 132 | expected_redirect_path = '/radius/saml2/additional-info/' |
|
0 commit comments