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,25 +690,20 @@ """ 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( dir_sha1_git_bin, list(map(lambda p: p.encode('utf-8'), paths))) - if not entity: raise NotFoundExc( "Directory or File '%s' pointed to by revision %s not found" % (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 +712,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( + "Content not found for revision %s" + % (sha1_git) + ) if with_data: c = _first_element(storage.content_get([content['sha1']])) content['data'] = c['data'] @@ -747,7 +749,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 @@ -11,6 +11,7 @@ from hypothesis import given from swh.model.hashutil import hash_to_bytes, hash_to_hex +from swh.model import from_disk from swh.web.common import service from swh.web.common.exc import BadInputExc, NotFoundExc @@ -296,7 +297,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, @@ -341,9 +341,57 @@ 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): + @given(revision(), unknown_content()) + def test_lookup_directory_with_revision_unknown_content( + self, revision, unknown_content): + # First retrieve a known directory + sha1_git_bin = service._to_sha1_bin(revision) + revision_ = service._first_element( + self.storage.revision_get([sha1_git_bin]) + ) + # Get a directory path from memory + dir_path = [e for e in self.directory_ls(revision_['directory']) + if e['type'] == 'file'] + dir_path = dir_path[0]['name'] + # Now modify the revision as follows + # A revision that points to a known directory + # Which points to unknown content + revision_['type'] = 'file' + revision_['id'] = service._to_sha1_bin( + revision[0:len(revision)-2]+"00" + ) + revision_['directory'] = service._to_sha1_bin( + revision[0:len(revision)-2]+"88" + ) + # A directory that points to unknown content + dir = {'id': service._to_sha1_bin( + revision[0:len(revision)-2]+"88" + ), + 'entries': [{ + 'name': bytes(dir_path.encode('utf-8')), + 'type': 'file', + 'target': service._to_sha1_bin( + unknown_content['sha1_git'] + ), + 'perms': from_disk.DentryPerms.content + }] + } + # Add the directory and revision in mem + self.storage.directory_add([dir]) + self.storage.revision_add([revision_]) + with self.assertRaises(NotFoundExc) as cm: + service.lookup_directory_with_revision( + ( + revision[0:len(revision)-2]+"00"), dir_path + ) + self.assertIn('Content not found for revision %s' % + revision[0:len(revision)-2]+"00", + 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)