Skip to content

Commit b5bf65a

Browse files
committed
Test case for duplicated BuildCommandResult
1 parent 7cfdb26 commit b5bf65a

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

readthedocs/api/v2/views/model_views.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,12 @@ def perform_create(self, serializer):
337337
if not build_api_key.project.builds.filter(pk=build_pk).exists():
338338
raise PermissionDenied()
339339

340-
if BuildCommandResult.objects.filter(**serializer.validated_data).exists():
340+
if BuildCommandResult.objects.filter(
341+
build=serializer.validated_data["build"],
342+
command=serializer.validated_data["command"],
343+
).exists():
341344
log.warning("Build command is duplicated. Skipping...")
342-
return Response(status=status.HTTP_204_NO_CONTENT)
345+
return
343346

344347
return super().perform_create(serializer)
345348

readthedocs/rtd_tests/tests/test_api.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,48 @@ def test_build_read_and_write_endpoints_for_build_api_token(self):
909909
resp = client.patch(f"/api/v2/build/{build.pk}/")
910910
self.assertEqual(resp.status_code, 404)
911911

912+
def test_build_commands_duplicated_command(self):
913+
"""Sending the same request twice should only create one BuildCommandResult."""
914+
project = get(
915+
Project,
916+
language="en",
917+
)
918+
version = project.versions.first()
919+
build = Build.objects.create(project=project, version=version)
920+
921+
self.assertEqual(BuildCommandResult.objects.count(), 0)
922+
923+
client = APIClient()
924+
_, build_api_key = BuildAPIKey.objects.create_key(project)
925+
client.credentials(HTTP_AUTHORIZATION=f"Token {build_api_key}")
926+
927+
now = timezone.now()
928+
start_time = now - datetime.timedelta(seconds=5)
929+
end_time = now
930+
931+
data = {
932+
"build": build.pk,
933+
"command": "git status",
934+
"description": "Git status",
935+
"exit_code": 0,
936+
"start_time": start_time,
937+
"end_time": end_time,
938+
}
939+
940+
response = client.post(
941+
"/api/v2/command/",
942+
data,
943+
format="json",
944+
)
945+
self.assertEqual(response.status_code, 201)
946+
response = client.post(
947+
"/api/v2/command/",
948+
data,
949+
format="json",
950+
)
951+
self.assertEqual(response.status_code, 201)
952+
self.assertEqual(BuildCommandResult.objects.count(), 1)
953+
912954
def test_build_commands_read_only_endpoints_for_normal_user(self):
913955
user_normal = get(User, is_staff=False)
914956
user_admin = get(User, is_staff=True)

0 commit comments

Comments
 (0)