forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_github.py
312 lines (241 loc) · 9.76 KB
/
test_github.py
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
import github3
from tests.utils import (BaseCase, load, mock)
def merge(first, second=None, **kwargs):
copy = first.copy()
copy.update(second or {})
copy.update(kwargs)
return copy
class TestGitHub(BaseCase):
def test_init(self):
g = github3.GitHub('foo', 'bar')
assert repr(g).endswith('[foo]>')
g = github3.GitHub(token='foo')
assert repr(g).endswith('{0:x}>'.format(id(g)))
def test_key(self):
self.response('key')
self.get('https://api.github.com/user/keys/10')
self.assertRaises(github3.GitHubError, self.g.key, 10)
assert self.request.called is False
self.login()
assert self.g.key(-1).is_null()
assert self.request.called is False
assert isinstance(self.g.key(10), github3.users.Key)
self.mock_assertions()
def test_login(self):
self.g.login('user', 'password')
assert self.g.session.auth == ('user', 'password')
self.g.login(token='FakeOAuthToken')
auth = self.g.session.headers.get('Authorization')
assert auth == 'token FakeOAuthToken'
# Unwritten test, not entirely sure how to mock this
def test_markdown(self):
self.response('archive')
self.post('https://api.github.com/markdown')
self.conf = dict(
data={
'text': 'Foo', 'mode': 'gfm', 'context': 'sigmavirus24/cfg'
}
)
assert self.g.markdown(
'Foo', 'gfm', 'sigmavirus24/cfg'
).startswith('archive_data')
self.mock_assertions()
self.post('https://api.github.com/markdown/raw')
self.conf['data'] = 'Foo'
self.g.markdown('Foo', raw=True)
self.mock_assertions()
assert self.g.markdown(None) == ''
self.not_called()
def test_meta(self):
self.response('meta')
self.get('https://api.github.com/meta')
meta = self.g.meta()
assert isinstance(meta, dict)
self.mock_assertions()
def test_octocat(self):
self.response('archive')
self.get('https://api.github.com/octocat')
assert self.g.octocat().startswith('archive_data')
self.mock_assertions()
def test_organization(self):
self.response('org')
self.get('https://api.github.com/orgs/github3py')
org = self.g.organization('github3py')
assert isinstance(org, github3.orgs.Organization)
self.mock_assertions()
def test_pubsubhubbub(self):
self.response('', 204)
self.post('https://api.github.com/hub')
body = [('hub.mode', 'subscribe'),
('hub.topic', 'https://github.com/foo/bar/events/push'),
('hub.callback', 'https://localhost/post')]
self.conf = {}
pubsubhubbub = self.g.pubsubhubbub
self.assertRaises(github3.GitHubError, pubsubhubbub, '', '', '')
self.login()
assert pubsubhubbub('', '', '') is False
self.not_called()
assert pubsubhubbub('foo', 'https://example.com', 'foo') is False
self.not_called()
d = dict([(k[4:], v) for k, v in body])
assert pubsubhubbub(**d) is True
_, kwargs = self.request.call_args
assert 'data' in kwargs
assert body == kwargs['data']
self.mock_assertions()
d['secret'] = 'secret'
body.append(('hub.secret', 'secret'))
assert pubsubhubbub(**d)
_, kwargs = self.request.call_args
assert 'data' in kwargs
assert body == kwargs['data']
self.mock_assertions()
def test_pull_request(self):
self.response('pull')
self.get('https://api.github.com/repos/sigmavirus24/'
'github3.py/pulls/18')
pr = None
with mock.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)
assert isinstance(pr, github3.pulls.PullRequest)
self.mock_assertions()
def test_set_client_id(self):
auth = ('idXXXXXXXXXXXX', 'secretXXXXXXXXXXXXXXXX')
self.g.set_client_id(*auth)
assert self.g.session.params['client_id'] == auth[0]
assert self.g.session.params['client_secret'] == auth[1]
def test_set_user_agent(self):
ua = 'Fake User Agents'
self.g.set_user_agent(ua)
assert self.g.session.headers['User-Agent'] == ua
self.g.set_user_agent(None)
assert self.g.session.headers['User-Agent'] == ua
def test_star(self):
self.response('', 204)
self.put('https://api.github.com/user/starred/sigmavirus24/github3.py')
self.conf = {'data': None}
self.assertRaises(github3.GitHubError, self.g.star, 'foo', 'bar')
self.login()
assert self.g.star(None, None) is False
assert self.g.star('sigmavirus24', 'github3.py')
self.mock_assertions()
def test_unfollow(self):
self.response('', 204)
self.delete('https://api.github.com/user/following/'
'sigmavirus24')
self.conf = {}
self.assertRaises(github3.GitHubError, self.g.unfollow, 'foo')
self.login()
assert self.g.unfollow(None) is False
assert self.g.unfollow('sigmavirus24')
self.mock_assertions()
def test_unstar(self):
self.response('', 204)
self.delete('https://api.github.com/user/starred/'
'sigmavirus24/github3.py')
self.conf = {}
self.assertRaises(github3.GitHubError, self.g.unstar, 'foo', 'bar')
self.login()
assert self.g.unstar(None, None) is False
assert self.g.unstar('sigmavirus24', 'github3.py')
self.mock_assertions()
def test_update_user(self):
self.login()
args = ('Ian Cordasco', '[email protected]', 'www.blog.com', 'company',
'loc', True, 'bio')
with mock.patch.object(github3.github.GitHub, 'user') as user:
with mock.patch.object(github3.users.User, 'update') as upd:
user.return_value = github3.users.User(load('user'), self.g)
upd.return_value = True
assert self.g.update_user(*args)
assert user.called
assert upd.called
upd.assert_called_with(*args)
def test_utf8_user(self):
self.response('utf8_user')
self.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')
def test_zen(self):
self.response('archive')
self.get('https://api.github.com/zen')
assert self.g.zen().startswith('archive_data')
self.mock_assertions()
class TestGitHubEnterprise(BaseCase):
def setUp(self):
super(TestGitHubEnterprise, self).setUp()
self.g = github3.GitHubEnterprise('https://github.example.com:8080/')
def test_admin_stats(self):
self.response('user')
self.get('https://github.example.com:8080/api/v3/enterprise/stats/all')
self.assertRaises(github3.GitHubError, self.g.admin_stats, None)
self.not_called()
self.login()
assert isinstance(self.g.admin_stats('all'), dict)
self.mock_assertions()
def test_repr(self):
assert repr(self.g).startswith('<GitHub Enterprise')
def test_pubsubhubbub(self):
self.response('', 204)
self.post('https://github.example.com:8080/api/v3/hub')
body = [('hub.mode', 'subscribe'),
('hub.topic',
'https://github.example.com:8080/foo/bar/events/push'),
('hub.callback', 'https://localhost/post')]
self.conf = {}
self.login()
d = dict([(k[4:], v) for k, v in body])
assert self.g.pubsubhubbub(**d)
_, kwargs = self.request.call_args
assert 'data' in kwargs
assert body == kwargs['data']
self.mock_assertions()
d['secret'] = 'secret'
body.append(('hub.secret', 'secret'))
assert self.g.pubsubhubbub(**d)
_, kwargs = self.request.call_args
assert 'data' in kwargs
assert body == kwargs['data']
self.mock_assertions()
class TestUnsecureGitHubEnterprise(BaseCase):
def setUp(self):
super(TestUnsecureGitHubEnterprise, self).setUp()
self.g = github3.GitHubEnterprise('https://github.example.com:8080/',
verify=False)
def test_skip_ssl_validation(self):
self.response('pull_enterprise')
self.g.pull_request('sigmavirus24', 'github3.py', 19)
assert False == self.g.session.verify
assert self.request.called
class TestGitHubStatus(BaseCase):
def setUp(self):
super(TestGitHubStatus, self).setUp()
self.g = github3.GitHubStatus()
self.api = 'https://status.github.com/'
def test_repr(self):
assert repr(self.g) == '<GitHub Status>'
def test_api(self):
self.response('user')
self.get(self.api + 'api.json')
assert isinstance(self.g.api(), dict)
self.mock_assertions()
def test_status(self):
self.response('user')
self.get(self.api + 'api/status.json')
assert isinstance(self.g.status(), dict)
self.mock_assertions()
def test_last_message(self):
self.response('user')
self.get(self.api + 'api/last-message.json')
assert isinstance(self.g.last_message(), dict)
self.mock_assertions()
def test_messages(self):
self.response('user')
self.get(self.api + 'api/messages.json')
assert isinstance(self.g.messages(), dict)
self.mock_assertions()