11import pytest
22from django .conf import settings
3+ from django .test import override_settings
34from social_core .exceptions import AuthException
45
56from ansible_base .authentication .authenticator_plugins .utils import get_authenticator_class
910from test_app .models import User
1011
1112
13+ def load_social_auth_settings ():
14+ return {"SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL" : settings .SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL }
15+
16+
1217@pytest .mark .django_db
1318class TestAuthenticationUtilsAuthentication :
1419 logger = 'ansible_base.authentication.utils.authentication.logger'
1520
16- def test_fake_backend_settings (self ):
21+ @pytest .mark .parametrize (
22+ "name, exp_val, username_is_full_email_setting" ,
23+ [
24+ ("USER_FIELDS" , ["username" , "email" ], False ),
25+ ("SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL" , True , True ),
26+ ("SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL" , False , False ),
27+ ("BOGUS" , None , False ),
28+ ],
29+ )
30+ def test_fake_backend_settings (self , name , exp_val , username_is_full_email_setting ):
31+ with override_settings (SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = username_is_full_email_setting ):
32+ with override_settings (
33+ ANSIBLE_BASE_SOCIAL_AUTH_STRATEGY_SETTINGS_FUNCTION = "test_app.tests.authentication.utils.test_authentication.load_social_auth_settings"
34+ ):
35+ backend = authentication .FakeBackend ()
36+ response = backend .setting (name )
37+ assert response == exp_val
38+
39+ def test_fake_backend_settings_with_default (self ):
1740 backend = authentication .FakeBackend ()
18- response = backend .setting ()
19- assert response == [ "username" , "email" ]
41+ response = backend .setting ("BOGUS" , "bogus_default" )
42+ assert response == "bogus_default"
2043
2144 def test_get_local_username_no_input (self ):
2245 response = authentication .get_local_username ({})
@@ -26,6 +49,22 @@ def test_get_local_user_username_existing_user(self, random_user):
2649 response = authentication .get_local_username ({'username' : random_user .username })
2750 assert len (response ) > len (random_user .username )
2851
52+ @pytest .mark .parametrize (
53+ "username_is_full_email_setting, expected_username" ,
54+ [
55+ 56+ (False , "new-user" ),
57+ ],
58+ )
59+ def test_get_local_username_with_email (self , username_is_full_email_setting , expected_username ):
60+ user_details = {
'username' :
'new-user' ,
'email' :
'[email protected] ' }
61+ with override_settings (SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = username_is_full_email_setting ):
62+ with override_settings (
63+ ANSIBLE_BASE_SOCIAL_AUTH_STRATEGY_SETTINGS_FUNCTION = "test_app.tests.authentication.utils.test_authentication.load_social_auth_settings"
64+ ):
65+ response = authentication .get_local_username (user_details )
66+ assert response == expected_username
67+
2968 @pytest .mark .parametrize (
3069 "related_authenticator,info_message,expected_username" ,
3170 [
0 commit comments