4
4
5
5
6
6
class TestGitHub (BaseCase ):
7
+ def test_init (self ):
8
+ g = github3 .GitHub ('foo' , 'bar' )
9
+ expect (repr (g ).endswith ('[foo]>' ))
10
+
11
+ g = github3 .GitHub (token = 'foo' )
12
+ expect (repr (g ).endswith ('{0:x}>' .format (id (g ))))
13
+
7
14
def test_authorization (self ):
8
15
self .response ('authorization' )
9
16
self .get ('https://api.github.com/authorizations/10' )
@@ -21,12 +28,17 @@ def test_authorize(self):
21
28
scopes = ['scope1' , 'scope2' ]
22
29
23
30
self .g .authorize (None , None , scopes )
24
- assert self .request . called is False
31
+ self .not_called ()
25
32
26
33
a = self .g .authorize ('user' , 'password' , scopes )
27
34
expect (a ).isinstance (github3 .auths .Authorization )
28
35
assert self .request .called is True
29
36
37
+ self .request .reset_mock ()
38
+
39
+ self .login ()
40
+ a = self .g .authorize (None , None , scopes = scopes )
41
+
30
42
def test_create_gist (self ):
31
43
self .response ('gist' , 201 )
32
44
@@ -81,6 +93,8 @@ def test_delete_key(self):
81
93
with patch .object (github3 .github .GitHub , 'key' ) as key :
82
94
key .return_value = github3 .users .Key (load ('key' ), self .g )
83
95
assert self .g .delete_key (10 ) is True
96
+ key .return_value = None
97
+ assert self .g .delete_key (10 ) is False
84
98
85
99
assert self .request .called is True
86
100
@@ -302,6 +316,28 @@ def test_iter_gists(self):
302
316
self .get ('https://api.github.com/gists' )
303
317
self .mock_assertions ()
304
318
319
+ def test_iter_notifications (self ):
320
+ self .response ('notification' , _iter = True )
321
+ self .get ('https://api.github.com/notifications' )
322
+ self .conf .update (params = None )
323
+
324
+ with expect .githuberror ():
325
+ self .g .iter_notifications ()
326
+
327
+ self .not_called ()
328
+ self .login ()
329
+ thread = next (self .g .iter_notifications ())
330
+ expect (thread ).isinstance (github3 .notifications .Thread )
331
+ self .mock_assertions ()
332
+
333
+ self .conf .update (params = {'all' : True })
334
+ next (self .g .iter_notifications (True ))
335
+ self .mock_assertions ()
336
+
337
+ self .conf .update (params = {'participating' : True })
338
+ next (self .g .iter_notifications (participating = True ))
339
+ self .mock_assertions ()
340
+
305
341
def test_iter_org_issues (self ):
306
342
self .response ('issue' , _iter = True )
307
343
self .get ('https://api.github.com/orgs/github3py/issues' )
@@ -377,6 +413,9 @@ def test_iter_repo_issues(self):
377
413
expect (i ).isinstance (github3 .issues .Issue )
378
414
self .mock_assertions ()
379
415
416
+ with expect .raises (StopIteration ):
417
+ next (self .g .iter_repo_issues (None , None ))
418
+
380
419
def test_iter_keys (self ):
381
420
self .response ('key' , _iter = True )
382
421
self .get ('https://api.github.com/user/keys' )
@@ -416,6 +455,10 @@ def test_iter_repos(self):
416
455
github3 .repos .Repository )
417
456
self .mock_assertions ()
418
457
458
+ self .conf .update (params = {'type' : 'all' , 'direction' : 'desc' })
459
+ next (self .g .iter_repos ('sigmavirus24' , 'all' , direction = 'desc' ))
460
+ self .mock_assertions ()
461
+
419
462
def test_iter_repos_sort (self ):
420
463
self .response ('repo' , _iter = True )
421
464
self .conf .update (params = {"sort" : "created" })
@@ -476,7 +519,26 @@ def test_login(self):
476
519
477
520
# Unwritten test, not entirely sure how to mock this
478
521
def test_markdown (self ):
479
- pass
522
+ self .response ('archive' )
523
+ self .post ('https://api.github.com/markdown' )
524
+ self .conf = dict (
525
+ data = {
526
+ 'text' : 'Foo' , 'mode' : 'gfm' , 'context' : 'sigmavirus24/cfg'
527
+ }
528
+ )
529
+
530
+ expect (self .g .markdown ('Foo' , 'gfm' , 'sigmavirus24/cfg' )) == (
531
+ b'archive_data'
532
+ )
533
+ self .mock_assertions ()
534
+
535
+ self .post ('https://api.github.com/markdown/raw' )
536
+ self .conf ['data' ] = 'Foo'
537
+ self .g .markdown ('Foo' , raw = True )
538
+ self .mock_assertions ()
539
+
540
+ expect (self .g .markdown (None )) == ''
541
+ self .not_called ()
480
542
481
543
def test_meta (self ):
482
544
self .response ('meta' )
@@ -485,6 +547,12 @@ def test_meta(self):
485
547
expect (meta ).isinstance (dict )
486
548
self .mock_assertions ()
487
549
550
+ def test_octocat (self ):
551
+ self .response ('archive' )
552
+ self .get ('https://api.github.com/octocat' )
553
+ expect (self .g .octocat ().startswith (b'archive_data' ))
554
+ self .mock_assertions ()
555
+
488
556
def test_organization (self ):
489
557
self .response ('org' )
490
558
10000
self .get ('https://api.github.com/orgs/github3py' )
@@ -518,6 +586,14 @@ def test_pubsubhubbub(self):
518
586
expect (body ) == kwargs ['data' ]
519
587
self .mock_assertions ()
520
588
589
+ d ['secret' ] = 'secret'
590
+ body .append (('hub.secret' , 'secret' ))
591
+ expect (self .g .pubsubhubbub (** d )).is_True ()
592
+ _ , kwargs = self .request .call_args
593
+ expect ('data' ).is_in (kwargs )
594
+ expect (body ) == kwargs ['data' ]
595
+ self .mock_assertions ()
596
+
521
597
def test_pull_request (self ):
522
598
self .response ('pull' )
523
599
self .get ('https://api.github.com/repos/sigmavirus24/'
@@ -572,8 +648,8 @@ def test_search_repos(self):
572
648
expect (repos [0 ].is_private ()) == repos [0 ].private
573
649
self .mock_assertions ()
574
650
575
- self .conf .update (params = {'language' : 'python' })
576
- repos = self .g .search_repos ('github3.py' , language = 'python' )
651
+ self .conf .update (params = {'language' : 'python' , 'start_page' : 10 })
652
+ repos = self .g .search_repos ('github3.py' , 'python' , 10 )
577
653
self .mock_assertions ()
578
654
579
655
def test_search_users (self ):
@@ -609,6 +685,9 @@ def test_set_user_agent(self):
609
685
self .g .set_user_agent (ua )
610
686
expect (self .g ._session .headers ['User-Agent' ]) == ua
611
687
688
+ self .g .set_user_agent (None )
689
+ expect (self .g ._session .headers ['User-Agent' ]) == ua
690
+
612
691
def test_star (self ):
613
692
self .response ('' , 204 )
614
693
self .put ('https://api.github.com/user/starred/sigmavirus24/github3.py' )
@@ -716,4 +795,64 @@ def test_utf8_user(self):
716
795
self .fail ('Regression caught. See PR #52. Names must be utf-8'
717
796
' encoded' )
718
797
719
- # no test_zen
798
+ def test_zen (self ):
799
+ self .response ('archive' )
800
+ self .get ('https://api.github.com/zen' )
801
+
802
+ expect (self .g .zen ()) == b'archive_data'
803
+ self .mock_assertions ()
804
+
805
+
806
+ class TestGitHubEnterprise (BaseCase ):
807
+ def setUp (self ):
808
+ super (TestGitHubEnterprise , self ).setUp ()
809
+ self .g = github3 .GitHubEnterprise ('https://github.example.com/' )
810
+
811
+ def test_admin_stats (self ):
812
+ self .response ('user' )
813
+ self .get ('https://github.example.com/api/v3/enterprise/stats/all' )
814
+
815
+ with expect .githuberror ():
816
+ self .g .admin_stats (None )
817
+
818
+ self .not_called ()
819
+ self .login ()
820
+ expect (self .g .admin_stats ('all' )).isinstance (dict )
821
+ self .mock_assertions ()
822
+
823
+ def test_repr (self ):
824
+ expect (repr (self .g ).startswith ('<GitHub Enterprise' )).is_True ()
825
+
826
+
827
+ class TestGitHubStatus (BaseCase ):
828
+ def setUp (self ):
829
+ super (TestGitHubStatus , self ).setUp ()
830
+ self .g = github3 .GitHubStatus ()
831
+ self .api = 'https://status.github.com/'
832
+
833
+ def test_repr (self ):
834
+ expect (repr (self .g )) == '<GitHub Status>'
835
+
836
+ def test_api (self ):
837
+ self .response ('user' )
838
+ self .get (self .api + 'api.json' )
839
+ expect (self .g .api ()).isinstance (dict )
840
+ self .mock_assertions ()
841
+
842
+ def test_status (self ):
843
+ self .response ('user' )
844
+ self .get (self .api + 'api/status.json' )
845
+ expect (self .g .status ()).isinstance (dict )
846
+ self .mock_assertions ()
847
+
848
+ def test_last_message (self ):
849
+ self .response ('user' )
850
+ self .get (self .api + 'api/last-message.json' )
851
+ expect (self .g .last_message ()).isinstance (dict )
852
+ self .mock_assertions ()
853
+
854
+ def test_messages (self ):
855
+ self .response ('user' )
856
+ self .get (self .api + 'api/messages.json' )
857
+ expect (self .g .messages ()).isinstance (dict )
858
+ self .mock_assertions ()
0 commit comments