Show More
@@ -1,246 +1,247 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | import unittest |
|
3 | 3 | import formencode |
|
4 | 4 | |
|
5 | 5 | from rhodecode.tests import * |
|
6 | 6 | |
|
7 | 7 | from rhodecode.model import validators as v |
|
8 | 8 | from rhodecode.model.users_group import UsersGroupModel |
|
9 | 9 | |
|
10 | 10 | from rhodecode.model.meta import Session |
|
11 | 11 | from rhodecode.model.repos_group import ReposGroupModel |
|
12 | 12 | from rhodecode.config.routing import ADMIN_PREFIX |
|
13 | from rhodecode.model.db import ChangesetStatus | |
|
13 | from rhodecode.model.db import ChangesetStatus, Repository | |
|
14 | 14 | from rhodecode.model.changeset_status import ChangesetStatusModel |
|
15 | 15 | from rhodecode.model.comment import ChangesetCommentsModel |
|
16 | 16 | |
|
17 | 17 | |
|
18 | 18 | class TestReposGroups(unittest.TestCase): |
|
19 | 19 | |
|
20 | 20 | def setUp(self): |
|
21 | 21 | pass |
|
22 | 22 | |
|
23 | 23 | def tearDown(self): |
|
24 | 24 | pass |
|
25 | 25 | |
|
26 | 26 | def test_Message_extractor(self): |
|
27 | 27 | validator = v.ValidUsername() |
|
28 | 28 | self.assertRaises(formencode.Invalid, validator.to_python, 'default') |
|
29 | 29 | |
|
30 | 30 | class StateObj(object): |
|
31 | 31 | pass |
|
32 | 32 | |
|
33 | 33 | self.assertRaises(formencode.Invalid, |
|
34 | 34 | validator.to_python, 'default', StateObj) |
|
35 | 35 | |
|
36 | 36 | def test_ValidUsername(self): |
|
37 | 37 | validator = v.ValidUsername() |
|
38 | 38 | |
|
39 | 39 | self.assertRaises(formencode.Invalid, validator.to_python, 'default') |
|
40 | 40 | self.assertRaises(formencode.Invalid, validator.to_python, 'new_user') |
|
41 | 41 | self.assertRaises(formencode.Invalid, validator.to_python, '.,') |
|
42 | 42 | self.assertRaises(formencode.Invalid, validator.to_python, |
|
43 | 43 | TEST_USER_ADMIN_LOGIN) |
|
44 | 44 | self.assertEqual('test', validator.to_python('test')) |
|
45 | 45 | |
|
46 | 46 | validator = v.ValidUsername(edit=True, old_data={'user_id': 1}) |
|
47 | 47 | |
|
48 | 48 | def test_ValidRepoUser(self): |
|
49 | 49 | validator = v.ValidRepoUser() |
|
50 | 50 | self.assertRaises(formencode.Invalid, validator.to_python, 'nouser') |
|
51 | 51 | self.assertEqual(TEST_USER_ADMIN_LOGIN, |
|
52 | 52 | validator.to_python(TEST_USER_ADMIN_LOGIN)) |
|
53 | 53 | |
|
54 | 54 | def test_ValidUsersGroup(self): |
|
55 | 55 | validator = v.ValidUsersGroup() |
|
56 | 56 | self.assertRaises(formencode.Invalid, validator.to_python, 'default') |
|
57 | 57 | self.assertRaises(formencode.Invalid, validator.to_python, '.,') |
|
58 | 58 | |
|
59 | 59 | gr = UsersGroupModel().create('test') |
|
60 | 60 | gr2 = UsersGroupModel().create('tes2') |
|
61 | 61 | Session.commit() |
|
62 | 62 | self.assertRaises(formencode.Invalid, validator.to_python, 'test') |
|
63 | 63 | assert gr.users_group_id != None |
|
64 | 64 | validator = v.ValidUsersGroup(edit=True, |
|
65 | 65 | old_data={'users_group_id': |
|
66 | 66 | gr2.users_group_id}) |
|
67 | 67 | |
|
68 | 68 | self.assertRaises(formencode.Invalid, validator.to_python, 'test') |
|
69 | 69 | self.assertRaises(formencode.Invalid, validator.to_python, 'TesT') |
|
70 | 70 | self.assertRaises(formencode.Invalid, validator.to_python, 'TEST') |
|
71 | 71 | UsersGroupModel().delete(gr) |
|
72 | 72 | UsersGroupModel().delete(gr2) |
|
73 | 73 | Session.commit() |
|
74 | 74 | |
|
75 | 75 | def test_ValidReposGroup(self): |
|
76 | 76 | validator = v.ValidReposGroup() |
|
77 | 77 | model = ReposGroupModel() |
|
78 | 78 | self.assertRaises(formencode.Invalid, validator.to_python, |
|
79 | 79 | {'group_name': HG_REPO, }) |
|
80 | 80 | gr = model.create(group_name='test_gr', group_description='desc', |
|
81 | 81 | parent=None, |
|
82 | 82 | just_db=True) |
|
83 | 83 | self.assertRaises(formencode.Invalid, |
|
84 | 84 | validator.to_python, {'group_name': gr.group_name, }) |
|
85 | 85 | |
|
86 | 86 | validator = v.ValidReposGroup(edit=True, |
|
87 | 87 | old_data={'group_id': gr.group_id}) |
|
88 | 88 | self.assertRaises(formencode.Invalid, |
|
89 | 89 | validator.to_python, { |
|
90 | 90 | 'group_name': gr.group_name + 'n', |
|
91 | 91 | 'group_parent_id': gr.group_id |
|
92 | 92 | }) |
|
93 | 93 | model.delete(gr) |
|
94 | 94 | |
|
95 | 95 | def test_ValidPassword(self): |
|
96 | 96 | validator = v.ValidPassword() |
|
97 | 97 | self.assertEqual('lol', validator.to_python('lol')) |
|
98 | 98 | self.assertEqual(None, validator.to_python(None)) |
|
99 | 99 | self.assertRaises(formencode.Invalid, validator.to_python, 'Δ ΔΕΌΕΊ') |
|
100 | 100 | |
|
101 | 101 | def test_ValidPasswordsMatch(self): |
|
102 | 102 | validator = v.ValidPasswordsMatch() |
|
103 | 103 | self.assertRaises(formencode.Invalid, |
|
104 | 104 | validator.to_python, {'password': 'pass', |
|
105 | 105 | 'password_confirmation': 'pass2'}) |
|
106 | 106 | |
|
107 | 107 | self.assertRaises(formencode.Invalid, |
|
108 | 108 | validator.to_python, {'new_password': 'pass', |
|
109 | 109 | 'password_confirmation': 'pass2'}) |
|
110 | 110 | |
|
111 | 111 | self.assertEqual({'new_password': 'pass', |
|
112 | 112 | 'password_confirmation': 'pass'}, |
|
113 | 113 | validator.to_python({'new_password': 'pass', |
|
114 | 114 | 'password_confirmation': 'pass'})) |
|
115 | 115 | |
|
116 | 116 | self.assertEqual({'password': 'pass', |
|
117 | 117 | 'password_confirmation': 'pass'}, |
|
118 | 118 | validator.to_python({'password': 'pass', |
|
119 | 119 | 'password_confirmation': 'pass'})) |
|
120 | 120 | |
|
121 | 121 | def test_ValidAuth(self): |
|
122 | 122 | validator = v.ValidAuth() |
|
123 | 123 | valid_creds = { |
|
124 | 124 | 'username': TEST_USER_REGULAR2_LOGIN, |
|
125 | 125 | 'password': TEST_USER_REGULAR2_PASS, |
|
126 | 126 | } |
|
127 | 127 | invalid_creds = { |
|
128 | 128 | 'username': 'err', |
|
129 | 129 | 'password': 'err', |
|
130 | 130 | } |
|
131 | 131 | self.assertEqual(valid_creds, validator.to_python(valid_creds)) |
|
132 | 132 | self.assertRaises(formencode.Invalid, |
|
133 | 133 | validator.to_python, invalid_creds) |
|
134 | 134 | |
|
135 | 135 | def test_ValidAuthToken(self): |
|
136 | 136 | validator = v.ValidAuthToken() |
|
137 | 137 | # this is untestable without a threadlocal |
|
138 | 138 | # self.assertRaises(formencode.Invalid, |
|
139 | 139 | # validator.to_python, 'BadToken') |
|
140 | 140 | validator |
|
141 | 141 | |
|
142 | 142 | def test_ValidRepoName(self): |
|
143 | 143 | validator = v.ValidRepoName() |
|
144 | 144 | |
|
145 | 145 | self.assertRaises(formencode.Invalid, |
|
146 | 146 | validator.to_python, {'repo_name': ''}) |
|
147 | 147 | |
|
148 | 148 | self.assertRaises(formencode.Invalid, |
|
149 | 149 | validator.to_python, {'repo_name': HG_REPO}) |
|
150 | 150 | |
|
151 | 151 | gr = ReposGroupModel().create(group_name='group_test', |
|
152 | 152 | group_description='desc', |
|
153 | 153 | parent=None,) |
|
154 | 154 | self.assertRaises(formencode.Invalid, |
|
155 | 155 | validator.to_python, {'repo_name': gr.group_name}) |
|
156 | 156 | |
|
157 | 157 | #TODO: write an error case for that ie. create a repo withinh a group |
|
158 | 158 | # self.assertRaises(formencode.Invalid, |
|
159 | 159 | # validator.to_python, {'repo_name': 'some', |
|
160 | 160 | # 'repo_group': gr.group_id}) |
|
161 | 161 | |
|
162 | 162 | def test_ValidForkName(self): |
|
163 | 163 | # this uses ValidRepoName validator |
|
164 | 164 | assert True |
|
165 | 165 | |
|
166 | 166 | @parameterized.expand([ |
|
167 | 167 | ('test', 'test'), ('lolz!', 'lolz'), (' aavv', 'aavv'), |
|
168 | 168 | ('ala ma kota', 'ala-ma-kota'), ('@nooo', 'nooo'), |
|
169 | 169 | ('$!haha lolz !', 'haha-lolz'), ('$$$$$', ''), ('{}OK!', 'OK'), |
|
170 | 170 | ('/]re po', 're-po')]) |
|
171 | 171 | def test_SlugifyName(self, name, expected): |
|
172 | 172 | validator = v.SlugifyName() |
|
173 | 173 | self.assertEqual(expected, validator.to_python(name)) |
|
174 | 174 | |
|
175 | 175 | def test_ValidCloneUri(self): |
|
176 | 176 | #TODO: write this one |
|
177 | 177 | pass |
|
178 | 178 | |
|
179 | 179 | def test_ValidForkType(self): |
|
180 | 180 | validator = v.ValidForkType(old_data={'repo_type': 'hg'}) |
|
181 | 181 | self.assertEqual('hg', validator.to_python('hg')) |
|
182 | 182 | self.assertRaises(formencode.Invalid, validator.to_python, 'git') |
|
183 | 183 | |
|
184 | 184 | def test_ValidPerms(self): |
|
185 | 185 | #TODO: write this one |
|
186 | 186 | pass |
|
187 | 187 | |
|
188 | 188 | def test_ValidSettings(self): |
|
189 | 189 | validator = v.ValidSettings() |
|
190 | 190 | self.assertEqual({'pass': 'pass'}, |
|
191 | 191 | validator.to_python(value={'user': 'test', |
|
192 | 192 | 'pass': 'pass'})) |
|
193 | 193 | |
|
194 | 194 | self.assertEqual({'user2': 'test', 'pass': 'pass'}, |
|
195 | 195 | validator.to_python(value={'user2': 'test', |
|
196 | 196 | 'pass': 'pass'})) |
|
197 | 197 | |
|
198 | 198 | def test_ValidPath(self): |
|
199 | 199 | validator = v.ValidPath() |
|
200 | 200 | self.assertEqual(TESTS_TMP_PATH, |
|
201 | 201 | validator.to_python(TESTS_TMP_PATH)) |
|
202 | 202 | self.assertRaises(formencode.Invalid, validator.to_python, |
|
203 | 203 | '/no_such_dir') |
|
204 | 204 | |
|
205 | 205 | def test_UniqSystemEmail(self): |
|
206 | 206 | validator = v.UniqSystemEmail(old_data={}) |
|
207 | 207 | |
|
208 | 208 | self.assertEqual('mail@python.org', |
|
209 | 209 | validator.to_python('MaiL@Python.org')) |
|
210 | 210 | |
|
211 | 211 | email = TEST_USER_REGULAR2_EMAIL |
|
212 | 212 | self.assertRaises(formencode.Invalid, validator.to_python, email) |
|
213 | 213 | |
|
214 | 214 | def test_ValidSystemEmail(self): |
|
215 | 215 | validator = v.ValidSystemEmail() |
|
216 | 216 | email = TEST_USER_REGULAR2_EMAIL |
|
217 | 217 | |
|
218 | 218 | self.assertEqual(email, validator.to_python(email)) |
|
219 | 219 | self.assertRaises(formencode.Invalid, validator.to_python, 'err') |
|
220 | 220 | |
|
221 | 221 | def test_LdapLibValidator(self): |
|
222 | 222 | validator = v.LdapLibValidator() |
|
223 | 223 | self.assertRaises(v.LdapImportError, validator.to_python, 'err') |
|
224 | 224 | |
|
225 | 225 | def test_AttrLoginValidator(self): |
|
226 | 226 | validator = v.AttrLoginValidator() |
|
227 | 227 | self.assertRaises(formencode.Invalid, validator.to_python, 123) |
|
228 | 228 | |
|
229 | 229 | def test_NotReviewedRevisions(self): |
|
230 | validator = v.NotReviewedRevisions() | |
|
230 | repo_id = Repository.get_by_repo_name(HG_REPO).repo_id | |
|
231 | validator = v.NotReviewedRevisions(repo_id) | |
|
231 | 232 | rev = '0' * 40 |
|
232 | 233 | # add status for a rev, that should throw an error because it is already |
|
233 | 234 | # reviewed |
|
234 | 235 | new_status = ChangesetStatus() |
|
235 | 236 | new_status.author = ChangesetStatusModel()._get_user(TEST_USER_ADMIN_LOGIN) |
|
236 | 237 | new_status.repo = ChangesetStatusModel()._get_repo(HG_REPO) |
|
237 | 238 | new_status.status = ChangesetStatus.STATUS_APPROVED |
|
238 | 239 | new_status.comment = None |
|
239 | 240 | new_status.revision = rev |
|
240 | 241 | Session().add(new_status) |
|
241 | 242 | Session().commit() |
|
242 | 243 | try: |
|
243 | 244 | self.assertRaises(formencode.Invalid, validator.to_python, [rev]) |
|
244 | 245 | finally: |
|
245 | 246 | Session().delete(new_status) |
|
246 | 247 | Session().commit() |
General Comments 0
You need to be logged in to leave comments.
Login now