diff --git a/swh/web/ui/service.py b/swh/web/ui/service.py --- a/swh/web/ui/service.py +++ b/swh/web/ui/service.py @@ -74,8 +74,8 @@ Args: query string of the form - Returns: Dict with key found to True or False, according to - whether the checksum is present or not + Returns: Dict with key found containing the hash info if the + hash is present, None if not. """ algo, hash = query.parse_hash(q) @@ -84,6 +84,20 @@ 'algo': algo} +def search_hash(q): + """Checks if the storage contains a given content checksum + + Args: query string of the form + + Returns: Dict with key found to True or False, according to + whether the checksum is present or not + + """ + algo, hash = query.parse_hash(q) + found = backend.content_find(algo, hash) + return {'found': found is not None} + + def lookup_hash_origin(q): """Return information about the checksum contained in the query q. diff --git a/swh/web/ui/tests/test_service.py b/swh/web/ui/tests/test_service.py --- a/swh/web/ui/tests/test_service.py +++ b/swh/web/ui/tests/test_service.py @@ -107,6 +107,45 @@ @patch('swh.web.ui.service.backend') @istest + def search_hash_does_not_exist(self, mock_backend): + # given + mock_backend.content_find = MagicMock(return_value=None) + + # when + actual_lookup = service.search_hash( + 'sha1_git:123caf10e9535160d90e874b45aa426de762f19f') + + # then + self.assertEquals({'found': False}, actual_lookup) + + # check the function has been called with parameters + mock_backend.content_find.assert_called_with( + 'sha1_git', + hex_to_hash('123caf10e9535160d90e874b45aa426de762f19f')) + + @patch('swh.web.ui.service.backend') + @istest + def search_hash_exist(self, mock_backend): + # given + stub_content = { + 'sha1': hex_to_hash('456caf10e9535160d90e874b45aa426de762f19f') + } + mock_backend.content_find = MagicMock(return_value=stub_content) + + # when + actual_lookup = service.search_hash( + 'sha1:456caf10e9535160d90e874b45aa426de762f19f') + + # then + self.assertEquals({'found': True}, actual_lookup) + + mock_backend.content_find.assert_called_with( + 'sha1', + hex_to_hash('456caf10e9535160d90e874b45aa426de762f19f'), + ) + + @patch('swh.web.ui.service.backend') + @istest def lookup_hash_origin(self, mock_backend): # given mock_backend.content_find_occurrence = MagicMock(return_value={