Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move SessionConfigResource to its own module, since it's not auth-rel…
…ated
- Loading branch information
Showing
5 changed files
with
110 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters