-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_mailer.py
More file actions
199 lines (172 loc) · 6.29 KB
/
test_mailer.py
File metadata and controls
199 lines (172 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import json
import aiohttp
import aiosmtplib
import aiosmtplib.response
import pytest
from aiohttp.helpers import sentinel
from aiohttp import streams
from aiohttp.test_utils import make_mocked_request
import mailer
diff = """\
diff --git a/.gitignore b/.gitignore
index c2b4fc703f7c..e0d0685fa7da 100644
--- a/.gitignore
+++ b/.gitignore
@@ -93,3 +93,4 @@ htmlcov/
Tools/msi/obj
Tools/ssl/amd64
Tools/ssl/win32
+foo
"""
data_with_commits = {
"ref": "refs/heads/master",
"commits": [
{
"id": "2d420b342509e6c2b597af82ea74c4cbb13e2abd",
"message": "Update .gitignore\n(cherry picked from commit 9d9ed0e5cceef45fd63dc1f7b3fe6e695da16e83)",
"timestamp": "2017-02-08T15:37:50+03:00",
"url": "https://github.com/fayton/cpython/commit/2d420b342509e6c2b597af82ea74c4cbb13e2abd",
"author": {
"name": "cbiggles",
"email": "berker.peksag+cbiggles@gmail.com",
"username": "cbiggles",
},
"committer": {
"name": "Berker Peksag",
"email": "berker.peksag@gmail.com",
"username": "berkerpeksag",
},
"added": [],
"removed": [],
"modified": [".gitignore"],
},
],
}
data_with_pusher = {
**data_with_commits,
"pusher": {
"name": "Berker Peksag",
"email": "berker.peksag@gmail.com",
"username": "berkerpeksag",
},
}
class FakeSMTP(aiosmtplib.SMTP):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sent_mails = []
async def ehlo(self):
return aiosmtplib.response.SMTPResponse(250, "EHLO")
async def login(self, username, password):
return aiosmtplib.response.SMTPResponse(235, "AUTH")
async def connect(self, *args, **kwargs):
return aiosmtplib.response.SMTPResponse(100, "ok")
async def send_message(
self,
message,
sender=None,
recipients=None,
mail_options=None,
rcpt_options=None,
timeout=None,
):
self.sent_mails.append(message)
return {}, "Ok"
def make_request(method, path, *, loop=None, headers=sentinel, data=None):
if headers is sentinel:
headers = {"Content-Type": "application/json"}
elif "Content-Type" not in headers:
headers["Content-Type"] = "application/json"
if data is not None:
data = json.dumps(data).encode()
payload = streams.StreamReader(loop=loop)
payload.feed_data(data)
payload.feed_eof()
headers.update({"Content-Length": str(len(data))})
else:
payload = sentinel
return make_mocked_request(method, path, headers=headers, payload=payload)
async def test_wrong_content_type(loop):
smtp = FakeSMTP(hostname="localhost", port=1025, loop=loop)
client = aiohttp.ClientSession(loop=loop)
request = make_request(
"POST", "/", headers={"content-type": "application/octet-stream"}
)
event = mailer.PushEvent(client, smtp, request)
with pytest.raises(mailer.ResponseExit) as exc:
await event.process()
assert (
str(exc.value)
== "can only accept application/json, not application/octet-stream"
)
async def test_empty_commits(loop):
data = data_with_commits.copy()
del data["commits"]
smtp = FakeSMTP(hostname="localhost", port=1025, loop=loop)
client = aiohttp.ClientSession(loop=loop)
request = make_request("POST", "/", data=data, loop=loop)
event = mailer.PushEvent(client, smtp, request)
with pytest.raises(mailer.ResponseExit) as exc:
await event.process()
assert str(exc.value) == "There is no commit to be processed."
async def test_invalid_branch_name(loop):
data = data_with_commits.copy()
data["ref"] = "refs/heads/invalid"
smtp = FakeSMTP(hostname="localhost", port=1025, loop=loop)
client = aiohttp.ClientSession(loop=loop)
request = make_request("POST", "/", data=data, loop=loop)
event = mailer.PushEvent(client, smtp, request)
with pytest.raises(mailer.ResponseExit) as exc:
await event.process()
assert str(exc.value) == "Invalid branch name."
async def test_send_email(loop):
smtp = FakeSMTP(hostname="localhost", port=1025, loop=loop)
client = aiohttp.ClientSession(loop=loop)
request = make_request("POST", "/", data=data_with_commits, loop=loop)
event = mailer.PushEvent(client, smtp, request)
resp = await event.process()
assert resp == "Ok"
assert len(smtp.sent_mails) == 1
mail = smtp.sent_mails[0]
assert mail["From"] == "Berker Peksag <sender@example.com>"
assert mail["To"] == "recipient@example.com"
assert mail["Subject"] == "Update .gitignore"
assert (
"(cherry picked from commit 9d9ed0e5cceef45fd63dc1f7b3fe6e695da16e83)"
not in mail["Subject"]
)
body = mail.get_body().as_string()
# TODO: We probably need a FakeDiff object to avoid making HTTP requests.
assert diff in body
async def test_github_as_committer(loop):
data = data_with_commits.copy()
data["commits"][0]["committer"] = {
"name": "GitHub",
"email": "noreply@github.com",
"username": "web-flow",
}
smtp = FakeSMTP(hostname="localhost", port=1025, loop=loop)
client = aiohttp.ClientSession(loop=loop)
request = make_request("POST", "/", data=data, loop=loop)
event = mailer.PushEvent(client, smtp, request)
resp = await event.process()
assert resp == "Ok"
assert len(smtp.sent_mails) == 1
mail = smtp.sent_mails[0]
assert mail["From"] == "cbiggles <sender@example.com>"
async def test_github_as_committer_with_pusher(loop):
data = data_with_pusher.copy()
data["commits"][0]["committer"] = {
"name": "GitHub",
"email": "noreply@github.com",
"username": "web-flow",
}
smtp = FakeSMTP(hostname="localhost", port=1025, loop=loop)
client = aiohttp.ClientSession(loop=loop)
request = make_request("POST", "/", data=data, loop=loop)
event = mailer.PushEvent(client, smtp, request)
resp = await event.process()
assert resp == "Ok"
assert len(smtp.sent_mails) == 1
mail = smtp.sent_mails[0]
assert mail["From"] == "Berker Peksag <sender@example.com>"
assert "committer: Berker Peksag <berker.peksag@gmail.com>" in str(mail)