From f0b919c231bc955ecc2dbfcb8683c1b32790e46f Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Wed, 18 May 2016 13:47:12 -0700 Subject: [PATCH 1/3] Adding mailgun tests. Fixes #181 Change-Id: I3a2e34668fca8084e0eafc7502bc07fe3fabefe4 --- managed_vms/mailgun/main_test.py | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 managed_vms/mailgun/main_test.py diff --git a/managed_vms/mailgun/main_test.py b/managed_vms/mailgun/main_test.py new file mode 100644 index 00000000000..0a4dff8a6b9 --- /dev/null +++ b/managed_vms/mailgun/main_test.py @@ -0,0 +1,78 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import re + +import pytest +import responses + + +@pytest.fixture +def app(monkeypatch): + monkeypatch.setenv('MAILGUN_DOMAIN_NAME', 'example.com') + monkeypatch.setenv('MAILGUN_API_KEY', 'apikey') + + import main + + main.app.testing = True + return main.app.test_client() + + +def test_index(app): + r = app.get('/') + assert r.status_code == 200 + + +@responses.activate +def test_send_error(app): + responses.add( + responses.POST, + re.compile(r'.*'), + body='Test error', + status=500) + + with pytest.raises(Exception): + app.post('/send/email', data={ + 'recipient': 'user@example.com', + 'submit': 'Send simple email'}) + + +@responses.activate +def test_send_simple(app): + responses.add( + responses.POST, + re.compile(r'.*'), + body='') + + response = app.post('/send/email', data={ + 'recipient': 'user@example.com', + 'submit': 'Send simple email'}) + assert response.status_code == 200 + + +@responses.activate +def test_send_complex(app, monkeypatch): + import main + monkeypatch.chdir(os.path.dirname(main.__file__)) + + responses.add( + responses.POST, + re.compile(r'.*'), + body='') + + response = app.post('/send/email', data={ + 'recipient': 'user@example.com', + 'submit': 'Send complex email'}) + assert response.status_code == 200 From 37133baeb3c714cc6435bedb8b7eb2fcb186c1ea Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Wed, 18 May 2016 14:01:55 -0700 Subject: [PATCH 2/3] Addressing review comments Change-Id: I8817ad375c8bbd1e2a1fe039941b97a440423436 --- managed_vms/mailgun/main_test.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/managed_vms/mailgun/main_test.py b/managed_vms/mailgun/main_test.py index 0a4dff8a6b9..cdb3f3ba2d7 100644 --- a/managed_vms/mailgun/main_test.py +++ b/managed_vms/mailgun/main_test.py @@ -17,6 +17,7 @@ import pytest import responses +import requests @pytest.fixture @@ -39,11 +40,11 @@ def test_index(app): def test_send_error(app): responses.add( responses.POST, - re.compile(r'.*'), + 'https://api.mailgun.net/v3/example.com/messages', body='Test error', status=500) - with pytest.raises(Exception): + with pytest.raises(requests.exceptions.HTTPError): app.post('/send/email', data={ 'recipient': 'user@example.com', 'submit': 'Send simple email'}) @@ -53,13 +54,14 @@ def test_send_error(app): def test_send_simple(app): responses.add( responses.POST, - re.compile(r'.*'), + 'https://api.mailgun.net/v3/example.com/messages', body='') response = app.post('/send/email', data={ 'recipient': 'user@example.com', 'submit': 'Send simple email'}) assert response.status_code == 200 + assert len(responses.calls) == 1 @responses.activate @@ -69,10 +71,11 @@ def test_send_complex(app, monkeypatch): responses.add( responses.POST, - re.compile(r'.*'), + 'https://api.mailgun.net/v3/example.com/messages', body='') response = app.post('/send/email', data={ 'recipient': 'user@example.com', 'submit': 'Send complex email'}) assert response.status_code == 200 + assert len(responses.calls) == 1 From c47b98890439ad317fe52b49cca6295d064440c3 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Wed, 18 May 2016 14:07:32 -0700 Subject: [PATCH 3/3] Fixing travis Change-Id: I58c03b71e61a486587f1c8c3a6ea3db54f6015d3 --- managed_vms/mailgun/main_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/managed_vms/mailgun/main_test.py b/managed_vms/mailgun/main_test.py index cdb3f3ba2d7..b92bda25913 100644 --- a/managed_vms/mailgun/main_test.py +++ b/managed_vms/mailgun/main_test.py @@ -13,11 +13,10 @@ # limitations under the License. import os -import re import pytest -import responses import requests +import responses @pytest.fixture