def test_api_revision(api_client, archive_data, revision): url = reverse("api-1-revision", url_args={"sha1_git": revision}) rv = check_api_get_responses(api_client, url, status_code=200) expected_revision = archive_data.revision_get(revision) enrich_revision(expected_revision, rv.wsgi_request) assert rv.data == expected_revision
def test_enrich_revision_without_children_or_parent(api_request_factory, archive_data, revision): revision_data = archive_data.revision_get(revision) del revision_data["parents"] url = reverse("api-1-revision", url_args={"sha1_git": revision}) request = api_request_factory.get(url) actual_revision = utils.enrich_revision(revision_data, request) revision_data["url"] = reverse("api-1-revision", url_args={"sha1_git": revision}, request=request) revision_data["history_url"] = reverse("api-1-revision-log", url_args={"sha1_git": revision}, request=request) revision_data["directory_url"] = reverse( "api-1-directory", url_args={"sha1_git": revision_data["directory"]}, request=request, ) assert actual_revision == revision_data
def _revision_directory_by(revision, path, request_path, limit=100, with_data=False): """ Compute the revision matching criterion's directory or content data. Args: revision: dictionary of criterions representing a revision to lookup path: directory's path to lookup request_path: request path which holds the original context to limit: optional query parameter to limit the revisions log (default to 100). For now, note that this limit could impede the transitivity conclusion about sha1_git not being an ancestor of with_data: indicate to retrieve the content's raw data if path resolves to a content. """ def enrich_directory_local(dir, context_url=request_path): return utils.enrich_directory(dir, context_url) rev_id, result = service.lookup_directory_through_revision( revision, path, limit=limit, with_data=with_data) content = result['content'] if result['type'] == 'dir': # dir_entries result['content'] = list(map(enrich_directory_local, content)) elif result['type'] == 'file': # content result['content'] = utils.enrich_content(content) elif result['type'] == 'rev': # revision result['content'] = utils.enrich_revision(content) return result
def test_enrich_revision_with_no_message(self, mock_django_reverse): # given mock_django_reverse.side_effect = self._reverse_rev_message_test # when expected_revision = { 'id': 'rev-id', 'url': '/api/revision/rev-id/', 'history_url': '/api/revision/rev-id/log/', 'message': None, 'parents': [{ 'id': '123', 'url': '/api/revision/123/' }], 'children': ['456'], 'children_urls': ['/api/revision/456/'], } actual_revision = utils.enrich_revision({ 'id': 'rev-id', 'message': None, 'parents': ['123'], 'children': ['456'], }) # then self.assertEqual(actual_revision, expected_revision) mock_django_reverse.assert_has_calls([ call('api-revision', url_args={'sha1_git': 'rev-id'}), call('api-revision-log', url_args={'sha1_git': 'rev-id'}), call('api-revision', url_args={'sha1_git': '123'}), call('api-revision', url_args={'sha1_git': '456'}) ])
def test_enrich_revision_without_children_or_parent( self, mock_django_reverse, ): # given def reverse_test(view_name, url_args): if view_name == 'api-revision': return '/api/revision/' + url_args['sha1_git'] + '/' elif view_name == 'api-revision-log': return '/api/revision/' + url_args['sha1_git'] + '/log/' elif view_name == 'api-directory': return '/api/directory/' + url_args['sha1_git'] + '/' elif view_name == 'api-person': return '/api/person/' + url_args['person_id'] + '/' mock_django_reverse.side_effect = reverse_test # when actual_revision = utils.enrich_revision({ 'id': 'rev-id', 'directory': '123', 'author': { 'id': '1' }, 'committer': { 'id': '2' }, }) expected_revision = { 'id': 'rev-id', 'directory': '123', 'url': '/api/revision/rev-id/', 'history_url': '/api/revision/rev-id/log/', 'directory_url': '/api/directory/123/', 'author': { 'id': '1' }, 'author_url': '/api/person/1/', 'committer': { 'id': '2' }, 'committer_url': '/api/person/2/' } # then self.assertEqual(actual_revision, expected_revision) mock_django_reverse.assert_has_calls([ call('api-revision', url_args={'sha1_git': 'rev-id'}), call('api-revision-log', url_args={'sha1_git': 'rev-id'}), call('api-person', url_args={'person_id': '1'}), call('api-person', url_args={'person_id': '2'}), call('api-directory', url_args={'sha1_git': '123'}) ])
def test_enrich_revision_with_invalid_message(api_request_factory, archive_data, revision, parent_revision, child_revision): revision_data = archive_data.revision_get(revision) revision_data["decoding_failures"] = ["message"] revision_data["parents"] = revision_data["parents"] + (parent_revision, ) revision_data["children"] = child_revision url = reverse("api-1-revision", url_args={"sha1_git": revision}) request = api_request_factory.get(url) actual_revision = utils.enrich_revision(revision_data, request) revision_data["url"] = reverse("api-1-revision", url_args={"sha1_git": revision}, request=request) revision_data["message_url"] = reverse("api-1-revision-raw-message", url_args={"sha1_git": revision}, request=request) revision_data["directory_url"] = reverse( "api-1-directory", url_args={"sha1_git": revision_data["directory"]}, request=request, ) revision_data["history_url"] = reverse("api-1-revision-log", url_args={"sha1_git": revision}, request=request) revision_data["parents"] = tuple({ "id": 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
def api_revision_directory(request, sha1_git, dir_path=None, with_data=False): """ .. http:get:: /api/1/revision/(sha1_git)/directory/[(path)/] Get information about directory (entry) objects associated to revisions. Each revision is associated to a single "root" directory. This endpoint behaves like :http:get:`/api/1/directory/(sha1_git)/[(path)/]`, but operates on the root directory associated to a given revision. :param string sha1_git: hexadecimal representation of the revision **sha1_git** identifier :param string path: optional parameter to get information about the directory entry pointed by that relative path {common_headers} :>json array content: directory entries as returned by :http:get:`/api/1/directory/(sha1_git)/[(path)/]` :>json string path: path of directory from the revision root one :>json string revision: the unique revision identifier :>json string type: the type of the directory :statuscode 200: no error :statuscode 400: an invalid **sha1_git** value has been provided :statuscode 404: requested revision can not be found in the archive **Example:** .. parsed-literal:: :swh_web_api:`revision/f1b94134a4b879bc55c3dacdb496690c8ebdc03f/directory/` """ rev_id, result = archive.lookup_directory_through_revision( {"sha1_git": sha1_git}, dir_path, with_data=with_data ) content = result["content"] if result["type"] == "dir": # dir_entries result["content"] = [ utils.enrich_directory_entry(entry, request=request) for entry in content ] elif result["type"] == "file": # content result["content"] = utils.enrich_content(content, request=request) elif result["type"] == "rev": # revision result["content"] = utils.enrich_revision(content, request=request) return result
def test_enrich_revision_no_context(api_request_factory, revision, parent_revision, child_revision): revision_data = { "id": revision, "parents": [parent_revision], "children": [child_revision], } url = reverse("api-1-revision", url_args={"sha1_git": revision}) request = api_request_factory.get(url) actual_revision = utils.enrich_revision(revision_data, request) revision_data["url"] = reverse("api-1-revision", url_args={"sha1_git": revision}, request=request) revision_data["history_url"] = reverse("api-1-revision-log", url_args={"sha1_git": revision}, request=request) revision_data["parents"] = tuple({ "id": parent_revision, "url": reverse( "api-1-revision", url_args={"sha1_git": parent_revision}, request=request, ), }) revision_data["children_urls"] = [ reverse("api-1-revision", url_args={"sha1_git": child_revision}, request=request) ] assert actual_revision == revision_data
def test_api_revision_directory_ok_returns_revision(api_client, archive_data, revision, person, date): rev_path = "foo" _dir = Directory(entries=(DirectoryEntry( name=rev_path.encode(), type="rev", target=hash_to_bytes(revision), perms=DentryPerms.revision, ), )) archive_data.directory_add([_dir]) rev = Revision( directory=_dir.id, author=person, committer=person, message=b"commit message", date=TimestampWithTimezone.from_datetime(date), committer_date=TimestampWithTimezone.from_datetime(date), synthetic=False, type=RevisionType.GIT, ) archive_data.revision_add([rev]) revision_id = hash_to_hex(rev.id) rev_data = archive_data.revision_get(revision) url = reverse( "api-1-revision-directory", { "sha1_git": revision_id, "dir_path": rev_path }, ) rv = check_api_get_responses(api_client, url, status_code=200) assert rv.data == { "content": enrich_revision(rev_data, request=rv.wsgi_request), "path": rev_path, "type": "rev", "revision": revision_id, }
def test_enrich_revision_with_children_and_parent_no_dir( api_request_factory, archive_data, revision, parent_revision, child_revision): revision_data = archive_data.revision_get(revision) del revision_data["directory"] revision_data["parents"] = revision_data["parents"] + (parent_revision, ) revision_data["children"] = child_revision url = reverse("api-1-revision", url_args={"sha1_git": revision}) request = api_request_factory.get(url) actual_revision = utils.enrich_revision(revision_data, request) revision_data["url"] = reverse("api-1-revision", url_args={"sha1_git": revision}, request=request) revision_data["history_url"] = reverse("api-1-revision-log", url_args={"sha1_git": revision}, request=request) revision_data["parents"] = tuple({ "id": 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