|
1 | 1 |
|
| 2 | +from django.contrib import messages |
2 | 3 | from django.contrib.auth.models import AnonymousUser |
3 | 4 | from django.contrib.messages.storage.fallback import FallbackStorage |
4 | 5 | from django.contrib.sessions.middleware import SessionMiddleware |
5 | 6 | from django.http import HttpResponse |
6 | 7 | from django.test import RequestFactory, override_settings |
7 | 8 | from requests.exceptions import ConnectionError as RequestsConnectionError |
| 9 | +from social_core.exceptions import AuthCanceled, AuthFailed |
8 | 10 |
|
9 | 11 | from dojo.middleware import CustomSocialAuthExceptionMiddleware |
10 | 12 |
|
@@ -46,10 +48,17 @@ def test_social_auth_exception_redirects_to_login(self): |
46 | 48 | "/login/keycloak-oauth2/", |
47 | 49 | "/login/github/", |
48 | 50 | ] |
49 | | - |
| 51 | + exceptions = [ |
| 52 | + (RequestsConnectionError("Host unreachable"), "Login via social authentication is temporarily unavailable. Please use the standard login below."), |
| 53 | + (AuthCanceled("User canceled login"), "Social login was canceled. Please try again or use the standard login."), |
| 54 | + (AuthFailed("Token exchange failed"), "Social login failed. Please try again or use the standard login."), |
| 55 | + ] |
50 | 56 | for path in login_paths: |
51 | | - with self.subTest(path=path): |
52 | | - request = self._prepare_request(path) |
53 | | - response = self.middleware.process_exception(request, RequestsConnectionError("Host unreachable")) |
54 | | - self.assertEqual(response.status_code, 302) |
55 | | - self.assertEqual(response.url, "/login") |
| 57 | + for exception, expected_message in exceptions: |
| 58 | + with self.subTest(path=path, exception=type(exception).__name__): |
| 59 | + request = self._prepare_request(path) |
| 60 | + response = self.middleware.process_exception(request, exception) |
| 61 | + self.assertEqual(response.status_code, 302) |
| 62 | + self.assertEqual(response.url, "/login") |
| 63 | + storage = list(messages.get_messages(request)) |
| 64 | + self.assertTrue(any(expected_message in str(msg) for msg in storage)) |
0 commit comments