/
test_www_oauth.py
348 lines (299 loc) · 13.8 KB
/
test_www_oauth.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
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
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
import os
import webbrowser
import mock
from twisted.internet import defer
from twisted.internet import reactor
from twisted.internet import threads
from twisted.python import failure
from twisted.trial import unittest
from twisted.web.resource import Resource
from twisted.web.server import Site
from buildbot.test.util import www
from buildbot.util import json
try:
import requests
except ImportError:
requests = None
if requests:
from buildbot.www import oauth2
class FakeResponse(object):
def __init__(self, _json):
self.json = lambda: _json
self.content = json.dumps(_json)
def raise_for_status(self):
pass
class OAuth2Auth(www.WwwTestMixin, unittest.TestCase):
def setUp(self):
if requests is None:
raise unittest.SkipTest("Need to install requests to test oauth2")
self.patch(requests, 'request', mock.Mock(spec=requests.request))
self.patch(requests, 'post', mock.Mock(spec=requests.post))
self.patch(requests, 'get', mock.Mock(spec=requests.get))
self.googleAuth = oauth2.GoogleAuth("ggclientID", "clientSECRET")
self.githubAuth = oauth2.GitHubAuth("ghclientID", "clientSECRET")
self.gitlabAuth = oauth2.GitLabAuth(
"https://gitlab.test/", "glclientID", "clientSECRET")
self.bitbucketAuth = oauth2.BitbucketAuth("bbclientID", "clientSECRET")
for auth in [self.googleAuth, self.githubAuth, self.gitlabAuth, self.bitbucketAuth]:
self._master = master = self.make_master(url='h:/a/b/', auth=auth)
auth.reconfigAuth(master, master.config)
@defer.inlineCallbacks
def test_getGoogleLoginURL(self):
res = yield self.googleAuth.getLoginURL('http://redir')
exp = ("https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2F"
"www.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.go"
"ogleapis.com%2Fauth%2Fuserinfo.profile&state=redirect%3Dhttp%253A%252F%252Fredir&redirect_uri=h%3A%2Fa%2Fb"
"%2Fauth%2Flogin&response_type=code&client_id=ggclientID")
self.assertEqual(res, exp)
res = yield self.googleAuth.getLoginURL(None)
exp = ("https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2F"
"www.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.go"
"ogleapis.com%2Fauth%2Fuserinfo.profile&redirect_uri=h%3A%2Fa%2Fb"
"%2Fauth%2Flogin&response_type=code&client_id=ggclientID")
self.assertEqual(res, exp)
@defer.inlineCallbacks
def test_getGithubLoginURL(self):
res = yield self.githubAuth.getLoginURL('http://redir')
exp = ("https://github.com/login/oauth/authorize?scope=user%3Aemail&state="
"redirect%3Dhttp%253A%252F%252Fredir&redirect_uri=h%3A%2Fa%2Fb%2F"
"auth%2Flogin&response_type=code&client_id=ghclientID")
self.assertEqual(res, exp)
res = yield self.githubAuth.getLoginURL(None)
exp = ("https://github.com/login/oauth/authorize?scope=user%3Aemail&redirect_uri="
"h%3A%2Fa%2Fb%2Fauth%2Flogin&response_type=code&client_id=ghclientID")
self.assertEqual(res, exp)
@defer.inlineCallbacks
def test_getGitLabLoginURL(self):
res = yield self.gitlabAuth.getLoginURL('http://redir')
exp = ("https://gitlab.test/oauth/authorize"
"?state=redirect%3Dhttp%253A%252F%252Fredir"
"&redirect_uri=h%3A%2Fa%2Fb%2Fauth%2Flogin"
"&response_type=code"
"&client_id=glclientID")
self.assertEqual(res, exp)
res = yield self.gitlabAuth.getLoginURL(None)
exp = ("https://gitlab.test/oauth/authorize"
"?redirect_uri=h%3A%2Fa%2Fb%2Fauth%2Flogin"
"&response_type=code"
"&client_id=glclientID")
self.assertEqual(res, exp)
@defer.inlineCallbacks
def test_getBitbucketLoginURL(self):
res = yield self.bitbucketAuth.getLoginURL('http://redir')
exp = ("https://bitbucket.org/site/oauth2/authorize?state=redirect%3D"
"http%253A%252F%252Fredir&redirect_uri=h%3A%2Fa%2Fb%2Fauth%2F"
"login&response_type=code&client_id=bbclientID")
self.assertEqual(res, exp)
res = yield self.bitbucketAuth.getLoginURL(None)
exp = ("https://bitbucket.org/site/oauth2/authorize?redirect_uri=h%3A%2"
"Fa%2Fb%2Fauth%2Flogin&response_type=code&client_id=bbclientID")
self.assertEqual(res, exp)
@defer.inlineCallbacks
def test_GoogleVerifyCode(self):
requests.get.side_effect = []
requests.post.side_effect = [
FakeResponse(dict(access_token="TOK3N"))]
self.googleAuth.get = mock.Mock(side_effect=[dict(
name="foo bar",
email="bar@foo", picture="http://pic")])
res = yield self.googleAuth.verifyCode("code!")
self.assertEqual({'avatar_url': 'http://pic', 'email': 'bar@foo',
'full_name': 'foo bar', 'username': 'bar'}, res)
@defer.inlineCallbacks
def test_GithubVerifyCode(self):
requests.get.side_effect = []
requests.post.side_effect = [
FakeResponse(dict(access_token="TOK3N"))]
self.githubAuth.get = mock.Mock(side_effect=[
dict( # /user
login="bar",
name="foo bar",
email="buzz@bar"),
[ # /user/emails
{'email': 'buzz@bar', 'verified': True, 'primary': False},
{'email': 'bar@foo', 'verified': True, 'primary': True}],
[dict( # /users/bar/orgs
login="group",)
]])
res = yield self.githubAuth.verifyCode("code!")
self.assertEqual({'email': 'bar@foo',
'username': 'bar',
'groups': ['group'],
'full_name': 'foo bar'}, res)
@defer.inlineCallbacks
def test_GitlabVerifyCode(self):
requests.get.side_effect = []
requests.post.side_effect = [
FakeResponse(dict(access_token="TOK3N"))]
self.gitlabAuth.get = mock.Mock(side_effect=[
{ # /user
"name": "Foo Bar",
"username": "fbar",
"id": 5,
"avatar_url": "https://avatar/fbar.png",
"email": "foo@bar",
"twitter": "fb",
},
[ # /groups
{"id": 10, "name": "Hello", "path": "hello"},
{"id": 20, "name": "Group", "path": "grp"},
]])
res = yield self.gitlabAuth.verifyCode("code!")
self.assertEqual({"full_name": "Foo Bar",
"username": "fbar",
"email": "foo@bar",
"avatar_url": "https://avatar/fbar.png",
"groups": ["hello", "grp"]}, res)
@defer.inlineCallbacks
def test_BitbucketVerifyCode(self):
requests.get.side_effect = []
requests.post.side_effect = [
FakeResponse(dict(access_token="TOK3N"))]
self.bitbucketAuth.get = mock.Mock(side_effect=[
dict( # /user
username="bar",
display_name="foo bar"),
dict( # /user/emails
values=[
{'email': 'buzz@bar', 'is_primary': False},
{'email': 'bar@foo', 'is_primary': True}]),
dict( # /teams?role=member
values=[
{'username': 'hello'},
{'username': 'grp'}])
])
res = yield self.bitbucketAuth.verifyCode("code!")
self.assertEqual({'email': 'bar@foo',
'username': 'bar',
"groups": ["hello", "grp"],
'full_name': 'foo bar'}, res)
@defer.inlineCallbacks
def test_loginResource(self):
class fakeAuth(object):
homeUri = "://me"
getLoginURL = mock.Mock(side_effect=lambda x: defer.succeed("://"))
verifyCode = mock.Mock(
side_effect=lambda code: defer.succeed({"username": "bar"}))
userInfoProvider = None
rsrc = self.githubAuth.getLoginResource()
rsrc.auth = fakeAuth()
res = yield self.render_resource(rsrc, '/')
rsrc.auth.getLoginURL.assert_called_once_with(None)
rsrc.auth.verifyCode.assert_not_called()
self.assertEqual(res, {'redirected': '://'})
rsrc.auth.getLoginURL.reset_mock()
rsrc.auth.verifyCode.reset_mock()
res = yield self.render_resource(rsrc, '/?code=code!')
rsrc.auth.getLoginURL.assert_not_called()
rsrc.auth.verifyCode.assert_called_once_with("code!")
self.assertEqual(self.master.session.user_info, {'username': 'bar'})
self.assertEqual(res, {'redirected': '://me'})
def test_getConfig(self):
self.assertEqual(self.githubAuth.getConfigDict(), {'fa_icon': 'fa-github', 'autologin': False,
'name': 'GitHub', 'oauth2': True})
self.assertEqual(self.googleAuth.getConfigDict(), {'fa_icon': 'fa-google-plus', 'autologin': False,
'name': 'Google', 'oauth2': True})
self.assertEqual(self.gitlabAuth.getConfigDict(), {'fa_icon': 'fa-git', 'autologin': False,
'name': 'GitLab', 'oauth2': True})
self.assertEqual(self.bitbucketAuth.getConfigDict(), {'fa_icon': 'fa-bitbucket', 'autologin': False,
'name': 'Bitbucket', 'oauth2': True})
# unit tests are not very usefull to write new oauth support
# so following is an e2e test, which opens a browser, and do the oauth
# negociation. The browser window close in the end of the test
# in order to use this tests, you need to create Github/Google ClientID (see doc on how to do it)
# point OAUTHCONF environment variable to a file with following params:
# {
# "GitHubAuth": {
# "CLIENTID": "XX
# "CLIENTSECRET": "XX"
# },
# "GoogleAuth": {
# "CLIENTID": "XX",
# "CLIENTSECRET": "XX"
# }
# "GitLabAuth": {
# "INSTANCEURI": "XX",
# "CLIENTID": "XX",
# "CLIENTSECRET": "XX"
# }
# }
class OAuth2AuthGitHubE2E(www.WwwTestMixin, unittest.TestCase):
authClass = "GitHubAuth"
def _instantiateAuth(self, cls, config):
return cls(config["CLIENTID"], config["CLIENTSECRET"])
def setUp(self):
if requests is None:
raise unittest.SkipTest("Need to install requests to test oauth2")
if "OAUTHCONF" not in os.environ:
raise unittest.SkipTest(
"Need to pass OAUTHCONF path to json file via environ to run this e2e test")
import json
config = json.load(open(os.environ['OAUTHCONF']))[self.authClass]
from buildbot.www import oauth2
self.auth = self._instantiateAuth(
getattr(oauth2, self.authClass), config)
# 5000 has to be hardcoded, has oauth clientids are bound to a fully
# classified web site
master = self.make_master(url='http://localhost:5000/', auth=self.auth)
self.auth.reconfigAuth(master, master.config)
def tearDown(self):
from twisted.internet.tcp import Server
# browsers has the bad habbit on not closing the persistent
# connections, so we need to hack them away to make trial happy
f = failure.Failure(Exception("test end"))
for reader in reactor.getReaders():
if isinstance(reader, Server):
reader.connectionLost(f)
@defer.inlineCallbacks
def test_E2E(self):
d = defer.Deferred()
import twisted
twisted.web.http._logDateTimeUsers = 1
class HomePage(Resource):
isLeaf = True
def render_GET(self, request):
info = request.getSession().user_info
reactor.callLater(0, d.callback, info)
return "<html><script>setTimeout(close,1000)</script><body>WORKED: %s</body></html>" % (info)
class MySite(Site):
def makeSession(self):
uid = self._mkuid()
session = self.sessions[uid] = self.sessionFactory(self, uid)
return session
root = Resource()
root.putChild("", HomePage())
auth = Resource()
root.putChild('auth', auth)
auth.putChild('login', self.auth.getLoginResource())
site = MySite(root)
l = reactor.listenTCP(5000, site)
def thd():
res = requests.get('http://localhost:5000/auth/login')
webbrowser.open(res.content)
threads.deferToThread(thd)
res = yield d
yield l.stopListening()
yield site.stopFactory()
self.assertIn("full_name", res)
self.assertIn("email", res)
self.assertIn("username", res)
class OAuth2AuthGoogleE2E(OAuth2AuthGitHubE2E):
authClass = "GoogleAuth"
class OAuth2AuthGitLabE2E(OAuth2AuthGitHubE2E):
authClass = "GitLabAuth"
def _instantiateAuth(self, cls, config):
return cls(config["INSTANCEURI"], config["CLIENTID"], config["CLIENTSECRET"])