~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_simple.py

  • Committer: Jelmer Vernooij
  • Date: 2011-02-10 17:21:19 UTC
  • mfrom: (423.1.1 drop-pre-1.16)
  • Revision ID: jelmer@samba.org-20110210172119-6xa7yiq773p5fqcb
Merge dropping of workarounds for pre-1.16 versions of Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from configobj import ConfigObj
 
1
# Copyright (C) 2008, 2009 Canonical Ltd.
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
#
 
17
 
2
18
import cgi
3
 
import unittest
4
 
import os
5
 
import tempfile
6
 
import shutil
7
19
import logging
8
20
 
9
 
import cherrypy
10
 
from turbogears import testutil
11
 
 
12
 
import bzrlib
13
 
 
14
 
from loggerhead.controllers import Root
15
 
 
16
 
def test_simple():
 
21
from bzrlib.tests import TestCaseWithTransport
 
22
from bzrlib.util.configobj.configobj import ConfigObj
 
23
from bzrlib import config
 
24
 
 
25
from loggerhead.apps.branch import BranchWSGIApp
 
26
from paste.fixture import TestApp
 
27
from paste.httpexceptions import HTTPExceptionHandler
 
28
 
 
29
 
 
30
 
 
31
def test_config_root():
 
32
    from loggerhead.apps.config import Root
17
33
    config = ConfigObj()
18
 
    r = Root(config)
19
 
    cherrypy.root = r
20
 
    testutil.create_request('/')
21
 
    assert 'loggerhead branches' in cherrypy.response.body[0]
22
 
 
23
 
 
24
 
class BasicTests(object):
25
 
 
26
 
 
27
 
    # there are so i can run it with py.test and take advantage of the
28
 
    # error reporting...
29
 
    def setup_method(self, meth):
30
 
        self.setUp()
31
 
 
32
 
    def teardown_method(self, meth):
33
 
        self.tearDown()
 
34
    app = TestApp(HTTPExceptionHandler(Root(config)))
 
35
    res = app.get('/')
 
36
    res.mustcontain('loggerhead branches')
 
37
 
 
38
 
 
39
class BasicTests(TestCaseWithTransport):
34
40
 
35
41
    def setUp(self):
36
 
        #logging.basicConfig(level=logging.DEBUG)
37
 
        self.bzrbranch = None
38
 
        self.old_bzrhome = None
 
42
        TestCaseWithTransport.setUp(self)
 
43
        logging.basicConfig(level=logging.ERROR)
 
44
        logging.getLogger('bzr').setLevel(logging.CRITICAL)
39
45
 
40
46
    def createBranch(self):
41
 
        self.old_bzrhome = bzrlib.osutils.set_or_unset_env('BZR_HOME', '')
42
 
        self.bzrbranch = tempfile.mkdtemp()
43
 
        self.branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(
44
 
            self.bzrbranch, force_new_tree=True)
45
 
        self.tree = self.branch.bzrdir.open_workingtree()
46
 
 
47
 
    config_template = """
48
 
    [project]
49
 
        [[branch]]
50
 
            branch_name = 'branch'
51
 
            folder = '%(branch)s'
52
 
    """
53
 
 
54
 
    def setUpLoggerhead(self):
55
 
        ini = self.config_template%dict(branch=self.bzrbranch)
56
 
 
57
 
        config = ConfigObj(ini.splitlines())
58
 
        cherrypy.root = Root(config)
59
 
 
60
 
    def tearDown(self):
61
 
        if self.bzrbranch is not None:
62
 
            shutil.rmtree(self.bzrbranch)
63
 
        bzrlib.osutils.set_or_unset_env('BZR_HOME', self.old_bzrhome)
 
47
        self.tree = self.make_branch_and_tree('.')
 
48
 
 
49
    def setUpLoggerhead(self, **kw):
 
50
        branch_app = BranchWSGIApp(self.tree.branch, '', **kw).app
 
51
        return TestApp(HTTPExceptionHandler(branch_app))
 
52
 
64
53
 
65
54
class TestWithSimpleTree(BasicTests):
66
55
 
68
57
        BasicTests.setUp(self)
69
58
        self.createBranch()
70
59
 
71
 
        f = open(os.path.join(self.bzrbranch, 'myfilename'), 'w')
72
60
        self.filecontents = ('some\nmultiline\ndata\n'
73
61
                             'with<htmlspecialchars\n')
74
 
        try:
75
 
            f.write(self.filecontents)
76
 
        finally:
77
 
            f.close()
 
62
        self.build_tree_contents(
 
63
            [('myfilename', self.filecontents)])
78
64
        self.tree.add('myfilename')
79
65
        self.fileid = self.tree.path2id('myfilename')
80
66
        self.msg = 'a very exciting commit message <'
81
67
        self.revid = self.tree.commit(message=self.msg)
82
68
 
83
 
        self.setUpLoggerhead()
84
 
 
85
 
    def test_index(self):
86
 
        testutil.create_request('/')
87
 
        link = '<a href="/project/branch">branch</a>'
88
 
        assert link in cherrypy.response.body[0]
89
 
 
90
69
    def test_changes(self):
91
 
        testutil.create_request('/project/branch/changes')
92
 
        assert cgi.escape(self.msg) in cherrypy.response.body[0]
 
70
        app = self.setUpLoggerhead()
 
71
        res = app.get('/changes')
 
72
        res.mustcontain(cgi.escape(self.msg))
 
73
 
 
74
    def test_changes_branch_from(self):
 
75
        app = self.setUpLoggerhead(served_url="lp:loggerhead")
 
76
        res = app.get('/changes')
 
77
        self.failUnless("To get this branch, use:" in res)
 
78
        self.failUnless("lp:loggerhead" in res)
 
