Parent: [ddf08c] (diff)

Child: [a4dc01] (diff)

Download this file

test_validators.py    161 lines (119 with data), 5.6 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
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
# 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 unittest
import formencode as fe
from allura import model as M
from allura.lib import validators as v
from allura.lib.decorators import task
from alluratest.controller import setup_basic_test
def setUp():
setup_basic_test()
@task
def dummy_task(*args, **kw):
pass
class TestJsonConverter(unittest.TestCase):
val = v.JsonConverter
def test_valid(self):
self.assertEqual({}, self.val.to_python('{}'))
def test_invalid(self):
with self.assertRaises(fe.Invalid):
self.val.to_python('{')
class TestJsonFile(unittest.TestCase):
val = v.JsonFile
class FieldStorage(object):
def __init__(self, content):
self.value = content
def test_valid(self):
self.assertEqual({}, self.val.to_python(self.FieldStorage('{}')))
def test_invalid(self):
with self.assertRaises(fe.Invalid):
self.val.to_python(self.FieldStorage('{'))
class TestUserMapFile(unittest.TestCase):
val = v.UserMapJsonFile()
class FieldStorage(object):
def __init__(self, content):
self.value = content
def test_valid(self):
self.assertEqual({"user_old": "user_new"}, self.val.to_python(
self.FieldStorage('{"user_old": "user_new"}')))
def test_invalid(self):
with self.assertRaises(fe.Invalid):
self.val.to_python(self.FieldStorage('{"user_old": 1}'))
def test_as_string(self):
val = v.UserMapJsonFile(as_string=True)
self.assertEqual('{"user_old": "user_new"}', val.to_python(
self.FieldStorage('{"user_old": "user_new"}')))
class TestUserValidator(unittest.TestCase):
val = v.UserValidator
def test_valid(self):
self.assertEqual(M.User.by_username('root'), self.val.to_python('root'))
def test_invalid(self):
with self.assertRaises(fe.Invalid) as cm:
self.val.to_python('fakeuser')
self.assertEqual(str(cm.exception), "Invalid username")
class TestTaskValidator(unittest.TestCase):
val = v.TaskValidator
def test_valid(self):
self.assertEqual(dummy_task, self.val.to_python('allura.tests.test_validators.dummy_task'))
def test_invalid_name(self):
with self.assertRaises(fe.Invalid) as cm:
self.val.to_python('badname')
self.assertTrue(str(cm.exception).startswith('Invalid task name'))
def test_import_failure(self):
with self.assertRaises(fe.Invalid) as cm:
self.val.to_python('allura.does.not.exist')
self.assertEqual(str(cm.exception), 'Could not import "allura.does.not.exist"')
def test_attr_lookup_failure(self):
with self.assertRaises(fe.Invalid) as cm:
self.val.to_python('allura.tests.test_validators.typo')
self.assertEqual(str(cm.exception), 'Module has no attribute "typo"')
def test_not_a_task(self):
with self.assertRaises(fe.Invalid) as cm:
self.val.to_python('allura.tests.test_validators.setUp')
self.assertEqual(str(cm.exception), '"allura.tests.test_validators.setUp" is not a task.')
class TestPathValidator(unittest.TestCase):
val = v.PathValidator(strip=True, if_missing={}, if_empty={})
def test_valid_project(self):
project = M.Project.query.get(shortname='test')
d = self.val.to_python('/p/test')
self.assertEqual(d['project'], project)
self.assertTrue('app' not in d)
def test_valid_app(self):
project = M.Project.query.get(shortname='test')
app = project.app_instance('admin')
d = self.val.to_python('/p/test/admin/')
self.assertEqual(d['project'], project)
self.assertEqual(d['app'].config._id, app.config._id)
def test_invalid_format(self):
with self.assertRaises(fe.Invalid) as cm:
self.val.to_python('test')
self.assertTrue(str(cm.exception).startswith(
'You must specify at least a neighborhood and project'))
def test_invalid_neighborhood(self):
with self.assertRaises(fe.Invalid) as cm:
self.val.to_python('/q/test')
self.assertEqual(str(cm.exception), 'Invalid neighborhood: /q/')
def test_invalid_project(self):
with self.assertRaises(fe.Invalid) as cm:
self.val.to_python('/p/badproject')
self.assertEqual(str(cm.exception), 'Invalid project: badproject')
def test_invalid_app_mount_point(self):
with self.assertRaises(fe.Invalid) as cm:
self.val.to_python('/p/test/badapp')
self.assertEqual(str(cm.exception), 'Invalid app mount point: badapp')
def test_no_input(self):
self.assertEqual({}, self.val.to_python(''))