@@ -305,3 +305,63 @@ def test_authentication_get_userinfo_invalid_response():
305305 match = "Invalid response format or token verification failed" ,
306306 ):
307307 oidc_backend .get_userinfo ("fake_access_token" , None , None )
308+
309+
310+ def test_authentication_getter_existing_disabled_user_via_sub (
311+ django_assert_num_queries , monkeypatch
312+ ):
313+ """
314+ If an existing user matches the sub but is disabled,
315+ an error should be raised and a user should not be created.
316+ """
317+
318+ klass = OIDCAuthenticationBackend ()
319+ db_user = UserFactory (is_active = False )
320+
321+ def get_userinfo_mocked (* args ):
322+ return {
323+ "sub" : db_user .sub ,
324+ "email" : db_user .email ,
325+ "first_name" : "John" ,
326+ "last_name" : "Doe" ,
327+ }
328+
329+ monkeypatch .setattr (OIDCAuthenticationBackend , "get_userinfo" , get_userinfo_mocked )
330+
331+ with (
332+ django_assert_num_queries (1 ),
333+ pytest .raises (SuspiciousOperation , match = "User account is disabled" ),
334+ ):
335+ klass .get_or_create_user (access_token = "test-token" , id_token = None , payload = None )
336+
337+ assert models .User .objects .count () == 1
338+
339+
340+ def test_authentication_getter_existing_disabled_user_via_email (
341+ django_assert_num_queries , monkeypatch
342+ ):
343+ """
344+ If an existing user does not matches the sub but matches the email and is disabled,
345+ an error should be raised and a user should not be created.
346+ """
347+
348+ klass = OIDCAuthenticationBackend ()
349+ db_user = UserFactory (is_active = False )
350+
351+ def get_userinfo_mocked (* args ):
352+ return {
353+ "sub" : "random" ,
354+ "email" : db_user .email ,
355+ "first_name" : "John" ,
356+ "last_name" : "Doe" ,
357+ }
358+
359+ monkeypatch .setattr (OIDCAuthenticationBackend , "get_userinfo" , get_userinfo_mocked )
360+
361+ with (
362+ django_assert_num_queries (2 ),
363+ pytest .raises (SuspiciousOperation , match = "User account is disabled" ),
364+ ):
365+ klass .get_or_create_user (access_token = "test-token" , id_token = None , payload = None )
366+
367+ assert models .User .objects .count () == 1
0 commit comments