79
        app = self.setUpLoggerhead(served_url=None)
 
80
        res = app.get('/changes')
 
81
        self.failIf("To get this branch, use:" in res)
93
82
 
94
83
    def test_changes_search(self):
95
 
        testutil.create_request('/project/branch/changes?q=foo')
96
 
        assert 'Sorry, no results found for your search.' in cherrypy.response.body[0]
 
84
        app = self.setUpLoggerhead()
 
85
        res = app.get('/changes', params={'q': 'foo'})
 
86
        res.mustcontain('Sorry, no results found for your search.')
97
87
 
98
88
    def test_annotate(self):
99
 
        testutil.create_request('/project/branch/annotate?'
100
 
                                + 'file_id='+self.fileid)
 
89
        app = self.setUpLoggerhead()
 
90
        res = app.get('/annotate', params={'file_id': self.fileid})
101
91
        for line in self.filecontents.splitlines():
102
 
            assert cgi.escape(line) in cherrypy.response.body[0]
 
92
            res.mustcontain(cgi.escape(line))
103
93
 
104
94
    def test_inventory(self):
105
 
        testutil.create_request('/project/branch/files')
106
 
        assert 'myfilename' in cherrypy.response.body[0]
 
95
        app = self.setUpLoggerhead()
 
96
        res = app.get('/files')
 
97
        res.mustcontain('myfilename')
 
98
        res = app.get('/files/')
 
99
        res.mustcontain('myfilename')
 
100
        res = app.get('/files/1')
 
101
        res.mustcontain('myfilename')
 
102
        res = app.get('/files/1/')
 
103
        res.mustcontain('myfilename')
 
104
        res = app.get('/files/1/?file_id=' + self.tree.path2id(''))
 
105
        res.mustcontain('myfilename')
 
106
 
 
107
    def test_inventory_bad_rev_404(self):
 
108
        app = self.setUpLoggerhead()
 
109
        res = app.get('/files/200', status=404)
 
110
        res = app.get('/files/invalid-revid', status=404)
 
111
 
 
112
    def test_inventory_bad_path_404(self):
 
113
        app = self.setUpLoggerhead()
 
114
        res = app.get('/files/1/hooha', status=404)
 
115
        res = app.get('/files/1?file_id=dssadsada', status=404)
107
116
 
108
117
    def test_revision(self):
109
 
        testutil.create_request('/project/branch/revision/' + self.revid)
110
 
        assert 'myfilename' in cherrypy.response.body[0]
 
118
        app = self.setUpLoggerhead()
 
119
        res = app.get('/revision/1')
 
120
        res.mustcontain('myfilename')
111
121
 
112
 
class TestWithSimpleTreeAndCache(TestWithSimpleTree):
113
 
    config_template = """
114
 
    testing = True
115
 
    [project]
116
 
        [[branch]]
117
 
            branch_name = 'branch'
118
 
            folder = '%(branch)s'
119
 
            cachepath = '%(branch)s/cache'
120
 
    """
121
122
 
122
123
class TestEmptyBranch(BasicTests):
 
124
    """Test that an empty branch doesn't break"""
123
125
 
124
126
    def setUp(self):
125
127
        BasicTests.setUp(self)
126
128
        self.createBranch()
127
 
        self.setUpLoggerhead()
128
 
 
129
 
    def test_index(self):
130
 
        testutil.create_request('/')
131
 
        link = '<a href="/project/branch">branch</a>'
132
 
        assert link in cherrypy.response.body[0]
133
129
 
134
130
    def test_changes(self):
135
 
        testutil.create_request('/project/branch/changes')
136
 
        assert 'No revisions!' in cherrypy.response.body[0]
137
 
 
138
 
class TestEmptyBranchWithCache(TestEmptyBranch):
139
 
    config_template = """
140
 
    testing = True
141
 
    [project]
142
 
        [[branch]]
143
 
            branch_name = 'branch'
144
 
            folder = '%(branch)s'
145
 
            cachepath = '%(branch)s/cache'
146
 
    """
147
 
 
 
131
        app = self.setUpLoggerhead()
 
132
        res = app.get('/changes')
 
133
        res.mustcontain('No revisions!')
 
134
 
 
135
    def test_inventory(self):
 
136
        app = self.setUpLoggerhead()
 
137
        res = app.get('/files')
 
138
        res.mustcontain('No revisions!')
 
139
 
 
140
 
 
141
class TestHiddenBranch(BasicTests):
 
142
    """
 
143
    Test that hidden branches aren't shown
 
144
    FIXME: not tested that it doesn't show up on listings
 
145
    """
 
146
 
 
147
    def setUp(self):
 
148
        BasicTests.setUp(self)
 
149
        self.createBranch()
 
150
        locations = config.locations_config_filename()
 
151
        config.ensure_config_dir_exists()
 
152
        open(locations, 'wb').write('[%s]\nhttp_serve = False'
 
153
                                    % (self.tree.branch.base,))
 
154
 
 
155
    def test_no_access(self):
 
156
        app = self.setUpLoggerhead()
 
157
        res = app.get('/changes', status=404)
 
158
 
 
159
 
 
160
#class TestGlobalConfig(BasicTests):
 
161
#    """
 
162
#    Test that global config settings are respected
 
163
#    """
 
164
 
 
165
#    def setUp(self):
 
166
#        BasicTests.setUp(self)
 
167
#        self.createBranch()
 
168
#        config.GlobalConfig().set_user_option('http_version', 'True')
 
169
 
 
170
#    def test_setting_respected(self):
 
171
        #FIXME: Figure out how to test this properly
 
172
#        app = self.setUpLoggerhead()
 
173
#        res = app.get('/changes', status=200)