def get_hashes(self, version): data = hashin.get_package_hashes(self.name, version=version, algorithm="sha256", python_versions=PYTHON_VERSIONS, verbose=True) return data["hashes"]
def get_hashes(self, version): data = hashin.get_package_hashes( self.name, version=version, algorithm="sha256", python_versions=PYTHON_VERSIONS, verbose=True ) return data["hashes"]
def test_get_package_hashes(self, murlopen): def mocked_get(url, **options): if url == "https://pypi.org/pypi/hashin/json": return _Response({ 'info': { 'version': '0.10', 'name': 'hashin', }, 'releases': { '0.10': [{ 'url': 'https://pypi.org/packages/2.7/p/hashin/hashin-0.10-py2-none-any.whl', 'digests': { 'sha256': 'aaaaa', } }, { 'url': 'https://pypi.org/packages/3.3/p/hashin/hashin-0.10-py3-none-any.whl', 'digests': { 'sha256': 'bbbbb', } }, { 'url': 'https://pypi.org/packages/source/p/hashin/hashin-0.10.tar.gz', 'digests': { 'sha256': 'ccccc', } }] } }) raise NotImplementedError(url) murlopen.side_effect = mocked_get result = hashin.get_package_hashes( package='hashin', version='0.10', algorithm='sha256', ) expected = { 'package': 'hashin', 'version': '0.10', 'hashes': [{ 'hash': 'aaaaa' }, { 'hash': 'bbbbb' }, { 'hash': 'ccccc' }] } self.assertEqual(result, expected)
def get_dependency_hash(dependency_name, dependency_version, algorithm): hashes = hashin.get_package_hashes(dependency_name, version=dependency_version, algorithm=algorithm) return json.dumps({"result": hashes["hashes"]})
def test_get_package_hashes_without_version(self, murlopen): def mocked_get(url, **options): if url == 'https://pypi.python.org/pypi/hashin/json': return _Response({ 'info': { 'version': '0.10', 'name': 'hashin', }, 'releases': { '0.10': [{ 'url': 'https://pypi.python.org/packages/2.7/p/hashin/hashin-0.10-py2-none-any.whl', }, { 'url': 'https://pypi.python.org/packages/3.3/p/hashin/hashin-0.10-py3-none-any.whl', }, { 'url': 'https://pypi.python.org/packages/source/p/hashin/hashin-0.10.tar.gz', }] } }) elif url == 'https://pypi.python.org/packages/2.7/p/hashin/hashin-0.10-py2-none-any.whl': return _Response(b'Some py2 wheel content\n') elif url == 'https://pypi.python.org/packages/3.3/p/hashin/hashin-0.10-py3-none-any.whl': return _Response(b'Some py3 wheel content\n') elif url == 'https://pypi.python.org/packages/source/p/hashin/hashin-0.10.tar.gz': return _Response(b'Some tarball content\n') elif url == 'https://pypi.python.org/pypi/uggamugga/json': return _Response({ 'info': { 'version': '1.2.3', 'name': 'uggamugga', }, 'releases': {} # Note! }) raise NotImplementedError(url) murlopen.side_effect = mocked_get stdout_buffer = StringIO() with redirect_stdout(stdout_buffer): result = hashin.get_package_hashes( package='hashin', verbose=True, # python_versions=('3.5',), ) self.assertEqual(result['package'], 'hashin') self.assertEqual(result['version'], '0.10') self.assertTrue(result['hashes']) stdout = stdout_buffer.getvalue() self.assertTrue('Latest version for hashin is 0.10' in stdout) # Let's do it again and mess with a few things. # First specify python_versions. stdout_buffer = StringIO() with redirect_stdout(stdout_buffer): result = hashin.get_package_hashes( package='hashin', verbose=True, python_versions=('3.5', ), ) self.assertEqual(len(result['hashes']), 2) # instead of 3 # Specify an unrecognized python version self.assertRaises( hashin.PackageError, hashin.get_package_hashes, package='hashin', python_versions=('2.99999', ), ) # Look for a package without any releases self.assertRaises( hashin.PackageError, hashin.get_package_hashes, package='uggamugga', )
def test_get_package_hashes(self, murlopen): def mocked_get(url, **options): if url == "https://pypi.python.org/pypi/hashin/json": return _Response({ 'info': { 'version': '0.10', 'name': 'hashin', }, 'releases': { '0.10': [{ 'url': 'https://pypi.python.org/packages/2.7/p/hashin/hashin-0.10-py2-none-any.whl', }, { 'url': 'https://pypi.python.org/packages/3.3/p/hashin/hashin-0.10-py3-none-any.whl', }, { 'url': 'https://pypi.python.org/packages/source/p/hashin/hashin-0.10.tar.gz', }] } }) elif url == "https://pypi.python.org/packages/2.7/p/hashin/hashin-0.10-py2-none-any.whl": return _Response(b"Some py2 wheel content\n") elif url == "https://pypi.python.org/packages/3.3/p/hashin/hashin-0.10-py3-none-any.whl": return _Response(b"Some py3 wheel content\n") elif url == "https://pypi.python.org/packages/source/p/hashin/hashin-0.10.tar.gz": return _Response(b"Some tarball content\n") raise NotImplementedError(url) murlopen.side_effect = mocked_get result = hashin.get_package_hashes( package='hashin', version='0.10', algorithm='sha512', ) expected = { 'package': 'hashin', 'version': '0.10', 'hashes': [{ 'url': 'https://pypi.python.org/packages/2.7/p/hashin/hashin-0.10-py2-none-any.whl', 'hash': '45d1c5d2237a3b4f78b4198709fb2ecf1f781c8234ce3d94356f2100a36739433952c6c13b2843952f608949e6baa9f95055a314487cd8fb3f9d76522d8edb50' }, { 'url': 'https://pypi.python.org/packages/3.3/p/hashin/hashin-0.10-py3-none-any.whl', 'hash': '0d63bf4c115154781846ecf573049324f06b021a1d4b92da4fae2bf491da2b83a13096b14d73e73cefad36855f4fa936bac4b2357dabf05a2b1e7329ff1e5455' }, { 'url': 'https://pypi.python.org/packages/source/p/hashin/hashin-0.10.tar.gz', 'hash': 'c32e6d9fb09dc36ab9222c4606a1f43a2dcc183a8c64bdd9199421ef779072c174fa044b155babb12860cf000e36bc4d358694fa22420c997b1dd75b623d4daa' }] } self.assertEqual(result, expected)