[d61857]: / ForgeGit / forgegit / tests / functional / test_auth.py  Maximize  Restore  History

Download this file

113 lines (95 with data), 4.2 kB

  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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import mock
import tg
from allura import model as M
from allura.tests import TestController
from allura.tests.decorators import with_tool
from forgegit.tests import with_git
@mock.patch.dict(tg.config, {'ip_address_header': 'X-Forwarded-For'})
class TestGitUserPermissions(TestController):
allow = dict(allow_read=True, allow_write=True, allow_create=True)
read = dict(allow_read=True, allow_write=False, allow_create=False)
disallow = dict(allow_read=False, allow_write=False, allow_create=False)
def test_unknown_project(self):
self._check_repo('/git/foo/bar', status=404)
def test_unknown_app(self):
r = self._check_repo('/git/test/bar')
assert r == self.disallow, r
@with_git
def test_repo_write(self):
r = self._check_repo('/git/test/src-git.git')
assert r == self.allow, r
r = self._check_repo('/git/test/src-git')
assert r == self.allow, r
@with_git
def test_subdir(self):
r = self._check_repo('/git/test/src-git.git/foo')
assert r == self.allow, r
r = self._check_repo('/git/test/src-git/foo')
assert r == self.allow, r
@with_git
def test_neighborhood(self):
r = self._check_repo('/git/test.p/src-git.git')
assert r == self.allow, r
@with_git
def test_repo_read(self):
r = self._check_repo(
'/git/test.p/src-git.git',
username='test-user')
assert r == self.read, r
def test_unknown_user(self):
self._check_repo(
'/git/test.p/src-git.git',
username='test-usera',
status=404)
@with_git
def test_disabled_or_pending_user(self):
for flag in ('disabled', 'pending'):
M.User.query.update({'username': 'test-admin'}, {'$set': {flag: True}})
self._check_repo('/git/test/src-git.git', status=404)
self.app.get('/auth/repo_permissions', params=dict(username='test-admin'), status=404)
M.User.query.update({'username': 'test-admin'}, {'$set': {flag: False}})
assert self._check_repo('/git/test/src-git.git') == self.allow
@with_tool('test', 'Git', 'src.c++.git', 'Git', type='git')
def test_dot_and_plus(self):
r = self._check_repo('/git/test.p/src.c++.git')
assert r == self.allow, r
@with_git
def test_ip_address_checks(self):
path = '/git/test/src-git.git'
with mock.patch.dict(tg.config, {'ip_address_header': ''}):
self._check_repo(path, status=403)
self._check_repo(path, headers={'X-Forwarded-For': '8.8.8.8'}, status=403)
self._check_repo(path, headers={'X-Forwarded-For': '10.0.0.1, 8.8.8.8'}, status=403)
def _check_repo(self, path, username='test-admin', **kw):
url = '/auth/repo_permissions'
r = self.app.get(url, params=dict(
repo_path=path,
username=username), **kw)
try:
return r.json
except Exception:
return r
@with_git
def test_list_repos(self):
r = self.app.get('/auth/repo_permissions',
params=dict(username='test-admin'), status=200)
assert json.loads(r.text) == {"allow_write": [
'/git/test/src-git',
]}