diff --git a/swh/web/api/utils.py b/swh/web/api/utils.py --- a/swh/web/api/utils.py +++ b/swh/web/api/utils.py @@ -164,9 +164,12 @@ - filetype_url: its filetype information """ + checksums = content + if 'checksums' in content: + checksums = content['checksums'] for h in ['sha1', 'sha1_git', 'sha256']: - if h in content: - q = '%s:%s' % (h, content[h]) + if h in checksums: + q = '%s:%s' % (h, checksums[h]) if top_url: content['content_url'] = reverse('content', kwargs={'q': q}) content['data_url'] = reverse('content-raw', kwargs={'q': q}) 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 @@ -651,6 +651,14 @@ """ algo, hash = query.parse_hash(q) c = storage.content_find({algo: hash}) + if c: + # group all computed content checksums in a dictionnary + checksums = {} + for hash in hashutil.ALGORITHMS: + if hash in c: + checksums[hash] = c[hash] + del c[hash] + c['checksums'] = checksums return converters.from_content(c) diff --git a/swh/web/tests/api/views/test_content.py b/swh/web/tests/api/views/test_content.py --- a/swh/web/tests/api/views/test_content.py +++ b/swh/web/tests/api/views/test_content.py @@ -381,13 +381,17 @@ @patch('swh.web.api.views.content.service') @istest - def api_content_metadata(self, mock_service): + def test_api_content_metadata(self, mock_service): # given mock_service.lookup_content.return_value = { - 'sha1': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', - 'sha1_git': 'b4e8f472ffcb01a03875b26e462eb568739f6882', - 'sha256': '83c0e67cc80f60caf1fcbec2d84b0ccd7968b3be4735637006560' - 'cde9b067a4f', + 'checksums': { + 'blake2s256': '685395c5dc57cada459364f0946d3dd45bad5f' + 'cbabc1048edb44380f1d31d0aa', + 'sha1': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', + 'sha1_git': 'b4e8f472ffcb01a03875b26e462eb568739f6882', + 'sha256': '83c0e67cc80f60caf1fcbec2d84b0ccd7968b3be47' + '35637006560cde9b067a4f', + }, 'length': 17, 'status': 'visible' } @@ -399,6 +403,14 @@ self.assertEquals(rv.status_code, 200) self.assertEquals(rv['Content-Type'], 'application/json') self.assertEquals(rv.data, { + 'checksums': { + 'blake2s256': '685395c5dc57cada459364f0946d3dd45bad5f' + 'cbabc1048edb44380f1d31d0aa', + 'sha1': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', + 'sha1_git': 'b4e8f472ffcb01a03875b26e462eb568739f6882', + 'sha256': '83c0e67cc80f60caf1fcbec2d84b0ccd7968b3be47' + '35637006560cde9b067a4f', + }, 'data_url': '/api/1/content/' 'sha1:40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03/raw/', 'filetype_url': '/api/1/content/' @@ -407,10 +419,6 @@ 'sha1:40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03/language/', 'license_url': '/api/1/content/' 'sha1:40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03/license/', - 'sha1': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', - 'sha1_git': 'b4e8f472ffcb01a03875b26e462eb568739f6882', - 'sha256': '83c0e67cc80f60caf1fcbec2d84b0ccd7968b3be4735637006560c' - 'de9b067a4f', 'length': 17, 'status': 'visible' }) 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 @@ -18,12 +18,15 @@ class ServiceTestCase(unittest.TestCase): def setUp(self): - self.SHA1_SAMPLE = '18d8be353ed3480476f032475e7c233eff7371d5' + self.BLAKE2S256_SAMPLE = ('685395c5dc57cada459364f0946d3dd45b' + 'ad5fcbabc1048edb44380f1d31d0aa') + self.BLAKE2S256_SAMPLE_BIN = hash_to_bytes(self.BLAKE2S256_SAMPLE) + self.SHA1_SAMPLE = '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03' self.SHA1_SAMPLE_BIN = hash_to_bytes(self.SHA1_SAMPLE) - self.SHA256_SAMPLE = ('39007420ca5de7cb3cfc15196335507e' - 'e76c98930e7e0afa4d2747d3bf96c926') + self.SHA256_SAMPLE = ('8abb0aa566452620ecce816eecdef4792d77a' + '293ad8ea82a4d5ecb4d36f7e560') self.SHA256_SAMPLE_BIN = hash_to_bytes(self.SHA256_SAMPLE) - self.SHA1GIT_SAMPLE = '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03' + self.SHA1GIT_SAMPLE = '25d1a2e8f32937b0f498a5ca87f823d8df013c01' self.SHA1GIT_SAMPLE_BIN = hash_to_bytes(self.SHA1GIT_SAMPLE) self.DIRECTORY_ID = '7834ef7e7c357ce2af928115c6c6a42b7e2a44e6' self.DIRECTORY_ID_BIN = hash_to_bytes(self.DIRECTORY_ID) @@ -84,13 +87,17 @@ } self.SAMPLE_CONTENT = { - 'sha1': self.SHA1_SAMPLE, - 'sha256': self.SHA256_SAMPLE, - 'sha1_git': self.SHA1GIT_SAMPLE, + 'checksums': { + 'blake2s256': self.BLAKE2S256_SAMPLE, + 'sha1': self.SHA1_SAMPLE, + 'sha256': self.SHA256_SAMPLE, + 'sha1_git': self.SHA1GIT_SAMPLE, + }, 'length': 190, 'status': 'absent' } self.SAMPLE_CONTENT_RAW = { + 'blake2s256': self.BLAKE2S256_SAMPLE_BIN, 'sha1': self.SHA1_SAMPLE_BIN, 'sha256': self.SHA256_SAMPLE_BIN, 'sha1_git': self.SHA1GIT_SAMPLE_BIN, @@ -1567,7 +1574,7 @@ # when actual_content = service.lookup_content_raw( - 'sha1:18d8be353ed3480476f032475e7c233eff7371d5') + 'sha1:' + self.SHA1_SAMPLE) # then self.assertIsNone(actual_content)