@@ -47,3 +47,100 @@ def test_dupe_slug(ldap_authenticator):
47
47
48
48
dupe .save ()
49
49
assert dupe .slug != ldap_slug , "authenticator slugs should be unique"
50
+
51
+
52
+ @pytest .mark .django_db
53
+ @mock .patch ("ansible_base.authentication.authenticator_plugins.oidc.OpenIdConnectAuth.JWT_ALGORITHMS" , ["RS256" , "HS256" ])
54
+ @mock .patch ("logging.getLogger" )
55
+ def test_oidc_jwt_algorithms_auto_population (mock_get_logger ):
56
+ """Test that JWT algorithms are automatically populated when creating an OIDC authenticator"""
57
+ from ansible_base .authentication .models import Authenticator
58
+
59
+ # Mock the logger
60
+ mock_logger = mock .MagicMock ()
61
+ mock_get_logger .return_value = mock_logger
62
+
63
+ # Create OIDC authenticator without JWT_ALGORITHMS
64
+ oidc_config = {
65
+ "OIDC_ENDPOINT" : "https://example.com" ,
66
+ "VERIFY_SSL" : True ,
67
+ "KEY" : "test-client-id" ,
68
+ "SECRET" : "test-client-secret" ,
69
+ }
70
+
71
+ # Mock the OIDC plugin to return algorithms from .well-known
72
+ with mock .patch ("ansible_base.authentication.authenticator_plugins.oidc.AuthenticatorPlugin._get_jwt_algorithms" ) as mock_get_algs :
73
+ mock_get_algs .return_value = ["RS256" , "ES256" ]
74
+
75
+ oidc_auth = Authenticator .objects .create (name = "Test OIDC" , type = "ansible_base.authentication.authenticator_plugins.oidc" , configuration = oidc_config )
76
+
77
+ # Verify that JWT_ALGORITHMS was populated
78
+ assert "JWT_ALGORITHMS" in oidc_auth .configuration
79
+ assert oidc_auth .configuration ["JWT_ALGORITHMS" ] == ["RS256" , "ES256" ]
80
+ mock_logger .info .assert_called_with ("Successfully populated JWT algorithms: ['RS256', 'ES256']" )
81
+
82
+
83
+ @pytest .mark .django_db
84
+ def test_oidc_jwt_algorithms_not_populated_when_already_set ():
85
+ """Test that JWT algorithms are not modified when already configured"""
86
+ from ansible_base .authentication .models import Authenticator
87
+
88
+ # Create OIDC authenticator with JWT_ALGORITHMS already set
89
+ oidc_config = {
90
+ "OIDC_ENDPOINT" : "https://example.com" ,
91
+ "VERIFY_SSL" : True ,
92
+ "KEY" : "test-client-id" ,
93
+ "SECRET" : "test-client-secret" ,
94
+ "JWT_ALGORITHMS" : ["RS256" ], # Already configured
95
+ }
96
+
97
+ oidc_auth = Authenticator .objects .create (
98
+ name = "Test OIDC Configured" , type = "ansible_base.authentication.authenticator_plugins.oidc" , configuration = oidc_config
99
+ )
100
+
101
+ # Verify that JWT_ALGORITHMS was not modified
102
+ assert oidc_auth .configuration ["JWT_ALGORITHMS" ] == ["RS256" ]
103
+
104
+
105
+ @pytest .mark .django_db
106
+ def test_non_oidc_authenticator_not_affected ():
107
+ """Test that non-OIDC authenticators are not affected by JWT algorithm logic"""
108
+ from ansible_base .authentication .models import Authenticator
109
+
110
+ # Create LDAP authenticator (non-OIDC)
111
+ ldap_config = {
112
+ "SERVER_URI" : "ldap://example.com" ,
113
+ "BIND_DN" : "cn=admin,dc=example,dc=com" ,
114
+ "BIND_PASSWORD" : "password" ,
115
+ "USER_SEARCH" : ["ou=users,dc=example,dc=com" , "SCOPE_SUBTREE" , "(sAMAccountName=%(user)s)" ],
116
+ }
117
+
118
+ ldap_auth = Authenticator .objects .create (name = "Test LDAP" , type = "ansible_base.authentication.authenticator_plugins.ldap" , configuration = ldap_config )
119
+
120
+ # Verify that LDAP authenticator doesn't have JWT_ALGORITHMS
121
+ assert "JWT_ALGORITHMS" not in ldap_auth .configuration
122
+
123
+
124
+ @pytest .mark .django_db
125
+ @mock .patch ("ansible_base.authentication.authenticator_plugins.oidc.OpenIdConnectAuth.JWT_ALGORITHMS" , ["RS256" , "HS256" ])
126
+ @mock .patch ("logging.getLogger" )
127
+ def test_oidc_jwt_algorithms_auto_population_on_update (mock_get_logger ):
128
+ """Test that JWT algorithms are auto-populated when updating an OIDC authenticator"""
129
+ from ansible_base .authentication .models import Authenticator
130
+
131
+ # Mock the logger
132
+ mock_logger = mock .MagicMock ()
133
+ mock_get_logger .return_value = mock_logger
134
+
135
+ # Create OIDC authenticator without JWT_ALGORITHMS
136
+ oidc_config = {
137
+ "OIDC_ENDPOINT" : "https://example.com" ,
138
+ "VERIFY_SSL" : True ,
139
+ "KEY" : "test-client-id" ,
140
+ "SECRET" : "test-client-secret" ,
141
+ }
142
+
143
+ oidc_auth = Authenticator .objects .create (name = "Test OIDC Update" , type = "ansible_base.authentication.authenticator_plugins.oidc" , configuration = oidc_config )
144
+
145
+ assert "JWT_ALGORITHMS" in oidc_auth .configuration
146
+ assert oidc_auth .configuration ["JWT_ALGORITHMS" ] == ["RS256" , "HS256" ]
0 commit comments