Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_origin_save.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from datetime import datetime, timedelta | from datetime import datetime, timedelta | ||||
import pytest | import pytest | ||||
from django.utils import timezone | from django.utils import timezone | ||||
from swh.web.common.models import ( | from swh.web.common.models import ( | ||||
SAVE_REQUEST_ACCEPTED, | SAVE_REQUEST_ACCEPTED, | ||||
SAVE_REQUEST_PENDING, | SAVE_REQUEST_PENDING, | ||||
SAVE_REQUEST_REJECTED, | SAVE_REQUEST_REJECTED, | ||||
SAVE_TASK_FAILED, | SAVE_TASK_FAILED, | ||||
SAVE_TASK_NOT_CREATED, | SAVE_TASK_NOT_CREATED, | ||||
SAVE_TASK_NOT_YET_SCHEDULED, | SAVE_TASK_NOT_YET_SCHEDULED, | ||||
SAVE_TASK_SCHEDULED, | SAVE_TASK_SCHEDULED, | ||||
SAVE_TASK_SUCCEEDED, | SAVE_TASK_SUCCEEDED, | ||||
VISIT_STATUS_FAILED, | |||||
VISIT_STATUS_FULL, | |||||
SaveOriginRequest, | SaveOriginRequest, | ||||
SaveUnauthorizedOrigin, | SaveUnauthorizedOrigin, | ||||
) | ) | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.utils import check_api_get_responses, check_api_post_responses | from swh.web.tests.utils import check_api_get_responses, check_api_post_responses | ||||
pytestmark = pytest.mark.django_db | pytestmark = pytest.mark.django_db | ||||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | def check_save_request_status( | ||||
api_client, | api_client, | ||||
mocker, | mocker, | ||||
origin_url, | origin_url, | ||||
expected_request_status, | expected_request_status, | ||||
expected_task_status, | expected_task_status, | ||||
scheduler_task_status="next_run_not_scheduled", | scheduler_task_status="next_run_not_scheduled", | ||||
scheduler_task_run_status=None, | scheduler_task_run_status=None, | ||||
visit_date=None, | visit_date=None, | ||||
visit_status=None, | |||||
): | ): | ||||
mock_scheduler = mocker.patch("swh.web.common.origin_save.scheduler") | mock_scheduler = mocker.patch("swh.web.common.origin_save.scheduler") | ||||
mock_scheduler.get_tasks.return_value = [ | mock_scheduler.get_tasks.return_value = [ | ||||
{ | { | ||||
"priority": "high", | "priority": "high", | ||||
"policy": "oneshot", | "policy": "oneshot", | ||||
"type": "load-git", | "type": "load-git", | ||||
"arguments": {"kwargs": {"repo_url": origin_url}, "args": []}, | "arguments": {"kwargs": {"repo_url": origin_url}, "args": []}, | ||||
Show All 20 Lines | ): | ||||
url = reverse( | url = reverse( | ||||
"api-1-save-origin", url_args={"visit_type": "git", "origin_url": origin_url} | "api-1-save-origin", url_args={"visit_type": "git", "origin_url": origin_url} | ||||
) | ) | ||||
mock_visit_date = mocker.patch( | mock_visit_date = mocker.patch( | ||||
("swh.web.common.origin_save." "_get_visit_info_for_save_request") | ("swh.web.common.origin_save." "_get_visit_info_for_save_request") | ||||
) | ) | ||||
mock_visit_date.return_value = (visit_date, None) | mock_visit_date.return_value = (visit_date, visit_status) | ||||
response = check_api_get_responses(api_client, url, status_code=200) | response = check_api_get_responses(api_client, url, status_code=200) | ||||
save_request_data = response.data[0] | save_request_data = response.data[0] | ||||
assert save_request_data["save_request_status"] == expected_request_status | assert save_request_data["save_request_status"] == expected_request_status | ||||
assert save_request_data["save_task_status"] == expected_task_status | assert save_request_data["save_task_status"] == expected_task_status | ||||
assert save_request_data["visit_status"] == visit_status | |||||
# Check that save task status is still available when | # Check that save task status is still available when | ||||
# the scheduler task has been archived | # the scheduler task has been archived | ||||
mock_scheduler.get_tasks.return_value = [] | mock_scheduler.get_tasks.return_value = [] | ||||
response = check_api_get_responses(api_client, url, status_code=200) | response = check_api_get_responses(api_client, url, status_code=200) | ||||
save_request_data = response.data[0] | save_request_data = response.data[0] | ||||
assert save_request_data["save_task_status"] == expected_task_status | assert save_request_data["save_task_status"] == expected_task_status | ||||
assert save_request_data["visit_status"] == visit_status | |||||
def test_save_request_rejected(api_client, mocker): | def test_save_request_rejected(api_client, mocker): | ||||
origin_url = "https://github.com/user/illegal_repo" | origin_url = "https://github.com/user/illegal_repo" | ||||
check_created_save_request_status( | check_created_save_request_status( | ||||
api_client, mocker, origin_url, expected_request_status=SAVE_REQUEST_REJECTED, | api_client, mocker, origin_url, expected_request_status=SAVE_REQUEST_REJECTED, | ||||
) | ) | ||||
check_save_request_status( | check_save_request_status( | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | check_save_request_status( | ||||
api_client, | api_client, | ||||
mocker, | mocker, | ||||
origin_url, | origin_url, | ||||
expected_request_status=SAVE_REQUEST_ACCEPTED, | expected_request_status=SAVE_REQUEST_ACCEPTED, | ||||
expected_task_status=SAVE_TASK_SUCCEEDED, | expected_task_status=SAVE_TASK_SUCCEEDED, | ||||
scheduler_task_status="completed", | scheduler_task_status="completed", | ||||
scheduler_task_run_status="eventful", | scheduler_task_run_status="eventful", | ||||
visit_date=visit_date, | visit_date=visit_date, | ||||
visit_status=VISIT_STATUS_FULL, | |||||
) | ) | ||||
def test_save_request_failed(api_client, mocker): | def test_save_request_failed(api_client, mocker): | ||||
origin_url = "https://gitlab.com/inkscape/inkscape" | origin_url = "https://gitlab.com/inkscape/inkscape" | ||||
check_created_save_request_status( | check_created_save_request_status( | ||||
api_client, | api_client, | ||||
mocker, | mocker, | ||||
Show All 13 Lines | def test_save_request_failed(api_client, mocker): | ||||
check_save_request_status( | check_save_request_status( | ||||
api_client, | api_client, | ||||
mocker, | mocker, | ||||
origin_url, | origin_url, | ||||
expected_request_status=SAVE_REQUEST_ACCEPTED, | expected_request_status=SAVE_REQUEST_ACCEPTED, | ||||
expected_task_status=SAVE_TASK_FAILED, | expected_task_status=SAVE_TASK_FAILED, | ||||
scheduler_task_status="disabled", | scheduler_task_status="disabled", | ||||
scheduler_task_run_status="failed", | scheduler_task_run_status="failed", | ||||
visit_status=VISIT_STATUS_FAILED, | |||||
) | ) | ||||
def test_create_save_request_only_when_needed(api_client, mocker): | def test_create_save_request_only_when_needed(api_client, mocker): | ||||
origin_url = "https://github.com/webpack/webpack" | origin_url = "https://github.com/webpack/webpack" | ||||
SaveOriginRequest.objects.create( | SaveOriginRequest.objects.create( | ||||
visit_type="git", | visit_type="git", | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
▲ Show 20 Lines • Show All 76 Lines • Show Last 20 Lines |