-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
api/tests/unit/custom_auth/jwt_cookie/test_unit_jwt_cookie_authentication.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import Type | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from rest_framework.request import Request | ||
from rest_framework_simplejwt.exceptions import ( | ||
AuthenticationFailed, | ||
InvalidToken, | ||
TokenError, | ||
) | ||
from rest_framework_simplejwt.tokens import Token | ||
|
||
from custom_auth.jwt_cookie.authentication import JWTCookieAuthentication | ||
from custom_auth.jwt_cookie.constants import JWT_SLIDING_COOKIE_KEY | ||
from users.models import FFAdminUser | ||
|
||
|
||
class TestJWTCookieAuthentication: | ||
def test_authenticate_without_cookie(self) -> None: | ||
# Given | ||
auth = JWTCookieAuthentication() | ||
request = MagicMock(spec=Request) | ||
request.COOKIES = {} | ||
|
||
# When | ||
result = auth.authenticate(request) | ||
|
||
# Then | ||
assert result is None | ||
|
||
def test_authenticate_valid_cookie(self) -> None: | ||
# Given | ||
auth = JWTCookieAuthentication() | ||
request = MagicMock(spec=Request) | ||
raw_token = "valid_token" | ||
request.COOKIES = {JWT_SLIDING_COOKIE_KEY: raw_token} | ||
|
||
validated_token = MagicMock(spec=Token) | ||
user = MagicMock(spec=FFAdminUser) | ||
|
||
# Mock the validation and user retrieval | ||
with patch.object( | ||
auth, "get_validated_token", return_value=validated_token | ||
) as mock_validate: | ||
with patch.object(auth, "get_user", return_value=user) as mock_get_user: | ||
# When | ||
result = auth.authenticate(request) | ||
|
||
# Then | ||
assert result == (user, validated_token) | ||
mock_validate.assert_called_once_with(raw_token) | ||
mock_get_user.assert_called_once_with(validated_token) | ||
|
||
@pytest.mark.parametrize( | ||
"exception_class", [InvalidToken, TokenError, AuthenticationFailed] | ||
) | ||
def test_authenticate_invalid_cookie(self, exception_class: Type[Exception]) -> None: | ||
# Given | ||
auth = JWTCookieAuthentication() | ||
request = MagicMock(spec=Request) | ||
raw_token = "invalid_token" | ||
request.COOKIES = {JWT_SLIDING_COOKIE_KEY: raw_token} | ||
|
||
# Test that no further exceptions are raised if the token is invalid in any way | ||
with patch.object( | ||
auth, "get_validated_token", side_effect=exception_class("Error") | ||
): | ||
# When | ||
result = auth.authenticate(request) | ||
|
||
# Then | ||
assert result is None |