diff --git a/swh/web/common/service.py b/swh/web/common/service.py --- a/swh/web/common/service.py +++ b/swh/web/common/service.py @@ -88,7 +88,7 @@ """ algo, hash = query.parse_hash(q) - found = storage.content_find({algo: hash}) + found = _first_element(storage.content_find({algo: hash})) return {'found': converters.from_content(found), 'algo': algo} @@ -103,7 +103,7 @@ """ algo, hash = query.parse_hash(q) - found = storage.content_find({algo: hash}) + found = _first_element(storage.content_find({algo: hash})) return {'found': found is not None} @@ -119,7 +119,7 @@ """ algo, hash = query.parse_hash(q) if algo != 'sha1': - hashes = storage.content_find({algo: hash}) + hashes = _first_element(storage.content_find({algo: hash})) if not hashes: return None return hashes['sha1'] @@ -690,13 +690,10 @@ """ sha1_git_bin = _to_sha1_bin(sha1_git) - revision = _first_element(storage.revision_get([sha1_git_bin])) if not revision: raise NotFoundExc('Revision %s not found' % sha1_git) - dir_sha1_git_bin = revision['directory'] - if dir_path: paths = dir_path.strip(os.path.sep).split(os.path.sep) entity = storage.directory_entry_get_by_path( @@ -708,7 +705,6 @@ % (dir_path, sha1_git)) else: entity = {'type': 'dir', 'target': dir_sha1_git_bin} - if entity['type'] == 'dir': directory_entries = storage.directory_ls(entity['target']) or [] return {'type': 'dir', @@ -717,7 +713,14 @@ 'content': list(map(converters.from_directory_entry, directory_entries))} elif entity['type'] == 'file': # content - content = storage.content_find({'sha1_git': entity['target']}) + content = _first_element( + storage.content_find({'sha1_git': entity['target']}) + ) + if not content: + raise NotFoundExc( + "Revision %s not found" + % (entity['target']) + ) if with_data: c = _first_element(storage.content_get([content['sha1']])) content['data'] = c['data'] @@ -747,7 +750,7 @@ """ algo, hash = query.parse_hash(q) - c = storage.content_find({algo: hash}) + c = _first_element(storage.content_find({algo: hash})) if not c: raise NotFoundExc('Content with %s checksum equals to %s not found!' % (algo, hashutil.hash_to_hex(hash))) diff --git a/swh/web/tests/common/test_service.py b/swh/web/tests/common/test_service.py --- a/swh/web/tests/common/test_service.py +++ b/swh/web/tests/common/test_service.py @@ -295,7 +295,6 @@ self, revision, unknown_revision): sha1_git_root = unknown_revision sha1_git = revision - with self.assertRaises(NotFoundExc) as cm: service.lookup_revision_with_context(sha1_git_root, sha1_git) self.assertIn('Revision root %s not found' % sha1_git_root, @@ -340,9 +339,23 @@ self.assertIn('Revision %s not found' % unknown_revision, cm.exception.args[0]) - @given(revision()) - def test_lookup_directory_with_revision_ko_path_to_nowhere(self, revision): + def test_lookup_directory_with_revision_unknown_content(self): + unknown_content_ = { + 'sha1_git': '0cbad36778b71ee6a97e04e9e6201d2629f448d3' + } + revision_ = {'id': '5aad84493ee10c65fc28dfb1f9a4471917f902aa'} + sha1_git = revision_['id'] + revision_['target'] = unknown_content_['sha1_git'] + revision_['type'] = 'file' + with self.assertRaises(NotFoundExc) as cm: + service.lookup_directory_with_revision(sha1_git) + self.assertIn('Revision %s not found' % sha1_git, + cm.exception.args[0]) + + @given(revision()) + def test_lookup_directory_with_revision_ko_path_to_nowhere( + self, revision): invalid_path = 'path/to/something/unknown' with self.assertRaises(NotFoundExc) as cm: service.lookup_directory_with_revision(revision, invalid_path)