1
1
import pytest
2
2
from django .conf import settings
3
+ from django .test import override_settings
3
4
from social_core .exceptions import AuthException
4
5
5
6
from ansible_base .authentication .authenticator_plugins .utils import get_authenticator_class
9
10
from test_app .models import User
10
11
11
12
13
+ def load_social_auth_settings ():
14
+ return {"SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL" : settings .SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL }
15
+
16
+
12
17
@pytest .mark .django_db
13
18
class TestAuthenticationUtilsAuthentication :
14
19
logger = 'ansible_base.authentication.utils.authentication.logger'
15
20
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 ):
17
40
backend = authentication .FakeBackend ()
18
- response = backend .setting ()
19
- assert response == [ "username" , "email" ]
41
+ response = backend .setting ("BOGUS" , "bogus_default" )
42
+ assert response == "bogus_default"
20
43
21
44
def test_get_local_username_no_input (self ):
22
45
response = authentication .get_local_username ({})
@@ -26,6 +49,22 @@ def test_get_local_user_username_existing_user(self, random_user):
26
49
response = authentication .get_local_username ({'username' : random_user .username })
27
50
assert len (response ) > len (random_user .username )
28
51
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
+
29
68
@pytest .mark .parametrize (
30
69
"related_authenticator,info_message,expected_username" ,
31
70
[
0 commit comments