Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_snapshot.py
# Copyright (C) 2018 The Software Heritage developers | # Copyright (C) 2018 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 | ||||
import random | import random | ||||
from hypothesis import given | from hypothesis import given | ||||
from rest_framework.test import APITestCase | from rest_framework.test import APITestCase | ||||
from swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.data import random_sha1 | |||||
from swh.web.tests.strategies import ( | from swh.web.tests.strategies import ( | ||||
snapshot, unknown_snapshot, new_snapshot | snapshot, new_snapshot | ||||
) | ) | ||||
from swh.web.tests.testcase import WebTestCase | from swh.web.tests.testcase import WebTestCase | ||||
class SnapshotApiTestCase(WebTestCase, APITestCase): | class SnapshotApiTestCase(WebTestCase, APITestCase): | ||||
@given(snapshot()) | @given(snapshot()) | ||||
def test_api_snapshot(self, snapshot): | def test_api_snapshot(self, snapshot): | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | def test_api_snapshot_filtered(self, snapshot): | ||||
expected_data = self.snapshot_get_branches( | expected_data = self.snapshot_get_branches( | ||||
snapshot, target_types=target_type) | snapshot, target_types=target_type) | ||||
expected_data = self._enrich_snapshot(expected_data) | expected_data = self._enrich_snapshot(expected_data) | ||||
self.assertEqual(rv.status_code, 200) | self.assertEqual(rv.status_code, 200) | ||||
self.assertEqual(rv['Content-Type'], 'application/json') | self.assertEqual(rv['Content-Type'], 'application/json') | ||||
self.assertEqual(rv.data, expected_data) | self.assertEqual(rv.data, expected_data) | ||||
@given(unknown_snapshot()) | def test_api_snapshot_errors(self): | ||||
def test_api_snapshot_errors(self, unknown_snapshot): | unknown_snapshot_ = random_sha1() | ||||
url = reverse('api-snapshot', | url = reverse('api-snapshot', | ||||
url_args={'snapshot_id': '63ce369'}) | url_args={'snapshot_id': '63ce369'}) | ||||
rv = self.client.get(url) | rv = self.client.get(url) | ||||
self.assertEqual(rv.status_code, 400) | self.assertEqual(rv.status_code, 400) | ||||
url = reverse('api-snapshot', | url = reverse('api-snapshot', | ||||
url_args={'snapshot_id': unknown_snapshot}) | url_args={'snapshot_id': unknown_snapshot_}) | ||||
rv = self.client.get(url) | rv = self.client.get(url) | ||||
self.assertEqual(rv.status_code, 404) | self.assertEqual(rv.status_code, 404) | ||||
def _enrich_snapshot(self, snapshot): | def _enrich_snapshot(self, snapshot): | ||||
def _get_branch_url(target_type, target): | def _get_branch_url(target_type, target): | ||||
url = None | url = None | ||||
if target_type == 'revision': | if target_type == 'revision': | ||||
url = reverse('api-revision', url_args={'sha1_git': target}) | url = reverse('api-revision', url_args={'sha1_git': target}) | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | def test_api_snapshot_null_branch(self, new_snapshot): | ||||
snp_id = hash_to_hex(snp_dict['id']) | snp_id = hash_to_hex(snp_dict['id']) | ||||
for branch in snp_dict['branches'].keys(): | for branch in snp_dict['branches'].keys(): | ||||
snp_dict['branches'][branch] = None | snp_dict['branches'][branch] = None | ||||
break | break | ||||
self.storage.snapshot_add([snp_dict]) | self.storage.snapshot_add([snp_dict]) | ||||
url = reverse('api-snapshot', | url = reverse('api-snapshot', | ||||
url_args={'snapshot_id': snp_id}) | url_args={'snapshot_id': snp_id}) | ||||
rv = self.client.get(url) | rv = self.client.get(url) | ||||
self.assertEqual(rv.status_code, 200) | self.assertEqual(rv.status_code, 200, rv.data) |