Skip to content

Commit 806f61c

Browse files
sampaccoudPanchoutNathan
authored andcommitted
✅(backend) fix randomly failing test due to delay before check
There is a delay between the time the signature is issued and the time it is checked. Although this delay is minimal, if the signature is issued at the end of a second, both timestamps can differ of 1s. > assert response["X-Amz-Date"] == timezone.now().strftime("%Y%m%dT%H%M%SZ") AssertionError: assert equals failed '20250504T175307Z' '20250504T175308Z'
1 parent 43481db commit 806f61c

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/backend/core/tests/documents/test_api_documents_duplicate.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pycrdt
1515
import pytest
1616
import requests
17+
from freezegun import freeze_time
1718
from rest_framework.test import APIClient
1819

1920
from core import factories, models
@@ -133,19 +134,21 @@ def test_api_documents_duplicate_success(index):
133134

134135
# Ensure access persists after the owner loses access to the original document
135136
models.DocumentAccess.objects.filter(document=document).delete()
136-
response = client.get(
137-
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=image_refs[0][1]
138-
)
139137

140-
assert response.status_code == 200
138+
now = timezone.now()
139+
with freeze_time(now):
140+
response = client.get(
141+
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=image_refs[0][1]
142+
)
141143

144+
assert response.status_code == 200
145+
assert response["X-Amz-Date"] == now.strftime("%Y%m%dT%H%M%SZ")
142146
authorization = response["Authorization"]
143147
assert "AWS4-HMAC-SHA256 Credential=" in authorization
144148
assert (
145149
"SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature="
146150
in authorization
147151
)
148-
assert response["X-Amz-Date"] == timezone.now().strftime("%Y%m%dT%H%M%SZ")
149152

150153
s3_url = urlparse(settings.AWS_S3_ENDPOINT_URL)
151154
response = requests.get(

src/backend/core/tests/documents/test_api_documents_media_auth.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import pytest
1414
import requests
15+
from freezegun import freeze_time
1516
from rest_framework.test import APIClient
1617

1718
from core import factories, models
@@ -52,9 +53,11 @@ def test_api_documents_media_auth_anonymous_public():
5253
factories.DocumentFactory(id=document_id, link_reach="public", attachments=[key])
5354

5455
original_url = f"http://localhost/media/{key:s}"
55-
response = APIClient().get(
56-
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=original_url
57-
)
56+
now = timezone.now()
57+
with freeze_time(now):
58+
response = APIClient().get(
59+
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=original_url
60+
)
5861

5962
assert response.status_code == 200
6063

@@ -64,7 +67,7 @@ def test_api_documents_media_auth_anonymous_public():
6467
"SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature="
6568
in authorization
6669
)
67-
assert response["X-Amz-Date"] == timezone.now().strftime("%Y%m%dT%H%M%SZ")
70+
assert response["X-Amz-Date"] == now.strftime("%Y%m%dT%H%M%SZ")
6871

6972
s3_url = urlparse(settings.AWS_S3_ENDPOINT_URL)
7073
file_url = f"{settings.AWS_S3_ENDPOINT_URL:s}/impress-media-storage/{key:s}"
@@ -307,9 +310,11 @@ def test_api_documents_media_auth_related(via, mock_user_teams):
307310
mock_user_teams.return_value = ["lasuite", "unknown"]
308311
factories.TeamDocumentAccessFactory(document=document, team="lasuite")
309312

310-
response = client.get(
311-
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=media_url
312-
)
313+
now = timezone.now()
314+
with freeze_time(now):
315+
response = client.get(
316+
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=media_url
317+
)
313318

314319
assert response.status_code == 200
315320

@@ -319,7 +324,7 @@ def test_api_documents_media_auth_related(via, mock_user_teams):
319324
"SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature="
320325
in authorization
321326
)
322-
assert response["X-Amz-Date"] == timezone.now().strftime("%Y%m%dT%H%M%SZ")
327+
assert response["X-Amz-Date"] == now.strftime("%Y%m%dT%H%M%SZ")
323328

324329
s3_url = urlparse(settings.AWS_S3_ENDPOINT_URL)
325330
file_url = f"{settings.AWS_S3_ENDPOINT_URL:s}/impress-media-storage/{key:s}"

0 commit comments

Comments
 (0)