8000 Don't use TestClient in tests · python/webhook-mailer@2e33aee · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e33aee

Browse files
committed
Don't use TestClient in tests
1 parent 090e2bb commit 2e33aee

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

test_mailer.py

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import aiohttp
44
import aiosmtplib
55
import aiosmtplib.response
6+
import pytest
7+
8+
from aiohttp.helpers import sentinel
9+
from aiohttp import streams
10+
from aiohttp.test_utils import make_mocked_request
611

712
import mailer
813

@@ -52,48 +57,62 @@ async def send_message(self, message, sender=None, recipients=None,
5257
return {}, 'Ok'
5358

5459

55-
async def test_wrong_content_type(test_client, loop):
60+
def make_request(method, path, *, loop=None, headers=sentinel, data=None):
61+
if headers is sentinel:
62+
headers = {'content-type': 'application/json'}
63+
if data is not None:
64+
data = json.dumps(data).encode()
65+
payload = streams.StreamReader(loop=loop)
66+
payload.feed_data(data)
67+
payload.feed_eof()
68+
headers.update({'Content-Type': 'application/json',
69+
'Content-Length': str(len(data))})
70+
else:
71+
payload = sentinel
72+
return make_mocked_request(method, path, headers=headers, payload=payload)
73+
74+
75+
async def test_wrong_content_type(loop):
5676
smtp = FakeSMTP(hostname='localhost', port=1025, loop=loop)
57-
app = mailer.application(loop=loop, smtp=smtp)
58-
client = await test_client(app)
59-
resp = await client.post('/', headers=dict(content_type='application/octet-stream'))
60-
assert resp.status == 415
61-
text = await resp.text()
62-
assert text == 'can only accept application/json, not application/octet-stream'
77+
client = aiohttp.ClientSession(loop=loop)
78+
request = make_request('POST', '/', headers={'content-type': 'application/octet-stream'})
79+
event = mailer.PushEvent(client, smtp, request)
80+
with pytest.raises(mailer.ResponseExit) as exc:
81+
await event.process()
82+
assert str(exc.value) == 'can only accept application/json, not application/octet-stream'
6383

6484

65-
async def test_empty_commits(test_client, loop):
85+
async def test_empty_commits(loop):
6686
data = data_with_commits.copy()
6787
del data['commits']
6888
smtp = FakeSMTP(hostname='localhost', port=1025, loop=loop)
69-
app = mailer.application(loop=loop, smtp=smtp)
70-
client = await test_client(app)
71-
resp = await client.post('/', headers={'content-type': 'application/json'}, data=json.dumps(data))
72-
assert resp.status == 204
73-
text = await resp.text()
74-
assert not text
89+
client = aiohttp.ClientSession(loop=loop)
90+
request = make_request('POST', '/', data=data, loop=loop)
91+
event = mailer.PushEvent(client, smtp, request)
92+
with pytest.raises(mailer.ResponseExit) as exc:
93+
await event.process()
94+
assert str(exc.value) == 'There is no commit to be processed.'
7595

7696

77-
async def test_invalid_branch_name(test_client, loop):
97+
async def test_invalid_branch_name(loop):
7898
data = data_with_commits.copy()
7999
data['ref'] = 'refs/heads/invalid'
80100
smtp = FakeSMTP(hostname='localhost', port=1025, loop=loop)
81-
app = mailer.application(loop=loop, smtp=smtp)
82-
client = await test_client(app)
83-
resp = await client.post('/', headers={'content-type': 'application/json'}, data=json.dumps(data))
84-
assert resp.status == 204
85-
text = await resp.text()
86-
assert not text
101+
client = aiohttp.ClientSession(loop=loop)
102+
request = make_request('POST', '/', data=data, loop=loop)
103+
event = mailer.PushEvent(client, smtp, request)
104+
with pytest.raises(mailer.ResponseExit) as exc:
105+
await event.process()
106+
assert str(exc.value) == 'Invalid branch name.'
87107

88108

89-
async def test_send_email(test_client, loop):
109+
async def test_send_email(loop):
90110
smtp = FakeSMTP(hostname='localhost', port=1025, loop=loop)
91-
app = mailer.application(loop=loop, smtp=smtp)
92-
client = await test_client(app)
93-
resp = await client.post('/', headers={'content-type': 'application/json'}, data=json.dumps(data_with_commits))
94-
assert resp.status == 200
95-
text = await resp.text()
96-
assert text == 'Ok'
111+
client = aiohttp.ClientSession(loop=loop)
112+
request = make_request('POST', '/', data=data_with_commits, loop=loop)
113+
event = mailer.PushEvent(client, smtp, request)
114+
resp = await event.process()
115+
assert resp == 'Ok'
97116
assert len(smtp.sent_mails) == 1
98117
mail = smtp.sent_mails[0]
99118
assert mail['From'] == 'Berker Peksag <sender@example.com>'

0 commit comments

Comments
 (0)
0