1
# Copyright (C) 2007, 2008, 2009, 2011 Canonical Ltd.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
4
from bzrlib.tests import TestCaseWithTransport
24
from bzrlib.util.configobj.configobj import ConfigObj
26
from configobj import ConfigObj
27
from bzrlib import config
5
from bzrlib.util.configobj.configobj import ConfigObj
29
7
from loggerhead.apps.branch import BranchWSGIApp
30
from loggerhead.apps.http_head import HeadMiddleware
31
8
from paste.fixture import TestApp
32
from paste.httpexceptions import HTTPExceptionHandler, HTTPMovedPermanently
9
from paste.httpexceptions import HTTPExceptionHandler
13
def test_config_root():
14
from loggerhead.apps.config import Root
16
app = TestApp(HTTPExceptionHandler(Root(config)))
18
res.mustcontain('loggerhead branches')
36
21
class BasicTests(TestCaseWithTransport):
57
42
self.filecontents = ('some\nmultiline\ndata\n'
58
43
'with<htmlspecialchars\n')
59
filenames = ['myfilename', 'anotherfile<']
60
44
self.build_tree_contents(
61
(filename, self.filecontents) for filename in filenames)
62
for filename in filenames:
63
self.tree.add(filename, '%s-id' % filename)
45
[('myfilename', self.filecontents)])
46
self.tree.add('myfilename')
64
47
self.fileid = self.tree.path2id('myfilename')
65
48
self.msg = 'a very exciting commit message <'
66
49
self.revid = self.tree.commit(message=self.msg)
70
53
res = app.get('/changes')
71
54
res.mustcontain(cgi.escape(self.msg))
73
def test_changes_for_file(self):
74
app = self.setUpLoggerhead()
75
res = app.get('/changes?filter_file_id=myfilename-id')
76
res.mustcontain(cgi.escape(self.msg))
78
56
def test_changes_branch_from(self):
79
57
app = self.setUpLoggerhead(served_url="lp:loggerhead")
80
58
res = app.get('/changes')
92
70
def test_annotate(self):
93
71
app = self.setUpLoggerhead()
94
72
res = app.get('/annotate', params={'file_id': self.fileid})
95
# If pygments is installed, it inserts <span class="pyg" content into
96
# the output, to trigger highlighting. And it specifically highlights
97
# the < that we are interested in seeing in the output.
98
# Without pygments we have a simple: 'with<htmlspecialchars'
100
# '<span class='pyg-n'>with</span><span class='pyg-o'><</span>'
101
# '<span class='pyg-n'>htmlspecialchars</span>
102
# So we pre-filter the body, to make sure remove spans of that type.
103
body_no_span = re.sub(r'<span class="pyg-.">', '', res.body)
104
body_no_span = body_no_span.replace('</span>', '')
105
73
for line in self.filecontents.splitlines():
106
escaped = cgi.escape(line)
107
self.assertTrue(escaped in body_no_span,
108
"did not find %r in %r" % (escaped, body_no_span))
74
res.mustcontain(cgi.escape(line))
110
76
def test_inventory(self):
111
77
app = self.setUpLoggerhead()
155
118
res = app.get('/files')
156
119
res.mustcontain('No revisions!')
159
class TestHiddenBranch(BasicTests):
161
Test that hidden branches aren't shown
162
FIXME: not tested that it doesn't show up on listings
166
BasicTests.setUp(self)
168
locations = config.locations_config_filename()
169
config.ensure_config_dir_exists()
170
open(locations, 'wb').write('[%s]\nhttp_serve = False'
171
% (self.tree.branch.base,))
173
def test_no_access(self):
174
app = self.setUpLoggerhead()
175
res = app.get('/changes', status=404)
178
class TestControllerRedirects(BasicTests):
180
Test that a file under /files redirects to /view,
181
and a directory under /view redirects to /files.
185
BasicTests.setUp(self)
187
self.build_tree(('file', 'folder/', 'folder/file'))
188
self.tree.smart_add([])
191
def test_view_folder(self):
192
app = TestApp(BranchWSGIApp(self.tree.branch, '').app)
194
e = self.assertRaises(HTTPMovedPermanently, app.get, '/view/head:/folder')
195
self.assertEqual(e.location(), '/files/head:/folder')
197
def test_files_file(self):
198
app = TestApp(BranchWSGIApp(self.tree.branch, '').app)
200
e = self.assertRaises(HTTPMovedPermanently, app.get, '/files/head:/folder/file')
201
self.assertEqual(e.location(), '/view/head:/folder/file')
202
e = self.assertRaises(HTTPMovedPermanently, app.get, '/files/head:/file')
203
self.assertEqual(e.location(), '/view/head:/file')
206
class TestHeadMiddleware(BasicTests):
209
BasicTests.setUp(self)
211
self.msg = 'trivial commit message'
212
self.revid = self.tree.commit(message=self.msg)
214
def setUpLoggerhead(self, **kw):
215
branch_app = BranchWSGIApp(self.tree.branch, '', **kw).app
216
return TestApp(HTTPExceptionHandler(HeadMiddleware(branch_app)))
219
app = self.setUpLoggerhead()
220
res = app.get('/changes')
221
res.mustcontain(self.msg)
222
self.assertEqual('text/html', res.header('Content-Type'))
225
app = self.setUpLoggerhead()
226
res = app.get('/changes', extra_environ={'REQUEST_METHOD': 'HEAD'})
227
self.assertEqual('text/html', res.header('Content-Type'))
228
self.assertEqualDiff('', res.body)
231
#class TestGlobalConfig(BasicTests):
233
# Test that global config settings are respected
237
# BasicTests.setUp(self)
238
# self.createBranch()
239
# config.GlobalConfig().set_user_option('http_version', 'True')
241
# def test_setting_respected(self):
242
#FIXME: Figure out how to test this properly
243
# app = self.setUpLoggerhead()
244
# res = app.get('/changes', status=200)