Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/admin/test_origin_save.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2019 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 urllib.parse import unquote | from urllib.parse import unquote | ||||
import pytest | import pytest | ||||
from django.contrib.auth import get_user_model | from django.contrib.auth import get_user_model | ||||
from swh.web.common.models import ( | from swh.web.common.models import ( | ||||
SaveAuthorizedOrigin, SaveUnauthorizedOrigin, SaveOriginRequest | SaveAuthorizedOrigin, SaveUnauthorizedOrigin, SaveOriginRequest | ||||
) | ) | ||||
from swh.web.common.origin_save import can_save_origin | from swh.web.common.origin_save import ( | ||||
can_save_origin, PENDING_SAVE_REQUESTS_COUNT_METRIC) | |||||
from swh.web.common.models import ( | from swh.web.common.models import ( | ||||
SAVE_REQUEST_PENDING, SAVE_REQUEST_ACCEPTED, | SAVE_REQUEST_PENDING, SAVE_REQUEST_ACCEPTED, | ||||
SAVE_REQUEST_REJECTED, SAVE_TASK_NOT_YET_SCHEDULED | SAVE_REQUEST_REJECTED, SAVE_TASK_NOT_YET_SCHEDULED | ||||
) | ) | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
_user_name = 'swh-web-admin' | _user_name = 'swh-web-admin' | ||||
_user_mail = 'admin@swh-web.org' | _user_mail = 'admin@swh-web.org' | ||||
Show All 18 Lines | |||||
def check_not_login(client, url): | def check_not_login(client, url): | ||||
login_url = reverse('login', query_params={'next': url}) | login_url = reverse('login', query_params={'next': url}) | ||||
response = client.post(url) | response = client.post(url) | ||||
assert response.status_code == 302 | assert response.status_code == 302 | ||||
assert unquote(response.url) == login_url | assert unquote(response.url) == login_url | ||||
def test_add_authorized_origin_url(client): | def test_add_authorized_origin_url(client, mocker): | ||||
authorized_url = 'https://scm.adullact.net/anonscm/' | authorized_url = 'https://scm.adullact.net/anonscm/' | ||||
assert can_save_origin(authorized_url) == SAVE_REQUEST_PENDING | assert can_save_origin(authorized_url) == SAVE_REQUEST_PENDING | ||||
url = reverse('admin-origin-save-add-authorized-url', | url = reverse('admin-origin-save-add-authorized-url', | ||||
url_args={'origin_url': authorized_url}) | url_args={'origin_url': authorized_url}) | ||||
check_not_login(client, url) | check_not_login(client, url) | ||||
assert can_save_origin(authorized_url) == SAVE_REQUEST_PENDING | assert can_save_origin(authorized_url) == SAVE_REQUEST_PENDING | ||||
mock_statsd = mocker.patch('swh.web.common.origin_save.statsd') | |||||
client.login(username=_user_name, password=_user_password) | client.login(username=_user_name, password=_user_password) | ||||
response = client.post(url) | response = client.post(url) | ||||
assert response.status_code == 200 | assert response.status_code == 200 | ||||
assert can_save_origin(authorized_url) == SAVE_REQUEST_ACCEPTED | assert can_save_origin(authorized_url) == SAVE_REQUEST_ACCEPTED | ||||
_check_pending_requests_stats_sent(mock_statsd) | |||||
def test_remove_authorized_origin_url(client): | def test_remove_authorized_origin_url(client): | ||||
assert can_save_origin(_authorized_origin_url) == SAVE_REQUEST_ACCEPTED | assert can_save_origin(_authorized_origin_url) == SAVE_REQUEST_ACCEPTED | ||||
url = reverse('admin-origin-save-remove-authorized-url', | url = reverse('admin-origin-save-remove-authorized-url', | ||||
url_args={'origin_url': _authorized_origin_url}) | url_args={'origin_url': _authorized_origin_url}) | ||||
check_not_login(client, url) | check_not_login(client, url) | ||||
assert can_save_origin(_authorized_origin_url) == SAVE_REQUEST_ACCEPTED | assert can_save_origin(_authorized_origin_url) == SAVE_REQUEST_ACCEPTED | ||||
client.login(username=_user_name, password=_user_password) | client.login(username=_user_name, password=_user_password) | ||||
response = client.post(url) | response = client.post(url) | ||||
assert response.status_code == 200 | assert response.status_code == 200 | ||||
assert can_save_origin(_authorized_origin_url) == SAVE_REQUEST_PENDING | assert can_save_origin(_authorized_origin_url) == SAVE_REQUEST_PENDING | ||||
def test_add_unauthorized_origin_url(client): | def test_add_unauthorized_origin_url(client, mocker): | ||||
unauthorized_url = 'https://www.yahoo./' | unauthorized_url = 'https://www.yahoo./' | ||||
assert can_save_origin(unauthorized_url) == SAVE_REQUEST_PENDING | assert can_save_origin(unauthorized_url) == SAVE_REQUEST_PENDING | ||||
url = reverse('admin-origin-save-add-unauthorized-url', | url = reverse('admin-origin-save-add-unauthorized-url', | ||||
url_args={'origin_url': unauthorized_url}) | url_args={'origin_url': unauthorized_url}) | ||||
check_not_login(client, url) | check_not_login(client, url) | ||||
assert can_save_origin(unauthorized_url) == SAVE_REQUEST_PENDING | assert can_save_origin(unauthorized_url) == SAVE_REQUEST_PENDING | ||||
mock_statsd = mocker.patch('swh.web.common.origin_save.statsd') | |||||
client.login(username=_user_name, password=_user_password) | client.login(username=_user_name, password=_user_password) | ||||
response = client.post(url) | response = client.post(url) | ||||
assert response.status_code == 200 | assert response.status_code == 200 | ||||
assert can_save_origin(unauthorized_url) == SAVE_REQUEST_REJECTED | assert can_save_origin(unauthorized_url) == SAVE_REQUEST_REJECTED | ||||
_check_pending_requests_stats_sent(mock_statsd) | |||||
def test_remove_unauthorized_origin_url(client): | def test_remove_unauthorized_origin_url(client): | ||||
assert can_save_origin(_unauthorized_origin_url) == SAVE_REQUEST_REJECTED | assert can_save_origin(_unauthorized_origin_url) == SAVE_REQUEST_REJECTED | ||||
url = reverse('admin-origin-save-remove-unauthorized-url', | url = reverse('admin-origin-save-remove-unauthorized-url', | ||||
url_args={'origin_url': _unauthorized_origin_url}) | url_args={'origin_url': _unauthorized_origin_url}) | ||||
check_not_login(client, url) | check_not_login(client, url) | ||||
Show All 32 Lines | tasks_data = [ | ||||
'arguments': { | 'arguments': { | ||||
'kwargs': { | 'kwargs': { | ||||
'repo_url': origin_url | 'repo_url': origin_url | ||||
}, | }, | ||||
'args': [] | 'args': [] | ||||
}, | }, | ||||
'status': 'next_run_not_scheduled', | 'status': 'next_run_not_scheduled', | ||||
'id': 1, | 'id': 1, | ||||
} | } | ||||
] | ] | ||||
mock_scheduler.create_tasks.return_value = tasks_data | mock_scheduler.create_tasks.return_value = tasks_data | ||||
mock_scheduler.get_tasks.return_value = tasks_data | mock_scheduler.get_tasks.return_value = tasks_data | ||||
mock_statsd = mocker.patch('swh.web.common.origin_save.statsd') | |||||
client.login(username=_user_name, password=_user_password) | client.login(username=_user_name, password=_user_password) | ||||
response = client.post(accept_request_url) | response = client.post(accept_request_url) | ||||
assert response.status_code == 200 | assert response.status_code == 200 | ||||
response = client.get(save_request_url) | response = client.get(save_request_url) | ||||
assert response.status_code == 200 | assert response.status_code == 200 | ||||
assert response.data[0]['save_request_status'] == SAVE_REQUEST_ACCEPTED | assert response.data[0]['save_request_status'] == SAVE_REQUEST_ACCEPTED | ||||
assert response.data[0]['save_task_status'] == SAVE_TASK_NOT_YET_SCHEDULED | assert response.data[0]['save_task_status'] == SAVE_TASK_NOT_YET_SCHEDULED | ||||
_check_pending_requests_stats_sent(mock_statsd) | |||||
def test_reject_pending_save_request(client, mocker): | def test_reject_pending_save_request(client, mocker): | ||||
mock_scheduler = mocker.patch('swh.web.common.origin_save.scheduler') | mock_scheduler = mocker.patch('swh.web.common.origin_save.scheduler') | ||||
visit_type = 'git' | visit_type = 'git' | ||||
origin_url = 'https://wikipedia.com' | origin_url = 'https://wikipedia.com' | ||||
save_request_url = reverse('api-1-save-origin', | save_request_url = reverse('api-1-save-origin', | ||||
url_args={'visit_type': visit_type, | url_args={'visit_type': visit_type, | ||||
'origin_url': origin_url}) | 'origin_url': origin_url}) | ||||
response = client.post(save_request_url, data={}, | response = client.post(save_request_url, data={}, | ||||
content_type='application/x-www-form-urlencoded') | content_type='application/x-www-form-urlencoded') | ||||
assert response.status_code == 200 | assert response.status_code == 200 | ||||
assert response.data['save_request_status'] == SAVE_REQUEST_PENDING | assert response.data['save_request_status'] == SAVE_REQUEST_PENDING | ||||
reject_request_url = reverse('admin-origin-save-request-reject', | reject_request_url = reverse('admin-origin-save-request-reject', | ||||
url_args={'visit_type': visit_type, | url_args={'visit_type': visit_type, | ||||
'origin_url': origin_url}) | 'origin_url': origin_url}) | ||||
check_not_login(client, reject_request_url) | check_not_login(client, reject_request_url) | ||||
mock_statsd = mocker.patch('swh.web.common.origin_save.statsd') | |||||
client.login(username=_user_name, password=_user_password) | client.login(username=_user_name, password=_user_password) | ||||
response = client.post(reject_request_url) | response = client.post(reject_request_url) | ||||
assert response.status_code == 200 | assert response.status_code == 200 | ||||
tasks_data = [ | tasks_data = [ | ||||
{ | { | ||||
'priority': 'high', | 'priority': 'high', | ||||
'policy': 'oneshot', | 'policy': 'oneshot', | ||||
Show All 11 Lines | def test_reject_pending_save_request(client, mocker): | ||||
mock_scheduler.create_tasks.return_value = tasks_data | mock_scheduler.create_tasks.return_value = tasks_data | ||||
mock_scheduler.get_tasks.return_value = tasks_data | mock_scheduler.get_tasks.return_value = tasks_data | ||||
response = client.get(save_request_url) | response = client.get(save_request_url) | ||||
assert response.status_code == 200 | assert response.status_code == 200 | ||||
assert response.data[0]['save_request_status'] == SAVE_REQUEST_REJECTED | assert response.data[0]['save_request_status'] == SAVE_REQUEST_REJECTED | ||||
_check_pending_requests_stats_sent(mock_statsd) | |||||
def test_remove_save_request(client): | def test_remove_save_request(client): | ||||
sor = SaveOriginRequest.objects.create(visit_type='git', | sor = SaveOriginRequest.objects.create(visit_type='git', | ||||
origin_url='https://wikipedia.com', # noqa | origin_url='https://wikipedia.com', # noqa | ||||
status=SAVE_REQUEST_PENDING) | status=SAVE_REQUEST_PENDING) | ||||
assert SaveOriginRequest.objects.count() == 1 | assert SaveOriginRequest.objects.count() == 1 | ||||
remove_request_url = reverse('admin-origin-save-request-remove', | remove_request_url = reverse('admin-origin-save-request-remove', | ||||
url_args={'sor_id': sor.id}) | url_args={'sor_id': sor.id}) | ||||
check_not_login(client, remove_request_url) | check_not_login(client, remove_request_url) | ||||
client.login(username=_user_name, password=_user_password) | client.login(username=_user_name, password=_user_password) | ||||
response = client.post(remove_request_url) | response = client.post(remove_request_url) | ||||
assert response.status_code == 200 | assert response.status_code == 200 | ||||
assert SaveOriginRequest.objects.count() == 0 | assert SaveOriginRequest.objects.count() == 0 | ||||
def _check_pending_requests_stats_sent(mock_statsd): | |||||
pending_save_requests = SaveOriginRequest.objects.filter( | |||||
status=SAVE_REQUEST_PENDING) | |||||
mock_statsd.gauge.assert_called_once_with( | |||||
PENDING_SAVE_REQUESTS_COUNT_METRIC, | |||||
pending_save_requests.count()) |