From eeb71f90bc1b312ef3374e60b9aaa6797487ea39 Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Mon, 4 Nov 2024 10:51:56 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA(backend)=20add=20test=20for=20inac?= =?UTF-8?q?tive=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add failing test for case when user is inactive. This case was highlighted by @qbey and was previously untested. Fix will follow in subsequent commit. --- .../tests/authentication/test_backends.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/backend/core/tests/authentication/test_backends.py b/src/backend/core/tests/authentication/test_backends.py index 07e4bb02..c63dc8df 100644 --- a/src/backend/core/tests/authentication/test_backends.py +++ b/src/backend/core/tests/authentication/test_backends.py @@ -123,3 +123,23 @@ def test_models_oidc_user_getter_empty_sub(django_assert_num_queries, monkeypatc klass.get_or_create_user(access_token="test-token", id_token=None, payload=None) assert models.User.objects.exists() is False + + +def test_authentication_get_inactive_user(monkeypatch): + """Test an exception is raised when attempting to authenticate inactive user.""" + + klass = OIDCAuthenticationBackend() + db_user = UserFactory(is_active=False) + + def get_userinfo_mocked(*args): + return {"sub": db_user.sub} + + monkeypatch.setattr(OIDCAuthenticationBackend, "get_userinfo", get_userinfo_mocked) + + with ( + pytest.raises( + SuspiciousOperation, + match="User account is disabled", + ), + ): + klass.get_or_create_user(access_token="test-token", id_token=None, payload=None)