Skip to content

Commit 1761936

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 5b17feb commit 1761936

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
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: 31 additions & 20 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}"
@@ -167,9 +170,11 @@ def test_api_documents_media_auth_anonymous_attachments():
167170
parent = factories.DocumentFactory(link_reach="public")
168171
factories.DocumentFactory(parent=parent, link_reach="restricted", attachments=[key])
169172

170-
response = APIClient().get(
171-
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=media_url
172-
)
173+
now = timezone.now()
174+
with freeze_time(now):
175+
response = APIClient().get(
176+
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=media_url
177+
)
173178

174179
assert response.status_code == 200
175180

@@ -179,7 +184,7 @@ def test_api_documents_media_auth_anonymous_attachments():
179184
"SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature="
180185
in authorization
181186
)
182-
assert response["X-Amz-Date"] == timezone.now().strftime("%Y%m%dT%H%M%SZ")
187+
assert response["X-Amz-Date"] == now.strftime("%Y%m%dT%H%M%SZ")
183188

184189
s3_url = urlparse(settings.AWS_S3_ENDPOINT_URL)
185190
file_url = f"{settings.AWS_S3_ENDPOINT_URL:s}/impress-media-storage/{key:s}"
@@ -221,9 +226,11 @@ def test_api_documents_media_auth_authenticated_public_or_authenticated(reach):
221226

222227
factories.DocumentFactory(id=document_id, link_reach=reach, attachments=[key])
223228

224-
response = client.get(
225-
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=media_url
226-
)
229+
now = timezone.now()
230+
with freeze_time(now):
231+
response = client.get(
232+
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=media_url
233+
)
227234

228235
assert response.status_code == 200
229236

@@ -233,7 +240,7 @@ def test_api_documents_media_auth_authenticated_public_or_authenticated(reach):
233240
"SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature="
234241
in authorization
235242
)
236-
assert response["X-Amz-Date"] == timezone.now().strftime("%Y%m%dT%H%M%SZ")
243+
assert response["X-Amz-Date"] == now.strftime("%Y%m%dT%H%M%SZ")
237244

238245
s3_url = urlparse(settings.AWS_S3_ENDPOINT_URL)
239246
file_url = f"{settings.AWS_S3_ENDPOINT_URL:s}/impress-media-storage/{key:s}"
@@ -307,9 +314,11 @@ def test_api_documents_media_auth_related(via, mock_user_teams):
307314
mock_user_teams.return_value = ["lasuite", "unknown"]
308315
factories.TeamDocumentAccessFactory(document=document, team="lasuite")
309316

310-
response = client.get(
311-
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=media_url
312-
)
317+
now = timezone.now()
318+
with freeze_time(now):
319+
response = client.get(
320+
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=media_url
321+
)
313322

314323
assert response.status_code == 200
315324

@@ -319,7 +328,7 @@ def test_api_documents_media_auth_related(via, mock_user_teams):
319328
"SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature="
320329
in authorization
321330
)
322-
assert response["X-Amz-Date"] == timezone.now().strftime("%Y%m%dT%H%M%SZ")
331+
assert response["X-Amz-Date"] == now.strftime("%Y%m%dT%H%M%SZ")
323332

324333
s3_url = urlparse(settings.AWS_S3_ENDPOINT_URL)
325334
file_url = f"{settings.AWS_S3_ENDPOINT_URL:s}/impress-media-storage/{key:s}"
@@ -373,10 +382,12 @@ def test_api_documents_media_auth_missing_status_metadata():
373382

374383
factories.DocumentFactory(id=document_id, link_reach="public", attachments=[key])
375384

385+
now = timezone.now()
376386
original_url = f"http://localhost/media/{key:s}"
377-
response = APIClient().get(
378-
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=original_url
379-
)
387+
with freeze_time(now):
388+
response = APIClient().get(
389+
"/api/v1.0/documents/media-auth/", HTTP_X_ORIGINAL_URL=original_url
390+
)
380391

381392
assert response.status_code == 200
382393

@@ -386,7 +397,7 @@ def test_api_documents_media_auth_missing_status_metadata():
386397
"SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature="
387398
in authorization
388399
)
389-
assert response["X-Amz-Date"] == timezone.now().strftime("%Y%m%dT%H%M%SZ")
400+
assert response["X-Amz-Date"] == now.strftime("%Y%m%dT%H%M%SZ")
390401

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

0 commit comments

Comments
 (0)