/
base.py
365 lines (337 loc) · 11 KB
/
base.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- coding: utf-8 -*-
"""
Test base class with commonly used methods and variables
"""
import json
import re
import unittest
import httpretty
class TestGithubBase(unittest.TestCase):
"""Test Github actions and backing library."""
OAUTH2_TOKEN = '12345'
ORG = 'NOT_REAL'
URL = 'http://localhost/'
TEST_COURSE = 'devops.001'
TEST_TERM = 'Spring_2999'
TEST_NEW_TERM = 'Spring_9999'
TEST_DESCRIPTION = 'foo'
TEST_PREFIX = 'testo'
TEST_REPO = '{0}-{1}-{2}'.format(
TEST_PREFIX, TEST_COURSE.replace('.', ''), TEST_TERM
)
TEST_RERUN_REPO = '{0}-{1}-{2}'.format(
TEST_PREFIX, TEST_COURSE.replace('.', ''), TEST_NEW_TERM
)
TEST_TEAM = 'Test-Deploy'
TEST_TEAM_ID = 1
TEST_TEAM_MEMBERS = ['archlight', 'bizarnage', 'chemistro', 'dreadnought']
TEST_STAGING_GR = 'http://gr/'
TEST_PRODUCTION_GR = 'http://prod-gr/'
def callback_repo_check(self, request, uri, headers, status_code=404):
"""Handle mocked API request for repo existence check."""
self.assertEqual(
request.headers['Authorization'],
'token {0}'.format(self.OAUTH2_TOKEN)
)
# Handle the new "rerun" repo differently
if self.TEST_RERUN_REPO in uri:
status_code = 404
return (status_code, headers, json.dumps({'message': 'testing'}))
def callback_repo_create(self, request, uri, headers, status_code=201):
"""Mock repo creation API call."""
# Disabling unused-argument because this is a callback with
# required method signature.
# pylint: disable=unused-argument
self.assertEqual(
request.headers['Authorization'],
'token {0}'.format(self.OAUTH2_TOKEN)
)
repo_dict = json.loads(request.body)
self.assertTrue(
repo_dict['name'] in [self.TEST_REPO, self.TEST_RERUN_REPO]
)
self.assertEqual(repo_dict['description'], self.TEST_DESCRIPTION)
self.assertEqual(repo_dict['private'], True)
return (status_code, headers, json.dumps({'html_url': 'testing'}))
def callback_team_list(
self, request, uri, headers, status_code=200, more=False
):
"""Mock team listing API call."""
# All arguments needed for tests
# pylint: disable=too-many-arguments
self.assertEqual(
request.headers['Authorization'],
'token {0}'.format(self.OAUTH2_TOKEN)
)
page1 = [
{
'id': 1,
'name': self.TEST_TEAM
},
{
'id': 1,
'name': self.TEST_REPO
}
]
page2 = [
{
'id': 3,
'name': 'Other Team'
},
]
current_page = request.querystring.get('page', [u'1'])
current_page = int(current_page[0])
if current_page == 2:
body = page2
else:
body = page1
if more and current_page == 1:
headers['Link'] = (
'<{uri}?page=2>; rel="next",'
'<{uri}?page=2>; rel="last"'
).format(uri=uri)
if status_code == 404:
return (status_code, headers, json.dumps({'error': 'error'}))
return (status_code, headers, json.dumps(body))
def callback_team_members(
self, request, uri, headers,
status_code=200, members=None
):
"""
Return team membership list
"""
# Disabling unused-argument because this is a callback with
# required method signature.
# pylint: disable=unused-argument,too-many-arguments
if members is None:
members = self.TEST_TEAM_MEMBERS
self.assertEqual(
request.headers['Authorization'],
'token {0}'.format(self.OAUTH2_TOKEN)
)
return (status_code, headers, json.dumps(
[dict(login=x) for x in members]
))
def callback_team_create(
self, request, uri, headers, status_code=201, read_only=True
):
"""
Create a new team as requested
"""
# Disabling unused-argument because this is a callback with
# required method signature.
# pylint: disable=unused-argument,too-many-arguments
self.assertEqual(
request.headers['Authorization'],
'token {0}'.format(self.OAUTH2_TOKEN)
)
json_body = json.loads(request.body)
for item in ['name', 'permission']:
self.assertTrue(item in json_body.keys())
if read_only:
self.assertEqual(json_body['permission'], 'pull')
else:
self.assertEqual(json_body['permission'], 'push')
return (status_code, headers, json.dumps({'id': 2}))
@staticmethod
def callback_team_membership(
request, uri, headers, success=True, action_list=None
):
"""Manage both add and delete of team membership.
``action_list`` is a list of tuples with (``username``,
``added (bool)``) to track state of membership since this will
get called multiple times in one library call.
"""
# pylint: disable=too-many-arguments
username = uri.rsplit('/', 1)[1]
if not success:
status_code = 500
if request.method == 'DELETE':
if success:
status_code = 204
action_list.append((username, False))
if request.method == 'PUT':
status_code = 200
action_list.append((username, True))
return (status_code, headers, '')
def callback_team_repo(self, request, uri, headers, status_code=204):
"""Mock adding a repo to a team API call."""
self.assertEqual(
request.headers['Authorization'],
'token {0}'.format(self.OAUTH2_TOKEN)
)
self.assertIsNotNone(re.match(
'{url}teams/[13]/repos/{org}/({repo}|{rerun_repo})'.format(
url=re.escape(self.URL),
org=self.ORG,
repo=re.escape(self.TEST_REPO),
rerun_repo=re.escape(self.TEST_RERUN_REPO)
),
uri
))
if status_code == 422:
return (status_code, headers, json.dumps({
"message": "Validation Failed",
}))
return (status_code, headers, '')
def register_repo_check(self, body):
"""Register repo check URL and method."""
httpretty.register_uri(
httpretty.GET,
re.compile(
'^{url}repos/{org}/({repo}|{repo_rerun})$'.format(
url=self.URL,
org=self.ORG,
repo=re.escape(self.TEST_REPO),
repo_rerun=re.escape(self.TEST_RERUN_REPO)
)
),
body=body
)
def register_repo_create(self, body):
"""Register url for repo create."""
httpretty.register_uri(
httpretty.POST,
'{url}orgs/{org}/repos'.format(
url=self.URL,
org=self.ORG,
),
body=body
)
def register_hook_create(self, body, status):
"""
Simple hook creation URL registration.
"""
test_url = '{url}repos/{org}/{repo}/hooks'.format(
url=self.URL,
org=self.ORG,
repo=self.TEST_REPO
)
# Register for hook endpoint
httpretty.register_uri(
httpretty.POST,
test_url,
body=body,
status=status
)
def register_hook_list(self, body=None, status=200):
"""
Simple hook list URL.
"""
if body is None:
body = json.dumps(
[{
'url': '{url}repos/{org}/{repo}/hooks/1'.format(
url=self.URL, org=self.ORG, repo=self.TEST_REPO
)
}]
)
test_url = '{url}repos/{org}/{repo}/hooks'.format(
url=self.URL,
org=self.ORG,
repo=self.TEST_REPO
)
# Register for hook endpoint
httpretty.register_uri(
httpretty.GET,
test_url,
body=body,
status=status
)
def register_hook_delete(self, status=204):
"""
Simple hook list URL.
"""
test_url = '{url}repos/{org}/{repo}/hooks/1'.format(
url=self.URL,
org=self.ORG,
repo=self.TEST_REPO
)
# Register for hook endpoint
httpretty.register_uri(
httpretty.DELETE,
test_url,
body='',
status=status
)
def register_team_list(self, body):
"""
Team listing API.
"""
httpretty.register_uri(
httpretty.GET,
'{url}orgs/{org}/teams'.format(
url=self.URL,
org=self.ORG,
),
body=body
)
def register_team_create(self, body):
"""
Create team URL/method
"""
httpretty.register_uri(
httpretty.POST,
'{url}orgs/{org}/teams'.format(
url=self.URL,
org=self.ORG,
),
body=body
)
def register_team_members(self, body):
"""
Team membership list API.
"""
httpretty.register_uri(
httpretty.GET,
re.compile(
r'^{url}teams/\d+/members$'.format(
url=re.escape(self.URL)
)
),
body=body
)
def register_team_membership(self, body):
"""
Register adding and removing team members.
"""
url_regex = re.compile(r'^{url}teams/\d+/memberships/\w+$'.format(
url=re.escape(self.URL),
))
httpretty.register_uri(
httpretty.PUT, url_regex, body=body
)
httpretty.register_uri(
httpretty.DELETE, url_regex, body=body
)
def register_team_repo_add(self, body):
"""
Register team repo addition.
"""
httpretty.register_uri(
httpretty.PUT,
re.compile(
r'^{url}teams/\d+/repos/{org}/({repo}|{rerun_repo})$'.format(
url=self.URL,
org=self.ORG,
repo=re.escape(self.TEST_REPO),
rerun_repo=re.escape(self.TEST_RERUN_REPO)
)
),
body=body
)
def register_create_file(self, status=201):
"""
File creation API
"""
httpretty.register_uri(
httpretty.PUT,
re.compile(
r'^{url}repos/{org}/{repo}/contents/.+$'.format(
url=re.escape(self.URL),
org=re.escape(self.ORG),
repo=re.escape(self.TEST_REPO),
)
),
status=status
)