def test___init__(self, super___init__): """ Assert correct behavior from the __init__() method. """ step = steps.PublishMetadataStep() super___init__.assert_called_once_with(constants.PUBLISH_STEP_METADATA) self.assertEqual(step.context, None) self.assertEqual(step.redirect_context, None) self.assertEqual(step.description, _('Publishing Python Metadata.'))
def test_process_main(self, _create_package_index, makedirs, mock_get_packages, mock_open): """ Assert all the correct calls from process_main(). """ step = steps.PublishMetadataStep() conduit = mock.MagicMock() mock_get_packages.return_value = _GET_PACKAGES_RETURN step.get_conduit = mock.MagicMock(return_value=conduit) step.parent = mock.MagicMock() step.parent.web_working_dir = '/some/path/' step.process_main() # Assert correct usage of various mocked items step.get_conduit.assert_called_once_with() mock_get_packages.assert_called_once_with(conduit.repo_id) makedirs.assert_called_once_with( os.path.join(step.parent.web_working_dir, 'simple')) mock_open.assert_called_once_with( os.path.join(step.parent.web_working_dir, 'simple', 'index.html'), 'w') # Assert that the two calls to _create_package_index for each package name are correct self.assertEqual(_create_package_index.call_count, 2) expected_packages_by_name = steps._get_packages(conduit) for call in _create_package_index.mock_calls: expected_packages = expected_packages_by_name[call[1][0]] self.assertEqual( call[1][1], os.path.join(step.parent.web_working_dir, 'simple')) self.assertEqual(call[1][2], expected_packages) del expected_packages_by_name[call[1][0]] self.assertEqual(expected_packages_by_name, {}) # Assert that the resulting HTML index is correct write = mock_open.return_value.__enter__.return_value index_html = write.mock_calls[0][1][0] html = ElementTree.fromstring(index_html) head = html.find('head') title = head.find('title') self.assertEqual(title.text, 'Simple Index') meta = head.find('meta') self.assertEqual(meta.get('name'), 'api-version') self.assertEqual(meta.get('value'), '2') body = html.find('body') # There should be four subelements, two anchors and two breaks self.assertEqual(len(body.findall('br')), 2) self.assertEqual(len(body.findall('a')), 2) anchors = body.findall('a') self.assertEqual(set([a.get('href') for a in anchors]), set(['nectar', 'pulp_python_plugins'])) self.assertEqual(set([a.text for a in anchors]), set(['nectar', 'pulp_python_plugins']))
def test__create_package_index(self, makedirs, mock_open): """ Assert all the correct calls from _create_package_index(). """ step = steps.PublishMetadataStep() name = 'test_package' simple_path = os.path.join('/', 'path', 'to', 'simple') packages = [ { 'version': '2.4.3', 'filename': 'test_package-2.4.3.tar.gz', 'checksum': 'sum', 'checksum_type': 'barlow' }, { 'version': '2.5.0', 'filename': 'test_package-2.5.0.tar.gz', 'checksum': 'different', 'checksum_type': 'barlow' }, ] step._create_package_index(name, simple_path, packages) # Assert the right files and directories are made makedirs.assert_called_once_with(os.path.join(simple_path, name)) mock_open.assert_called_once_with( os.path.join(simple_path, name, 'index.html'), 'w') # Assert that the resulting HTML index is correct write = mock_open.return_value.__enter__.return_value index_html = write.mock_calls[0][1][0] html = ElementTree.fromstring(index_html) head = html.find('head') title = head.find('title') self.assertEqual(title.text, 'Links for %s' % name) meta = head.find('meta') self.assertEqual(meta.get('name'), 'api-version') self.assertEqual(meta.get('value'), '2') body = html.find('body') # There should be four subelements, two anchors and two breaks self.assertEqual(len(body.findall('br')), 2) self.assertEqual(len(body.findall('a')), 2) anchors = body.findall('a') hrefs = [ os.path.join('..', '..', steps._get_package_path(name, p['filename'])) + '#%s=%s' % (p['checksum_type'], p['checksum']) for p in packages ] self.assertEqual(set([a.get('href') for a in anchors]), set(hrefs)) self.assertEqual(set([a.text for a in anchors]), set([p['filename'] for p in packages]))