Skip to content

Commit 6775a5c

Browse files
author
maxim-lixakov
committed
[DOP-21482] - add test with expired access_token
1 parent f751a87 commit 6775a5c

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

syncmaster/backend/providers/auth/keycloak_provider.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ async def get_current_user(self, access_token: str, *args, **kwargs) -> Any:
8181
self.redirect_to_auth(request.url.path)
8282

8383
try:
84+
# if user is disabled or blocked in Keycloak after the token is issued, he will
85+
# remain authorized until the token expires (not more than 15 minutes in MTS SSO)
8486
token_info = self.keycloak_openid.decode_token(token=access_token)
8587
except Exception as e:
8688
log.info("Access token is invalid or expired: %s", e)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from tests.test_unit.test_auth.auth_fixtures.keycloak_fixture import (
22
create_session_cookie,
33
mock_keycloak_realm,
4+
mock_keycloak_token_refresh,
45
mock_keycloak_well_known,
56
rsa_keys,
67
)

tests/test_unit/test_auth/auth_fixtures/keycloak_fixture.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,39 @@ def mock_keycloak_realm(settings, rsa_keys):
120120
status=200,
121121
content_type="application/json",
122122
)
123+
124+
125+
@pytest.fixture
126+
def mock_keycloak_token_refresh(settings, rsa_keys):
127+
server_url = settings.auth.server_url
128+
realm_name = settings.auth.client_id
129+
token_url = f"{server_url}/realms/{realm_name}/protocol/openid-connect/token"
130+
131+
# generate new access and refresh tokens
132+
expires_in = int(time.time()) + 1000
133+
private_pem = rsa_keys["private_pem"]
134+
payload = {
135+
"sub": "mock_user_id",
136+
"preferred_username": "mock_username",
137+
"email": "[email protected]",
138+
"given_name": "Mock",
139+
"middle_name": "User",
140+
"family_name": "Name",
141+
"exp": expires_in,
142+
}
143+
144+
new_access_token = jwt.encode(payload, private_pem, algorithm="RS256")
145+
new_refresh_token = "mock_new_refresh_token"
146+
147+
responses.add(
148+
responses.POST,
149+
token_url,
150+
json={
151+
"access_token": new_access_token,
152+
"refresh_token": new_refresh_token,
153+
"token_type": "bearer",
154+
"expires_in": expires_in,
155+
},
156+
status=200,
157+
content_type="application/json",
158+
)

tests/test_unit/test_auth/test_auth_keycloak.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
import pytest
24
import responses
35
from httpx import AsyncClient
@@ -68,6 +70,50 @@ async def test_get_keycloak_user_authorized(
6870
}
6971

7072

73+
@responses.activate
74+
@pytest.mark.parametrize(
75+
"settings",
76+
[
77+
{
78+
"auth": {
79+
"provider": KEYCLOAK_PROVIDER,
80+
},
81+
},
82+
],
83+
indirect=True,
84+
)
85+
async def test_get_keycloak_user_expired_access_token(
86+
caplog,
87+
client: AsyncClient,
88+
simple_user: MockUser,
89+
settings: Settings,
90+
create_session_cookie,
91+
mock_keycloak_well_known,
92+
mock_keycloak_realm,
93+
mock_keycloak_token_refresh,
94+
):
95+
session_cookie = create_session_cookie(simple_user, expire_in_msec=-100000000) # expired access token
96+
headers = {
97+
"Cookie": f"session={session_cookie}",
98+
}
99+
100+
with caplog.at_level(logging.DEBUG):
101+
response = await client.get(
102+
f"/v1/users/{simple_user.id}",
103+
headers=headers,
104+
)
105+
106+
assert "Access token is invalid or expired" in caplog.text
107+
assert "Access token refreshed and decoded successfully" in caplog.text
108+
109+
assert response.status_code == 200
110+
assert response.json() == {
111+
"id": simple_user.id,
112+
"is_superuser": simple_user.is_superuser,
113+
"username": simple_user.username,
114+
}
115+
116+
71117
@responses.activate
72118
@pytest.mark.parametrize(
73119
"settings",

0 commit comments

Comments
 (0)