def test_get_url(self): """ Assert correct behavior from the get_url() method. """ bz = bugs.Bugzilla() patch_config = {'bz_baseurl': 'https://example.com/bz'} with mock.patch.dict('bodhi.server.bugs.config', patch_config): self.assertEqual(bz.get_url('42'), 'https://example.com/bz/show_bug.cgi?id=42')
def test_update_details_exception(self, mock_exceptionlog): """Test we log an exception if update_details raises one""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.side_effect = Exception() bz.update_details(0, 0) mock_exceptionlog.assert_called_once_with('Unknown exception from Bugzilla')
def test__connect_without_creds(self, __init__): """Test the _connect() method when the config does not contain credentials.""" bz = bugs.Bugzilla() patch_config = {'bz_server': 'https://example.com/bz'} with mock.patch.dict('bodhi.server.bugs.config', patch_config): bz._connect() __init__.assert_called_once_with(url='https://example.com/bz', cookiefile=None, tokenfile=None)
def test__connect_with_api_key(self, __init__): """Test the _connect() method when the config contains an api_key.""" bz = bugs.Bugzilla() patch_config = {'bz_server': 'https://example.com/bz', 'bugzilla_api_key': 'api_key'} with mock.patch.dict('bodhi.server.bugs.config', patch_config): bz._connect() __init__.assert_called_once_with(url='https://example.com/bz', api_key='api_key', cookiefile=None, tokenfile=None)
def test_comment_successful(self, exception): """Test the comment() method with a success case.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz.comment(1411188, 'A nice message.') bz._bz.getbug.assert_called_once_with(1411188) bz._bz.getbug.return_value.addcomment.assert_called_once_with('A nice message.') # No exceptions should have been logged self.assertEqual(exception.call_count, 0)
def test_update_details_keywords_str(self): """Assert that we split the keywords into a list when they are a str.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bug = mock.MagicMock() bug.keywords = 'some words but sEcuriTy is in the middle of them' bug_entity = models.Bug() bz.update_details(bug, bug_entity) self.assertTrue(bug_entity.security)
def test_bz_with__bz_set(self, _connect): """ Assert correct behavior of the bz() method when _bz is already set. """ bz = bugs.Bugzilla() bz._bz = mock.MagicMock() return_value = bz.bz self.assertTrue(return_value is bz._bz) self.assertEqual(_connect.call_count, 0)
def test_getbug(self): """ Assert correct behavior on the getbug() method. """ bz = bugs.Bugzilla() bz._bz = mock.MagicMock() return_value = bz.getbug(1411188) self.assertTrue(return_value is bz._bz.getbug.return_value) bz._bz.getbug.assert_called_once_with(1411188)
def test_close_fault(self, exception): """Assert that an xmlrpc Fault is caught and logged by close().""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.close.side_effect = xmlrpc_client.Fault( 410, 'You must log in before using this part of Red Hat Bugzilla.') # This should not raise an Exception. bz.close(12345, {'bodhi': ['bodhi-3.1.0-1.fc27']}, 'whabam!') exception.assert_called_once_with('Unable to close bug #12345')
def test_on_qa_product_skipped(self, info): """Test the on_qa() method when the bug's product is not in the bz_products config.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.product = 'not fedora!' bz.on_qa(1411188, 'A message.') bz._bz.getbug.assert_called_once_with(1411188) info.assert_called_once_with("Skipping set on_qa on 'not fedora!' bug #1411188") self.assertEqual(bz._bz.getbug.return_value.setstatus.call_count, 0)
def test_modified_product_skipped(self, info): """Test the modified() method when the bug's product is not in the bz_products config.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.product = 'not fedora!' bz.modified(1411188) bz._bz.getbug.assert_called_once_with(1411188) info.assert_called_once_with("Skipping 'not fedora!' bug") self.assertEqual(bz._bz.getbug.return_value.setstatus.call_count, 0)
def test_modified_exception(self, exception_log): """Test the modified() method logs an exception if encountered""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.side_effect = Exception() bz.modified(1411188) bz._bz.getbug.assert_called_once_with(1411188) exception_log.assert_called_once_with("Unable to alter bug #1411188") self.assertEqual(bz._bz.getbug.return_value.setstatus.call_count, 0)
def test_modified_after_verified(self): """Test the modified() method when the status of bug is VERIFIED.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.product = 'aproduct' bz._bz.getbug.return_value.bug_status = 'VERIFIED' bz.modified(1411188, 'A mean message.') bz._bz.getbug.assert_called_once_with(1411188) bz._bz.getbug.return_value.addcomment.assert_called_once_with('A mean message.')
def test_on_qa_fault(self, exception): """on_qa() should gracefully handle Bugzilla faults.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.side_effect = xmlrpc.client.Fault(42, 'The meaning') bz.on_qa(1563797, 'Bodhi has fixed all of your bugs.') bz._bz.getbug.assert_called_once_with(1563797) exception.assert_called_once_with( 'Got fault from Bugzilla on #%d: fault code: %d, fault string: %s', 1563797, 42, 'The meaning')
def test_close_product_skipped(self, info): """Test the close() method when the bug's product is not in the bz_products config.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.product = 'not fedora!' bz.close(12345, {'bodhi': 'bodhi-35.103.109-1.fc27'}, 'Fixed. Closing bug and adding version to fixed_in field.') bz._bz.getbug.assert_called_once_with(12345) info.assert_called_once_with("Skipping set closed on 'not fedora!' bug #12345") self.assertEqual(bz._bz.getbug.return_value.setstatus.call_count, 0)
def test_modified(self, info): """Test the modified() method""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.product = 'aproduct' bz._bz.getbug.return_value.bug_status = 'NEW' bz.modified(1411188) bz._bz.getbug.assert_called_once_with(1411188) info.assert_called_once_with("Setting bug #1411188 status to MODIFIED") self.assertEqual(bz._bz.getbug.return_value.setstatus.call_count, 1)
def test__connect_with_creds_and_api_key(self, __init__): """Test the _connect() method when the config contains credentials and an api_key.""" bz = bugs.Bugzilla() patch_config = {'bodhi_email': '*****@*****.**', 'bodhi_password': '******', 'bz_server': 'https://example.com/bz', 'bugzilla_api_key': 'api_key'} with mock.patch.dict('bodhi.server.bugs.config', patch_config): bz._connect() # Using an API key should cause the credentials to be ignored. __init__.assert_called_once_with(url='https://example.com/bz', api_key='api_key', cookiefile=None, tokenfile=None)
def test_comment_unexpected_exception(self, exception): """Test the comment() method with an unexpected Exception.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.addcomment.side_effect = Exception( 'Ran out of internet fluid, please refill.') bz.comment(1411188, 'A nice message.') bz._bz.getbug.assert_called_once_with(1411188) bz._bz.getbug.return_value.addcomment.assert_called_once_with('A nice message.') exception.assert_called_once_with('Unable to add comment to bug #1411188')
def test_update_details_parent_bug(self): """Assert that a parent bug gets marked as such.""" bz = bugs.Bugzilla() bug = mock.MagicMock() bug.product = 'Security Response' bug.short_desc = 'Fedora gets you, good job guys!' bug_entity = mock.MagicMock() bug_entity.bug_id = 1419157 bz.update_details(bug, bug_entity) self.assertIs(bug_entity.parent, True) self.assertEqual(bug_entity.title, 'Fedora gets you, good job guys!')
def test_on_qa_success(self, exception): """ Test the on_qa() method with a success case. """ bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz.on_qa(1411188, 'A message.') bz._bz.getbug.assert_called_once_with(1411188) bz._bz.getbug.return_value.setstatus.assert_called_once_with( 'ON_QA', comment='A message.') self.assertEqual(exception.call_count, 0)
def test_update_details_xmlrpc_fault(self, exception): """Test we log an exception if update_details raises one""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.side_effect = xmlrpc_client.Fault(42, 'You found the meaning.') bug = mock.MagicMock() bug.bug_id = 123 bz.update_details(0, bug) self.assertEqual(bug.title, 'Invalid bug number') bz._bz.getbug.assert_called_once_with(123) exception.assert_called_once_with('Got fault from Bugzilla')
def test_comment_too_long(self, exception): """Assert that the comment() method gets angry if the comment is too long.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() oh_my = u'All work aind no play makes bowlofeggs a dull… something something… ' long_comment = oh_my * (65535 // len(oh_my) + 1) bz.comment(1411188, long_comment) self.assertEqual(bz._bz.getbug.call_count, 0) # An exception should have been logged exception.assert_called_once_with( u'Comment too long for bug #1411188: {}'.format(long_comment))
def test_bz_with__bz_None(self, __init__): """ Assert correct behavior of the bz() method when _bz is None. """ bz = bugs.Bugzilla() patch_config = {'bz_server': 'https://example.com/bz'} with mock.patch.dict('bodhi.server.bugs.config', patch_config): return_value = bz.bz __init__.assert_called_once_with(url='https://example.com/bz', cookiefile=None, tokenfile=None) self.assertTrue(return_value is bz._bz)
def test_modified(self, info): """Ensure correct execution of the modified() method.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.product = 'aproduct' bz._bz.getbug.return_value.bug_status = 'NEW' bz.modified(1411188, 'A mean message.') bz._bz.getbug.assert_called_once_with(1411188) info.assert_called_once_with("Setting bug #1411188 status to MODIFIED") bz._bz.getbug.return_value.setstatus.assert_called_once_with( 'MODIFIED', comment='A mean message.')
def test_on_qa_success(self, info): """ Test the on_qa() method with a success case. """ bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.product = 'aproduct' bz.on_qa(1411188, 'A message.') bz._bz.getbug.assert_called_once_with(1411188) bz._bz.getbug.return_value.setstatus.assert_called_once_with( 'ON_QA', comment='A message.') assert info.call_count == 0
def test_on_qa_private_bug(self, info): """on_qa() should gracefully handle private bugs.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.side_effect = xmlrpc.client.Fault( 102, ('You are not authorized to access bug #1563797. To see this bug, you must first log in' 'to an account with the appropriate permissions.')) bz.on_qa(1563797, 'Bodhi has fixed all of your bugs.') bz._bz.getbug.assert_called_once_with(1563797) info.assert_called_once_with('Cannot retrieve private bug #%d.', 1563797)
def test_update_details_xmlrpc_fault(self, error): """Test we log an error if update_details raises one""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.side_effect = xmlrpc.client.Fault(42, 'You found the meaning.') bug = mock.MagicMock() bug.bug_id = 123 bz.update_details(0, bug) self.assertEqual(bug.title, 'Invalid bug number') bz._bz.getbug.assert_called_once_with(123) error.assert_called_once_with( 'Got fault from Bugzilla on #%d: fault code: %d, fault string: %s', 123, 42, 'You found the meaning.', exc_info=True)
def test_close_private_bug(self, info): """close() should gracefully handle private bugs.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.side_effect = xmlrpc.client.Fault( 102, ('You are not authorized to access bug #1563797. To see this bug, you must first log in' 'to an account with the appropriate permissions.')) bz.close(1563797, {'bodhi': 'bodhi-35.103.109-1.fc27'}, 'Fixed. Closing bug and adding version to fixed_in field.') bz._bz.getbug.assert_called_once_with(1563797) info.assert_called_once_with('Cannot retrieve private bug #%d.', 1563797)
def test_close_successful(self, exception): """Test the close() method with a success case.""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.component = 'bodhi' bz.close(12345, {'bodhi': 'bodhi-3.1.0-1.fc27'}, 'Fixed. Closing bug and adding version to fixed_in field.') bz._bz.getbug.assert_called_once_with(12345) bz._bz.getbug.return_value.close.assert_called_once_with( 'ERRATA', comment='Fixed. Closing bug and adding version to fixed_in field.', fixedin='bodhi-3.1.0-1.fc27') self.assertEqual(exception.call_count, 0)
def test_close_fault(self, error): """Assert that an xmlrpc Fault is caught and logged by close().""" bz = bugs.Bugzilla() bz._bz = mock.MagicMock() bz._bz.getbug.return_value.product = 'aproduct' bz._bz.getbug.return_value.close.side_effect = xmlrpc.client.Fault( 410, 'You must log in before using this part of Red Hat Bugzilla.') # This should not raise an Exception. bz.close(12345, {'bodhi': 'bodhi-3.1.0-1.fc27'}, 'whabam!') error.assert_called_once_with( 'Got fault from Bugzilla on #%d: fault code: %d, fault string: %s', 12345, 410, 'You must log in before using this part of Red Hat Bugzilla.', exc_info=True)