def setUp(self, mock_fetch_key): """Re-use vars.""" mock_fetch_key.return_value = 'test_auth' self.mock_fetch_key = mock_fetch_key self.client = MailgunClient()
class TestMailgunClientBase(unittest.TestCase): """Class for testing MailgunClient functions.""" @mock.patch.object(MailgunManager, 'fetch_apikey') def setUp(self, mock_fetch_key): """Re-use vars.""" mock_fetch_key.return_value = 'test_auth' self.mock_fetch_key = mock_fetch_key self.client = MailgunClient() def test_name_assignment(self): self.assertEqual(self.client.name, "Mailgun") def test_management_url_assignment(self): self.assertEqual(self.client.management_url, MAILGUN_API) def test_auth_assignment(self): self.assertEqual(self.client.auth, ('api', 'test_auth')) def test_manager_assignment(self): assert isinstance(self.client._manager, MailgunManager) def test_manager_attributes(self): self.assertEqual(self.client._manager.resource_class, MailgunDomain) self.assertEqual(self.client._manager.response_key, 'domain') self.assertEqual(self.client._manager.plural_response_key, 'items') self.assertEqual(self.client._manager.uri_base, 'domains') @mock.patch.object(requests, 'get') def test_api_request_success(self, mock_requests): req = mock.Mock() req.json.return_value = {'testing': 'response'} req.status_code = 200 mock_requests.return_value = req expected = (req.status_code, req.json()) results = self.client._api_request('/blahblah', 'GET') self.assertEqual(expected, results) mock_requests.assert_called_with('https://api.mailgun.net/v2/blahblah', headers={'Accept': 'application/json'}, auth=('api', 'test_auth')) @mock.patch.object(requests, 'get') def test_api_request_exception(self, mock_requests): mock_requests.side_effect = requests.exceptions.RequestException('test') self.assertRaisesRegexp(exc.ClientException, 'Unknown error occurred ' 'in api request', self.client._api_request, '/blahblah', 'GET') @mock.patch.object(MailgunClient, 'method_post') def test_create_mailing_list(self, mock_post): return_json = { 'list': { 'access_level': 'readonly', 'address': '*****@*****.**', 'created_at': 'Fri, 27 Sep 2013 20:54:39 GMT', 'description': 'testing', 'members_count': 0, 'name': '' }, 'message': 'Mailing list has been created' } mock_post.return_value = (200, return_json) results = self.client.create_mailing_list('*****@*****.**', 'testing') self.assertEqual(results, (200, return_json)) mock_post.assert_called_with('/lists', data={'description': 'testing', 'address': '*****@*****.**'}) @mock.patch.object(MailgunClient, 'method_post') def test_add_list_member(self, mock_post): return_json = { "member": { "subscribed": True, "name": "", "vars": {}, "address": "*****@*****.**" }, "message": "Mailing list member has been created" } mock_post.return_value = (200, return_json) results = self.client.add_list_member('*****@*****.**', '*****@*****.**') self.assertEqual(results, (200, return_json)) mock_post.assert_called_with('/lists/[email protected]/members', data={'subscribed': True, 'address': '*****@*****.**'}) @mock.patch.object(MailgunClient, 'method_put') def test_update_list_member(self, mock_put): return_json = { "member": { "subscribed": False, "name": "", "vars": {}, "address": "*****@*****.**" }, "message": "Mailing list member has been updated" } mock_put.return_value = (200, return_json) results = self.client.update_list_member('*****@*****.**', '*****@*****.**', subscribed=False) self.assertEqual(results, (200, return_json)) mock_put.assert_called_with('/lists/[email protected]/members/[email protected]', data={'subscribed': False}) @mock.patch.object(MailgunClient, 'method_get') def test_list_members(self, mock_get): return_json = { "items": [ { "subscribed": True, "name": "", "vars": {}, "address": "*****@*****.**" } ], "total_count": 1 } mock_get.return_value = (200, return_json) results = self.client.list_members('*****@*****.**') self.assertEqual(results, (200, return_json)) mock_get.assert_called_with('/lists/[email protected]/members') @mock.patch.object(MailgunClient, 'method_get') def test_get_list_stats(self, mock_get): return_json = { "unique": { "clicked": { "recipient": 0, "link": 0 }, "opened": { "recipient": 0 } }, "total": { "complained": 0, "delivered": 0, "clicked": 0, "opened": 0, "unsubscribed": 0, "bounced": 0, "dropped": 0 } } mock_get.return_value = (200, return_json) results = self.client.get_list_stats('*****@*****.**') self.assertEqual(results, (200, return_json)) mock_get.assert_called_with('/lists/[email protected]/stats') @mock.patch.object(MailgunClient, 'method_delete') def test_delete_list_member(self, mock_delete): return_json = { "member": { "address": "*****@*****.**" }, "message": "Mailing list member has been deleted" } mock_delete.return_value = (200, return_json) results = self.client.delete_list_member('*****@*****.**', '*****@*****.**') self.assertEqual(results, (200, return_json)) mock_delete.assert_called_with('/lists/[email protected]/members/[email protected]') @mock.patch.object(MailgunClient, 'method_delete') def test_delete_mailing_list(self, mock_delete): return_json = { "message": "Mailing list has been deleted", "address": "*****@*****.**" } mock_delete.return_value = (200, return_json) results = self.client.delete_mailing_list('*****@*****.**') self.assertEqual(results, (200, return_json)) mock_delete.assert_called_with('/lists/[email protected]') @mock.patch.object(MailgunClient, 'method_post') def test_create_domain_success(self, mock_post): """Verifies method calls and results when creating domain.""" return_json = { "domain": { "created_at": "Wed, 27 Oct 2013 18:38:22 GMT", "smtp_login": "******", "name": "test.com", "smtp_password": "******" }, "message": "Domain has been created" } mock_post.return_value = (200, return_json) domain = self.client.create('test.com', 'supasecret') self.assertEqual('test.com', domain.name) self.assertEqual('supasecret', domain.smtp_password) mock_post.assert_called_with('/domains', data={'name': 'test.com', 'smtp_password': '******'}) @mock.patch.object(MailgunClient, 'method_post') def test_create_domain_400(self, mock_post): mock_post.return_value = (400, {'message': 'testing'}) self.assertRaisesRegexp(exc.DomainRecordNotUnique, "testing", self.client.create, 'test.com', 'blah') @mock.patch.object(MailgunClient, 'method_post') def test_create_domain_500(self, mock_post): mock_post.return_value = (500, {'message': 'Unknown Error'}) self.assertRaisesRegexp(exc.DomainCreationFailed, "Unknown Error", self.client.create, 'test.com', 'blah') @mock.patch.object(MailgunClient, 'method_get') def test_get_domain(self, mock_get): return_json = { "domain": { "created_at": "Wed, 27 Oct 2013 18:38:22 GMT", "smtp_login": "******", "name": "test.com", "smtp_password": "******" }, "receiving_dns_records": [ {"record_type" : "MX", "priority": 10, "value": "mxa.mailgun.org"}, {"record_type" : "MX", "priority": 10, "value": "mxb.mailgun.org"} ], "sending_dns_records": [ {"record_type" : "TXT", "name": "samples.mailgun.org", "value": 1}, {"record_type" : "CNAME", "name": "email.samples.mailgun.org", "value": 1} ] } mock_get.return_value = (200, return_json) domain = self.client.get('test.com') self.assertEqual(domain.receiving_dns_records, return_json['receiving_dns_records']) self.assertEqual(domain.sending_dns_records, return_json['sending_dns_records']) @mock.patch.object(MailgunClient, 'method_delete') def test_delete_domain(self, mock_delete): mock_delete.return_value = (500, {'message': 'Failed Delete'}) self.assertRaisesRegexp(exc.DomainDeletionFailed, 'Failed Delete', self.client.delete, 'test.com')