Skip to content

Commit 61cf214

Browse files
committed
✅(backend) fix randomly failing test due to delay before check
The signature is issued when the request is made but we only check the timestamp after a few other lines of code/checks. In some cases, this was causing a 1s difference in the timestamp (because the request was made at the just a fraction of a second before the next instruction...) and the test was failing: > assert response["X-Amz-Date"] == timezone.now().strftime("%Y%m%dT%H%M%SZ") AssertionError: assert equals failed '20250504T175307Z' '20250504T175308Z'
1 parent 8e097e9 commit 61cf214

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import base64
66
import uuid
77
from io import BytesIO
8+
from unittest import mock
89
from urllib.parse import urlparse
910

1011
from django.conf import settings
@@ -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 mock.patch("django.utils.timezone.now", return_value=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(

0 commit comments

Comments
 (0)