diff --git a/swh/deposit/api/common.py b/swh/deposit/api/common.py --- a/swh/deposit/api/common.py +++ b/swh/deposit/api/common.py @@ -220,20 +220,22 @@ The Deposit instance saved or updated. """ - complete_date: Optional[datetime.datetime] = None - if in_progress is False: - complete_date = timezone.now() - status_type = DEPOSIT_STATUS_DEPOSITED + self._complete_deposit(deposit) else: - status_type = DEPOSIT_STATUS_PARTIAL + deposit.status = DEPOSIT_STATUS_PARTIAL + deposit.save() + + return deposit - # update metadata - deposit.complete_date = complete_date - deposit.status = status_type + def _complete_deposit(self, deposit: Deposit) -> None: + """Marks the deposit as 'deposited', then schedule a check task if configured + to do so.""" + deposit.complete_date = timezone.now() + deposit.status = DEPOSIT_STATUS_DEPOSITED + deposit.save() if self.config["checks"]: - deposit.save() # needed to have a deposit id scheduler = self.scheduler if deposit.status == DEPOSIT_STATUS_DEPOSITED and not deposit.check_task_id: task = create_oneshot_task_dict( @@ -245,9 +247,7 @@ check_task_id = scheduler.create_tasks([task])[0]["id"] deposit.check_task_id = check_task_id - deposit.save() - - return deposit + deposit.save() def _deposit_request_put( self, @@ -834,9 +834,9 @@ collection_name: the associated client deposit: deposit to be finalized """ - deposit.complete_date = timezone.now() - deposit.status = DEPOSIT_STATUS_DEPOSITED - deposit.save() + self._complete_deposit(deposit) + + assert deposit.complete_date is not None return Receipt( deposit_id=deposit.id, diff --git a/swh/deposit/tests/api/test_deposit_schedule.py b/swh/deposit/tests/api/test_deposit_schedule.py --- a/swh/deposit/tests/api/test_deposit_schedule.py +++ b/swh/deposit/tests/api/test_deposit_schedule.py @@ -11,7 +11,12 @@ import pytest from rest_framework import status -from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED +from swh.deposit.config import ( + COL_IRI, + DEPOSIT_STATUS_DEPOSITED, + DEPOSIT_STATUS_PARTIAL, + SE_IRI, +) from swh.deposit.parsers import parse_xml @@ -28,12 +33,32 @@ return datetime.datetime.now(tz=datetime.timezone.utc) +def assert_task_for_deposit( + swh_scheduler, deposit_id, timestamp_before_call, timestamp_after_call +): + tasks = swh_scheduler.grab_ready_tasks("check-deposit") + assert len(tasks) == 1 + task = tasks[0] + + assert timestamp_before_call <= task.pop("next_run") <= timestamp_after_call + assert task["arguments"] == { + "args": [], + "kwargs": {"collection": "test", "deposit_id": deposit_id,}, + } + assert task["policy"] == "oneshot" + assert task["type"] == "check-deposit" + assert task["retries_left"] == 3 + + def test_add_deposit_schedules_check( authenticated_client, deposit_collection, sample_archive, swh_scheduler ): - """Posting deposit on collection creates a checker task + """Posting deposit by POST Col-IRI creates a checker task """ + tasks = swh_scheduler.grab_ready_tasks("check-deposit") + assert len(tasks) == 0 + external_id = "external-id-schedules-check" url = reverse(COL_IRI, args=[deposit_collection.name]) @@ -59,17 +84,50 @@ response_content = parse_xml(BytesIO(response.content)) actual_state = response_content["swh:deposit_status"] assert actual_state == DEPOSIT_STATUS_DEPOSITED - deposit_id = response_content["swh:deposit_id"] + deposit_id = int(response_content["swh:deposit_id"]) + + assert_task_for_deposit( + swh_scheduler, deposit_id, timestamp_before_call, timestamp_after_call + ) + + +def test_update_deposit_schedules_check( + authenticated_client, + deposit_collection, + partial_deposit_with_metadata, + atom_dataset, + swh_scheduler, +): + """Updating deposit by POST SE-IRI creates a checker task + + """ + deposit = partial_deposit_with_metadata + assert deposit.status == DEPOSIT_STATUS_PARTIAL tasks = swh_scheduler.grab_ready_tasks("check-deposit") - assert len(tasks) == 1 - task = tasks[0] + assert len(tasks) == 0 - assert timestamp_before_call <= task.pop("next_run") <= timestamp_after_call - assert task["arguments"] == { - "args": [], - "kwargs": {"collection": "test", "deposit_id": int(deposit_id),}, - } - assert task["policy"] == "oneshot" - assert task["type"] == "check-deposit" - assert task["retries_left"] == 3 + update_uri = reverse(SE_IRI, args=[deposit_collection.name, deposit.id]) + + timestamp_before_call = now() + + response = authenticated_client.post( + update_uri, + content_type="application/atom+xml;type=entry", + data="", + size=0, + HTTP_IN_PROGRESS=False, + ) + + timestamp_after_call = now() + + assert response.status_code == status.HTTP_200_OK + + response_content = parse_xml(BytesIO(response.content)) + actual_state = response_content["swh:deposit_status"] + assert actual_state == DEPOSIT_STATUS_DEPOSITED + assert deposit.id == int(response_content["swh:deposit_id"]) + + assert_task_for_deposit( + swh_scheduler, deposit.id, timestamp_before_call, timestamp_after_call + )