~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_simple.py

  • Committer: Martin Albisetti
  • Date: 2008-09-10 22:53:39 UTC
  • mfrom: (219.1.3 reloader)
  • Revision ID: argentina@gmail.com-20080910225339-p987y5hgtxrq8p5c
Add --reload option to restart LH automatically when developing. (Guillermo Gonzalez)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
18
1
import cgi
 
2
import os
 
3
import tempfile
 
4
import shutil
19
5
import logging
20
6
 
21
 
from bzrlib.tests import TestCaseWithTransport
22
 
from bzrlib.util.configobj.configobj import ConfigObj
23
 
from bzrlib import config
 
7
import bzrlib.bzrdir
 
8
import bzrlib.osutils
 
9
from configobj import ConfigObj
24
10
 
25
11
from loggerhead.apps.branch import BranchWSGIApp
26
12
from paste.fixture import TestApp
27
 
from paste.httpexceptions import HTTPExceptionHandler
28
 
 
29
13
 
30
14
 
31
15
def test_config_root():
32
16
    from loggerhead.apps.config import Root
33
17
    config = ConfigObj()
34
 
    app = TestApp(HTTPExceptionHandler(Root(config)))
 
18
    app = TestApp(Root(config))
35
19
    res = app.get('/')
36
20
    res.mustcontain('loggerhead branches')
37
21
 
38
22
 
39
 
class BasicTests(TestCaseWithTransport):
 
23
class BasicTests(object):
 
24
 
 
25
    # setup_method and teardown_method are so i can run the tests with
 
26
    # py.test and take advantage of the error reporting.
 
27
    def setup_method(self, meth):
 
28
        self.setUp()
 
29
 
 
30
    def teardown_method(self, meth):
 
31
        self.tearDown()
40
32
 
41
33
    def setUp(self):
42
 
        TestCaseWithTransport.setUp(self)
43
 
        logging.basicConfig(level=logging.ERROR)
44
 
        logging.getLogger('bzr').setLevel(logging.CRITICAL)
 
34
        logging.basicConfig(level=logging.DEBUG)
 
35
        self.bzrbranch = None
 
36
        self.old_bzrhome = None
45
37
 
46
38
    def createBranch(self):
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))
 
39
        self.old_bzrhome = bzrlib.osutils.set_or_unset_env('BZR_HOME', '')
 
40
        self.bzrbranch = tempfile.mkdtemp()
 
41
        self.branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(
 
42
            self.bzrbranch, force_new_tree=True)
 
43
        self.tree = self.branch.bzrdir.open_workingtree()
 
44
 
 
45
    config_template = """
 
46
    [project]
 
47
        [[branch]]
 
48
            branch_name = 'branch'
 
49
            folder = '%(branch)s'
 
50
    """
 
51
 
 
52
    def setUpLoggerhead(self):
 
53
        app = TestApp(BranchWSGIApp(self.branch).app)
 
54
        return app
 
55
 
 
56
    def tearDown(self):
 
57
        if self.bzrbranch is not None:
 
58
            shutil.rmtree(self.bzrbranch)
 
59
        bzrlib.osutils.set_or_unset_env('BZR_HOME', self.old_bzrhome)
52
60
 
53
61
 
54
62
class TestWithSimpleTree(BasicTests):
57
65
        BasicTests.setUp(self)
58
66
        self.createBranch()
59
67
 
 
68
        f = open(os.path.join(self.bzrbranch, 'myfilename'), 'w')
60
69
        self.filecontents = ('some\nmultiline\ndata\n'
61
70
                             'with<htmlspecialchars\n')
62
 
        self.build_tree_contents(
63
 
            [('myfilename', self.filecontents)])
 
71
        try:
 
72
            f.write(self.filecontents)
 
73
        finally:
 
74
            f.close()
64
75
        self.tree.add('myfilename')
65
76
        self.fileid = self.tree.path2id('myfilename')
66
77
        self.msg = 'a very exciting commit message <'
67
78
        self.revid = self.tree.commit(message=self.msg)
68
79
 
 
80
 
69
81
    def test_changes(self):
70
82
        app = self.setUpLoggerhead()
71
83
        res = app.get('/changes')
72
84
        res.mustcontain(cgi.escape(self.msg))
73
85
 
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)
82
 
 
83
86
    def test_changes_search(self):
84
87
        app = self.setUpLoggerhead()
85
88
        res = app.get('/changes', params={'q': 'foo'})
87
90
 
88
91
    def test_annotate(self):
89
92
        app = self.setUpLoggerhead()
90
 
        res = app.get('/annotate', params={'file_id': self.fileid})
 
93
        res = app.get('/annotate', params={'file_id':self.fileid})
91
94
        for line in self.filecontents.splitlines():
92
95
            res.mustcontain(cgi.escape(line))
93
96
 
95
98
        app = self.setUpLoggerhead()
96
99
        res = app.get('/files')
97
100
        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)
116
101
 
117
102
    def test_revision(self):
118
103
        app = self.setUpLoggerhead()
121
106
 
122
107
 
123
108
class TestEmptyBranch(BasicTests):
124
 
    """Test that an empty branch doesn't break"""
125
109
 
126
110
    def setUp(self):
127
111
        BasicTests.setUp(self)
132
116
        res = app.get('/changes')
133
117
        res.mustcontain('No revisions!')
134
118
 
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)