Show More
@@ -1,258 +1,251 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2017 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 | from paste.deploy import loadapp |
|
32 | 32 | from paste.script.appinstall import SetupCommand |
|
33 | 33 | |
|
34 | 34 | import pylons |
|
35 | 35 | import pylons.test |
|
36 | 36 | from pylons import config, url |
|
37 | 37 | from pylons.i18n.translation import _get_translator |
|
38 | 38 | from pylons.util import ContextObj |
|
39 | 39 | |
|
40 | 40 | from routes.util import URLGenerator |
|
41 | 41 | from nose.plugins.skip import SkipTest |
|
42 | 42 | import pytest |
|
43 | 43 | |
|
44 | 44 | from rhodecode import is_windows |
|
45 | 45 | from rhodecode.config.routing import ADMIN_PREFIX |
|
46 | 46 | from rhodecode.model.meta import Session |
|
47 | 47 | from rhodecode.model.db import User |
|
48 | 48 | from rhodecode.lib import auth |
|
49 | 49 | from rhodecode.lib.helpers import flash, link_to |
|
50 | 50 | from rhodecode.lib.utils2 import safe_unicode, safe_str |
|
51 | 51 | from rhodecode.tests.utils import get_session_from_response |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | log = logging.getLogger(__name__) |
|
55 | 55 | |
|
56 | 56 | __all__ = [ |
|
57 | 57 | 'get_new_dir', 'TestController', 'SkipTest', |
|
58 | 58 | 'url', 'link_to', 'ldap_lib_installed', 'clear_all_caches', |
|
59 | 59 | 'assert_session_flash', 'login_user', |
|
60 | 60 | 'TESTS_TMP_PATH', 'HG_REPO', 'GIT_REPO', 'SVN_REPO', |
|
61 | 61 | 'NEW_HG_REPO', 'NEW_GIT_REPO', |
|
62 | 62 | 'HG_FORK', 'GIT_FORK', 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_ADMIN_PASS', |
|
63 | 63 | 'TEST_USER_REGULAR_LOGIN', 'TEST_USER_REGULAR_PASS', |
|
64 | 64 | 'TEST_USER_REGULAR_EMAIL', 'TEST_USER_REGULAR2_LOGIN', |
|
65 | 65 | 'TEST_USER_REGULAR2_PASS', 'TEST_USER_REGULAR2_EMAIL', 'TEST_HG_REPO', |
|
66 | 66 | 'TEST_HG_REPO_CLONE', 'TEST_HG_REPO_PULL', 'TEST_GIT_REPO', |
|
67 | 67 | 'TEST_GIT_REPO_CLONE', 'TEST_GIT_REPO_PULL', 'SCM_TESTS', |
|
68 | 68 | ] |
|
69 | 69 | |
|
70 | 70 | # Invoke websetup with the current config file |
|
71 | 71 | # SetupCommand('setup-app').run([config_file]) |
|
72 | 72 | |
|
73 | 73 | # SOME GLOBALS FOR TESTS |
|
74 | 74 | TEST_DIR = tempfile.gettempdir() |
|
75 | 75 | |
|
76 | 76 | TESTS_TMP_PATH = jn(TEST_DIR, 'rc_test_%s' % _RandomNameSequence().next()) |
|
77 | 77 | TEST_USER_ADMIN_LOGIN = 'test_admin' |
|
78 | 78 | TEST_USER_ADMIN_PASS = 'test12' |
|
79 | 79 | TEST_USER_ADMIN_EMAIL = 'test_admin@mail.com' |
|
80 | 80 | |
|
81 | 81 | TEST_USER_REGULAR_LOGIN = 'test_regular' |
|
82 | 82 | TEST_USER_REGULAR_PASS = 'test12' |
|
83 | 83 | TEST_USER_REGULAR_EMAIL = 'test_regular@mail.com' |
|
84 | 84 | |
|
85 | 85 | TEST_USER_REGULAR2_LOGIN = 'test_regular2' |
|
86 | 86 | TEST_USER_REGULAR2_PASS = 'test12' |
|
87 | 87 | TEST_USER_REGULAR2_EMAIL = 'test_regular2@mail.com' |
|
88 | 88 | |
|
89 | 89 | HG_REPO = 'vcs_test_hg' |
|
90 | 90 | GIT_REPO = 'vcs_test_git' |
|
91 | 91 | SVN_REPO = 'vcs_test_svn' |
|
92 | 92 | |
|
93 | 93 | NEW_HG_REPO = 'vcs_test_hg_new' |
|
94 | 94 | NEW_GIT_REPO = 'vcs_test_git_new' |
|
95 | 95 | |
|
96 | 96 | HG_FORK = 'vcs_test_hg_fork' |
|
97 | 97 | GIT_FORK = 'vcs_test_git_fork' |
|
98 | 98 | |
|
99 | 99 | ## VCS |
|
100 | 100 | SCM_TESTS = ['hg', 'git'] |
|
101 | 101 | uniq_suffix = str(int(time.mktime(datetime.datetime.now().timetuple()))) |
|
102 | 102 | |
|
103 | 103 | TEST_GIT_REPO = jn(TESTS_TMP_PATH, GIT_REPO) |
|
104 | 104 | TEST_GIT_REPO_CLONE = jn(TESTS_TMP_PATH, 'vcsgitclone%s' % uniq_suffix) |
|
105 | 105 | TEST_GIT_REPO_PULL = jn(TESTS_TMP_PATH, 'vcsgitpull%s' % uniq_suffix) |
|
106 | 106 | |
|
107 | 107 | TEST_HG_REPO = jn(TESTS_TMP_PATH, HG_REPO) |
|
108 | 108 | TEST_HG_REPO_CLONE = jn(TESTS_TMP_PATH, 'vcshgclone%s' % uniq_suffix) |
|
109 | 109 | TEST_HG_REPO_PULL = jn(TESTS_TMP_PATH, 'vcshgpull%s' % uniq_suffix) |
|
110 | 110 | |
|
111 | 111 | TEST_REPO_PREFIX = 'vcs-test' |
|
112 | 112 | |
|
113 | 113 | |
|
114 | 114 | # skip ldap tests if LDAP lib is not installed |
|
115 | 115 | ldap_lib_installed = False |
|
116 | 116 | try: |
|
117 | 117 | import ldap |
|
118 | 118 | ldap_lib_installed = True |
|
119 | 119 | except ImportError: |
|
120 | 120 | # means that python-ldap is not installed |
|
121 | 121 | pass |
|
122 | 122 | |
|
123 | 123 | |
|
124 | 124 | def clear_all_caches(): |
|
125 | 125 | from beaker.cache import cache_managers |
|
126 | 126 | for _cache in cache_managers.values(): |
|
127 | 127 | _cache.clear() |
|
128 | 128 | |
|
129 | 129 | |
|
130 | 130 | def get_new_dir(title): |
|
131 | 131 | """ |
|
132 | 132 | Returns always new directory path. |
|
133 | 133 | """ |
|
134 | 134 | from rhodecode.tests.vcs.utils import get_normalized_path |
|
135 | 135 | name_parts = [TEST_REPO_PREFIX] |
|
136 | 136 | if title: |
|
137 | 137 | name_parts.append(title) |
|
138 | 138 | hex_str = hashlib.sha1('%s %s' % (os.getpid(), time.time())).hexdigest() |
|
139 | 139 | name_parts.append(hex_str) |
|
140 | 140 | name = '-'.join(name_parts) |
|
141 | 141 | path = os.path.join(TEST_DIR, name) |
|
142 | 142 | return get_normalized_path(path) |
|
143 | 143 | |
|
144 | 144 | |
|
145 | 145 | @pytest.mark.usefixtures('app', 'index_location') |
|
146 | 146 | class TestController(object): |
|
147 | 147 | |
|
148 | 148 | maxDiff = None |
|
149 | 149 | |
|
150 | 150 | def log_user(self, username=TEST_USER_ADMIN_LOGIN, |
|
151 | 151 | password=TEST_USER_ADMIN_PASS): |
|
152 | 152 | self._logged_username = username |
|
153 | 153 | self._session = login_user_session(self.app, username, password) |
|
154 | 154 | self.csrf_token = auth.get_csrf_token(self._session) |
|
155 | 155 | |
|
156 | 156 | return self._session['rhodecode_user'] |
|
157 | 157 | |
|
158 | 158 | def logout_user(self): |
|
159 | 159 | logout_user_session(self.app, auth.get_csrf_token(self._session)) |
|
160 | 160 | self.csrf_token = None |
|
161 | 161 | self._logged_username = None |
|
162 | 162 | self._session = None |
|
163 | 163 | |
|
164 | 164 | def _get_logged_user(self): |
|
165 | 165 | return User.get_by_username(self._logged_username) |
|
166 | 166 | |
|
167 | # TODO: remove, use plain assert in tests | |
|
168 | def assertEqual(self, a, b, msg=None): | |
|
169 | if msg: | |
|
170 | assert a == b, msg | |
|
171 | else: | |
|
172 | assert a == b | |
|
173 | ||
|
174 | 167 | |
|
175 | 168 | def login_user_session( |
|
176 | 169 | app, username=TEST_USER_ADMIN_LOGIN, password=TEST_USER_ADMIN_PASS): |
|
177 | 170 | from rhodecode.tests.functional.test_login import login_url |
|
178 | 171 | response = app.post( |
|
179 | 172 | login_url, |
|
180 | 173 | {'username': username, 'password': password}) |
|
181 | 174 | if 'invalid user name' in response.body: |
|
182 | 175 | pytest.fail('could not login using %s %s' % (username, password)) |
|
183 | 176 | |
|
184 | 177 | assert response.status == '302 Found' |
|
185 | 178 | response = response.follow() |
|
186 | 179 | assert response.status == '200 OK' |
|
187 | 180 | |
|
188 | 181 | session = get_session_from_response(response) |
|
189 | 182 | assert 'rhodecode_user' in session |
|
190 | 183 | rc_user = session['rhodecode_user'] |
|
191 | 184 | assert rc_user.get('username') == username |
|
192 | 185 | assert rc_user.get('is_authenticated') |
|
193 | 186 | |
|
194 | 187 | return session |
|
195 | 188 | |
|
196 | 189 | |
|
197 | 190 | def logout_user_session(app, csrf_token): |
|
198 | 191 | from rhodecode.tests.functional.test_login import logut_url |
|
199 | 192 | app.post(logut_url, {'csrf_token': csrf_token}, status=302) |
|
200 | 193 | |
|
201 | 194 | |
|
202 | 195 | def login_user(app, username=TEST_USER_ADMIN_LOGIN, |
|
203 | 196 | password=TEST_USER_ADMIN_PASS): |
|
204 | 197 | return login_user_session(app, username, password)['rhodecode_user'] |
|
205 | 198 | |
|
206 | 199 | |
|
207 | 200 | def assert_session_flash(response=None, msg=None, category=None): |
|
208 | 201 | """ |
|
209 | 202 | Assert on a flash message in the current session. |
|
210 | 203 | |
|
211 | 204 | :param msg: Required. The expected message. Will be evaluated if a |
|
212 | 205 | :class:`LazyString` is passed in. |
|
213 | 206 | :param response: Optional. For functional testing, pass in the response |
|
214 | 207 | object. Otherwise don't pass in any value. |
|
215 | 208 | :param category: Optional. If passed, the message category will be |
|
216 | 209 | checked as well. |
|
217 | 210 | """ |
|
218 | 211 | if msg is None: |
|
219 | 212 | raise ValueError("Parameter msg is required.") |
|
220 | 213 | |
|
221 | 214 | messages = flash.pop_messages() |
|
222 | 215 | msg = _eval_if_lazy(msg) |
|
223 | 216 | |
|
224 | 217 | assert messages, 'unable to find message `%s` in empty flash list' % msg |
|
225 | 218 | message = messages[0] |
|
226 | 219 | |
|
227 | 220 | message_text = _eval_if_lazy(message.message) |
|
228 | 221 | |
|
229 | 222 | if msg not in message_text: |
|
230 | 223 | fail_msg = u'msg `%s` not found in session ' \ |
|
231 | 224 | u'flash: got `%s` (type:%s) instead' % ( |
|
232 | 225 | msg, message_text, type(message_text)) |
|
233 | 226 | |
|
234 | 227 | pytest.fail(safe_str(fail_msg)) |
|
235 | 228 | if category: |
|
236 | 229 | assert category == message.category |
|
237 | 230 | |
|
238 | 231 | |
|
239 | 232 | def _eval_if_lazy(value): |
|
240 | 233 | return value.eval() if hasattr(value, 'eval') else value |
|
241 | 234 | |
|
242 | 235 | |
|
243 | 236 | def assert_not_in_session_flash(response, msg, category=None): |
|
244 | 237 | assert 'flash' in response.session, 'Response session has no flash key' |
|
245 | 238 | message_category, message_text = response.session['flash'][0] |
|
246 | 239 | if msg in message_text: |
|
247 | 240 | msg = u'msg `%s` found in session flash: got `%s` instead' % ( |
|
248 | 241 | msg, message_text) |
|
249 | 242 | pytest.fail(safe_str(msg)) |
|
250 | 243 | if category: |
|
251 | 244 | assert category == message_category |
|
252 | 245 | |
|
253 | 246 | |
|
254 | 247 | def assert_session_flash_is_empty(response): |
|
255 | 248 | if 'flash' in response.session: |
|
256 | 249 | msg = 'flash messages are present in session:%s' % \ |
|
257 | 250 | response.session['flash'][0] |
|
258 | 251 | pytest.fail(safe_str(msg)) |
General Comments 0
You need to be logged in to leave comments.
Login now