Skip to content

Commit

Permalink
move SessionConfigResource to its own module, since it's not auth-rel…
Browse files Browse the repository at this point in the history
…ated
  • Loading branch information
djmitche committed Apr 13, 2014
1 parent a49a011 commit e84445a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 67 deletions.
30 changes: 2 additions & 28 deletions master/buildbot/test/unit/test_www_auth.py
Expand Up @@ -18,39 +18,13 @@

from buildbot.test.util import www
from buildbot.www import auth
from buildbot.www import config
from twisted.internet import defer
from twisted.trial import unittest
from twisted.web._auth.wrapper import UnauthorizedResource
from twisted.web.resource import IResource


class SessionConfigResource(www.WwwTestMixin, unittest.TestCase):

@defer.inlineCallbacks
def test_render(self):
_auth = auth.NoAuth()
_auth.maybeAutoLogin = mock.Mock()
master = self.make_master(url='h:/a/b/', auth=_auth)
rsrc = auth.SessionConfigResource(master)
rsrc.reconfigResource(master.config)

res = yield self.render_resource(rsrc, '/')
_auth.maybeAutoLogin.assert_called()
exp = 'this.config = {"url": "h:/a/b/", "user": {"anonymous": true}, "auth": {"name": "NoAuth"}, "port": null}'
self.assertEqual(res, exp)

master.session.user_info = dict(name="me", email="me@me.org")
res = yield self.render_resource(rsrc, '/')
exp = 'this.config = {"url": "h:/a/b/", "user": {"email": "me@me.org", "name": "me"}, "auth": {"name": "NoAuth"}, "port": null}'
self.assertEqual(res, exp)

master = self.make_master(url='h:/a/c/', auth=_auth)
rsrc.reconfigResource(master.config)
res = yield self.render_resource(rsrc, '/')
exp = 'this.config = {"url": "h:/a/c/", "user": {"anonymous": true}, "auth": {"name": "NoAuth"}, "port": null}'
self.assertEqual(res, exp)


class LoginResource(www.WwwTestMixin, unittest.TestCase):

@defer.inlineCallbacks
Expand Down Expand Up @@ -115,7 +89,7 @@ class RemoteUserAuth(www.WwwTestMixin, unittest.TestCase):
def test_RemoteUserAuth(self):
_auth = auth.RemoteUserAuth()
master = self.make_master(url='h:/a/b/', auth=_auth)
rsrc = auth.SessionConfigResource(master)
rsrc = config.SessionConfigResource(master)
rsrc.reconfigResource(master.config)

res = yield self.render_resource(rsrc, '/')
Expand Down
49 changes: 49 additions & 0 deletions master/buildbot/test/unit/test_www_config.py
@@ -0,0 +1,49 @@
# 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 mock

from buildbot.test.util import www
from buildbot.www import auth
from buildbot.www import config
from twisted.internet import defer
from twisted.trial import unittest


class SessionConfigResource(www.WwwTestMixin, unittest.TestCase):

@defer.inlineCallbacks
def test_render(self):
_auth = auth.NoAuth()
_auth.maybeAutoLogin = mock.Mock()
master = self.make_master(url='h:/a/b/', auth=_auth)
rsrc = config.SessionConfigResource(master)
rsrc.reconfigResource(master.config)

res = yield self.render_resource(rsrc, '/')
_auth.maybeAutoLogin.assert_called()
exp = 'this.config = {"url": "h:/a/b/", "user": {"anonymous": true}, "auth": {"name": "NoAuth"}, "port": null}'
self.assertEqual(res, exp)

master.session.user_info = dict(name="me", email="me@me.org")
res = yield self.render_resource(rsrc, '/')
exp = 'this.config = {"url": "h:/a/b/", "user": {"email": "me@me.org", "name": "me"}, "auth": {"name": "NoAuth"}, "port": null}'
self.assertEqual(res, exp)

