Skip to content

Commit db91f3a

Browse files
committed
feat: add tests for the AuthDB cleanup function
1 parent b49a19c commit db91f3a

3 files changed

Lines changed: 109 additions & 1 deletion

File tree

diracx-db/tests/auth/test_authorization_flow.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from diracx.core.exceptions import AuthorizationError
77
from diracx.db.sql.auth.db import AuthDB
8+
from diracx.db.sql.auth.schema import FlowStatus
89

910
MAX_VALIDITY = 2
1011
EXPIRED = 0
@@ -74,3 +75,39 @@ async def test_insert(auth_db: AuthDB):
7475
)
7576

7677
assert uuid1 != uuid2
78+
79+
80+
async def test_clean_authorization_flows(auth_db: AuthDB):
81+
# Insert two authorization flows
82+
async with auth_db as auth_db:
83+
uuid1 = await auth_db.insert_authorization_flow(
84+
"client_id", "scope", "code_challenge", "S256", "redirect_uri"
85+
)
86+
uuid2 = await auth_db.insert_authorization_flow(
87+
"client_id2", "scope2", "code_challenge2", "S256", "redirect_uri2"
88+
)
89+
90+
id_token = {"sub": "myIdToken"}
91+
92+
async with auth_db as auth_db:
93+
code1, _ = await auth_db.authorization_flow_insert_id_token(uuid1, id_token, 1)
94+
code2, _ = await auth_db.authorization_flow_insert_id_token(uuid2, id_token, 1)
95+
96+
async with auth_db as auth_db:
97+
await auth_db.update_authorization_flow_status(code1, FlowStatus.DONE)
98+
await auth_db.update_authorization_flow_status(code2, FlowStatus.ERROR)
99+
100+
# Check the number of deleted authorization flow (should be 0)
101+
async with auth_db as auth_db:
102+
deleted_auth = await auth_db.clean_expired_authorization_flows(max_retention=30)
103+
assert deleted_auth == 0
104+
105+
# Check the number of deleted authorization flow (should be 2)
106+
async with auth_db as auth_db:
107+
deleted_auth = await auth_db.clean_expired_authorization_flows(max_retention=0)
108+
assert deleted_auth == 2
109+
110+
# Check the number of deleted authorization flow (should be 0 because there is nothing left to delete)
111+
async with auth_db as auth_db:
112+
deleted_auth = await auth_db.clean_expired_authorization_flows(max_retention=0)
113+
assert deleted_auth == 0

diracx-db/tests/auth/test_device_flow.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from diracx.core.exceptions import AuthorizationError
1010
from diracx.db.sql.auth.db import AuthDB
11-
from diracx.db.sql.auth.schema import USER_CODE_LENGTH
11+
from diracx.db.sql.auth.schema import USER_CODE_LENGTH, FlowStatus
1212
from diracx.db.sql.utils.functions import substract_date
1313

1414
MAX_VALIDITY = 2
@@ -139,3 +139,33 @@ async def test_device_flow_insert_id_token(auth_db: AuthDB):
139139
async with auth_db as auth_db:
140140
res = await auth_db.get_device_flow(device_code)
141141
assert res["IDToken"] == id_token
142+
143+
144+
async def test_clean_device_flows(auth_db: AuthDB):
145+
# Insert two device flows
146+
async with auth_db as auth_db:
147+
user_code1, device_code1 = await auth_db.insert_device_flow(
148+
"client_id", "scope"
149+
)
150+
user_code2, device_code2 = await auth_db.insert_device_flow(
151+
"client_id", "scope"
152+
)
153+
154+
async with auth_db as auth_db:
155+
await auth_db.update_device_flow_status(device_code1, FlowStatus.DONE)
156+
await auth_db.update_device_flow_status(device_code2, FlowStatus.ERROR)
157+
158+
# Check the number of deleted device flows (should be 0)
159+
async with auth_db as auth_db:
160+
deleted_device = await auth_db.clean_expired_device_flows(max_retention=30)
161+
assert deleted_device == 0
162+
163+
# Check the number of deleted device flows (should be 2)
164+
async with auth_db as auth_db:
165+
deleted_device = await auth_db.clean_expired_device_flows(max_retention=0)
166+
assert deleted_device == 2
167+
168+
# Check the number of deleted device flow (should be 0 because there is nothing left to delete)
169+
async with auth_db as auth_db:
170+
deleted_device = await auth_db.clean_expired_device_flows(max_retention=0)
171+
assert deleted_device == 0

diracx-db/tests/auth/test_refresh_token.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,44 @@ async def test_get_refresh_tokens(auth_db: AuthDB):
257257

258258
# Check the number of retrieved refresh tokens (should be 3 refresh tokens)
259259
assert len(refresh_tokens) == 2
260+
261+
262+
async def test_clean_refresh_tokens(auth_db: AuthDB):
263+
# Insert two refresh tokens
264+
jtis = []
265+
async with auth_db as auth_db:
266+
for _ in range(2):
267+
jti = uuid7()
268+
await auth_db.insert_refresh_token(
269+
jti,
270+
"subject",
271+
"scope",
272+
)
273+
jtis.append(jti)
274+
275+
# Revoke one of the refresh token
276+
async with auth_db as auth_db:
277+
await auth_db.revoke_refresh_token(jtis[0])
278+
279+
# Check the number of deleted refresh tokens (should be 0)
280+
async with auth_db as auth_db:
281+
deleted_expired, deleted_revoked = await auth_db.clean_expired_refresh_token(
282+
max_validity=10, max_retention=30
283+
)
284+
assert deleted_expired == 0
285+
assert deleted_revoked == 0
286+
287+
# Check the number of deleted refresh tokens (should be 1 of each)
288+
async with auth_db as auth_db:
289+
deleted_expired, deleted_revoked = await auth_db.clean_expired_refresh_token(
290+
max_validity=0, max_retention=0
291+
)
292+
assert deleted_expired == 1
293+
assert deleted_revoked == 1
294+
295+
# Get all refresh tokens (Admin)
296+
async with auth_db as auth_db:
297+
refresh_tokens = await auth_db.get_user_refresh_tokens()
298+
299+
# Check the number of retrieved refresh tokens (should be 0)
300+
assert len(refresh_tokens) == 0

0 commit comments

Comments
 (0)