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
|
# Copyright (C) 2007, 2008, 2009, 2011 Canonical Ltd.
#
# This program 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
from __future__ import absolute_import
from html import escape
import json
import logging
import re
from io import BytesIO
from breezy.tests import TestCaseWithTransport
from configobj import ConfigObj
from breezy import config
from ..apps.branch import BranchWSGIApp
from ..apps.http_head import HeadMiddleware
from paste.fixture import TestApp
from paste.httpexceptions import HTTPExceptionHandler, HTTPMovedPermanently
from .fixtures import (
SampleBranch,
)
class BasicTests(TestCaseWithTransport):
def setUp(self):
TestCaseWithTransport.setUp(self)
logging.basicConfig(level=logging.ERROR)
logging.getLogger('bzr').setLevel(logging.CRITICAL)
def createBranch(self):
self.tree = self.make_branch_and_tree('.')
def setUpLoggerhead(self, **kw):
branch_app = BranchWSGIApp(self.tree.branch, '', **kw).app
return TestApp(HTTPExceptionHandler(branch_app))
def assertOkJsonResponse(self, app, env):
start, content = consume_app(app, env)
self.assertEqual('200 OK', start[0])
self.assertEqual('application/json', dict(start[1])['Content-Type'])
self.assertEqual(None, start[2])
json.loads(content.decode('UTF-8'))
def make_branch_app(self, branch, **kw):
branch_app = BranchWSGIApp(branch, friendly_name='friendly-name', **kw)
branch_app._environ = {
'wsgi.url_scheme':'',
'SERVER_NAME':'',
'SERVER_PORT':'80',
}
branch_app._url_base = ''
return branch_app
class TestWithSimpleTree(BasicTests):
def setUp(self):
BasicTests.setUp(self)
self.sample_branch_fixture = SampleBranch(self)
# XXX: This could be cleaned up more... -- mbp 2011-11-25
self.useFixture(self.sample_branch_fixture)
self.tree = self.sample_branch_fixture.tree
self.path = self.sample_branch_fixture.path
self.filecontents = self.sample_branch_fixture.filecontents
self.msg = self.sample_branch_fixture.msg
def test_public_private(self):
app = self.make_branch_app(self.tree.branch, private=True)
self.assertEqual(app.public_private_css(), 'private')
app = self.make_branch_app(self.tree.branch)
self.assertEqual(app.public_private_css(), 'public')
def test_changes(self):
app = self.setUpLoggerhead()
res = app.get('/changes')
res.mustcontain(escape(self.msg))
def test_changes_for_file(self):
app = self.setUpLoggerhead()
res = app.get('/changes?filter_path=%s' % self.path)
res.mustcontain(escape(self.msg))
def test_changes_branch_from(self):
app = self.setUpLoggerhead(served_url="lp:loggerhead")
res = app.get('/changes')
self.failUnless("To get this branch, use:" in res)
self.failUnless("lp:loggerhead" in res)
def test_changes_search(self):
app = self.setUpLoggerhead()
res = app.get('/changes', params={'q': 'foo'})
res.mustcontain('Sorry, no results found for your search.')
def test_annotate(self):
app = self.setUpLoggerhead()
res = app.get('/annotate/1/%s' % self.path, params={})
# If pygments is installed, it inserts <span class="pyg" content into
# the output, to trigger highlighting. And it specifically highlights
# the < that we are interested in seeing in the output.
# Without pygments we have a simple: 'with<htmlspecialchars'
# With it, we have
# '<span class='pyg-n'>with</span><span class='pyg-o'><</span>'
# '<span class='pyg-n'>htmlspecialchars</span>
# So we pre-filter the body, to make sure remove spans of that type.
body_no_span = re.sub(b'<span class="pyg-.">', b'', res.body)
body_no_span = body_no_span.replace(b'</span>', b'')
for line in self.filecontents.splitlines():
escaped = escape(line).encode('utf-8')
self.assertTrue(escaped in body_no_span,
"did not find %r in %r" % (escaped, body_no_span))
def test_inventory(self):
app = self.setUpLoggerhead()
res = app.get('/files')
res.mustcontain('myfilename')
res = app.get('/files/')
res.mustcontain('myfilename')
res = app.get('/files/1')
res.mustcontain('myfilename')
res = app.get('/files/1/')
res.mustcontain('myfilename')
def test_inventory_bad_rev_404(self):
app = self.setUpLoggerhead()
res = app.get('/files/200', status=404)
res = app.get('/files/invalid-revid', status=404)
def test_inventory_bad_path_404(self):
app = self.setUpLoggerhead()
res = app.get('/files/1/hooha', status=404)
def test_revision(self):
app = self.setUpLoggerhead()
res = app.get('/revision/1')
res.mustcontain(no=['anotherfile<'])
res.mustcontain('anotherfile<')
res.mustcontain('myfilename')
class TestEmptyBranch(BasicTests):
"""Test that an empty branch doesn't break"""
def setUp(self):
BasicTests.setUp(self)
self.createBranch()
def test_changes(self):
app = self.setUpLoggerhead()
res = app.get('/changes')
res.mustcontain('No revisions!')
def test_inventory(self):
app = self.setUpLoggerhead()
res = app.get('/files')
res.mustcontain('No revisions!')
class TestHiddenBranch(BasicTests):
"""
Test that hidden branches aren't shown
FIXME: not tested that it doesn't show up on listings
"""
def setUp(self):
BasicTests.setUp(self)
self.createBranch()
try:
locations = config.locations_config_filename()
except AttributeError:
from breezy import bedding
locations = bedding.locations_config_path()
ensure_config_dir_exists = bedding.ensure_config_dir_exists
else:
ensure_config_dir_exists = config.ensure_config_dir_exists
ensure_config_dir_exists()
with open(locations, 'w') as f:
f.write('[%s]\nhttp_serve = False' % (
self.tree.branch.base,))
def test_no_access(self):
app = self.setUpLoggerhead()
res = app.get('/changes', status=404)
class TestControllerRedirects(BasicTests):
"""
Test that a file under /files redirects to /view,
and a directory under /view redirects to /files.
"""
def setUp(self):
BasicTests.setUp(self)
self.createBranch()
self.build_tree(('file', 'folder/', 'folder/file'))
self.tree.smart_add([])
self.tree.commit('')
def test_view_folder(self):
app = TestApp(BranchWSGIApp(self.tree.branch, '').app)
e = self.assertRaises(HTTPMovedPermanently, app.get, '/view/head:/folder')
self.assertEqual(e.location(), '/files/head:/folder')
def test_files_file(self):
app = TestApp(BranchWSGIApp(self.tree.branch, '').app)
e = self.assertRaises(HTTPMovedPermanently, app.get, '/files/head:/folder/file')
self.assertEqual(e.location(), '/view/head:/folder/file')
e = self.assertRaises(HTTPMovedPermanently, app.get, '/files/head:/file')
self.assertEqual(e.location(), '/view/head:/file')
class TestHeadMiddleware(BasicTests):
def setUp(self):
BasicTests.setUp(self)
self.createBranch()
self.msg = 'trivial commit message'
self.revid = self.tree.commit(message=self.msg)
def setUpLoggerhead(self, **kw):
branch_app = BranchWSGIApp(self.tree.branch, '', **kw).app
return TestApp(HTTPExceptionHandler(HeadMiddleware(branch_app)))
def test_get(self):
app = self.setUpLoggerhead()
res = app.get('/changes')
res.mustcontain(self.msg)
self.assertEqual('text/html', res.header('Content-Type'))
def test_head(self):
app = self.setUpLoggerhead()
res = app.get('/changes', extra_environ={'REQUEST_METHOD': 'HEAD'})
self.assertEqual('text/html', res.header('Content-Type'))
self.assertEqualDiff(b'', res.body)
def consume_app(app, env):
body = BytesIO()
start = []
def start_response(status, headers, exc_info=None):
start.append((status, headers, exc_info))
return body.write
extra_content = list(app(env, start_response))
body.writelines(extra_content)
return start[0], body.getvalue()
#class TestGlobalConfig(BasicTests):
# """
# Test that global config settings are respected
# """
# def setUp(self):
# BasicTests.setUp(self)
# self.createBranch()
# config.GlobalConfig().set_user_option('http_version', 'True')
# def test_setting_respected(self):
#FIXME: Figure out how to test this properly
# app = self.setUpLoggerhead()
# res = app.get('/changes', status=200)
|