master = self.make_master(url='h:/a/c/', auth=_auth)
rsrc.reconfigResource(master.config)
res = yield self.render_resource(rsrc, '/')
exp = 'this.config = {"url": "h:/a/c/", "user": {"anonymous": true}, "auth": {"name": "NoAuth"}, "port": null}'
self.assertEqual(res, exp)
38 changes: 0 additions & 38 deletions master/buildbot/www/auth.py
Expand Up @@ -17,9 +17,7 @@

from zope.interface import implements

from buildbot.interfaces import IConfigured
from buildbot.util import config
from buildbot.util import json
from buildbot.www import resource

from twisted.cred.checkers import FilePasswordDB
Expand Down Expand Up @@ -159,42 +157,6 @@ def __init__(self, users, **kwargs):
**kwargs)


class SessionConfigResource(resource.Resource):
# enable reconfigResource calls
needsReconfig = True

def reconfigResource(self, new_config):
self.config = new_config.www

def render_GET(self, request):
return self.asyncRenderHelper(request, self.renderConfig)

@defer.inlineCallbacks
def renderConfig(self, request):
config = {}
request.setHeader("content-type", 'text/javascript')
request.setHeader("Cache-Control", "public;max-age=0")

session = request.getSession()
try:
yield self.config['auth'].maybeAutoLogin(request)
except Error, e:
config["on_load_warning"] = e.message

if hasattr(session, "user_info"):
config.update({"user": session.user_info})
else:
config.update({"user": {"anonymous": True}})
config.update(self.config)

def toJson(obj):
obj = IConfigured(obj).getConfigDict()
if isinstance(obj, dict):
return obj
return repr(obj) + " not yet IConfigured"
defer.returnValue("this.config = " + json.dumps(config, default=toJson))


class LoginResource(resource.Resource):
# enable reconfigResource calls
needsReconfig = True
Expand Down
57 changes: 57 additions & 0 deletions master/buildbot/www/config.py
@@ -0,0 +1,57 @@
# 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

from buildbot.interfaces import IConfigured
from buildbot.util import json
from buildbot.www import resource
from twisted.internet import defer
from twisted.web.error import Error


class SessionConfigResource(resource.Resource):
# enable reconfigResource calls
needsReconfig = True

def reconfigResource(self, new_config):
self.config = new_config.www

def render_GET(self, request):
return self.asyncRenderHelper(request, self.renderConfig)

@defer.inlineCallbacks
def renderConfig(self, request):
config = {}
request.setHeader("content-type", 'text/javascript')
request.setHeader("Cache-Control", "public;max-age=0")

session = request.getSession()
try:
yield self.config['auth'].maybeAutoLogin(request)
except Error, e:
config["on_load_warning"] = e.message

if hasattr(session, "user_info"):
config.update({"user": session.user_info})
else:
config.update({"user": {"anonymous": True}})
config.update(self.config)

def toJson(obj):
obj = IConfigured(obj).getConfigDict()
if isinstance(obj, dict):
return obj
return repr(obj) + " not yet IConfigured"
defer.returnValue("this.config = " +
json.dumps(config, default=toJson))
3 changes: 2 additions & 1 deletion master/buildbot/www/service.py
Expand Up @@ -19,6 +19,7 @@
from buildbot.util import service
from buildbot.www import auth
from buildbot.www import avatar
from buildbot.www import config as wwwconfig
from buildbot.www import rest
from buildbot.www import sse
from buildbot.www import ws
Expand Down Expand Up @@ -133,7 +134,7 @@ def setupSite(self, new_config):
root.putChild(key, self.apps[key].resource)

# /config.js
root.putChild('config.js', auth.SessionConfigResource(self.master))
root.putChild('config.js', wwwconfig.SessionConfigResource(self.master))

# /login
root.putChild('login', new_config.www['auth'].getLoginResource(self.master))
Expand Down

0 comments on commit e84445a

Please sign in to comment.