|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +import pytest |
| 18 | +import requests |
| 19 | +import responses |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def app(monkeypatch): |
| 24 | + monkeypatch.setenv('MAILGUN_DOMAIN_NAME', 'example.com') |
| 25 | + monkeypatch.setenv('MAILGUN_API_KEY', 'apikey') |
| 26 | + |
| 27 | + import main |
| 28 | + |
| 29 | + main.app.testing = True |
| 30 | + return main.app.test_client() |
| 31 | + |
| 32 | + |
| 33 | +def test_index(app): |
| 34 | + r = app.get('/') |
| 35 | + assert r.status_code == 200 |
| 36 | + |
| 37 | + |
| 38 | +@responses.activate |
| 39 | +def test_send_error(app): |
| 40 | + responses.add( |
| 41 | + responses.POST, |
| 42 | + 'https://api.mailgun.net/v3/example.com/messages', |
| 43 | + body='Test error', |
| 44 | + status=500) |
| 45 | + |
| 46 | + with pytest.raises(requests.exceptions.HTTPError): |
| 47 | + app.post('/send/email', data={ |
| 48 | + |
| 49 | + 'submit': 'Send simple email'}) |
| 50 | + |
| 51 | + |
| 52 | +@responses.activate |
| 53 | +def test_send_simple(app): |
| 54 | + responses.add( |
| 55 | + responses.POST, |
| 56 | + 'https://api.mailgun.net/v3/example.com/messages', |
| 57 | + body='') |
| 58 | + |
| 59 | + response = app.post('/send/email', data={ |
| 60 | + |
| 61 | + 'submit': 'Send simple email'}) |
| 62 | + assert response.status_code == 200 |
| 63 | + assert len(responses.calls) == 1 |
| 64 | + |
| 65 | + |
| 66 | +@responses.activate |
| 67 | +def test_send_complex(app, monkeypatch): |
| 68 | + import main |
| 69 | + monkeypatch.chdir(os.path.dirname(main.__file__)) |
| 70 | + |
| 71 | + responses.add( |
| 72 | + responses.POST, |
| 73 | + 'https://api.mailgun.net/v3/example.com/messages', |
| 74 | + body='') |
| 75 | + |
| 76 | + response = app.post('/send/email', data={ |
| 77 | + |
| 78 | + 'submit': 'Send complex email'}) |
| 79 | + assert response.status_code == 200 |
| 80 | + assert len(responses.calls) == 1 |
0 commit comments