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