|
3 | 3 | import aiohttp
|
4 | 4 | import aiosmtplib
|
5 | 5 | 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 |
6 | 11 |
|
7 | 12 | import mailer
|
8 | 13 |
|
@@ -52,48 +57,62 @@ async def send_message(self, message, sender=None, recipients=None,
|
52 | 57 | return {}, 'Ok'
|
53 | 58 |
|
54 | 59 |
|
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): |
56 | 76 | 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' |
63 | 83 |
|
64 | 84 |
|
65 |
| -async def test_empty_commits(test_client, loop): |
| 85 | +async def test_empty_commits(loop): |
66 | 86 | data = data_with_commits.copy()
|
67 | 87 | del data['commits']
|
68 | 88 | 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.' |
75 | 95 |
|
76 | 96 |
|
77 |
| -async def test_invalid_branch_name(test_client, loop): |
| 97 | +async def test_invalid_branch_name(loop): |
78 | 98 | data = data_with_commits.copy()
|
79 | 99 | data['ref'] = 'refs/heads/invalid'
|
80 | 100 | 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.' |
87 | 107 |
|
88 | 108 |
|
89 |
| -async def test_send_email(test_client, loop): |
| 109 | +async def test_send_email(loop): |
90 | 110 | 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' |
97 | 116 | assert len(smtp.sent_mails) == 1
|
98 | 117 | mail = smtp.sent_mails[0]
|
99 | 118 | assert mail['From'] == 'Berker Peksag <sender@example.com>'
|
|
0 commit comments