forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_github.py
More file actions
641 lines (510 loc) · 23.5 KB
/
Copy pathtest_github.py
File metadata and controls
641 lines (510 loc) · 23.5 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import github3
from mock import patch
from tests.utils import (generate_response, expect, BaseCase, load)
class TestGitHub(BaseCase):
def test_authorization(self):
self.request.return_value = generate_response('authorization')
self.args = ('get', 'https://api.github.com/authorizations/10')
with expect.githuberror():
self.g.authorization(10)
assert self.request.called is False
self.login()
a = self.g.authorization(10)
expect(a).isinstance(github3.auths.Authorization)
self.mock_assertions()
def test_authorize(self):
self.request.return_value = generate_response('authorization', 201)
scopes = ['scope1', 'scope2']
self.g.authorize(None, None, scopes)
assert self.request.called is False
a = self.g.authorize('user', 'password', scopes)
expect(a).isinstance(github3.auths.Authorization)
assert self.request.called is True
def test_create_gist(self):
self.request.return_value = generate_response('gist', 201)
g = self.g.create_gist('description', 'files')
expect(g).isinstance(github3.gists.Gist)
assert self.request.called is True
def test_create_issue(self):
self.request.return_value = generate_response('issue', 201)
self.login()
i = self.g.create_issue(None, None, None)
assert i is None
assert self.request.called is False
i = self.g.create_issue('user', 'repo', '')
assert i is None
assert self.request.called is False
with patch.object(github3.GitHub, 'repository') as repo:
repo.return_value = github3.repos.Repository(load('repo'),
self.g) # nopep8
i = self.g.create_issue('user', 'repo', 'Title')
expect(i).isinstance(github3.issues.Issue)
assert self.request.called is True
def test_create_key(self):
self.request.return_value = generate_response('key', 201)
with expect.githuberror():
k = self.g.create_key(None, None)
assert k is None
assert self.request.called is False
self.login()
k = self.g.create_key('Name', 'Key')
expect(k).isinstance(github3.users.Key)
assert self.request.called is True
def test_create_repo(self):
self.request.return_value = generate_response('repo', 201)
self.login()
r = self.g.create_repo('Repository')
expect(r).isinstance(github3.repos.Repository)
assert self.request.called is True
def test_delete_key(self):
self.request.return_value = generate_response(None, 204)
self.login()
with patch.object(github3.github.GitHub, 'key') as key:
key.return_value = github3.users.Key(load('key'), self.g)
assert self.g.delete_key(10) is True
assert self.request.called is True
def test_follow(self):
self.request.return_value = generate_response(None, 204)
self.args = ('put',
'https://api.github.com/user/following/sigmavirus24')
self.conf = dict(headers={'Content-Length': '0'}, data=None)
with expect.githuberror():
self.g.follow('sigmavirus24')
self.login()
assert self.g.follow(None) is False
assert self.g.follow('sigmavirus24') is True
self.mock_assertions()
def test_gist(self):
self.request.return_value = generate_response('gist', 200)
self.args = ('get', 'https://api.github.com/gists/10')
expect(self.g.gist(10)).isinstance(github3.gists.Gist)
self.mock_assertions()
def test_is_starred(self):
self.request.return_value = generate_response(None, 204)
self.args = ('get', 'https://api.github.com/user/starred/user/repo')
with expect.githuberror():
self.g.is_starred('user', 'repo')
self.login()
expect(self.g.is_starred(None, None)).is_False()
assert self.request.called is False
expect(self.g.is_starred('user', 'repo')).is_True()
self.mock_assertions()
def test_is_subscribed(self):
self.request.return_value = generate_response(None, 204)
self.args = ('get',
'https://api.github.com/user/subscriptions/user/repo')
with expect.githuberror():
self.g.is_subscribed('user', 'repo')
self.login()
expect(self.g.is_subscribed(None, None)).is_False()
assert self.request.called is False
expect(self.g.is_subscribed('user', 'repo')).is_True()
self.mock_assertions()
def test_issue(self):
self.request.return_value = generate_response('issue', 200)
self.args = ('get',
'https://api.github.com/repos/sigmavirus24/github3.py/'
'issues/1'
)
assert self.g.issue(None, None, 0) is None
with patch.object(github3.github.GitHub, 'repository') as repo:
repo.return_value = github3.repos.Repository(load('repo'))
i = self.g.issue('user', 'repo', 1)
expect(i).isinstance(github3.issues.Issue)
self.mock_assertions()
def test_key(self):
self.request.return_value = generate_response('key')
self.args = ('get', 'https://api.github.com/user/keys/10')
with expect.githuberror():
self.g.key(10)
assert self.request.called is False
self.login()
assert self.g.key(-1) is None
assert self.request.called is False
expect(self.g.key(10)).isinstance(github3.users.Key)
self.mock_assertions()
def test_iter_all_repos(self):
self.request.return_value = generate_response('repo', _iter=True)
self.args = ('get', 'https://api.github.com/repositories')
self.conf.update(params=None)
repo = next(self.g.iter_all_repos())
expect(repo).isinstance(github3.repos.Repository)
self.mock_assertions()
def test_iter_all_users(self):
self.request.return_value = generate_response('user', _iter=True)
self.args = ('get', 'https://api.github.com/users')
self.conf.update(params=None)
repo = next(self.g.iter_all_users())
expect(repo).isinstance(github3.users.User)
self.mock_assertions()
def test_iter_authorizations(self):
self.request.return_value = generate_response('authorization',
_iter=True) # nopep8
self.args = ('get', 'https://api.github.com/authorizations')
self.conf.update(params=None)
with expect.githuberror():
self.g.iter_authorizations()
assert self.request.called is False
self.login()
auth = next(self.g.iter_authorizations())
expect(auth).isinstance(github3.auths.Authorization)
self.mock_assertions()
def test_iter_emails(self):
self.request.return_value = generate_response('emails')
self.args = ('get', 'https://api.github.com/user/emails')
self.conf.update(params=None)
with expect.githuberror():
self.g.iter_emails()
assert self.request.called is False
self.login()
email = next(self.g.iter_emails())
expect(email) == 'user@example.com'
self.mock_assertions()
def test_iter_events(self):
self.request.return_value = generate_response('event', _iter=True)
self.args = ('get', 'https://api.github.com/events')
self.conf.update(params=None)
event = next(self.g.iter_events())
expect(event).isinstance(github3.events.Event)
self.mock_assertions()
def test_iter_followers(self):
self.request.return_value = generate_response('user', _iter=True)
self.args = ('get',
'https://api.github.com/users/sigmavirus24/followers')
self.conf.update(params=None)
with expect.githuberror():
self.g.iter_followers()
with patch.object(github3.github.GitHub, 'user') as ghuser:
ghuser.return_value = github3.users.User(load('user'))
u = next(self.g.iter_followers('sigmavirus24'))
expect(u).isinstance(github3.users.User)
assert self.request.called is True
self.mock_assertions()
self.login()
v = next(self.g.iter_followers())
expect(v).isinstance(github3.users.User)
self.args = (self.args[0],
'https://api.github.com/user/followers')
assert self.request.called is True
self.mock_assertions()
def test_iter_following(self):
self.request.return_value = generate_response('user', _iter=True)
self.args = ('get',
'https://api.github.com/users/sigmavirus24/following')
self.conf.update(params=None)
with expect.githuberror():
next(self.g.iter_following())
assert self.request.called is False
with patch.object(github3.github.GitHub, 'user') as ghuser:
ghuser.return_value = github3.users.User(load('user'))
u = next(self.g.iter_following('sigmavirus24'))
expect(u).isinstance(github3.users.User)
self.mock_assertions()
self.login()
v = next(self.g.iter_following())
expect(v).isinstance(github3.users.User)
self.args = (self.args[0],
'https://api.github.com/user/following')
self.mock_assertions()
def test_iter_gists(self):
self.request.return_value = generate_response('gist', _iter=True)
self.args = ('get', 'https://api.github.com/users/sigmavirus24/gists')
self.conf.update(params=None)
g = next(self.g.iter_gists('sigmavirus24'))
expect(g).isinstance(github3.gists.Gist)
self.mock_assertions()
self.login()
h = next(self.g.iter_gists())
expect(h).isinstance(github3.gists.Gist)
self.mock_assertions()
def test_iter_org_issues(self):
self.request.return_value = generate_response('issue', _iter=True)
self.args = ('get', 'https://api.github.com/orgs/github3py/issues')
self.conf.update(params={})
with expect.githuberror():
self.g.iter_org_issues('github3py')
self.login()
i = next(self.g.iter_org_issues('github3py'))
expect(i).isinstance(github3.issues.Issue)
self.mock_assertions()
params = {'filter': 'assigned', 'state': 'closed', 'labels': 'bug',
'sort': 'created', 'direction': 'asc',
'since': '2012-05-20T23:10:27Z'}
self.conf.update(params=params)
j = next(self.g.iter_org_issues('github3py', **params))
expect(j).isinstance(github3.issues.Issue)
self.mock_assertions()
def test_iter_issues(self):
self.request.return_value = generate_response('issue', _iter=True)
self.args = ('get', 'https://api.github.com/issues')
self.conf.update(params={})
with expect.githuberror():
self.g.iter_issues()
self.login()
expect(next(self.g.iter_issues())).isinstance(github3.issues.Issue)
self.mock_assertions()
params = {'filter': 'assigned', 'state': 'closed', 'labels': 'bug',
'sort': 'created', 'direction': 'asc',
'since': '2012-05-20T23:10:27Z'}
self.conf.update(params=params)
expect(next(self.g.iter_issues(**params))).isinstance(
github3.issues.Issue) # nopep8
self.mock_assertions()
def test_iter_user_issues(self):
self.request.return_value = generate_response('issue', _iter=True)
self.args = ('get', 'https://api.github.com/user/issues')
self.conf.update(params={})
with expect.githuberror():
self.g.iter_user_issues()
self.login()
expect(next(self.g.iter_user_issues())).isinstance(
github3.issues.Issue) # nopep8
self.mock_assertions()
params = {'filter': 'assigned', 'state': 'closed', 'labels': 'bug',
'sort': 'created', 'direction': 'asc',
'since': '2012-05-20T23:10:27Z'}
self.conf.update(params=params)
expect(next(self.g.iter_user_issues(**params))).isinstance(
github3.issues.Issue) # nopep8
self.mock_assertions()
def test_iter_keys(self):
self.request.return_value = generate_response('key', _iter=True)
self.args = ('get', 'https://api.github.com/user/keys')
self.conf.update(params=None)
with expect.githuberror():
self.g.iter_keys()
self.login()
expect(next(self.g.iter_keys())).isinstance(github3.users.Key)
self.mock_assertions()
def test_iter_repos(self):
self.request.return_value = generate_response('repo', _iter=True)
self.args = ('get', 'https://api.github.com/user/repos')
self.conf.update(params={})
self.login()
expect(next(self.g.iter_repos())).isinstance(github3.repos.Repository)
self.mock_assertions()
self.args = ('get', 'https://api.github.com/users/sigmavirus24/repos')
expect(next(self.g.iter_repos('sigmavirus24'))).isinstance(
github3.repos.Repository) # nopep8
self.mock_assertions()
def test_iter_starred(self):
self.request.return_value = generate_response('repo', _iter=True)
self.args = ('get', 'https://api.github.com/user/starred')
self.conf.update(params=None)
self.login()
expect(next(self.g.iter_starred())).isinstance(
github3.repos.Repository) # nopep8
self.mock_assertions()
with patch.object(github3.github.GitHub, 'user') as user:
user.return_value = github3.users.User(load('user'))
self.args = ('get',
'https://api.github.com/users/sigmavirus24/starred')
expect(next(self.g.iter_starred('sigmavirus24'))).isinstance(
github3.repos.Repository) # nopep8
self.mock_assertions()
def test_iter_subscriptions(self):
self.request.return_value = generate_response('repo', _iter=True)
self.args = ('get', 'https://api.github.com/user/subscriptions')
self.conf.update(params=None)
self.login()
expect(next(self.g.iter_subscriptions())).isinstance(
github3.repos.Repository) # nopep8
self.mock_assertions()
with patch.object(github3.github.GitHub, 'user') as user:
user.return_value = github3.users.User(load('user'))
self.args = ('get',
'https://api.github.com/users/sigmavirus24/'
'subscriptions'
)
expect(next(self.g.iter_subscriptions('sigmavirus24'))).isinstance(
github3.repos.Repository) # nopep8
self.mock_assertions()
def test_login(self):
self.g.login('user', 'password')
expect(self.g._session.auth) == ('user', 'password')
self.g.login(token='FakeOAuthToken')
auth = self.g._session.headers.get('Authorization')
expect(auth) == 'token FakeOAuthToken'
# Unwritten test, not entirely sure how to mock this
def test_markdown(self):
pass
def test_pull_request(self):
self.request.return_value = generate_response('pull')
self.args = ('get',
'https://api.github.com/repos/sigmavirus24/'
'github3.py/pulls/18'
)
pr = None
with patch.object(github3.github.GitHub, 'repository') as repo:
repo.return_value = github3.repos.Repository(load('repo'))
pr = self.g.pull_request('sigmavirus24', 'github3.py', 18)
expect(pr).isinstance(github3.pulls.PullRequest)
self.mock_assertions()
def test_organization(self):
self.request.return_value = generate_response('org')
self.args = ('get', 'https://api.github.com/orgs/github3py')
org = self.g.organization('github3py')
expect(org).isinstance(github3.orgs.Organization)
self.mock_assertions()
def test_repository(self):
self.request.return_value = generate_response('repo')
repo = self.g.repository(None, None)
expect(repo).is_None()
expect(self.request.called).is_False()
self.args = ('get',
'https://api.github.com/repos/sigmavirus24/github3.py')
repo = self.g.repository('sigmavirus24', 'github3.py')
expect(repo).isinstance(github3.repos.Repository)
self.mock_assertions()
def test_search_issues(self):
self.request.return_value = generate_response('legacy_issue')
self.args = ('get',
'https://api.github.com/legacy/{0}/{1}/{2}/'
'{3}/{4}/{5}'.format(
'issues', 'search', 'sigmavirus24', 'github3.py',
'closed', 'requests'))
self.conf.update({'params': {}})
issues = self.g.search_issues('sigmavirus24', 'github3.py', 'closed',
'requests')
expect(issues[0]).isinstance(github3.legacy.LegacyIssue)
self.mock_assertions()
self.conf.update({'params': {'start_page': 2}})
issues = self.g.search_issues('sigmavirus24', 'github3.py', 'closed',
'requests', 2)
self.mock_assertions()
def test_search_repos(self):
self.request.return_value = generate_response('legacy_repo')
self.args = ('get',
'https://api.github.com/{0}/{1}/{2}/{3}'.format(
'legacy', 'repos', 'search', 'github3.py')
)
self.conf.update(params={})
repos = self.g.search_repos('github3.py')
expect(repos[0]).isinstance(github3.legacy.LegacyRepo)
self.mock_assertions()
self.conf.update(params={'language': 'python'})
repos = self.g.search_repos('github3.py', language='python')
self.mock_assertions()
def test_search_users(self):
self.request.return_value = generate_response('legacy_user')
self.args = ('get',
'https://api.github.com/{0}/{1}/{2}/{3}'.format(
'legacy', 'user', 'search', 'sigmavirus24')
)
self.conf.update({'params': {}})
users = self.g.search_users('sigmavirus24')
expect(users[0]).isinstance(github3.legacy.LegacyUser)
self.mock_assertions()
self.conf.update({'params': {'start_page': 2}})
self.g.search_users('sigmavirus24', 2)
self.mock_assertions()
def test_search_email(self):
self.request.return_value = generate_response('legacy_email')
self.args = ('get',
'https://api.github.com/{0}/{1}/{2}/{3}'.format(
'legacy', 'user', 'email', 'graffatcolmingov@gmail.com')
)
user = self.g.search_email('graffatcolmingov@gmail.com')
expect(user).isinstance(github3.legacy.LegacyUser)
self.mock_assertions()
def test_set_client_id(self):
auth = ('idXXXXXXXXXXXX', 'secretXXXXXXXXXXXXXXXX')
self.g.set_client_id(*auth)
expect(self.g._session.params['client_id']) == auth[0]
expect(self.g._session.params['client_secret']) == auth[1]
def test_set_user_agent(self):
ua = 'Fake User Agents'
self.g.set_user_agent(ua)
expect(self.g._session.headers['User-Agent']) == ua
expect(self.g._session.config['base_headers']['User-Agent']) == ua
def test_star(self):
self.request.return_value = generate_response('', 204)
self.args = ('put',
'https://api.github.com/user/starred/'
'sigmavirus24/github3.py')
self.conf = {'headers': {'Content-Length': '0'}, 'data': None}
with expect.githuberror():
self.g.star('foo', 'bar')
self.login()
expect(self.g.star(None, None)).is_False()
expect(self.g.star('sigmavirus24', 'github3.py')).is_True()
self.mock_assertions()
def test_subscribe(self):
self.request.return_value = generate_response('', 204)
self.args = ('put',
'https://api.github.com/user/subscriptions/'
'sigmavirus24/github3.py')
self.conf = {'headers': {'Content-Length': '0'}, 'data': None}
with expect.githuberror():
self.g.subscribe('foo', 'bar')
self.login()
expect(self.g.subscribe(None, None)).is_False()
expect(self.g.subscribe('sigmavirus24', 'github3.py')).is_True()
self.mock_assertions()
def test_unfollow(self):
self.request.return_value = generate_response('', 204)
self.args = ('delete',
'https://api.github.com/user/following/'
'sigmavirus24')
self.conf = {}
with expect.githuberror():
self.g.unfollow('foo')
self.login()
expect(self.g.unfollow(None)).is_False()
expect(self.g.unfollow('sigmavirus24')).is_True()
self.mock_assertions()
def test_unstar(self):
self.request.return_value = generate_response('', 204)
self.args = ('delete',
'https://api.github.com/user/starred/'
'sigmavirus24/github3.py')
self.conf = {}
with expect.githuberror():
self.g.unstar('foo', 'bar')
self.login()
expect(self.g.unstar(None, None)).is_False()
expect(self.g.unstar('sigmavirus24', 'github3.py')).is_True()
self.mock_assertions()
def test_unsubscribe(self):
self.request.return_value = generate_response('', 204)
self.args = ('delete',
'https://api.github.com/user/subscriptions/'
'sigmavirus24/github3.py')
self.conf = {}
with expect.githuberror():
self.g.unsubscribe('foo', 'bar')
self.login()
expect(self.g.unsubscribe(None, None)).is_False()
expect(self.g.unsubscribe('sigmavirus24', 'github3.py')).is_True()
self.mock_assertions()
def test_update_user(self):
self.login()
args = ('Ian Cordasco', 'example@mail.com', 'www.blog.com', 'company',
'loc', True, 'bio')
with patch.object(github3.github.GitHub, 'user') as user:
with patch.object(github3.users.User, 'update') as upd:
user.return_value = github3.users.User(load('user'), self.g)
upd.return_value = True
expect(self.g.update_user(*args)).is_True()
expect(user.called).is_True()
expect(upd.called).is_True()
upd.assert_called_with(*args)
def test_user(self):
self.request.return_value = generate_response('user')
self.args = ('get', 'https://api.github.com/users/sigmavirus24')
expect(self.g.user('sigmavirus24')).isinstance(github3.users.User)
self.mock_assertions()
self.args = ('get', 'https://api.github.com/user')
self.login()
expect(self.g.user()).isinstance(github3.users.User)
self.mock_assertions()
def test_utf8_user(self):
self.request.return_value = generate_response('utf8_user')
self.args = ('get', 'https://api.github.com/users/alejandrogomez')
u = self.g.user('alejandrogomez')
try:
repr(u)
except UnicodeEncodeError:
self.fail('Regression caught. See PR #52. Names must be utf-8'
' encoded')
# no test_zen