Skip to content

Commit 3281234

Browse files
author
Jon Wayne Parrott
committed
Adding sendgrid tests, fixes #182
Change-Id: I24c59c7c240fb93e3bfb73f21c1f61df830cddc9
1 parent c5c6c95 commit 3281234

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

managed_vms/sendgrid/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def send_email():
3838
to = request.form.get('to')
3939
if not to:
4040
return ('Please provide an email address in the "to" query string '
41-
'parameter.')
41+
'parameter.'), 400
4242

4343
sg = sendgrid.SendGridClient(SENDGRID_API_KEY)
4444

@@ -52,7 +52,7 @@ def send_email():
5252
status, response = sg.send(message)
5353

5454
if status != 200:
55-
return 'An error occurred: {}'.format(response)
55+
return 'An error occurred: {}'.format(response), 500
5656

5757
return 'Email sent.'
5858
# [END example]

managed_vms/sendgrid/main_test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2016 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 main
16+
17+
import mock
18+
import pytest
19+
20+
21+
@pytest.fixture
22+
def app(monkeypatch):
23+
monkeypatch.setenv('SENDGRID_API_KEY', 'apikey')
24+
monkeypatch.setenv('SENDGRID_SENDER', '[email protected]')
25+
26+
import main
27+
28+
main.app.testing = True
29+
return main.app.test_client()
30+
31+
32+
def test_get(app):
33+
r = app.get('/')
34+
assert r.status_code == 200
35+
36+
37+
@mock.patch.object(
38+
main.sendgrid.SendGridClient, 'send', return_value=(200, "OK"))
39+
def test_post(send_mock, app):
40+
r = app.post('/send/email', data={
41+
42+
})
43+
assert r.status_code == 200
44+
assert send_mock.called

0 commit comments

Comments
 (0)