11
2+ from django .contrib import messages
23from django .contrib .auth .models import AnonymousUser
34from django .contrib .messages .storage .fallback import FallbackStorage
45from django .contrib .sessions .middleware import SessionMiddleware
56from django .http import HttpResponse
6- from django .test import RequestFactory , override_settings
7+ from django .test import RequestFactory
78from requests .exceptions import ConnectionError as RequestsConnectionError
9+ from social_core .exceptions import AuthCanceled , AuthFailed
810
911from dojo .middleware import CustomSocialAuthExceptionMiddleware
1012
1113from .dojo_test_case import DojoTestCase
1214
1315
14- @override_settings (
15- SOCIAL_AUTH_OIDC_AUTH_ENABLED = True ,
16- SOCIAL_AUTH_AUTH0_OAUTH2_ENABLED = True ,
17- GOOGLE_OAUTH_ENABLED = True ,
18- SOCIAL_AUTH_OKTA_OAUTH2_ENABLED = True ,
19- AZUREAD_TENANT_OAUTH2_ENABLED = True ,
20- GITLAB_OAUTH2_ENABLED = True ,
21- KEYCLOAK_OAUTH2_ENABLED = True ,
22- GITHUB_ENTERPRISE_OAUTH2_ENABLED = True ,
23- )
2416class TestSocialAuthFailureHandling (DojoTestCase ):
2517
2618 def setUp (self ):
@@ -46,10 +38,17 @@ def test_social_auth_exception_redirects_to_login(self):
4638 "/login/keycloak-oauth2/" ,
4739 "/login/github/" ,
4840 ]
49-
41+ exceptions = [
42+ (RequestsConnectionError ("Host unreachable" ), "Login via social authentication is temporarily unavailable. Please use the standard login below." ),
43+ (AuthCanceled ("User canceled login" ), "Social login was canceled. Please try again or use the standard login." ),
44+ (AuthFailed ("Token exchange failed" ), "Social login failed. Please try again or use the standard login." ),
45+ ]
5046 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" )
47+ for exception , expected_message in exceptions :
48+ with self .subTest (path = path , exception = type (exception ).__name__ ):
49+ request = self ._prepare_request (path )
50+ response = self .middleware .process_exception (request , exception )
51+ self .assertEqual (response .status_code , 302 )
52+ self .assertEqual (response .url , "/login" )
53+ storage = list (messages .get_messages (request ))
54+ self .assertTrue (any (expected_message in str (msg ) for msg in storage ))
0 commit comments