-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_naucse_hooks.py
More file actions
220 lines (175 loc) · 8.58 KB
/
test_naucse_hooks.py
File metadata and controls
220 lines (175 loc) · 8.58 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import hashlib
import json
import random
import re
import shutil
import string
import tempfile
from pathlib import Path
from unittest.mock import patch
from uuid import uuid4
import pytest
import requests_mock
import yaml
import naucse_hooks
TRIGGER_URL = "/trigger"
@pytest.mark.parametrize("denormalized,normalized", [
("https://github.com/baxterthehacker/public-repo.git", "github.com/baxterthehacker/public-repo"),
("https://github.com/baxterthehacker/public-repo", "github.com/baxterthehacker/public-repo"),
("http://github.com/baxterthehacker/public-repo.git", "github.com/baxterthehacker/public-repo"),
("http://github.com/baxterthehacker/public-repo", "github.com/baxterthehacker/public-repo"),
])
def test_normalize_repo(denormalized, normalized):
assert naucse_hooks.normalize_repo(denormalized) == normalized
@pytest.mark.parametrize("repo1,repo2,same", [
("https://github.com/baxterthehacker/public-repo.git", "https://github.com/baxterthehacker/public-repo.git", True),
("https://github.com/baxterthehacker/public-repo.git", "https://github.com/baxterthehacker/public-repo", True),
("https://github.com/baxterthehacker/public-repo.git", "http://github.com/baxterthehacker/public-repo.git", True),
("https://github.com/baxterthehacker/public-repo.git", "http://github.com/baxterthehacker/public-repo", True),
("https://github.com/baxterthehacker/public-repo.git", "https://github.com/baxterthehacker/public-repo.git", True),
("https://github.com/baxterthehacker/public-repo", "https://github.com/baxterthehacker/public-repo.git", True),
("http://github.com/baxterthehacker/public-repo.git", "https://github.com/baxterthehacker/public-repo.git", True),
("http://github.com/baxterthehacker/public-repo", "https://github.com/baxterthehacker/public-repo.git", True),
("https://github.com/baxterthehacker/public-repo.git", "https://github.com/baxthehacker/public-repo.git", False),
("https://github.com/baxterthehacker/public-repo.git", "https://github.com/baxterthehacker/public.git", False),
("https://github.com/prusinsky/naucse.python.cz/", "https://github.com/prusinsky/naucse.python.cz.git", True),
])
def test_same_repo(repo1, repo2, same):
assert naucse_hooks.same_repo(repo1, repo2) == same
def test_get_last_commit_in_branch():
# sha1 hash is 40 hexdec characters
assert len(naucse_hooks.get_last_commit_in_branch("https://github.com/pyvec/naucse-hooks.git", "master")) == 40
@pytest.fixture()
def testapp():
app = naucse_hooks.app
app.testing = True
app.config["NAUCSE_GIT_URL"] = "https://github.com/pyvec/naucse.python.cz"
app.config["NAUCSE_BRANCH"] = "master"
app.config["GITHUB_TOKEN"] = "".join([random.choice(string.ascii_lowercase) for _ in range(20)])
return app
@pytest.fixture()
def testclient(testapp):
return testapp.test_client()
@pytest.fixture(scope="module")
def fake_repo():
repo = Path(tempfile.mkdtemp())
(repo / "courses.yml").write_text(yaml.dump({
"a": {
"url": "https://github.com/baxterthehacker/public-repo.git",
"branch": "root_folder"
},
"b": {
"url": "https://github.com/baxterthehacker/public-repo.git",
"branch": "nested_one"
},
"c": {
"url": "https://github.com/baxterthehacker/public-repo.git",
"branch": "nested_two"
},
"foo": {
"url": "https://github.com/baxterthehacker/public-repo.git",
"branch": "main_course"
}
}))
yield repo
shutil.rmtree(repo, ignore_errors=True)
def test_pull_latest(testapp):
with testapp.test_request_context():
path = naucse_hooks.get_latest_naucse()
assert path.exists()
assert path.is_dir()
assert len(list(path.iterdir())) != 0
@pytest.mark.parametrize("repo,branch,present", [
("https://github.com/baxterthehacker/public-repo.git", "nested_two", True),
("https://github.com/baxterthehacker/public-repo", "nested_two", True),
("http://github.com/baxterthehacker/public-repo.git", "nested_two", True),
("http://github.com/baxterthehacker/public-repo", "nested_two", True),
("https://github.com/baxterthehacker/public-repo.git", "root_folder", True),
("https://github.com/baxterthehacker/public-repo", "root_folder", True),
("http://github.com/baxterthehacker/public-repo.git", "root_folder", True),
("http://github.com/baxterthehacker/public-repo", "root_folder", True),
("https://github.com/baxterthehacker/public-repo.git", "wrong_filename", False),
("https://github.com/baxterthehacker/public-repo.git", "wrong_filename_two", False),
("https://github.com/baxterthehacker/public-repo.git", "main_course", True),
("https://github.com/baxthehacker/public-repo.git", "nested_two", False),
])
def test_is_branch_in_naucse(fake_repo, mocker, repo, branch, present):
mocker.patch("naucse_hooks.get_latest_naucse", lambda: fake_repo)
assert naucse_hooks.is_branch_in_naucse(repo, branch) == present
def test_trigger_build(testapp):
with requests_mock.Mocker() as m:
m.post(re.compile(r"https://api.github.com/repos/(.+)/dispatches"),
json={"success": True},
status_code=200)
naucse_hooks.trigger_build("https://github.com/baxthehacker/public-repo.git", "nested_two")
assert m.call_count == 1
assert m.last_request.method == "POST"
assert m.last_request.url == "https://api.github.com/repos/pyvec/naucse.python.cz/dispatches"
request = m.last_request.json()
assert request["event_type"] == "Redeploy"
assert request["client_payload"]["message"] == \
"Triggered by https://github.com/baxthehacker/public-repo.git/nested_two"
assert m.last_request.headers["Content-Type"] == "application/json"
assert m.last_request.headers["Authorization"] == f"token {testapp.config['GITHUB_TOKEN']}"
assert m.last_request.headers["Accept"] == "application/vnd.github.v3+json"
@patch("naucse_hooks.get_last_commit_in_branch", lambda *args: hashlib.sha1(bytes(str(uuid4()), "utf-8")))
def test_trigger(testclient, mocker, fake_repo):
mocked_trigger_build = mocker.patch("naucse_hooks.trigger_build")
mocker.patch("naucse_hooks.get_latest_naucse", lambda: fake_repo)
# wrong method
response = testclient.get(TRIGGER_URL)
assert response.status_code == 405
# repository missing
response = testclient.post(TRIGGER_URL, data=json.dumps({
"branch": "foo"
}))
assert response.status_code == 400
# branch missing
response = testclient.post(TRIGGER_URL, data=json.dumps({
"repository": "foo"
}))
assert response.status_code == 400
# invalid json
response = testclient.post(TRIGGER_URL, data=json.dumps({
"repository": "foo",
"branch": "bar"
})[:-1])
assert response.status_code == 400
# push to a branch not in naucse
response = testclient.post(TRIGGER_URL, data=json.dumps({
"repository": "missing",
"branch": "missing"
}))
assert response.status_code == 400
# all were incorrect requests, so no build was triggered
assert mocked_trigger_build.call_count == 0
response = testclient.post(TRIGGER_URL, data=json.dumps({
"repository": "https://github.com/baxterthehacker/public-repo.git",
"branch": "nested_two"
}))
assert response.status_code == 200
assert mocked_trigger_build.call_count == 1
def test_dos_protection(testclient, mocker, fake_repo):
mocked_last_commit = mocker.patch("naucse_hooks.get_last_commit_in_branch")
mocked_trigger_build = mocker.patch("naucse_hooks.trigger_build")
mocked_last_commit.return_value = hashlib.sha1(b"initial value")
mocker.patch("naucse_hooks.get_latest_naucse", lambda: fake_repo)
response = testclient.post(TRIGGER_URL, data=json.dumps({
"repository": "https://github.com/baxterthehacker/public-repo.git",
"branch": "nested_two"
}))
assert response.status_code == 200
assert mocked_trigger_build.call_count == 1
response = testclient.post(TRIGGER_URL, data=json.dumps({
"repository": "https://github.com/baxterthehacker/public-repo.git",
"branch": "nested_two"
}))
assert response.status_code == 400
assert mocked_trigger_build.call_count == 1
mocked_last_commit.return_value = hashlib.sha1(b"new commit value")
response = testclient.post(TRIGGER_URL, data=json.dumps({
"repository": "https://github.com/baxterthehacker/public-repo.git",
"branch": "nested_two"
}))
assert response.status_code == 200
assert mocked_trigger_build.call_count == 2