p["id"], "url": reverse( "api-1-revision", url_args={"sha1_git": p["id"]}, request=request), } for p in revision_data["parents"]) revision_data["children_urls"] = [ reverse("api-1-revision", url_args={"sha1_git": child_revision}, request=request) ] assert actual_revision == revision_data @given(snapshot()) def test_enrich_snapshot(api_request_factory, archive_data, snapshot): snapshot_data = archive_data.snapshot_get(snapshot) url = reverse("api-1-snapshot", url_args={"snapshot_id": snapshot}) request = api_request_factory.get(url) actual_snapshot = utils.enrich_snapshot(snapshot_data, request) for _, b in snapshot_data["branches"].items(): if b["target_type"] in ("directory", "revision", "release"): b["target_url"] = reverse( f'api-1-{b["target_type"]}', url_args={"sha1_git": b["target"]}, request=request, )
dir_entries = [ e for e in archive_data.directory_ls(rev_data["directory"]) if e["type"] == "file" ] dir_entry = random.choice(dir_entries) assert archive.lookup_directory_through_revision( {"sha1_git": revision}, dir_entry["name"], with_data=True) == ( revision, archive.lookup_directory_with_revision(revision, dir_entry["name"], with_data=True), ) @given(content(), directory(), release(), revision(), snapshot()) def test_lookup_known_objects(archive_data, content, directory, release, revision, snapshot): expected = archive_data.content_find(content) assert archive.lookup_object(CONTENT, content["sha1_git"]) == expected expected = archive_data.directory_get(directory) assert archive.lookup_object(DIRECTORY, directory) == expected expected = archive_data.release_get(release) assert archive.lookup_object(RELEASE, release) == expected expected = archive_data.revision_get(revision) assert archive.lookup_object(REVISION, revision) == expected expected = {**archive_data.snapshot_get(snapshot), "next_branch": None}
class SnapshotApiTestCase(WebTestCase, APITestCase): @given(snapshot()) def test_api_snapshot(self, snapshot): url = reverse('api-snapshot', url_args={'snapshot_id': snapshot}) rv = self.client.get(url) self.assertEqual(rv.status_code, 200) self.assertEqual(rv['Content-Type'], 'application/json') expected_data = self.snapshot_get(snapshot) expected_data = self._enrich_snapshot(expected_data) self.assertEqual(rv.data, expected_data) @given(snapshot()) def test_api_snapshot_paginated(self, snapshot): branches_offset = 0 branches_count = 2 snapshot_branches = [] for k, v in sorted(self.snapshot_get(snapshot)['branches'].items()): snapshot_branches.append({ 'name': k, 'target_type': v['target_type'], 'target': v['target'] }) whole_snapshot = {'id': snapshot, 'branches': {}, 'next_branch': None} while branches_offset < len(snapshot_branches): branches_from = snapshot_branches[branches_offset]['name'] url = reverse('api-snapshot', url_args={'snapshot_id': snapshot}, query_params={ 'branches_from': branches_from, 'branches_count': branches_count }) rv = self.client.get(url) self.assertEqual(rv.status_code, 200) self.assertEqual(rv['Content-Type'], 'application/json') expected_data = self.snapshot_get_branches(snapshot, branches_from, branches_count) expected_data = self._enrich_snapshot(expected_data) branches_offset += branches_count if branches_offset < len(snapshot_branches): next_branch = snapshot_branches[branches_offset]['name'] expected_data['next_branch'] = next_branch else: expected_data['next_branch'] = None self.assertEqual(rv.data, expected_data) whole_snapshot['branches'].update(expected_data['branches']) if branches_offset < len(snapshot_branches): next_url = reverse('api-snapshot', url_args={'snapshot_id': snapshot}, query_params={ 'branches_from': next_branch, 'branches_count': branches_count }) self.assertEqual(rv['Link'], '<%s>; rel="next"' % next_url) else: self.assertFalse(rv.has_header('Link')) url = reverse('api-snapshot', url_args={'snapshot_id': snapshot}) rv = self.client.get(url) self.assertEqual(rv.status_code, 200) self.assertEqual(rv['Content-Type'], 'application/json') self.assertEqual(rv.data, whole_snapshot) @given(snapshot()) def test_api_snapshot_filtered(self, snapshot): snapshot_branches = [] for k, v in sorted(self.snapshot_get(snapshot)['branches'].items()): snapshot_branches.append({ 'name': k, 'target_type': v['target_type'], 'target': v['target'] }) target_type = random.choice(snapshot_branches)['target_type'] url = reverse('api-snapshot', url_args={'snapshot_id': snapshot}, query_params={'target_types': target_type}) rv = self.client.get(url) expected_data = self.snapshot_get_branches(snapshot, target_types=target_type) expected_data = self._enrich_snapshot(expected_data) self.assertEqual(rv.status_code, 200) self.assertEqual(rv['Content-Type'], 'application/json') self.assertEqual(rv.data, expected_data) @given(unknown_snapshot()) def test_api_snapshot_errors(self, unknown_snapshot): url = reverse('api-snapshot', url_args={'snapshot_id': '63ce369'}) rv = self.client.get(url) self.assertEqual(rv.status_code, 400) url = reverse('api-snapshot', url_args={'snapshot_id': unknown_snapshot}) rv = self.client.get(url) self.assertEqual(rv.status_code, 404) def _enrich_snapshot(self, snapshot): def _get_branch_url(target_type, target): url = None if target_type == 'revision': url = reverse('api-revision', url_args={'sha1_git': target}) if target_type == 'release': url = reverse('api-release', url_args={'sha1_git': target}) return url for branch in snapshot['branches'].keys(): target = snapshot['branches'][branch]['target'] target_type = snapshot['branches'][branch]['target_type'] snapshot['branches'][branch]['target_url'] = \ _get_branch_url(target_type, target) for branch in snapshot['branches'].keys(): target = snapshot['branches'][branch]['target'] target_type = snapshot['branches'][branch]['target_type'] if target_type == 'alias': if target in snapshot['branches']: snapshot['branches'][branch]['target_url'] = \ snapshot['branches'][target]['target_url'] else: snp = self.snapshot_get_branches(snapshot['id'], branches_from=target, branches_count=1) alias_target = snp['branches'][target]['target'] alias_target_type = snp['branches'][target]['target_type'] snapshot['branches'][branch]['target_url'] = \ _get_branch_url(alias_target_type, alias_target) return snapshot @given(snapshot()) def test_api_snapshot_uppercase(self, snapshot): url = reverse('api-snapshot-uppercase-checksum', url_args={'snapshot_id': snapshot.upper()}) resp = self.client.get(url) self.assertEqual(resp.status_code, 302) redirect_url = reverse('api-snapshot-uppercase-checksum', url_args={'snapshot_id': snapshot}) self.assertEqual(resp['location'], redirect_url)
class SwhBrowseIdTest(WebTestCase): @given(content()) def test_content_id_browse(self, content): cnt_sha1_git = content['sha1_git'] swh_id = swh_id_prefix + 'cnt:' + cnt_sha1_git url = reverse('browse-swh-id', url_args={'swh_id': swh_id}) query_string = 'sha1_git:' + cnt_sha1_git content_browse_url = reverse('browse-content', url_args={'query_string': query_string}) resp = self.client.get(url) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['location'], content_browse_url) @given(directory()) def test_directory_id_browse(self, directory): swh_id = swh_id_prefix + 'dir:' + directory url = reverse('browse-swh-id', url_args={'swh_id': swh_id}) directory_browse_url = reverse('browse-directory', url_args={'sha1_git': directory}) resp = self.client.get(url) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['location'], directory_browse_url) @given(revision()) def test_revision_id_browse(self, revision): swh_id = swh_id_prefix + 'rev:' + revision url = reverse('browse-swh-id', url_args={'swh_id': swh_id}) revision_browse_url = reverse('browse-revision', url_args={'sha1_git': revision}) resp = self.client.get(url) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['location'], revision_browse_url) query_params = {'origin_type': 'git', 'origin': 'https://github.com/user/repo'} url = reverse('browse-swh-id', url_args={'swh_id': swh_id}, query_params=query_params) revision_browse_url = reverse('browse-revision', url_args={'sha1_git': revision}, query_params=query_params) resp = self.client.get(url) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['location'], revision_browse_url) @given(release()) def test_release_id_browse(self, release): swh_id = swh_id_prefix + 'rel:' + release url = reverse('browse-swh-id', url_args={'swh_id': swh_id}) release_browse_url = reverse('browse-release', url_args={'sha1_git': release}) resp = self.client.get(url) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['location'], release_browse_url) query_params = {'origin_type': 'git', 'origin': 'https://github.com/user/repo'} url = reverse('browse-swh-id', url_args={'swh_id': swh_id}, query_params=query_params) release_browse_url = reverse('browse-release', url_args={'sha1_git': release}, query_params=query_params) resp = self.client.get(url) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['location'], release_browse_url) @given(snapshot()) def test_snapshot_id_browse(self, snapshot): swh_id = swh_id_prefix + 'snp:' + snapshot url = reverse('browse-swh-id', url_args={'swh_id': swh_id}) snapshot_browse_url = reverse('browse-snapshot', url_args={'snapshot_id': snapshot}) resp = self.client.get(url) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['location'], snapshot_browse_url) query_params = {'origin_type': 'git', 'origin': 'https://github.com/user/repo'} url = reverse('browse-swh-id', url_args={'swh_id': swh_id}, query_params=query_params) release_browse_url = reverse('browse-snapshot', url_args={'snapshot_id': snapshot}, query_params=query_params) resp = self.client.get(url) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['location'], release_browse_url) @given(release()) def test_bad_id_browse(self, release): swh_id = swh_id_prefix + 'foo:' + release url = reverse('browse-swh-id', url_args={'swh_id': swh_id}) resp = self.client.get(url) self.assertEqual(resp.status_code, 400) @given(content()) def test_content_id_optional_parts_browse(self, content): cnt_sha1_git = content['sha1_git'] optional_parts = ';lines=4-20;origin=https://github.com/user/repo' swh_id = swh_id_prefix + 'cnt:' + cnt_sha1_git + optional_parts url = reverse('browse-swh-id', url_args={'swh_id': swh_id}) query_string = 'sha1_git:' + cnt_sha1_git content_browse_url = reverse( 'browse-content', url_args={'query_string': query_string}, query_params={'origin': 'https://github.com/user/repo'}) content_browse_url += '#L4-L20' resp = self.client.get(url) self.assertEqual(resp.status_code, 302) self.assertEqual(resp['location'], content_browse_url)
class SwhIdsApiTestCase(WebTestCase, APITestCase): @given(origin(), content(), directory(), release(), revision(), snapshot()) def test_swh_id_resolve_success(self, origin, content, directory, release, revision, snapshot): for obj_type_short, obj_type, obj_id in (('cnt', CONTENT, content['sha1_git']), ('dir', DIRECTORY, directory), ('rel', RELEASE, release), ('rev', REVISION, revision), ('snp', SNAPSHOT, snapshot)): swh_id = 'swh:1:%s:%s;origin=%s' % (obj_type_short, obj_id, origin['url']) url = reverse('api-resolve-swh-pid', url_args={'swh_id': swh_id}) resp = self.client.get(url) if obj_type == CONTENT: url_args = {'query_string': 'sha1_git:%s' % obj_id} elif obj_type == SNAPSHOT: url_args = {'snapshot_id': obj_id} else: url_args = {'sha1_git': obj_id} browse_rev_url = reverse('browse-%s' % obj_type, url_args=url_args, query_params={'origin': origin['url']}) expected_result = { 'browse_url': browse_rev_url, 'metadata': { 'origin': origin['url'] }, 'namespace': 'swh', 'object_id': obj_id, 'object_type': obj_type, 'scheme_version': 1 } self.assertEqual(resp.status_code, 200) self.assertEqual(resp.data, expected_result) def test_swh_id_resolve_invalid(self): rev_id_invalid = '96db9023b8_foo_50d6c108e9a3' swh_id = 'swh:1:rev:%s' % rev_id_invalid url = reverse('api-resolve-swh-pid', url_args={'swh_id': swh_id}) resp = self.client.get(url) self.assertEqual(resp.status_code, 400) @given(unknown_content(), unknown_directory(), unknown_release(), unknown_revision(), unknown_snapshot()) def test_swh_id_resolve_not_found(self, unknown_content, unknown_directory, unknown_release, unknown_revision, unknown_snapshot): for obj_type_short, obj_id in (('cnt', unknown_content['sha1_git']), ('dir', unknown_directory), ('rel', unknown_release), ('rev', unknown_revision), ('snp', unknown_snapshot)): swh_id = 'swh:1:%s:%s' % (obj_type_short, obj_id) url = reverse('api-resolve-swh-pid', url_args={'swh_id': swh_id}) resp = self.client.get(url) self.assertEqual(resp.status_code, 404)