Skip to content

Commit 67ebcdb

Browse files
committed
Wip on services.users
1 parent 680041b commit 67ebcdb

File tree

5 files changed

+174
-16
lines changed

5 files changed

+174
-16
lines changed

pygithub3/tests/core/test_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ def test_DELEGATES_methods(self, request_method):
6161
self.c.get('')
6262
request_method.assert_called_with('get', '')
6363

64-
request_method.return_value = mock_response(201)
64+
request_method.return_value = mock_response('post')
6565
self.c.post('')
6666
request_method.assert_called_with('post', '')
6767

68-
request_method.return_value = mock_response(200)
68+
request_method.return_value = mock_response('patch')
6969
self.c.patch('')
7070
request_method.assert_called_with('patch', '')
7171

7272
self.c.put('')
7373
request_method.assert_called_with('put', '')
7474

75-
request_method.return_value = mock_response(204)
75+
request_method.return_value = mock_response('delete')
7676
self.c.delete('')
7777
request_method.assert_called_with('delete', '')
7878

pygithub3/tests/requests/test_core.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def test_BUILDER_builds_users(self):
3434
request = self.f('users.get')
3535
self.assertIsInstance(request, Request)
3636

37+
3738
class TestRequestUri(TestCase):
3839

3940
def test_SIMPLE_with_correct_args(self):
@@ -80,12 +81,17 @@ def test_with_clean_body(self):
8081

8182
class TestBodyParsers(TestCase):
8283

83-
def setUp(self):
84-
self.b = Body(
84+
def test_only_valid_keys(self):
85+
body = Body(
8586
dict(arg1='arg1', arg2='arg2', arg3='arg3', arg4='arg4'),
8687
('arg1', 'arg3', 'arg4'))
87-
88-
def test_RETURN_only_valid_keys(self):
89-
get_body_returns = self.b.parse()
90-
self.assertEqual(get_body_returns, dict(arg1='arg1', arg3='arg3',
88+
self.assertEqual(body.parse(), dict(arg1='arg1', arg3='arg3',
9189
arg4='arg4'))
90+
91+
def test_none(self):
92+
body = Body({}, ('arg1', 'arg2'))
93+
self.assertEqual(body.parse(), {})
94+
95+
def test_invalid_content(self):
96+
body = Body('invalid', ('arg1',))
97+
self.assertRaises(ValidationError, body.parse)

pygithub3/tests/services/test_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ def test_PUT(self, request_method):
3232
data=data, params=self.args)
3333

3434
def test_DELETE(self, request_method):
35-
request_method.return_value = mock_response(204)
35+
request_method.return_value = mock_response('delete')
3636
self.s._delete(self.r, **self.args)
3737
request_method.assert_called_with('delete', _('dummyrequest'),
3838
data=None, params=self.args)
3939

4040
def test_POST(self, request_method):
41-
request_method.return_value = mock_response(201)
41+
request_method.return_value = mock_response('post')
4242
self.s._post(self.r, **self.args)
4343
request_method.assert_called_with('post', _('dummyrequest'),
4444
data=None, params=self.args)

pygithub3/tests/services/test_users.py

Lines changed: 147 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
import requests
77
from 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
1012
from 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
1214
from pygithub3.tests.utils.services import _, mock_json
1315

1416
json.dumps = Mock(side_effect=mock_json)
1517
json.loads = Mock(side_effect=mock_json)
1618

19+
1720
@patch.object(requests.sessions.Session, 'request')
1821
class 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')

pygithub3/tests/utils/base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ def mock_json(content):
1111
return content
1212

1313

14-
def mock_response(status_code=200, content={}):
14+
def mock_response(status_code='get', content={}):
15+
CODES = dict(get=200, patch=200, post=201, delete=204)
1516
response = Mock(name='response')
16-
response.status_code = status_code
17+
response.status_code = CODES[str(status_code).lower()] or status_code
1718
response.content = content
1819
return response
1920

21+
22+
def mock_response_result(status_code='get'):
23+
response = mock_response(status_code, content=[{}, {}])
24+
response.headers = {'link': ''}
25+
return response
26+
27+
2028
class DummyResource(Resource):
2129
pass
2230

0 commit comments

Comments
 (0)