66import requests
77from mock import patch , Mock
88
9- from pygithub3 .services .users import User
9+ from pygithub3 .core .client import Client
10+ from pygithub3 .services .users import User , Emails , Followers , Keys
11+ from pygithub3 .exceptions import ValidationError
1012from pygithub3 .resources .base import json
11- from pygithub3 .tests .utils .base import mock_response
13+ from pygithub3 .tests .utils .base import mock_response , mock_response_result
1214from pygithub3 .tests .utils .services import _ , mock_json
1315
1416json .dumps = Mock (side_effect = mock_json )
1517json .loads = Mock (side_effect = mock_json )
1618
19+
1720@patch .object (requests .sessions .Session , 'request' )
1821class TestUserService (TestCase ):
1922
@@ -23,4 +26,145 @@ def setUp(self):
2326 def test_GET_without_user (self , request_method ):
2427 request_method .return_value = mock_response ()
2528 self .us .get ()
26- request_method .assert_called_with ('get' , _ ('user' ), params = {})
29+ self .assertEqual (request_method .call_args [0 ], ('get' , _ ('user' )))
30+
31+ def test_GET_with_user_in_arg (self , request_method ):
32+ request_method .return_value = mock_response ()
33+ self .us .get ('octocat' )
34+ self .assertEqual (request_method .call_args [0 ],
35+ ('get' , _ ('users/octocat' )))
36+
37+ def test_GET_with_user_in_service (self , request_method ):
38+ request_method .return_value = mock_response ()
39+ self .us .set_user ('octocat_service' )
40+ self .us .get ()
41+ self .assertEqual (request_method .call_args [0 ],
42+ ('get' , _ ('users/octocat_service' )))
43+
44+ def test_UPDATE_with_valid_data (self , request_method ):
45+ request_method .return_value = mock_response ('patch' )
46+ self .us .update ({'name' : 'dummy' })
47+ self .assertEqual (request_method .call_args [0 ], ('patch' , _ ('user' )))
48+
49+ def test_UPDATE_without_data (self , request_method ):
50+ self .assertRaises (ValidationError , self .us .update , {})
51+
52+
53+ @patch .object (requests .sessions .Session , 'request' )
54+ class TestEmailsService (TestCase ):
55+
56+ def setUp (self ):
57+ self .es = Emails ()
58+
59+ def test_LIST (self , request_method ):
60+ request_method .return_value = mock_response_result ()
61+ self .es .list ().all ()
62+ self .assertEqual (request_method .call_args [0 ],
63+ ('get' , _ ('user/emails' )))
64+
65+ def test_ADD_without_emails (self , request_method ):
66+ self .assertRaises (ValidationError , self .es .add )
67+ self .assertFalse (request_method .called )
68+
69+ def test_ADD_with_emails (self , request_method ):
70+ request_method .return_value = mock_response ('post' )
71+ self .es .add ('test@example.com' , 'test2@example.com' )
72+ self .assertEqual (request_method .call_args [0 ],
73+ ('post' , _ ('user/emails' )))
74+
75+ @patch .object (Client , 'request' )
76+ def test_ADD_filter_emails (self , client_request , dummy ):
77+ client_request .return_value = mock_response ('post' )
78+ self .es .add ('invalidemail.com' , 'inva@lid@xam.com' , 'test@xample.com' )
79+ self .assertEqual (client_request .call_args [1 ],
80+ dict (data = ('test@xample.com' , )))
81+
82+ def test_DELETE (self , request_method ):
83+ request_method .return_value = mock_response ('delete' )
84+ self .es .delete ('email_must_be_founded' ) # or 404 raises
85+ self .assertEqual (request_method .call_args [0 ],
86+ ('delete' , _ ('user/emails' )))
87+
88+
89+ @patch .object (requests .sessions .Session , 'request' )
90+ class TestFollowersService (TestCase ):
91+
92+ def setUp (self ):
93+ self .fs = Followers ()
94+
95+ def test_LIST_without_user (self , request_method ):
96+ request_method .return_value = mock_response_result ()
97+ self .fs .list ().all ()
98+ self .assertEqual (request_method .call_args [0 ],
99+ ('get' , _ ('user/followers' )))
100+
101+ def test_LIST_with_user_in_arg (self , request_method ):
102+ request_method .return_value = mock_response_result ()
103+ self .fs .list ('octocat' ).all ()
104+ self .assertEqual (request_method .call_args [0 ],
105+ ('get' , _ ('users/octocat/followers' )))
106+
107+ def test_LIST_with_user_in_service (self , request_method ):
108+ request_method .return_value = mock_response_result ()
109+ self .fs .set_user ('octocat_service' )
110+ self .fs .list ().all ()
111+ self .assertEqual (request_method .call_args [0 ],
112+ ('get' , _ ('users/octocat_service/followers' )))
113+
114+ def test_LIST_FOLLOWING_without_user (self , request_method ):
115+ request_method .return_value = mock_response_result ()
116+ self .fs .list_following ().all ()
117+ self .assertEqual (request_method .call_args [0 ],
118+ ('get' , _ ('user/following' )))
119+
120+ def test_LIST_FOLLOWING_with_user_in_arg (self , request_method ):
121+ request_method .return_value = mock_response_result ()
122+ self .fs .list_following ('octocat' ).all ()
123+ self .assertEqual (request_method .call_args [0 ],
124+ ('get' , _ ('users/octocat/following' )))
125+
126+ def test_LIST_FOLLOWING_with_user_in_service (self , request_method ):
127+ request_method .return_value = mock_response_result ()
128+ self .fs .set_user ('octocat_service' )
129+ self .fs .list_following ().all ()
130+ self .assertEqual (request_method .call_args [0 ],
131+ ('get' , _ ('users/octocat_service/following' )))
132+
133+ def test_IS_FOLLOWING (self , request_method ):
134+ self .fs .is_following ('octocat' )
135+ self .assertEqual (request_method .call_args [0 ],
136+ ('head' , _ ('user/following/octocat' )))
137+
138+ def test_FOLLOW (self , request_method ):
139+ #request_method.return_value = mock_response('put')
140+ self .fs .follow ('octocat' )
141+ self .assertEqual (request_method .call_args [0 ],
142+ ('put' , _ ('user/following/octocat' )))
143+
144+ def test_UNFOLLOW (self , request_method ):
145+ request_method .return_value = mock_response ('delete' )
146+ self .fs .unfollow ('octocat' )
147+ self .assertEqual (request_method .call_args [0 ],
148+ ('delete' , _ ('user/following/octocat' )))
149+
150+
151+ @patch .object (requests .sessions .Session , 'request' )
152+ class TestKeysService (TestCase ):
153+
154+ def setUp (self ):
155+ self .ks = Keys ()
156+
157+ def test_LIST (self , request_method ):
158+ request_method .return_value = mock_response_result ()
159+ self .ks .list ().all ()
160+ self .assertEqual (request_method .call_args [0 ],
161+ ('get' , _ ('user/key' )))
162+
163+ def test_GET (self , request_method ):
164+ request_method .return_value = mock_response ()
165+ self .ks .get (1 )
166+ self .assertEqual (request_method .call_args [0 ],
167+ ('get' , _ ('user/keys/1' )))
168+
169+ def test_ADD (self , request_method ):
170+ request_method .return_value = mock_response ('post' )
0 commit comments