Skip to content
Snippets Groups Projects
Verified Commit 29b30d4e authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

tests_deposit_private_update_status: Migrate to pytest

parent 8d49c14e
No related branches found
No related tags found
1 merge request!40Migrate most deposit tests to pytest
......@@ -10,7 +10,9 @@ from django.urls import reverse
from os import path, listdir
from typing import Mapping
from swh.deposit.config import DEPOSIT_STATUS_DEPOSITED, COL_IRI
from swh.deposit.config import (
DEPOSIT_STATUS_DEPOSITED, COL_IRI, DEPOSIT_STATUS_VERIFIED
)
from swh.deposit.models import Deposit
from swh.deposit.parsers import parse_xml
......@@ -50,6 +52,17 @@ def ready_deposit_ok(partial_deposit_with_metadata):
return deposit
@pytest.fixture
def ready_deposit_verified(partial_deposit_with_metadata):
"""Returns a deposit ready for checks (it will pass the checks).
"""
deposit = partial_deposit_with_metadata
deposit.status = DEPOSIT_STATUS_VERIFIED
deposit.save()
return deposit
@pytest.fixture
def ready_deposit_only_metadata(partial_deposit_only_metadata):
"""Deposit in status ready that will fail the checks (because missing
......
......@@ -7,57 +7,56 @@ import json
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from swh.deposit.models import Deposit, DEPOSIT_STATUS_DETAIL
from swh.deposit.config import PRIVATE_PUT_DEPOSIT, DEPOSIT_STATUS_VERIFIED
from swh.deposit.config import DEPOSIT_STATUS_LOAD_SUCCESS
from swh.deposit.tests.common import BasicTestCase
from swh.deposit.config import (
PRIVATE_PUT_DEPOSIT, DEPOSIT_STATUS_VERIFIED, DEPOSIT_STATUS_LOAD_SUCCESS
)
class UpdateDepositStatusTest(APITestCase, BasicTestCase):
"""Update the deposit's status scenario
PRIVATE_PUT_DEPOSIT_NC = PRIVATE_PUT_DEPOSIT + '-nc'
"""
def setUp(self):
super().setUp()
deposit = Deposit(status=DEPOSIT_STATUS_VERIFIED,
collection=self.collection,
client=self.user)
deposit.save()
self.deposit = Deposit.objects.get(pk=deposit.id)
assert self.deposit.status == DEPOSIT_STATUS_VERIFIED
def private_deposit_url(self, deposit_id):
return reverse(PRIVATE_PUT_DEPOSIT,
args=[self.collection.name, deposit_id])
def private_check_url_endpoints(collection, deposit):
"""There are 2 endpoints to check (one with collection, one without)"""
return [
reverse(PRIVATE_PUT_DEPOSIT, args=[collection.name, deposit.id]),
reverse(PRIVATE_PUT_DEPOSIT_NC, args=[deposit.id])
]
def test_update_deposit_status(self):
"""Existing status for update should return a 204 response
"""
url = self.private_deposit_url(self.deposit.id)
def test_update_deposit_status(
authenticated_client, deposit_collection, ready_deposit_verified):
"""Existing status for update should return a 204 response
"""
deposit = ready_deposit_verified
for url in private_check_url_endpoints(deposit_collection, deposit):
possible_status = set(DEPOSIT_STATUS_DETAIL.keys()) - set(
[DEPOSIT_STATUS_LOAD_SUCCESS])
for _status in possible_status:
response = self.client.put(
response = authenticated_client.put(
url,
content_type='application/json',
data=json.dumps({'status': _status}))
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
assert response.status_code == status.HTTP_204_NO_CONTENT
deposit = Deposit.objects.get(pk=self.deposit.id)
self.assertEqual(deposit.status, _status)
deposit = Deposit.objects.get(pk=deposit.id)
assert deposit.status == _status
def test_update_deposit_status_with_info(self):
"""Existing status for update with info should return a 204 response
deposit.status = DEPOSIT_STATUS_VERIFIED
deposit.save() # hack the same deposit
"""
url = self.private_deposit_url(self.deposit.id)
def test_update_deposit_status_with_info(
authenticated_client, deposit_collection, ready_deposit_verified):
"""Existing status for update with info should return a 204 response
"""
deposit = ready_deposit_verified
for url in private_check_url_endpoints(deposit_collection, deposit):
expected_status = DEPOSIT_STATUS_LOAD_SUCCESS
origin_url = 'something'
directory_id = '42a13fc721c8716ff695d0d62fc851d641f3a12b'
......@@ -69,7 +68,7 @@ class UpdateDepositStatusTest(APITestCase, BasicTestCase):
expected_swh_anchor_id_context = 'swh:1:rev:%s;origin=%s' % (
revision_id, origin_url)
response = self.client.put(
response = authenticated_client.put(
url,
content_type='application/json',
data=json.dumps({
......@@ -79,56 +78,63 @@ class UpdateDepositStatusTest(APITestCase, BasicTestCase):
'origin_url': origin_url,
}))
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
assert response.status_code == status.HTTP_204_NO_CONTENT
deposit = Deposit.objects.get(pk=deposit.id)
assert deposit.status == expected_status
assert deposit.swh_id == expected_swh_id
assert deposit.swh_id_context == expected_swh_id_context
assert deposit.swh_anchor_id == expected_swh_anchor_id
assert deposit.swh_anchor_id_context == expected_swh_anchor_id_context
deposit = Deposit.objects.get(pk=self.deposit.id)
self.assertEqual(deposit.status, expected_status)
self.assertEqual(deposit.swh_id, expected_swh_id)
self.assertEqual(deposit.swh_id_context, expected_swh_id_context)
self.assertEqual(deposit.swh_anchor_id, expected_swh_anchor_id)
self.assertEqual(deposit.swh_anchor_id_context,
expected_swh_anchor_id_context)
deposit.swh_id = None
deposit.swh_id_context = None
deposit.swh_anchor_id = None
deposit.swh_anchor_id_context = None
deposit.status = DEPOSIT_STATUS_VERIFIED
deposit.save()
def test_update_deposit_status_will_fail_with_unknown_status(self):
"""Unknown status for update should return a 400 response
"""
url = self.private_deposit_url(self.deposit.id)
def test_update_deposit_status_will_fail_with_unknown_status(
authenticated_client, deposit_collection, ready_deposit_verified):
"""Unknown status for update should return a 400 response
response = self.client.put(
"""
deposit = ready_deposit_verified
for url in private_check_url_endpoints(deposit_collection, deposit):
response = authenticated_client.put(
url,
content_type='application/json',
data=json.dumps({'status': 'unknown'}))
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_update_deposit_status_will_fail_with_no_status_key(self):
"""No status provided for update should return a 400 response
"""
url = self.private_deposit_url(self.deposit.id)
def test_update_deposit_status_will_fail_with_no_status_key(
authenticated_client, deposit_collection, ready_deposit_verified):
"""No status provided for update should return a 400 response
response = self.client.put(
"""
deposit = ready_deposit_verified
for url in private_check_url_endpoints(deposit_collection, deposit):
response = authenticated_client.put(
url,
content_type='application/json',
data=json.dumps({'something': 'something'}))
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_update_deposit_status_success_without_swh_id_fail(self):
"""Providing successful status without swh_id should return a 400
"""
url = self.private_deposit_url(self.deposit.id)
def test_update_deposit_status_success_without_swh_id_fail(
authenticated_client, deposit_collection, ready_deposit_verified):
"""Providing successful status without swh_id should return a 400
response = self.client.put(
"""
deposit = ready_deposit_verified
for url in private_check_url_endpoints(deposit_collection, deposit):
response = authenticated_client.put(
url,
content_type='application/json',
data=json.dumps({'status': DEPOSIT_STATUS_LOAD_SUCCESS}))
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
class UpdateDepositStatusTest2(UpdateDepositStatusTest):
def private_deposit_url(self, deposit_id):
return reverse(PRIVATE_PUT_DEPOSIT+'-nc', args=[deposit_id])
assert response.status_code == status.HTTP_400_BAD_REQUEST
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment