def test_env_list_url(self): with patch.dict('os.environ', {'MGSUB_DEFAULT_MAILINGLIST': '*****@*****.**'}, clear=True): mailinglist = MailgunList() url = mailinglist.get_list_url() exp = 'https://api.mailgun.net/v2/lists/[email protected]/members' self.assertEquals(url, exp)
def test_subscribe_duplicate(self): resp = \ '''{ "message": "Address already exists" }''' url = 'https://api.mailgun.net/v2/lists/[email protected]/members' responses.add(responses.POST, url, resp, content_type='application/json', status=400, adding_headers={'content-length': str(len(resp))}) mailinglist = MailgunList() result = mailinglist.subscribe('*****@*****.**', 'Bob Bar') self.assertEquals(result['member']['address'], '*****@*****.**')
def test_subscribe_400(self): resp = \ '''{ "message": "Bad request" }''' url = 'https://api.mailgun.net/v2/lists/[email protected]/members' responses.add(responses.POST, url, resp, content_type='application/json', status=400, adding_headers={'content-length': str(len(resp))}) mailinglist = MailgunList() with self.assertRaises(MailgunError) as cm: mailinglist.subscribe('*****@*****.**', 'Bob Bar') self.assertEqual(400, cm.exception.code) self.assertEqual('Bad request', cm.exception.json['message'])
def test_unsubscribe_success(self): resp = \ '''{ "member": { "address": "*****@*****.**" }, "message": "Mailing list member has been deleted" }''' url = 'https://api.mailgun.net/v2/lists/[email protected]/members/[email protected]' responses.add(responses.DELETE, url, resp, content_type='application/json') mailinglist = MailgunList() result = mailinglist.unsubscribe('*****@*****.**') self.assertEquals(result['member']['address'], '*****@*****.**')
class SignupForm(forms.Form): email = forms.EmailField() def __init__(self, list_email=None, *args, **kwargs): self.mailinglist = MailgunList(list_email) super(SignupForm, self).__init__(*args, **kwargs) def is_valid(self): if not super(SignupForm, self).is_valid(): return False if not self.subscribe(): self.add_error(None, 'There was a failure adding you to the mailing list') return False if getattr(settings, 'MGSUB_SEND_WELCOME', True): return self.send_welcome() return True def subscribe(self): try: return self.mailinglist.subscribe(self.cleaned_data['email']) except Exception, e: logger.error(e) return False
class SignupForm(forms.Form): email = forms.EmailField() def __init__(self, list_email=None, *args, **kwargs): self.mailinglist = MailgunList(list_email) super(SignupForm, self).__init__(*args, **kwargs) def is_valid(self): if not super(SignupForm, self).is_valid(): return False if not self.subscribe(): self.add_error( None, 'There was a failure adding you to the mailing list') return False if getattr(settings, 'MGSUB_SEND_WELCOME', True): return self.send_welcome() return True def subscribe(self): try: return self.mailinglist.subscribe(self.cleaned_data['email']) except Exception, e: logger.error(e) return False
def test_subscribe_success(self): resp = \ '''{ "member": { "vars": { "age": 26 }, "name": "Bob Bar", "subscribed": true, "address": "*****@*****.**" }, "message": "Mailing list member has been created" }''' url = 'https://api.mailgun.net/v2/lists/[email protected]/members' responses.add(responses.POST, url, resp, content_type='application/json') mailinglist = MailgunList() result = mailinglist.subscribe('*****@*****.**', 'Bob Bar') self.assertEquals(result['member']['address'], '*****@*****.**')
def test_list_url(self): mailinglist = MailgunList() url = mailinglist.get_list_url() exp = 'https://api.mailgun.net/v2/lists/[email protected]/members' self.assertEquals(url, exp)
def __init__(self, list_email=None, *args, **kwargs): self.mailinglist = MailgunList(list_email) super(SignupForm, self).__init__(*args, **kwargs)
def test_api_environ(self): with patch.dict('os.environ', {'MAILGUN_API_KEY': 'env'}): mailinglist = MailgunList() self.assertEqual(mailinglist.api_key, 'env')
def test_api_key(self): with patch.dict('os.environ', {}, clear=True): mailinglist = MailgunList() self.assertIsNone(mailinglist.api_key)
def test_api_settings(self): mailinglist = MailgunList() self.assertEqual(mailinglist.api_key, 'settings')
def test_member_url(self): mailinglist = MailgunList() url = mailinglist.get_member_url('*****@*****.**') self.assertIn('[email protected]/members/[email protected]', url)