aboutsummaryrefslogtreecommitdiff
path: root/gitosis/test/test_serve.py
blob: ec757a478beb5ac0642d3fcf5abd279aa77b0c88 (plain)
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
from nose.tools import eq_ as eq
from gitosis.test.util import assert_raises
 
import os
from ConfigParser import RawConfigParser
 
from gitosis import serve
from gitosis import repository
 
from gitosis.test import util
 
def test_bad_newLine():
    cfg = RawConfigParser()
    e = assert_raises(
        serve.CommandMayNotContainNewlineError,
        serve.serve,
        cfg=cfg,
        user='jdoe',
        command='ev\nil',
        )
    eq(str(e)'Command may not contain newline')
    assert isinstance(eserve.ServingError)
 
def test_bad_nospace():
    cfg = RawConfigParser()
    e = assert_raises(
        serve.UnknownCommandError,
        serve.serve,
        cfg=cfg,
        user='jdoe',
        command='git-upload-pack',
        )
    eq(str(e)'Unknown command denied')
    assert isinstance(eserve.ServingError)
 
def test_bad_command():
    cfg = RawConfigParser()
    e = assert_raises(
        serve.UnknownCommandError,
        serve.serve,
        cfg=cfg,
        user='jdoe',
        command="evil 'foo'",
        )
    eq(str(e)'Unknown command denied')
    assert isinstance(eserve.ServingError)
 
def test_bad_unsafeArguments():
    cfg = RawConfigParser()
    e = assert_raises(
        serve.UnsafeArgumentsError,
        serve.serve,
        cfg=cfg,
        user='jdoe',
        command='git-upload-pack /evil/attack',
        )
    eq(str(e)'Arguments to command look dangerous')
    assert isinstance(eserve.ServingError)
 
def test_bad_forbiddenCommand_read():
    cfg = RawConfigParser()
    e = assert_raises(
        serve.ReadAccessDenied,
        serve.serve,
        cfg=cfg,
        user='jdoe',
        command="git-upload-pack 'foo'",
        )
    eq(str(e)'Repository read access denied')
    assert isinstance(eserve.AccessDenied)
    assert isinstance(eserve.ServingError)
 
def test_bad_forbiddenCommand_write_noAccess():
    cfg = RawConfigParser()
    e = assert_raises(
        serve.ReadAccessDenied,
        serve.serve,
        cfg=cfg,
        user='jdoe',
        command="git-receive-pack 'foo'",
        )
    # error message talks about read in an effort to make it more 
    # obvious that jdoe doesn't have *even* read access 
    eq(str(e)'Repository read access denied')
    assert isinstance(eserve.AccessDenied)
    assert isinstance(eserve.ServingError)
 
def test_bad_forbiddenCommand_write_readAccess():
    cfg = RawConfigParser()
    cfg.add_section('group foo')
    cfg.set('group foo''members''jdoe')
    cfg.set('group foo''readonly''foo')
    e = assert_raises(
        serve.WriteAccessDenied,
        serve.serve,
        cfg=cfg,
        user='jdoe',
        command="git-receive-pack 'foo'",
        )
    eq(str(e)'Repository write access denied')
    assert isinstance(eserve.AccessDenied)
    assert isinstance(eserve.ServingError)
 
def test_simple_read():
    tmp = util.maketemp()
    repository.init(os.path.join(tmp'foo.git'))
    cfg = RawConfigParser()
    cfg.add_section('gitosis')
    cfg.set('gitosis''repositories'tmp)
    cfg.add_section('group foo')
    cfg.set('group foo''members''jdoe')
    cfg.set('group foo''readonly''foo')
    got = serve.serve(
        cfg=cfg,
        user='jdoe',
        command="git-upload-pack 'foo'",
        )
    eq(got"git-upload-pack '%s/foo'" % tmp)
 
def test_simple_write():
    tmp = util.maketemp()
    repository.init(os.path.join(tmp'foo.git'))
    cfg = RawConfigParser()
    cfg.add_section('gitosis')
    cfg.set('gitosis''repositories'tmp)
    cfg.add_section('group foo')
    cfg.set('group foo''members''jdoe')
    cfg.set('group foo''writable''foo')
    got = serve.serve(
        cfg=cfg,
        user='jdoe',
        command="git-receive-pack 'foo'",
        )
    eq(got"git-receive-pack '%s/foo'" % tmp)
 
def test_push_inits_if_needed():
    # a push to a non-existent repository (but where config authorizes 
    # you to do that) will create the repository on the fly 
    tmp = util.maketemp()
    cfg = RawConfigParser()
    cfg.add_section('gitosis')
    cfg.set('gitosis''repositories'tmp)
    cfg.add_section('group foo')
    cfg.set('group foo''members''jdoe')
    cfg.set('group foo''writable''foo')
    serve.serve(
        cfg=cfg,
        user='jdoe',
        command="git-receive-pack 'foo'",
        )
    eq(os.listdir(tmp)['foo.git'])
    assert os.path.isfile(os.path.join(tmp'foo.git''HEAD'))
 
def test_push_inits_if_needed_haveExtension():
    # a push to a non-existent repository (but where config authorizes 
    # you to do that) will create the repository on the fly 
    tmp = util.maketemp()
    cfg = RawConfigParser()
    cfg.add_section('gitosis')
    cfg.set('gitosis''repositories'tmp)
    cfg.add_section('group foo')
    cfg.set('group foo''members''jdoe')
    cfg.set('group foo''writable''foo')
    serve.serve(
        cfg=cfg,
        user='jdoe',
        command="git-receive-pack 'foo.git'",
        )
    eq(os.listdir(tmp)['foo.git'])
    assert os.path.isfile(os.path.join(tmp'foo.git''HEAD'))
 
def test_push_inits_if_needed_existsWithExtension():
    tmp = util.maketemp()
    os.mkdir(os.path.join(tmp'foo.git'))
    cfg = RawConfigParser()
    cfg.add_section('gitosis')
    cfg.set('gitosis''repositories'tmp)
    cfg.add_section('group foo')
    cfg.set('group foo''members''jdoe')
    cfg.set('group foo''writable''foo')
    serve.serve(
        cfg=cfg,
        user='jdoe',
        command="git-receive-pack 'foo'",
        )
    eq(os.listdir(tmp)['foo.git'])
    # it should *not* have HEAD here as we just mkdirred it and didn't 
    # create it properly, and the mock repo didn't have anything in 
    # it.. having HEAD implies serve ran git init, which is supposed 
    # to be unnecessary here 
    eq(os.listdir(os.path.join(tmp'foo.git'))[])