##// END OF EJS Templates
ldap: remove not used test reference for ldap library
marcink -
r2650:3a525687 default
parent child Browse files
Show More
@@ -1,247 +1,236 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import os
22 22 import time
23 23 import logging
24 24 import datetime
25 25 import hashlib
26 26 import tempfile
27 27 from os.path import join as jn
28 28
29 29 from tempfile import _RandomNameSequence
30 30
31 31 import pytest
32 32
33 33 from rhodecode.model.db import User
34 34 from rhodecode.lib import auth
35 35 from rhodecode.lib import helpers as h
36 36 from rhodecode.lib.helpers import flash, link_to
37 37 from rhodecode.lib.utils2 import safe_str
38 38
39 39
40 40 log = logging.getLogger(__name__)
41 41
42 42 __all__ = [
43 43 'get_new_dir', 'TestController',
44 'link_to', 'ldap_lib_installed', 'clear_all_caches',
44 'link_to', 'clear_all_caches',
45 45 'assert_session_flash', 'login_user', 'no_newline_id_generator',
46 46 'TESTS_TMP_PATH', 'HG_REPO', 'GIT_REPO', 'SVN_REPO',
47 47 'NEW_HG_REPO', 'NEW_GIT_REPO',
48 48 'HG_FORK', 'GIT_FORK', 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_ADMIN_PASS',
49 49 'TEST_USER_REGULAR_LOGIN', 'TEST_USER_REGULAR_PASS',
50 50 'TEST_USER_REGULAR_EMAIL', 'TEST_USER_REGULAR2_LOGIN',
51 51 'TEST_USER_REGULAR2_PASS', 'TEST_USER_REGULAR2_EMAIL', 'TEST_HG_REPO',
52 52 'TEST_HG_REPO_CLONE', 'TEST_HG_REPO_PULL', 'TEST_GIT_REPO',
53 53 'TEST_GIT_REPO_CLONE', 'TEST_GIT_REPO_PULL', 'SCM_TESTS',
54 54 ]
55 55
56 56
57 57 # SOME GLOBALS FOR TESTS
58 58 TEST_DIR = tempfile.gettempdir()
59 59
60 60 TESTS_TMP_PATH = jn(TEST_DIR, 'rc_test_%s' % _RandomNameSequence().next())
61 61 TEST_USER_ADMIN_LOGIN = 'test_admin'
62 62 TEST_USER_ADMIN_PASS = 'test12'
63 63 TEST_USER_ADMIN_EMAIL = 'test_admin@mail.com'
64 64
65 65 TEST_USER_REGULAR_LOGIN = 'test_regular'
66 66 TEST_USER_REGULAR_PASS = 'test12'
67 67 TEST_USER_REGULAR_EMAIL = 'test_regular@mail.com'
68 68
69 69 TEST_USER_REGULAR2_LOGIN = 'test_regular2'
70 70 TEST_USER_REGULAR2_PASS = 'test12'
71 71 TEST_USER_REGULAR2_EMAIL = 'test_regular2@mail.com'
72 72
73 73 HG_REPO = 'vcs_test_hg'
74 74 GIT_REPO = 'vcs_test_git'
75 75 SVN_REPO = 'vcs_test_svn'
76 76
77 77 NEW_HG_REPO = 'vcs_test_hg_new'
78 78 NEW_GIT_REPO = 'vcs_test_git_new'
79 79
80 80 HG_FORK = 'vcs_test_hg_fork'
81 81 GIT_FORK = 'vcs_test_git_fork'
82 82
83 83 ## VCS
84 84 SCM_TESTS = ['hg', 'git']
85 85 uniq_suffix = str(int(time.mktime(datetime.datetime.now().timetuple())))
86 86
87 87 TEST_GIT_REPO = jn(TESTS_TMP_PATH, GIT_REPO)
88 88 TEST_GIT_REPO_CLONE = jn(TESTS_TMP_PATH, 'vcsgitclone%s' % uniq_suffix)
89 89 TEST_GIT_REPO_PULL = jn(TESTS_TMP_PATH, 'vcsgitpull%s' % uniq_suffix)
90 90
91 91 TEST_HG_REPO = jn(TESTS_TMP_PATH, HG_REPO)
92 92 TEST_HG_REPO_CLONE = jn(TESTS_TMP_PATH, 'vcshgclone%s' % uniq_suffix)
93 93 TEST_HG_REPO_PULL = jn(TESTS_TMP_PATH, 'vcshgpull%s' % uniq_suffix)
94 94
95 95 TEST_REPO_PREFIX = 'vcs-test'
96 96
97 97
98 # skip ldap tests if LDAP lib is not installed
99 ldap_lib_installed = False
100 try:
101 import ldap
102 ldap_lib_installed = True
103 except ImportError:
104 ldap = None
105 # means that python-ldap is not installed
106 pass
107
108
109 98 def clear_all_caches():
110 99 from beaker.cache import cache_managers
111 100 for _cache in cache_managers.values():
112 101 _cache.clear()
113 102
114 103
115 104 def get_new_dir(title):
116 105 """
117 106 Returns always new directory path.
118 107 """
119 108 from rhodecode.tests.vcs.utils import get_normalized_path
120 109 name_parts = [TEST_REPO_PREFIX]
121 110 if title:
122 111 name_parts.append(title)
123 112 hex_str = hashlib.sha1('%s %s' % (os.getpid(), time.time())).hexdigest()
124 113 name_parts.append(hex_str)
125 114 name = '-'.join(name_parts)
126 115 path = os.path.join(TEST_DIR, name)
127 116 return get_normalized_path(path)
128 117
129 118
130 119 @pytest.mark.usefixtures('app', 'index_location')
131 120 class TestController(object):
132 121
133 122 maxDiff = None
134 123
135 124 def log_user(self, username=TEST_USER_ADMIN_LOGIN,
136 125 password=TEST_USER_ADMIN_PASS):
137 126 self._logged_username = username
138 127 self._session = login_user_session(self.app, username, password)
139 128 self.csrf_token = auth.get_csrf_token(self._session)
140 129
141 130 return self._session['rhodecode_user']
142 131
143 132 def logout_user(self):
144 133 logout_user_session(self.app, auth.get_csrf_token(self._session))
145 134 self.csrf_token = None
146 135 self._logged_username = None
147 136 self._session = None
148 137
149 138 def _get_logged_user(self):
150 139 return User.get_by_username(self._logged_username)
151 140
152 141
153 142 def login_user_session(
154 143 app, username=TEST_USER_ADMIN_LOGIN, password=TEST_USER_ADMIN_PASS):
155 144
156 145 response = app.post(
157 146 h.route_path('login'),
158 147 {'username': username, 'password': password})
159 148 if 'invalid user name' in response.body:
160 149 pytest.fail('could not login using %s %s' % (username, password))
161 150
162 151 assert response.status == '302 Found'
163 152 response = response.follow()
164 153 assert response.status == '200 OK'
165 154
166 155 session = response.get_session_from_response()
167 156 assert 'rhodecode_user' in session
168 157 rc_user = session['rhodecode_user']
169 158 assert rc_user.get('username') == username
170 159 assert rc_user.get('is_authenticated')
171 160
172 161 return session
173 162
174 163
175 164 def logout_user_session(app, csrf_token):
176 165 app.post(h.route_path('logout'), {'csrf_token': csrf_token}, status=302)
177 166
178 167
179 168 def login_user(app, username=TEST_USER_ADMIN_LOGIN,
180 169 password=TEST_USER_ADMIN_PASS):
181 170 return login_user_session(app, username, password)['rhodecode_user']
182 171
183 172
184 173 def assert_session_flash(response, msg=None, category=None, no_=None):
185 174 """
186 175 Assert on a flash message in the current session.
187 176
188 177 :param response: Response from give calll, it will contain flash
189 178 messages or bound session with them.
190 179 :param msg: The expected message. Will be evaluated if a
191 180 :class:`LazyString` is passed in.
192 181 :param category: Optional. If passed, the message category will be
193 182 checked as well.
194 183 :param no_: Optional. If passed, the message will be checked to NOT
195 184 be in the flash session
196 185 """
197 186 if msg is None and no_ is None:
198 187 raise ValueError("Parameter msg or no_ is required.")
199 188
200 189 if msg and no_:
201 190 raise ValueError("Please specify either msg or no_, but not both")
202 191
203 192 session = response.get_session_from_response()
204 193 messages = flash.pop_messages(session=session)
205 194 msg = _eval_if_lazy(msg)
206 195
207 196 if no_:
208 197 error_msg = 'unable to detect no_ message `%s` in empty flash list' % no_
209 198 else:
210 199 error_msg = 'unable to find message `%s` in empty flash list' % msg
211 200 assert messages, error_msg
212 201 message = messages[0]
213 202
214 203 message_text = _eval_if_lazy(message.message) or ''
215 204
216 205 if no_:
217 206 if no_ in message_text:
218 207 msg = u'msg `%s` found in session flash.' % (no_,)
219 208 pytest.fail(safe_str(msg))
220 209 else:
221 210 if msg not in message_text:
222 211 fail_msg = u'msg `%s` not found in session ' \
223 212 u'flash: got `%s` (type:%s) instead' % (
224 213 msg, message_text, type(message_text))
225 214
226 215 pytest.fail(safe_str(fail_msg))
227 216 if category:
228 217 assert category == message.category
229 218
230 219
231 220 def _eval_if_lazy(value):
232 221 return value.eval() if hasattr(value, 'eval') else value
233 222
234 223
235 224 def no_newline_id_generator(test_name):
236 225 """
237 226 Generates a test name without spaces or newlines characters. Used for
238 227 nicer output of progress of test
239 228 """
240 229 org_name = test_name
241 230 test_name = test_name\
242 231 .replace('\n', '_N') \
243 232 .replace('\r', '_N') \
244 233 .replace('\t', '_T') \
245 234 .replace(' ', '_S')
246 235
247 236 return test_name or 'test-with-empty-name'
@@ -1,295 +1,294 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21
22 22 import formencode
23 23 import pytest
24 24
25 25 from rhodecode.tests import (
26 26 HG_REPO, TEST_USER_REGULAR2_EMAIL, TEST_USER_REGULAR2_LOGIN,
27 TEST_USER_REGULAR2_PASS, TEST_USER_ADMIN_LOGIN, TESTS_TMP_PATH,
28 ldap_lib_installed)
27 TEST_USER_REGULAR2_PASS, TEST_USER_ADMIN_LOGIN, TESTS_TMP_PATH)
29 28
30 29 from rhodecode.model import validators as v
31 30 from rhodecode.model.user_group import UserGroupModel
32 31
33 32 from rhodecode.model.meta import Session
34 33 from rhodecode.model.repo_group import RepoGroupModel
35 34 from rhodecode.model.db import ChangesetStatus, Repository
36 35 from rhodecode.model.changeset_status import ChangesetStatusModel
37 36 from rhodecode.tests.fixture import Fixture
38 37
39 38 fixture = Fixture()
40 39
41 40 pytestmark = pytest.mark.usefixtures('baseapp')
42 41
43 42
44 43 @pytest.fixture
45 44 def localizer():
46 45 def func(msg):
47 46 return msg
48 47 return func
49 48
50 49
51 50 def test_Message_extractor(localizer):
52 51 validator = v.ValidUsername(localizer)
53 52 pytest.raises(formencode.Invalid, validator.to_python, 'default')
54 53
55 54 class StateObj(object):
56 55 pass
57 56
58 57 pytest.raises(
59 58 formencode.Invalid, validator.to_python, 'default', StateObj)
60 59
61 60
62 61 def test_ValidUsername(localizer):
63 62 validator = v.ValidUsername(localizer)
64 63
65 64 pytest.raises(formencode.Invalid, validator.to_python, 'default')
66 65 pytest.raises(formencode.Invalid, validator.to_python, 'new_user')
67 66 pytest.raises(formencode.Invalid, validator.to_python, '.,')
68 67 pytest.raises(
69 68 formencode.Invalid, validator.to_python, TEST_USER_ADMIN_LOGIN)
70 69 assert 'test' == validator.to_python('test')
71 70
72 71 validator = v.ValidUsername(localizer, edit=True, old_data={'user_id': 1})
73 72
74 73
75 74 def test_ValidRepoUser(localizer):
76 75 validator = v.ValidRepoUser(localizer)
77 76 pytest.raises(formencode.Invalid, validator.to_python, 'nouser')
78 77 assert TEST_USER_ADMIN_LOGIN == \
79 78 validator.to_python(TEST_USER_ADMIN_LOGIN)
80 79
81 80
82 81 def test_ValidUserGroup(localizer):
83 82 validator = v.ValidUserGroup(localizer)
84 83 pytest.raises(formencode.Invalid, validator.to_python, 'default')
85 84 pytest.raises(formencode.Invalid, validator.to_python, '.,')
86 85
87 86 gr = fixture.create_user_group('test')
88 87 gr2 = fixture.create_user_group('tes2')
89 88 Session().commit()
90 89 pytest.raises(formencode.Invalid, validator.to_python, 'test')
91 90 assert gr.users_group_id is not None
92 91 validator = v.ValidUserGroup(localizer,
93 92 edit=True,
94 93 old_data={'users_group_id': gr2.users_group_id})
95 94
96 95 pytest.raises(formencode.Invalid, validator.to_python, 'test')
97 96 pytest.raises(formencode.Invalid, validator.to_python, 'TesT')
98 97 pytest.raises(formencode.Invalid, validator.to_python, 'TEST')
99 98 UserGroupModel().delete(gr)
100 99 UserGroupModel().delete(gr2)
101 100 Session().commit()
102 101
103 102
104 103 @pytest.fixture(scope='function')
105 104 def repo_group(request):
106 105 model = RepoGroupModel()
107 106 gr = model.create(
108 107 group_name='test_gr', group_description='desc', just_db=True,
109 108 owner=TEST_USER_ADMIN_LOGIN)
110 109
111 110 def cleanup():
112 111 model.delete(gr)
113 112
114 113 request.addfinalizer(cleanup)
115 114
116 115 return gr
117 116
118 117
119 118 def test_ValidRepoGroup_same_name_as_repo(localizer):
120 119 validator = v.ValidRepoGroup(localizer)
121 120 with pytest.raises(formencode.Invalid) as excinfo:
122 121 validator.to_python({'group_name': HG_REPO})
123 122 expected_msg = 'Repository with name "vcs_test_hg" already exists'
124 123 assert expected_msg in str(excinfo.value)
125 124
126 125
127 126 def test_ValidRepoGroup_group_exists(localizer, repo_group):
128 127 validator = v.ValidRepoGroup(localizer)
129 128 with pytest.raises(formencode.Invalid) as excinfo:
130 129 validator.to_python({'group_name': repo_group.group_name})
131 130 expected_msg = 'Group "test_gr" already exists'
132 131 assert expected_msg in str(excinfo.value)
133 132
134 133
135 134 def test_ValidRepoGroup_invalid_parent(localizer, repo_group):
136 135 validator = v.ValidRepoGroup(localizer, edit=True,
137 136 old_data={'group_id': repo_group.group_id})
138 137 with pytest.raises(formencode.Invalid) as excinfo:
139 138 validator.to_python({
140 139 'group_name': repo_group.group_name + 'n',
141 140 'group_parent_id': repo_group.group_id,
142 141 })
143 142 expected_msg = 'Cannot assign this group as parent'
144 143 assert expected_msg in str(excinfo.value)
145 144
146 145
147 146 def test_ValidRepoGroup_edit_group_no_root_permission(localizer, repo_group):
148 147 validator = v.ValidRepoGroup(localizer,
149 148 edit=True, old_data={'group_id': repo_group.group_id},
150 149 can_create_in_root=False)
151 150
152 151 # Cannot change parent
153 152 with pytest.raises(formencode.Invalid) as excinfo:
154 153 validator.to_python({'group_parent_id': '25'})
155 154 expected_msg = 'no permission to store repository group in root location'
156 155 assert expected_msg in str(excinfo.value)
157 156
158 157 # Chaning all the other fields is allowed
159 158 validator.to_python({'group_name': 'foo', 'group_parent_id': '-1'})
160 159 validator.to_python(
161 160 {'user': TEST_USER_REGULAR2_LOGIN, 'group_parent_id': '-1'})
162 161 validator.to_python({'group_description': 'bar', 'group_parent_id': '-1'})
163 162 validator.to_python({'enable_locking': 'true', 'group_parent_id': '-1'})
164 163
165 164
166 165 def test_ValidPassword(localizer):
167 166 validator = v.ValidPassword(localizer)
168 167 assert 'lol' == validator.to_python('lol')
169 168 assert None == validator.to_python(None)
170 169 pytest.raises(formencode.Invalid, validator.to_python, 'Δ…Δ‡ΕΌΕΊ')
171 170
172 171
173 172 def test_ValidPasswordsMatch(localizer):
174 173 validator = v.ValidPasswordsMatch(localizer)
175 174 pytest.raises(
176 175 formencode.Invalid,
177 176 validator.to_python, {'password': 'pass',
178 177 'password_confirmation': 'pass2'})
179 178
180 179 pytest.raises(
181 180 formencode.Invalid,
182 181 validator.to_python, {'new_password': 'pass',
183 182 'password_confirmation': 'pass2'})
184 183
185 184 assert {'new_password': 'pass', 'password_confirmation': 'pass'} == \
186 185 validator.to_python({'new_password': 'pass',
187 186 'password_confirmation': 'pass'})
188 187
189 188 assert {'password': 'pass', 'password_confirmation': 'pass'} == \
190 189 validator.to_python({'password': 'pass',
191 190 'password_confirmation': 'pass'})
192 191
193 192
194 193 def test_ValidAuth(localizer, config_stub):
195 194 config_stub.testing_securitypolicy()
196 195 config_stub.include('rhodecode.authentication')
197 196
198 197 validator = v.ValidAuth(localizer)
199 198 valid_creds = {
200 199 'username': TEST_USER_REGULAR2_LOGIN,
201 200 'password': TEST_USER_REGULAR2_PASS,
202 201 }
203 202 invalid_creds = {
204 203 'username': 'err',
205 204 'password': 'err',
206 205 }
207 206 assert valid_creds == validator.to_python(valid_creds)
208 207 pytest.raises(
209 208 formencode.Invalid, validator.to_python, invalid_creds)
210 209
211 210
212 211 def test_ValidRepoName(localizer):
213 212 validator = v.ValidRepoName(localizer)
214 213
215 214 pytest.raises(
216 215 formencode.Invalid, validator.to_python, {'repo_name': ''})
217 216
218 217 pytest.raises(
219 218 formencode.Invalid, validator.to_python, {'repo_name': HG_REPO})
220 219
221 220 gr = RepoGroupModel().create(group_name='group_test',
222 221 group_description='desc',
223 222 owner=TEST_USER_ADMIN_LOGIN)
224 223 pytest.raises(
225 224 formencode.Invalid, validator.to_python, {'repo_name': gr.group_name})
226 225
227 226 #TODO: write an error case for that ie. create a repo withinh a group
228 227 # pytest.raises(formencode.Invalid,
229 228 # validator.to_python, {'repo_name': 'some',
230 229 # 'repo_group': gr.group_id})
231 230
232 231
233 232 def test_ValidForkName(localizer):
234 233 # this uses ValidRepoName validator
235 234 assert True
236 235
237 236 @pytest.mark.parametrize("name, expected", [
238 237 ('test', 'test'), ('lolz!', 'lolz'), (' aavv', 'aavv'),
239 238 ('ala ma kota', 'ala-ma-kota'), ('@nooo', 'nooo'),
240 239 ('$!haha lolz !', 'haha-lolz'), ('$$$$$', ''), ('{}OK!', 'OK'),
241 240 ('/]re po', 're-po')])
242 241 def test_SlugifyName(name, expected, localizer):
243 242 validator = v.SlugifyName(localizer)
244 243 assert expected == validator.to_python(name)
245 244
246 245
247 246 def test_ValidForkType(localizer):
248 247 validator = v.ValidForkType(localizer, old_data={'repo_type': 'hg'})
249 248 assert 'hg' == validator.to_python('hg')
250 249 pytest.raises(formencode.Invalid, validator.to_python, 'git')
251 250
252 251
253 252 def test_ValidPath(localizer):
254 253 validator = v.ValidPath(localizer)
255 254 assert TESTS_TMP_PATH == validator.to_python(TESTS_TMP_PATH)
256 255 pytest.raises(
257 256 formencode.Invalid, validator.to_python, '/no_such_dir')
258 257
259 258
260 259 def test_UniqSystemEmail(localizer):
261 260 validator = v.UniqSystemEmail(localizer, old_data={})
262 261
263 262 assert 'mail@python.org' == validator.to_python('MaiL@Python.org')
264 263
265 264 email = TEST_USER_REGULAR2_EMAIL
266 265 pytest.raises(formencode.Invalid, validator.to_python, email)
267 266
268 267
269 268 def test_ValidSystemEmail(localizer):
270 269 validator = v.ValidSystemEmail(localizer)
271 270 email = TEST_USER_REGULAR2_EMAIL
272 271
273 272 assert email == validator.to_python(email)
274 273 pytest.raises(formencode.Invalid, validator.to_python, 'err')
275 274
276 275
277 276 def test_NotReviewedRevisions(localizer):
278 277 repo_id = Repository.get_by_repo_name(HG_REPO).repo_id
279 278 validator = v.NotReviewedRevisions(localizer, repo_id)
280 279 rev = '0' * 40
281 280 # add status for a rev, that should throw an error because it is already
282 281 # reviewed
283 282 new_status = ChangesetStatus()
284 283 new_status.author = ChangesetStatusModel()._get_user(TEST_USER_ADMIN_LOGIN)
285 284 new_status.repo = ChangesetStatusModel()._get_repo(HG_REPO)
286 285 new_status.status = ChangesetStatus.STATUS_APPROVED
287 286 new_status.comment = None
288 287 new_status.revision = rev
289 288 Session().add(new_status)
290 289 Session().commit()
291 290 try:
292 291 pytest.raises(formencode.Invalid, validator.to_python, [rev])
293 292 finally:
294 293 Session().delete(new_status)
295 294 Session().commit()
General Comments 0
You need to be logged in to leave comments. Login now