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
664 lines (541 loc) · 21.1 KB
/
Copy pathtest_github.py
File metadata and controls
664 lines (541 loc) · 21.1 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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import pytest
from github3 import GitHubError
from github3.github import GitHub
from .helper import UnitHelper, UnitIteratorHelper
def url_for(path=''):
"""Simple function to generate URLs with the base GitHub URL."""
return 'https://api.github.com/' + path.strip('/')
class TestGitHub(UnitHelper):
described_class = GitHub
example_data = None
def test_authorization(self):
"""Show that a user can retrieve a specific authorization by id."""
self.instance.authorization(10)
self.session.get.assert_called_once_with(
url_for('authorizations/10'),
)
def test_authorization_requires_auth(self):
"""A user must be authenticated to retrieve an authorization."""
self.session.auth = None
with pytest.raises(GitHubError):
self.instance.authorization(1)
def test_authorize(self):
"""Show an authorization can be created for a user."""
self.instance.authorize('username', 'password', ['user', 'repo'])
self.session.temporary_basic_auth.assert_called_once_with(
'username', 'password'
)
self.post_called_with(
url_for('authorizations'),
data={'note': '', 'note_url': '', 'client_id': '',
'client_secret': '', 'scopes': ['user', 'repo']}
)
def test_check_authorization(self):
"""Test an app's ability to check a authorization token."""
self.instance.set_client_id('client-id', 'client-secret')
self.instance.check_authorization('super-fake-access-token')
self.session.get.assert_called_once_with(
url_for('applications/client-id/tokens/super-fake-access-token'),
params={'client_id': None, 'client_secret': None},
auth=('client-id', 'client-secret')
)
def test_two_factor_login(self):
"""Test the ability to pass two_factor_callback."""
self.instance.login('username', 'password',
two_factor_callback=lambda *args: 'foo')
def test_can_login_without_two_factor_callback(self):
"""Test that two_factor_callback is not required."""
self.instance.login('username', 'password')
self.instance.login(token='token')
class TestGitHubIterators(UnitIteratorHelper):
described_class = GitHub
example_data = None
def test_all_events(self):
"""Show that one can iterate over all public events."""
i = self.instance.all_events()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('events'),
params={'per_page': 100},
headers={}
)
def test_all_repositories(self):
"""Show that one can iterate over all repositories."""
i = self.instance.all_repositories()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('repositories'),
params={'per_page': 100},
headers={}
)
def test_all_repositories_per_page(self):
"""Show that one can iterate over all repositories with per_page."""
i = self.instance.all_repositories(per_page=25)
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('repositories'),
params={'per_page': 25},
headers={}
)
def test_all_repositories_since(self):
"""Show that one can limit the repositories returned."""
since = 100000
i = self.instance.all_repositories(since=since)
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('repositories'),
params={'per_page': 100, 'since': since},
headers={}
)
def test_all_users(self):
"""Show that one can iterate over all users."""
i = self.instance.all_users()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users'),
params={'per_page': 100},
headers={}
)
def test_all_users_per_page(self):
"""Show that one can iterate over all users with per_page."""
i = self.instance.all_users(per_page=25)
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users'),
params={'per_page': 25},
headers={}
)
def test_all_users_since(self):
"""Show that one can limit the users returned."""
since = 100000
i = self.instance.all_users(since=since)
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users'),
params={'per_page': 100, 'since': since},
headers={}
)
def test_authorizations(self):
"""
Show that an authenticated user can iterate over their authorizations.
"""
i = self.instance.authorizations()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('authorizations'),
params={'per_page': 100},
headers={}
)
def test_authorizations_requires_auth(self):
"""Show that one needs to authenticate to use #authorizations."""
self.session.auth = None
with pytest.raises(GitHubError):
self.instance.authorizations()
def test_emails(self):
"""Show that an authenticated user can iterate over their emails."""
i = self.instance.emails()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/emails'),
params={'per_page': 100},
headers={}
)
def test_emails_require_auth(self):
"""Show that one needs to authenticate to use #emails."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.emails()
def test_followers(self):
"""
Show that an authenticated user can iterate over their followers.
"""
i = self.instance.followers()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/followers'),
params={'per_page': 100},
headers={}
)
def test_followers_require_auth(self):
"""Show that one needs to authenticate to use #followers."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.followers()
def test_followers_of(self):
"""Show that one can authenticate over the followers of a user."""
i = self.instance.followers_of('sigmavirus24')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users/sigmavirus24/followers'),
params={'per_page': 100},
headers={}
)
def test_following(self):
"""
Show that an authenticated user can iterate the users they are
following.
"""
i = self.instance.following()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/following'),
params={'per_page': 100},
headers={}
)
def test_following_require_auth(self):
"""Show that one needs to authenticate to use #following."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.following()
def test_followed_by(self):
"""
Show that one can authenticate over the users followed by another.
"""
i = self.instance.followed_by('sigmavirus24')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users/sigmavirus24/following'),
params={'per_page': 100},
headers={}
)
def test_gists(self):
"""Show that an authenticated user can iterate over their gists."""
i = self.instance.gists()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('gists'),
params={'per_page': 100},
headers={}
)
def test_gists_requires_auth(self):
"""Show that one needs to authenticate to use #gists."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.gists()
def test_gists_by(self):
"""Show that an user's gists can be iterated over."""
i = self.instance.gists_by('sigmavirus24')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users/sigmavirus24/gists'),
params={'per_page': 100},
headers={}
)
def test_issues(self):
"""Show that an authenticated user can iterate over their issues."""
i = self.instance.issues()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('issues'),
params={'per_page': 100},
headers={}
)
def test_issues_with_params(self):
"""Show that issues can be filtered."""
params = {'filter': 'assigned', 'state': 'closed', 'labels': 'bug',
'sort': 'created', 'direction': 'asc',
'since': '2012-05-20T23:10:27Z'}
p = {'per_page': 100}
p.update(params)
i = self.instance.issues(**params)
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('issues'),
params=p,
headers={}
)
def test_issues_requires_auth(self):
"""Show that one needs to authenticate to use #issues."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.issues()
def test_keys(self):
"""
Show that an authenticated user can iterate over their public keys.
"""
i = self.instance.keys()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/keys'),
params={'per_page': 100},
headers={}
)
def test_keys_requires_auth(self):
"""Show that one needs to authenticate to use #keys."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.keys()
def test_notifications(self):
"""
Show that an authenticated user can iterate over their notifications.
"""
i = self.instance.notifications()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('notifications'),
params={'per_page': 100},
headers={},
)
def test_notifications_participating_in(self):
"""Show that the user can filter by pariticpating."""
i = self.instance.notifications(participating=True)
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('notifications'),
params={'per_page': 100, 'participating': 'true'},
headers={}
)
def test_notifications_all(self):
"""Show that the user can iterate over all of their notifications."""
i = self.instance.notifications(all=True)
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('notifications'),
params={'per_page': 100, 'all': 'true'},
headers={}
)
def test_notifications_requires_auth(self):
"""Show that one needs to authenticate to use #gists."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.notifications()
def test_organization_issues(self):
"""Show that one can iterate over an organization's issues."""
i = self.instance.organization_issues('org')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('orgs/org/issues'),
params={'per_page': 100},
headers={}
)
def test_organization_issues_with_params(self):
"""Show that one can pass parameters to #organization_issues."""
params = {'filter': 'assigned', 'state': 'closed', 'labels': 'bug',
'sort': 'created', 'direction': 'asc',
'since': '2012-05-20T23:10:27Z'}
i = self.instance.organization_issues('org', **params)
self.get_next(i)
p = {'per_page': 100}
p.update(params)
self.session.get.assert_called_once_with(
url_for('orgs/org/issues'),
params=p,
headers={}
)
def test_organization_issues_requires_auth(self):
"""Show that one needs to authenticate to use #organization_issues."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.organization_issues('org')
def test_organizations(self):
"""
Show that one can iterate over all of the authenticated user's orgs.
"""
i = self.instance.organizations()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/orgs'),
params={'per_page': 100},
headers={}
)
def test_organizations_requires_auth(self):
"""Show that one needs to authenticate to use #organizations."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.organizations()
def test_organizations_with(self):
"""Show that one can iterate over all of a user's orgs."""
i = self.instance.organizations_with('sigmavirus24')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users/sigmavirus24/orgs'),
params={'per_page': 100},
headers={}
)
def test_public_gists(self):
"""Show that all public gists can be iterated over."""
i = self.instance.public_gists()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('gists/public'),
params={'per_page': 100},
headers={}
)
def test_respositories(self):
"""
Show that an authenticated user can iterate over their repositories.
"""
i = self.instance.repositories()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/repos'),
params={'per_page': 100},
headers={}
)
def test_respositories_accepts_params(self):
"""Show that an #repositories accepts params."""
i = self.instance.repositories(type='all',
direction='desc',
sort='created')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/repos'),
params={'per_page': 100, 'type': 'all', 'direction': 'desc',
'sort': 'created'},
headers={}
)
def test_repositories_requires_auth(self):
"""Show that one needs to authenticate to use #repositories."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.repositories()
def test_issues_on(self):
"""Show that a user can iterate over a repository's issues."""
i = self.instance.issues_on('owner', 'repo')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('repos/owner/repo/issues'),
params={'per_page': 100},
headers={}
)
def test_issues_on_with_params(self):
"""Show that #issues_on accepts multiple parameters."""
params = {'milestone': 1, 'state': 'all', 'assignee': 'owner',
'mentioned': 'someone', 'labels': 'bug,high'}
i = self.instance.issues_on('owner', 'repo', **params)
self.get_next(i)
params.update(per_page=100)
self.session.get.assert_called_once_with(
url_for('repos/owner/repo/issues'),
params=params,
headers={}
)
def test_starred(self):
"""
Show that one can iterate over an authenticated user's stars.
"""
i = self.instance.starred()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/starred'),
params={'per_page': 100},
headers={}
)
def test_starred_requires_auth(self):
"""Show that one needs to authenticate to use #starred."""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.starred()
def test_starred_by(self):
"""Show that one can iterate over a user's stars."""
i = self.instance.starred_by('sigmavirus24')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users/sigmavirus24/starred'),
params={'per_page': 100},
headers={}
)
def test_subscriptions(self):
"""
Show that one can iterate over an authenticated user's subscriptions.
"""
i = self.instance.subscriptions()
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/subscriptions'),
params={'per_page': 100},
headers={}
)
def test_subscriptions_for(self):
"""Show that one can iterate over a user's subscriptions."""
i = self.instance.subscriptions_for('sigmavirus24')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users/sigmavirus24/subscriptions'),
params={'per_page': 100},
headers={}
)
def test_user_issues(self):
"""Test that one can iterate over a user's issues."""
i = self.instance.user_issues()
# Get the next item from the iterator
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/issues'),
params={'per_page': 100},
headers={}
)
def test_user_issues_requires_auth(self):
"""
Test that one must authenticate to interate over a user's issues.
"""
self.session.has_auth.return_value = False
with pytest.raises(GitHubError):
self.instance.user_issues()
def test_user_issues_with_parameters(self):
"""Test that one may pass parameters to GitHub#user_issues."""
# Set up the parameters to be sent
params = {'filter': 'assigned', 'state': 'closed', 'labels': 'bug',
'sort': 'created', 'direction': 'asc',
'since': '2012-05-20T23:10:27Z', 'per_page': 25}
# Make the call with the paramters
i = self.instance.user_issues(**params)
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('user/issues'),
params=params,
headers={}
)
def test_repositories_by(self):
"""Test that one can iterate over a user's repositories."""
i = self.instance.repositories_by('sigmavirus24')
# Get the next item from the iterator
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users/sigmavirus24/repos'),
params={'per_page': 100},
headers={}
)
def test_repositories_by_with_type(self):
"""
Test that one can iterate over a user's repositories with a type.
"""
i = self.instance.repositories_by('sigmavirus24', 'all')
self.get_next(i)
self.session.get.assert_called_once_with(
url_for('users/sigmavirus24/repos'),
params={'per_page': 100, 'type': 'all'},
headers={}
)
class TestGitHubAuthorizations(UnitHelper):
described_class = GitHub
example_data = None
def create_session_mock(self, *args):
session = super(TestGitHubAuthorizations,
self).create_session_mock(*args)
session.retrieve_client_credentials.return_value = ('id', 'secret')
return session
def test_revoke_authorization(self):
"""Test that GitHub#revoke_authorization calls the expected methods.
It should use the session's delete and temporary_basic_auth methods.
"""
self.instance.revoke_authorization('access_token')
self.session.delete.assert_called_once_with(
'https://api.github.com/applications/id/tokens/access_token',
params={'client_id': None, 'client_secret': None}
)
self.session.temporary_basic_auth.assert_called_once_with(
'id', 'secret'
)
def test_revoke_authorizations(self):
"""Test that GitHub#revoke_authorizations calls the expected methods.
It should use the session's delete and temporary_basic_auth methods.
"""
self.instance.revoke_authorizations()
self.session.delete.assert_called_once_with(
'https://api.github.com/applications/id/tokens',
params={'client_id': None, 'client_secret': None}
)
self.session.temporary_basic_auth.assert_called_once_with(
'id', 'secret'
)