Skip to content

Commit 6d22e91

Browse files
committed
✅(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 7351bc0 commit 6d22e91

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
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: 7 additions & 4 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
@@ -50,9 +51,11 @@ def test_api_documents_media_auth_anonymous_public():
5051
factories.DocumentFactory(id=document_id, link_reach="public", attachments=[key])
5152

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

5760
assert response.status_code == 200
5861

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

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

0 commit comments

Comments
 (0)