/
test_comments.py
325 lines (260 loc) · 11.6 KB
/
test_comments.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# -*- coding: utf-8 -*-
from hashlib import sha1
from mr.roboto.events import CommentOnPullRequest
from mr.roboto.subscriber import TriggerPullRequestJenkinsJobs
from tempfile import NamedTemporaryFile
from testfixtures import LogCapture
from webtest import TestApp as BaseApp
import copy
import hmac
import json
import mock
import pickle
import unittest
import urllib
"""
Reduced set of data sent by Github about a pull request.
Each payload is adapted later on to test all corner cases.
"""
COMMENT_PAYLOAD = {
'action': 'created',
'comment': {
'html_url': 'https://github.com/plone/plone.api/pull/42#issuecomment-290382',
'user': {'login': 'my-name'},
'body': 'Some random comment here',
},
'issue': {
'pull_request': {
'url': 'https://github.com/plone/mr.roboto/pull/1',
'html_url': 'https://github.com/plone/mr.roboto/pull/1',
'merged': False,
}
},
}
NO_COMMENT_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
del NO_COMMENT_PAYLOAD['comment']
NO_ISSUE_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
del NO_ISSUE_PAYLOAD['issue']
NO_PULL_REQUEST_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
del NO_PULL_REQUEST_PAYLOAD['issue']['pull_request']
JENKINS_USER_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
JENKINS_USER_PAYLOAD['comment']['user']['login'] = 'jenkins-plone-org'
MR_ROBOTO_USER_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
MR_ROBOTO_USER_PAYLOAD['comment']['user']['login'] = 'mister-roboto'
EDITED_COMMENT_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
EDITED_COMMENT_PAYLOAD['action'] = 'edit'
WHITELISTED_PKG_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
WHITELISTED_PKG_PAYLOAD['comment'][
'html_url'
] = 'https://github.com/plone/plone.releaser/pull/42#issuecomment-290382'
WHITELISTED_PKG_PAYLOAD['issue']['pull_request'][
'html_url'
] = 'https://github.com/plone/plone.releaser/pull/1'
CAN_NOT_GET_PR_INFO_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
CAN_NOT_GET_PR_INFO_PAYLOAD['issue']['pull_request']['url'] = 'https://unkown.pr'
COREDEV_PR_COMMENT_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
COREDEV_PR_COMMENT_PAYLOAD['issue']['pull_request']['url'] = 'https://buildout.coredev'
TRIGGER_PY3_JOBS_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
TRIGGER_PY3_JOBS_PAYLOAD['comment']['body'] = '@jenkins-plone-org please run jobs'
TRIGGER_NO_PY3_JOBS_PAYLOAD = copy.deepcopy(COMMENT_PAYLOAD)
TRIGGER_NO_PY3_JOBS_PAYLOAD['comment']['body'] = '@jenkins-plone-org please run jobs'
TRIGGER_NO_PY3_JOBS_PAYLOAD['issue']['pull_request'][
'url'
] = 'https://github.com/plone/plone.api/pull/1'
def minimal_main(global_config, **settings):
from github import Github
from pyramid.config import Configurator
config = Configurator(settings=settings)
config.include('cornice')
config.registry.settings['plone_versions'] = settings['plone_versions']
config.registry.settings['py3_versions'] = settings['py3_versions']
config.registry.settings['roboto_url'] = settings['roboto_url']
config.registry.settings['api_key'] = settings['api_key']
config.registry.settings['jenkins_user_id'] = settings['jenkins_user_id']
config.registry.settings['github_users'] = (
settings['jenkins_user_id'],
'mister-roboto',
)
config.registry.settings['jenkins_user_token'] = settings['jenkins_user_token']
config.registry.settings['github'] = Github(settings['github_token'])
config.scan('mr.roboto.views.comments')
config.end()
return config.make_wsgi_app()
class MockRequest(object):
def __init__(self, settings):
self._settings = settings
@property
def registry(self):
return self
@property
def settings(self):
return self._settings
def set_sources(self, data):
with NamedTemporaryFile(delete=False) as tmp_file:
sources_pickle = tmp_file.name
with open(sources_pickle, 'bw') as tmp_file_writer:
tmp_file_writer.write(pickle.dumps(data))
self._settings['sources_file'] = sources_pickle
def mocked_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
if args[0] == 'https://buildout.coredev':
return MockResponse(
{'base': {'ref': '5.2', 'repo': {'full_name': 'plone/buildout.coredev'}}},
200,
)
elif args[0] == 'https://github.com/plone/mr.roboto/pull/1':
return MockResponse(
{'base': {'ref': 'master', 'repo': {'full_name': 'plone/mr.roboto'}}}, 200
)
elif args[0] == 'https://github.com/plone/plone.api/pull/1':
return MockResponse(
{'base': {'ref': 'master', 'repo': {'full_name': 'plone/plone.api'}}}, 200
)
return MockResponse(None, 404)
class Base(unittest.TestCase):
def setUp(self):
self.settings = {
'plone_versions': ['5.1', '5.2'],
'py3_versions': ['3.6', '3.7'],
'roboto_url': 'http://jenkins.plone.org/roboto',
'api_key': 'xyz1234mnop',
'sources_file': 'sources_pickle',
'checkouts_file': 'checkouts_pickle',
'github_token': 'x',
'jenkins_user_id': 'jenkins-plone-org',
'jenkins_user_token': 'some-random-token',
}
app = minimal_main({}, **self.settings)
self.roboto = BaseApp(app)
def prepare_data(self, payload):
body = urllib.parse.urlencode({'payload': json.dumps(payload)})
hmac_value = hmac.new(self.settings['api_key'].encode(), body.encode(), sha1)
digest = hmac_value.hexdigest()
return digest, body
def call_view(self, payload):
digest, body = self.prepare_data(payload)
result = self.roboto.post(
'/run/comment', headers={'X-Hub_Signature': f'sha1={digest}'}, params=body
)
return result
class HandleCommentTests(Base):
def test_no_validation(self):
res = self.roboto.post('/run/comment')
self.assertIn('Token not active', res.ubody)
def test_ping_answer(self):
result = self.call_view({'ping': 'true'})
self.assertIn('No action', result.ubody)
def test_no_comment(self):
result = self.call_view(NO_COMMENT_PAYLOAD)
self.assertIn('Comment is missing in payload. No action.', result.ubody)
def test_no_issue(self):
result = self.call_view(NO_ISSUE_PAYLOAD)
self.assertIn(
'The comment is not from a pull request. No action.', result.ubody
)
def test_no_pull_request(self):
result = self.call_view(NO_PULL_REQUEST_PAYLOAD)
self.assertIn(
'The comment is not from a pull request. No action.', result.ubody
)
def test_jenkins_comment(self):
with LogCapture() as captured_data:
result = self.call_view(JENKINS_USER_PAYLOAD)
self.assertIn('Comment on PR ', result.ubody)
self.assertIn(
f' ignored as is from jenkins-plone-org. No action.', result.ubody
)
logger_record = captured_data.records[-1].msg
self.assertIn('COMMENT ', logger_record)
self.assertIn('IGNORED as it is from jenkins-plone-org', logger_record)
def test_mr_roboto_comment(self):
with LogCapture() as captured_data:
result = self.call_view(MR_ROBOTO_USER_PAYLOAD)
self.assertIn('Comment on PR ', result.ubody)
self.assertIn(f' ignored as is from mister-roboto. No action.', result.ubody)
logger_record = captured_data.records[-1].msg
self.assertIn('COMMENT ', logger_record)
self.assertIn('IGNORED as it is from mister-roboto', logger_record)
def test_comment_non_created_action(self):
with LogCapture() as captured_data:
self.call_view(EDITED_COMMENT_PAYLOAD)
logger_record = captured_data.records[-1].msg
self.assertIn('COMMENT ', logger_record)
self.assertIn('with action edit on pull request ', logger_record)
def test_regular_pull_request_comment(self):
result = self.call_view(COMMENT_PAYLOAD)
self.assertIn('Thanks! Handlers already took care of this comment', result)
class TriggerPullRequestJenkinsJobsTests(Base):
def _subscriber(self, payload, data):
request = MockRequest(self.settings)
request.set_sources(data)
event = CommentOnPullRequest(
payload['comment'], payload['issue']['pull_request'], request
)
return TriggerPullRequestJenkinsJobs(event)
def test_short_url(self):
subscriber = self._subscriber(COMMENT_PAYLOAD, {})
self.assertEqual(subscriber.short_url, 'plone/plone.api#42-290382')
def test_package_whitelisted_no_jobs_triggered(self):
with LogCapture() as captured_data:
self._subscriber(WHITELISTED_PKG_PAYLOAD, {})
logger_record = captured_data.records[-1].msg
self.assertIn(
'skip triggering jenkins jobs, repo is whitelisted', logger_record
)
def test_random_comment_no_jobs_triggered(self):
subscriber = self._subscriber(COMMENT_PAYLOAD, {})
with LogCapture() as captured_data:
result = subscriber._should_trigger_jobs()
self.assertEqual(len(captured_data.records), 0)
self.assertFalse(result)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_can_not_get_pr_info(self, mock_get):
subscriber = self._subscriber(CAN_NOT_GET_PR_INFO_PAYLOAD, {})
with LogCapture() as captured_data:
subscriber._which_plone_versions()
logger_record = captured_data.records[-1].msg
self.assertIn('Could not get information regarding pull request', logger_record)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_coredev_pull_request(self, mock_get):
sources = {('plone/mr.roboto', 'stable'): ['5.2']}
subscriber = self._subscriber(COREDEV_PR_COMMENT_PAYLOAD, sources)
versions = subscriber._which_plone_versions()
self.assertIn('5.2', versions)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_no_plone_target_pull_request(self, mock_get):
sources = {('plone/mr.roboto', 'stable'): ['5.0']}
subscriber = self._subscriber(COMMENT_PAYLOAD, sources)
with LogCapture() as captured_data:
versions = subscriber._which_plone_versions()
self.assertEqual(versions, [])
logger_record = captured_data.records[-1].msg
self.assertIn('Does not target any Plone version', logger_record)
@mock.patch('requests.get', side_effect=mocked_requests_get)
@mock.patch('requests.post')
def test_trigger_py3_jobs(self, mock_get, mock_post):
sources = {('plone/mr.roboto', 'master'): ['5.2']}
with LogCapture() as captured_data:
self._subscriber(TRIGGER_PY3_JOBS_PAYLOAD, sources)
self.assertIn(
'Triggered jenkins job for PR 5.2-3.7.', captured_data.records[-1].msg
)
self.assertIn(
'Triggered jenkins job for PR 5.2-3.6.', captured_data.records[-2].msg
)
self.assertIn(
'Triggered jenkins job for PR 5.2.', captured_data.records[-3].msg
)
@mock.patch('requests.get', side_effect=mocked_requests_get)
@mock.patch('requests.post')
def test_trigger_no_py3_jobs(self, mock_get, mock_post):
sources = {('plone/plone.api', 'master'): ['5.1']}
with LogCapture() as captured_data:
self._subscriber(TRIGGER_NO_PY3_JOBS_PAYLOAD, sources)
self.assertEqual(len(captured_data.records), 1)
self.assertIn('Triggered jenkins job for PR 5.1.', captured_data.records[0].msg)