From c5ce12c21f4633abc60e78542f20e26366a3ac1e Mon Sep 17 00:00:00 2001 From: Richard Mathie Date: Wed, 1 Jun 2016 11:04:06 +0100 Subject: [PATCH 1/4] listing routes --- flask_mailgun.py | 36 +++++++++++++++++++++++------------- tests/test_flask_mailgun.py | 12 +++++++++++- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/flask_mailgun.py b/flask_mailgun.py index 8497d71..b67e47b 100644 --- a/flask_mailgun.py +++ b/flask_mailgun.py @@ -11,6 +11,7 @@ import hmac import os import json +from collections import defaultdict from decorator import decorator from threading import Thread from werkzeug.utils import secure_filename @@ -190,19 +191,19 @@ def send_email(self, **kwargs): return responce def list_routes(self): - request = requests.get(os.path.join([self.api_url, 'routes']), + request = requests.get(self.routepoint, auth=self.auth) return json.loads(request.text).get('items') def route_exists(self, route): routes = self.list_routes() - if routes: - expressions = [r['expression'] for r in routes] - actions = [r['actions'] for r in routes] - return route['expression'] in expressions and route['action'] in actions - else: - return False + expression_action = defaultdict(list) + for r in routes: + expression_action[r['expression']].extend(r['actions']) + + current_actions = expression_action[route['expression']] + return set(route['action']) <= set(current_actions) def create_route(self, dest='/messages/', data=None,): self.dest = dest @@ -213,12 +214,14 @@ def create_route(self, dest='/messages/', data=None,): "expression": "match_recipient('%(route)s@%(domain)s')" % self.__dict__, "action": [action, "stop()"]} - # Create Route Only if it does not Exist # TODO should update? - if self.route_exist(route): + # Create Route Only if it does not Exist + # TODO should not it update? + if self.route_exists(route): return None else: - return requests.post(self.api_url + 'routes', auth=self.auth, - data=data) + return requests.post(self.routepoint, + auth=self.auth, + data=data) def verify_email(self, email): """Check that the email post came from mailgun @@ -241,10 +244,17 @@ def verify_email(self, email): if signature != signature_calc: raise MailGunException("Mailbox Error: credential verification failed.", "Signature doesn't match") + def api_route(self, *pieces): + pieces = [self.api_url] + [p for p in pieces] + return '/'.join(s.strip('/') for s in pieces) + @property def sendpoint(self): - url_pieces = [self.api_url, self.domain, '/messages'] - return '/'.join(s.strip('/') for s in url_pieces) + return self.api_route(self.domain, 'messages') + + @property + def routepoint(self): + return self.api_route('routes') @property def auth(self): diff --git a/tests/test_flask_mailgun.py b/tests/test_flask_mailgun.py index 18e8d4b..fe7e5eb 100644 --- a/tests/test_flask_mailgun.py +++ b/tests/test_flask_mailgun.py @@ -27,13 +27,23 @@ def setUp(self): self.mailgun = flask_mailgun.MailGun() self.mailgun.init_app(app) self.post_patcher = patch('flask_mailgun.requests.post') - self.mailgun.routes = MagicMock(return_value=None) + self.mailgun.mailgun_api.list_routes = MagicMock(return_value=[]) self.mock_post = self.post_patcher.start() def tearDown(self): self.post_patcher.stop() +class MailGunApiTest(MailgunTestBase): + def test_sendpoint(self): + self.assertEqual(self.mailgun.mailgun_api.sendpoint, + 'https://api.mailgun.net/v3/example.com/messages') + + def test_routpoint(self): + self.assertEqual(self.mailgun.mailgun_api.routepoint, + 'https://api.mailgun.net/v3/routes') + + class SendMessageTest(MailgunTestBase): def test_send_simple_message(self): message = {"from": "from@example.com", From dea1d255505752a4a03e00a5007610c543bf7a46 Mon Sep 17 00:00:00 2001 From: Richard Mathie Date: Wed, 1 Jun 2016 11:08:53 +0100 Subject: [PATCH 2/4] async is shakey --- tests/test_flask_mailgun.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_flask_mailgun.py b/tests/test_flask_mailgun.py index fe7e5eb..0a85c7c 100644 --- a/tests/test_flask_mailgun.py +++ b/tests/test_flask_mailgun.py @@ -12,7 +12,7 @@ import flask_mailgun from tests import config from tests.fixtures.email import make_email_request, make_email, sign_email - +import time def get_app(name): app = Flask(name) @@ -143,6 +143,7 @@ def test_receive_2_messages(self): self.assertEqual(response.status_code, 200) response = self.appclient.post('/upload', data=self.email2) self.assertEqual(response.status_code, 200) + time.sleep(1) self.assertEqual(self.receve_email_mock.call_count, 2) self.assertEqual(self.attachment_mock.call_count, 2) print "reveved 2 emails" From 14ef7526b2073f35e6e124b61dcf62f0e62b4848 Mon Sep 17 00:00:00 2001 From: Richard Mathie Date: Wed, 1 Jun 2016 11:13:29 +0100 Subject: [PATCH 3/4] async is shakey --- tests/test_flask_mailgun.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_flask_mailgun.py b/tests/test_flask_mailgun.py index 0a85c7c..d57a38f 100644 --- a/tests/test_flask_mailgun.py +++ b/tests/test_flask_mailgun.py @@ -125,6 +125,7 @@ class ReceiveMessageSyncTest(ReceiveMessageCallbacksTest): def test_receive_message(self): response = self.appclient.post('/upload', data=self.email) self.assertEqual(response.status_code, 200) + time.sleep(1) self.assertEqual(self.receve_email_mock.call_count, 1) self.assertEqual(self.attachment_mock.call_count, 1) print "reveved email" From f75a416bea652af86f5e48ecfa2e6d929d889a95 Mon Sep 17 00:00:00 2001 From: yunxi Date: Wed, 1 Jun 2016 13:28:08 +0100 Subject: [PATCH 4/4] master mergered with feature routes --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 57dc0a8..ff43095 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,7 @@ venv_test/ # Spyder IDE .spyderproject + +# Pylint +pylint.out +