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,75 @@ 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(unknown_content(), unknown_revision(), unknown_directory()) + def test_lookup_directory_with_revision_unknown_content( + self, unknown_content, unknown_revision, unknown_directory): + + dir_path = 'README.md' + # Create a revision that points to a directory + # Which points to unknown content + revision = dict() + revision = { + 'author': { + 'name': b'abcd', + 'email': b'abcd@company.org', + 'fullname': b'abcd abcd' + }, + 'committer': { + 'email': b'aaaa@company.org', + 'fullname': b'aaaa aaa', + 'name': b'aaa' + }, + 'committer_date': { + 'negative_utc': False, + 'offset': 0, + 'timestamp': 1437511651 + }, + 'date': { + 'negative_utc': False, + 'offset': 0, + 'timestamp': 1437511651 + }, + 'message': b'bleh', + 'metadata': [], + 'parents': [], + 'synthetic': False, + 'type': 'file', + 'id': service._to_sha1_bin( + unknown_revision + ), + 'directory': service._to_sha1_bin( + unknown_directory + ) + } + # A directory that points to unknown content + dir = { + 'id': service._to_sha1_bin( + unknown_directory + ), + '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( + unknown_revision, dir_path + ) + self.assertIn('Content not found for revision %s' % + unknown_revision, + 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)