Files
@ b0bd3332b715
Branch filter:
Location: kallithea/kallithea/tests/other/test_validators.py
b0bd3332b715
8.6 KiB
text/x-python
merge default to stable for 0.5.0
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 | # -*- coding: utf-8 -*-
import formencode
import pytest
from kallithea.model import validators as v
from kallithea.model.meta import Session
from kallithea.model.repo_group import RepoGroupModel
from kallithea.model.user_group import UserGroupModel
from kallithea.tests.base import *
from kallithea.tests.fixture import Fixture
fixture = Fixture()
@pytest.mark.usefixtures("test_context_fixture") # apply fixture for all test methods
class TestRepoGroups(TestController):
def teardown_method(self, method):
Session.remove()
def test_Message_extractor(self):
validator = v.ValidUsername()
with pytest.raises(formencode.Invalid):
validator.to_python('default')
class StateObj(object):
pass
with pytest.raises(formencode.Invalid):
validator.to_python('default', StateObj)
def test_ValidUsername(self):
validator = v.ValidUsername()
with pytest.raises(formencode.Invalid):
validator.to_python('default')
with pytest.raises(formencode.Invalid):
validator.to_python('new_user')
with pytest.raises(formencode.Invalid):
validator.to_python('.,')
with pytest.raises(formencode.Invalid):
validator.to_python(TEST_USER_ADMIN_LOGIN)
assert 'test' == validator.to_python('test')
validator = v.ValidUsername(edit=True, old_data={'user_id': 1})
def test_ValidRepoUser(self):
validator = v.ValidRepoUser()
with pytest.raises(formencode.Invalid):
validator.to_python('nouser')
assert TEST_USER_ADMIN_LOGIN == validator.to_python(TEST_USER_ADMIN_LOGIN)
def test_ValidUserGroup(self):
validator = v.ValidUserGroup()
with pytest.raises(formencode.Invalid):
validator.to_python(u'default')
with pytest.raises(formencode.Invalid):
validator.to_python(u'.,')
gr = fixture.create_user_group(u'test')
gr2 = fixture.create_user_group(u'tes2')
Session().commit()
with pytest.raises(formencode.Invalid):
validator.to_python(u'test')
assert gr.users_group_id is not None
validator = v.ValidUserGroup(edit=True,
old_data={'users_group_id':
gr2.users_group_id})
with pytest.raises(formencode.Invalid):
validator.to_python(u'test')
with pytest.raises(formencode.Invalid):
validator.to_python(u'TesT')
with pytest.raises(formencode.Invalid):
validator.to_python(u'TEST')
UserGroupModel().delete(gr)
UserGroupModel().delete(gr2)
Session().commit()
def test_ValidRepoGroup(self):
validator = v.ValidRepoGroup()
model = RepoGroupModel()
with pytest.raises(formencode.Invalid):
validator.to_python({'group_name': HG_REPO, })
gr = model.create(group_name=u'test_gr', group_description=u'desc',
parent=None,
just_db=True,
owner=TEST_USER_ADMIN_LOGIN)
with pytest.raises(formencode.Invalid):
validator.to_python({'group_name': gr.group_name, })
validator = v.ValidRepoGroup(edit=True,
old_data={'group_id': gr.group_id})
with pytest.raises(formencode.Invalid):
validator.to_python({
'group_name': gr.group_name + 'n',
'parent_group_id': gr.group_id
})
model.delete(gr)
def test_ValidPassword(self):
validator = v.ValidPassword()
assert 'lol' == validator.to_python('lol')
assert validator.to_python(None) is None
with pytest.raises(formencode.Invalid):
validator.to_python('ąćżź')
def test_ValidPasswordsMatch(self):
validator = v.ValidPasswordsMatch('new_password', 'password_confirmation')
with pytest.raises(formencode.Invalid):
validator.to_python({'new_password': 'pass',
'password_confirmation': 'pass2'})
with pytest.raises(formencode.Invalid):
validator.to_python({'new_password': 'pass',
'password_confirmation': 'pass2'})
assert {'new_password': 'pass',
'password_confirmation': 'pass'} == validator.to_python({'new_password': 'pass',
'password_confirmation': 'pass'})
assert {'new_password': 'pass',
'password_confirmation': 'pass'} == validator.to_python({'new_password': 'pass',
'password_confirmation': 'pass'})
def test_ValidAuth(self):
validator = v.ValidAuth()
valid_creds = {
'username': TEST_USER_REGULAR2_LOGIN,
'password': TEST_USER_REGULAR2_PASS,
}
invalid_creds = {
'username': 'err',
'password': 'err',
}
assert valid_creds == validator.to_python(valid_creds)
with pytest.raises(formencode.Invalid):
validator.to_python(invalid_creds)
def test_ValidRepoName(self):
validator = v.ValidRepoName()
with pytest.raises(formencode.Invalid):
validator.to_python({'repo_name': ''})
with pytest.raises(formencode.Invalid):
validator.to_python({'repo_name': HG_REPO})
gr = RepoGroupModel().create(group_name=u'group_test',
group_description=u'desc',
parent=None,
owner=TEST_USER_ADMIN_LOGIN)
with pytest.raises(formencode.Invalid):
validator.to_python({'repo_name': gr.group_name})
# TODO: write an error case for that ie. create a repo withinh a group
# self.assertRaises(formencode.Invalid,
# validator.to_python, {'repo_name': 'some',
# 'repo_group': gr.group_id})
def test_ValidForkName(self):
# this uses ValidRepoName validator
assert True
@parametrize('name,expected', [
('test', 'test'), ('lolz!', 'lolz'), (' aavv', 'aavv'),
('ala ma kota', 'ala-ma-kota'), ('@nooo', 'nooo'),
('$!haha lolz !', 'haha-lolz'), ('$$$$$', ''), ('{}OK!', 'OK'),
('/]re po', 're-po')])
def test_SlugifyName(self, name, expected):
validator = v.SlugifyName()
assert expected == validator.to_python(name)
def test_ValidCloneUri(self):
# TODO: write this one
pass
def test_ValidForkType(self):
validator = v.ValidForkType(old_data={'repo_type': 'hg'})
assert 'hg' == validator.to_python('hg')
with pytest.raises(formencode.Invalid):
validator.to_python('git')
def test_ValidPerms(self):
# TODO: write this one
pass
def test_ValidSettings(self):
validator = v.ValidSettings()
assert {'pass': 'pass'} == validator.to_python(value={'user': 'test',
'pass': 'pass'})
assert {'user2': 'test', 'pass': 'pass'} == validator.to_python(value={'user2': 'test',
'pass': 'pass'})
def test_ValidPath(self):
validator = v.ValidPath()
assert TESTS_TMP_PATH == validator.to_python(TESTS_TMP_PATH)
with pytest.raises(formencode.Invalid):
validator.to_python('/no_such_dir')
def test_UniqSystemEmail(self):
validator = v.UniqSystemEmail(old_data={})
assert 'mail@python.org' == validator.to_python('MaiL@Python.org')
email = TEST_USER_REGULAR2_EMAIL
with pytest.raises(formencode.Invalid):
validator.to_python(email)
def test_ValidSystemEmail(self):
validator = v.ValidSystemEmail()
email = TEST_USER_REGULAR2_EMAIL
assert email == validator.to_python(email)
with pytest.raises(formencode.Invalid):
validator.to_python('err')
def test_LdapLibValidator(self):
if ldap_lib_installed:
validator = v.LdapLibValidator()
assert "DN" == validator.to_python('DN')
else:
validator = v.LdapLibValidator()
with pytest.raises(v.LdapImportError):
validator.to_python('err')
def test_AttrLoginValidator(self):
validator = v.AttrLoginValidator()
assert 'DN_attr' == validator.to_python('DN_attr')
|