-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_webhook.py
More file actions
203 lines (167 loc) · 7.7 KB
/
Copy pathtest_webhook.py
File metadata and controls
203 lines (167 loc) · 7.7 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
200
201
202
203
"""GitHub Documentation WebHook handler tests."""
import functools
import subprocess
from unittest import mock
from aiohttp import web
import pytest
import webhook
from webhook import create_app, update_repo, verify_signature
run_check = functools.partial(subprocess.run, check=True)
async def test_signature(monkeypatch):
"""Test that signature verification fails and works as expected."""
with pytest.raises(web.HTTPBadRequest,
match='Secret for non-existent was not set'):
await verify_signature(b'unused', 'unused', 'non-existent', 'unused')
monkeypatch.setenv('WEBHOOK_REPO_SECRET', 'abcdef')
with pytest.raises(web.HTTPBadRequest, match='signature was invalid'):
await verify_signature(b'unused', 'sha256=incorrect', 'repo', 'unused')
# Signature found by passing data to `openssl dgst -sha256 -hmac $SECRET`.
monkeypatch.setenv('WEBHOOK_REPO_SECRET', 'abcdef')
await verify_signature(
b'{"data": "foo"}',
'sha256='
'ebf35862e15f2ac2aa1339ebefff9a88b8270fc8a33dec8f756a398036d86329',
'repo',
'unused')
async def test_update_repo(tmp_path_factory):
"""Test that updating a repository works as expected."""
# Set up a source repository.
src = tmp_path_factory.mktemp('src')
run_check(['git', 'init', '-b', 'gh-pages', src])
(src / 'readme.txt').write_text('Test repo information')
run_check(['git', 'add', 'readme.txt'], cwd=src)
run_check(['git', 'commit', '-m', 'Initial commit'], cwd=src)
# Make second from the first.
dest = tmp_path_factory.mktemp('dest')
run_check(['git', 'clone', src, dest])
# Make a second commit in source repository.
(src / 'install.txt').write_text('There is no installation.')
run_check(['git', 'add', 'install.txt'], cwd=src)
run_check(['git', 'commit', '-m', 'Add install information'], cwd=src)
src_stdout = run_check(['git', 'show-ref', '--head', 'HEAD'], cwd=src,
capture_output=True).stdout.splitlines()
src_commit = next(
(line for line in src_stdout if line.split()[-1] == 'HEAD'), '')
# Now this should correctly update the first repository.
assert await update_repo(dest, 'unused', 'matplotlib/dest')
dest_stdout = run_check(['git', 'show-ref', '--head', 'HEAD'], cwd=dest,
capture_output=True).stdout.splitlines()
dest_commit = next(
(line for line in dest_stdout if line.split()[-1] == 'HEAD'), '')
assert dest_commit == src_commit
async def test_ping(aiohttp_client, monkeypatch, tmp_path):
"""Test ping always works."""
monkeypatch.setenv('SITE_DIR', str(tmp_path))
client = await aiohttp_client(create_app())
resp = await client.get('/ping')
assert resp.status == 200
async def test_github_webhook_errors(aiohttp_client, monkeypatch, tmp_path):
"""Test invalid inputs to webhook."""
monkeypatch.setenv('SITE_DIR', str(tmp_path))
client = await aiohttp_client(create_app())
# Only /gh/<repo-name> exists.
resp = await client.get('/')
assert resp.status == 404
resp = await client.get('/gh')
assert resp.status == 404
# Not allowed if missing correct headers.
resp = await client.get('/gh/non-existent-repo')
assert resp.status == 405
resp = await client.post('/gh/non-existent-repo')
assert resp.status == 400
assert 'No delivery' in await resp.text()
resp = await client.post('/gh/non-existent-repo',
headers={'X-GitHub-Delivery': 'foo'})
assert resp.status == 400
assert 'No signature' in await resp.text()
monkeypatch.setattr(webhook, 'verify_signature',
mock.Mock(verify_signature, return_value=True))
valid_headers = {
'X-GitHub-Delivery': 'foo',
'X-Hub-Signature-256': 'unused',
'X-GitHub-Event': 'ping',
}
# Data should be JSON.
resp = await client.post('/gh/non-existent-repo', headers=valid_headers,
data='}{')
assert resp.status == 400
assert 'Invalid data input' in await resp.text()
# Some data fields are required.
resp = await client.post('/gh/non-existent-repo', headers=valid_headers,
data='{}')
assert resp.status == 400
assert 'Missing required fields' in await resp.text()
resp = await client.post(
'/gh/non-existent-repo', headers=valid_headers,
data='{"sender": {"login": "QuLogic"},'
' "repository": {"name": "foo", "owner": {"login": "foo"}}}')
assert resp.status == 400
assert 'incorrect organization' in await resp.text()
resp = await client.post(
'/gh/non-existent-repo', headers=valid_headers,
data='{"sender": {"login": "QuLogic"}, "repository":'
' {"name": "foo", "owner": {"login": "matplotlib"}}}')
assert resp.status == 400
assert 'incorrect repository' in await resp.text()
# Fields provided, but invalid.
resp = await client.post(
'/gh/non-existent-repo', headers=valid_headers,
data='{"sender": {"login": "QuLogic"}, "repository":'
' {"name": "..", "owner": {"login": "matplotlib"}}}')
assert resp.status == 400
assert 'incorrect repository' in await resp.text()
# Problem on our side.
resp = await client.post(
'/gh/non-existent',
headers={**valid_headers, 'X-GitHub-Event': 'push'},
data='{"sender": {"login": "QuLogic"}, "ref": "refs/heads/gh-pages", '
'"repository": {"name": "non-existent", '
'"owner": {"login": "matplotlib"}}}')
assert resp.status == 500
assert 'non-existent does not exist' in await resp.text()
async def test_github_webhook_valid(aiohttp_client, monkeypatch, tmp_path):
"""Test valid input to webhook."""
monkeypatch.setenv('SITE_DIR', str(tmp_path))
client = await aiohttp_client(create_app())
# Do no actual work, since that's tested above.
monkeypatch.setattr(webhook, 'verify_signature',
mock.Mock(verify_signature, return_value=True))
ur_mock = mock.Mock(update_repo, return_value=None)
monkeypatch.setattr(webhook, 'update_repo', ur_mock)
valid_headers = {
'X-GitHub-Delivery': 'foo',
'X-Hub-Signature-256': 'unused',
}
# Ping event just returns success.
resp = await client.post(
'/gh/non-existent-repo',
headers={**valid_headers, 'X-GitHub-Event': 'ping'},
data='{"sender": {"login": "QuLogic"}, "hook_id": 1234,'
' "zen": "Beautiful is better than ugly.",'
' "repository": {"name": "non-existent-repo",'
' "owner": {"login": "matplotlib"}}}')
assert resp.status == 200
ur_mock.assert_not_called()
# Push event to main branch should do nothing.
resp = await client.post(
'/gh/non-existent-repo',
headers={**valid_headers, 'X-GitHub-Event': 'push'},
data='{"sender": {"login": "QuLogic"},'
' "ref": "refs/heads/main",'
' "repository": {"name": "non-existent-repo",'
' "owner": {"login": "matplotlib"}}}')
assert resp.status == 200
ur_mock.assert_not_called()
# Push event to gh-pages branch should run an update.
tmp_repo = tmp_path / 'non-existent-repo'
(tmp_repo / '.git').mkdir(parents=True, exist_ok=True)
resp = await client.post(
'/gh/non-existent-repo',
headers={**valid_headers, 'X-GitHub-Event': 'push'},
data='{"sender": {"login": "QuLogic"},'
' "ref": "refs/heads/gh-pages",'
' "repository": {"name": "non-existent-repo",'
' "owner": {"login": "matplotlib"}}}')
assert resp.status == 200
ur_mock.assert_called_once_with(
tmp_repo, 'foo', 'matplotlib/non-existent-repo')