##// END OF EJS Templates
tests docs
marcink -
r3637:58c2b545 beta
parent child Browse files
Show More
@@ -1,202 +1,208 b''
1 """Pylons application test package
1 """Pylons application test package
2
2
3 This package assumes the Pylons environment is already loaded, such as
3 This package assumes the Pylons environment is already loaded, such as
4 when this script is imported from the `nosetests --with-pylons=test.ini`
4 when this script is imported from the `nosetests --with-pylons=test.ini`
5 command.
5 command.
6
6
7 This module initializes the application via ``websetup`` (`paster
7 This module initializes the application via ``websetup`` (`paster
8 setup-app`) and provides the base testing objects.
8 setup-app`) and provides the base testing objects.
9
10 nosetests -x - fail on first error
11 nosetests rhodecode.tests.functional.test_admin_settings:TestSettingsController.test_my_account
12 nosetests --pdb --pdb-failures
13 nosetests --with-coverage --cover-package=rhodecode.model.validators rhodecode.tests.test_validators
14
15 optional FLAGS:
16 RC_WHOOSH_TEST_DISABLE=1 - skip whoosh index building and tests
17 RC_NO_TMP_PATH=1 - disable new temp path for tests, used mostly for test_vcs_operations
18
9 """
19 """
10 import os
20 import os
11 import time
21 import time
12 import logging
22 import logging
13 import datetime
23 import datetime
14 import hashlib
24 import hashlib
15 import tempfile
25 import tempfile
16 from os.path import join as jn
26 from os.path import join as jn
17
27
18 from unittest import TestCase
28 from unittest import TestCase
19 from tempfile import _RandomNameSequence
29 from tempfile import _RandomNameSequence
20
30
21 from paste.deploy import loadapp
31 from paste.deploy import loadapp
22 from paste.script.appinstall import SetupCommand
32 from paste.script.appinstall import SetupCommand
23 from pylons import config, url
33 from pylons import config, url
24 from routes.util import URLGenerator
34 from routes.util import URLGenerator
25 from webtest import TestApp
35 from webtest import TestApp
26
36
27 from rhodecode import is_windows
37 from rhodecode import is_windows
28 from rhodecode.model.meta import Session
38 from rhodecode.model.meta import Session
29 from rhodecode.model.db import User
39 from rhodecode.model.db import User
30 from rhodecode.tests.nose_parametrized import parameterized
40 from rhodecode.tests.nose_parametrized import parameterized
31
41
32 import pylons.test
42 import pylons.test
33 from rhodecode.lib.utils2 import safe_unicode, safe_str
43 from rhodecode.lib.utils2 import safe_unicode, safe_str
34
44
35
45
36 os.environ['TZ'] = 'UTC'
46 os.environ['TZ'] = 'UTC'
37 if not is_windows:
47 if not is_windows:
38 time.tzset()
48 time.tzset()
39
49
40 log = logging.getLogger(__name__)
50 log = logging.getLogger(__name__)
41
51
42 __all__ = [
52 __all__ = [
43 'parameterized', 'environ', 'url', 'get_new_dir', 'TestController',
53 'parameterized', 'environ', 'url', 'get_new_dir', 'TestController',
44 'TESTS_TMP_PATH', 'HG_REPO', 'GIT_REPO', 'NEW_HG_REPO', 'NEW_GIT_REPO',
54 'TESTS_TMP_PATH', 'HG_REPO', 'GIT_REPO', 'NEW_HG_REPO', 'NEW_GIT_REPO',
45 'HG_FORK', 'GIT_FORK', 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_ADMIN_PASS',
55 'HG_FORK', 'GIT_FORK', 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_ADMIN_PASS',
46 'TEST_USER_REGULAR_LOGIN', 'TEST_USER_REGULAR_PASS',
56 'TEST_USER_REGULAR_LOGIN', 'TEST_USER_REGULAR_PASS',
47 'TEST_USER_REGULAR_EMAIL', 'TEST_USER_REGULAR2_LOGIN',
57 'TEST_USER_REGULAR_EMAIL', 'TEST_USER_REGULAR2_LOGIN',
48 'TEST_USER_REGULAR2_PASS', 'TEST_USER_REGULAR2_EMAIL', 'TEST_HG_REPO',
58 'TEST_USER_REGULAR2_PASS', 'TEST_USER_REGULAR2_EMAIL', 'TEST_HG_REPO',
49 'TEST_HG_REPO_CLONE', 'TEST_HG_REPO_PULL', 'TEST_GIT_REPO',
59 'TEST_HG_REPO_CLONE', 'TEST_HG_REPO_PULL', 'TEST_GIT_REPO',
50 'TEST_GIT_REPO_CLONE', 'TEST_GIT_REPO_PULL', 'HG_REMOTE_REPO',
60 'TEST_GIT_REPO_CLONE', 'TEST_GIT_REPO_PULL', 'HG_REMOTE_REPO',
51 'GIT_REMOTE_REPO', 'SCM_TESTS', '_get_repo_create_params',
61 'GIT_REMOTE_REPO', 'SCM_TESTS', '_get_repo_create_params',
52 '_get_group_create_params'
62 '_get_group_create_params'
53 ]
63 ]
54
64
55 # Invoke websetup with the current config file
65 # Invoke websetup with the current config file
56 # SetupCommand('setup-app').run([config_file])
66 # SetupCommand('setup-app').run([config_file])
57
67
58 ##RUNNING DESIRED TESTS
59 # nosetests -x rhodecode.tests.functional.test_admin_settings:TestSettingsController.test_my_account
60 # nosetests --pdb --pdb-failures
61 # nosetests --with-coverage --cover-package=rhodecode.model.validators rhodecode.tests.test_validators
62 environ = {}
68 environ = {}
63
69
64 #SOME GLOBALS FOR TESTS
70 #SOME GLOBALS FOR TESTS
65
71
66 TESTS_TMP_PATH = jn('/', 'tmp', 'rc_test_%s' % _RandomNameSequence().next())
72 TESTS_TMP_PATH = jn('/', 'tmp', 'rc_test_%s' % _RandomNameSequence().next())
67 TEST_USER_ADMIN_LOGIN = 'test_admin'
73 TEST_USER_ADMIN_LOGIN = 'test_admin'
68 TEST_USER_ADMIN_PASS = 'test12'
74 TEST_USER_ADMIN_PASS = 'test12'
69 TEST_USER_ADMIN_EMAIL = 'test_admin@mail.com'
75 TEST_USER_ADMIN_EMAIL = 'test_admin@mail.com'
70
76
71 TEST_USER_REGULAR_LOGIN = 'test_regular'
77 TEST_USER_REGULAR_LOGIN = 'test_regular'
72 TEST_USER_REGULAR_PASS = 'test12'
78 TEST_USER_REGULAR_PASS = 'test12'
73 TEST_USER_REGULAR_EMAIL = 'test_regular@mail.com'
79 TEST_USER_REGULAR_EMAIL = 'test_regular@mail.com'
74
80
75 TEST_USER_REGULAR2_LOGIN = 'test_regular2'
81 TEST_USER_REGULAR2_LOGIN = 'test_regular2'
76 TEST_USER_REGULAR2_PASS = 'test12'
82 TEST_USER_REGULAR2_PASS = 'test12'
77 TEST_USER_REGULAR2_EMAIL = 'test_regular2@mail.com'
83 TEST_USER_REGULAR2_EMAIL = 'test_regular2@mail.com'
78
84
79 HG_REPO = 'vcs_test_hg'
85 HG_REPO = 'vcs_test_hg'
80 GIT_REPO = 'vcs_test_git'
86 GIT_REPO = 'vcs_test_git'
81
87
82 NEW_HG_REPO = 'vcs_test_hg_new'
88 NEW_HG_REPO = 'vcs_test_hg_new'
83 NEW_GIT_REPO = 'vcs_test_git_new'
89 NEW_GIT_REPO = 'vcs_test_git_new'
84
90
85 HG_FORK = 'vcs_test_hg_fork'
91 HG_FORK = 'vcs_test_hg_fork'
86 GIT_FORK = 'vcs_test_git_fork'
92 GIT_FORK = 'vcs_test_git_fork'
87
93
88 ## VCS
94 ## VCS
89 SCM_TESTS = ['hg', 'git']
95 SCM_TESTS = ['hg', 'git']
90 uniq_suffix = str(int(time.mktime(datetime.datetime.now().timetuple())))
96 uniq_suffix = str(int(time.mktime(datetime.datetime.now().timetuple())))
91
97
92 GIT_REMOTE_REPO = 'git://github.com/codeinn/vcs.git'
98 GIT_REMOTE_REPO = 'git://github.com/codeinn/vcs.git'
93
99
94 TEST_GIT_REPO = jn(TESTS_TMP_PATH, GIT_REPO)
100 TEST_GIT_REPO = jn(TESTS_TMP_PATH, GIT_REPO)
95 TEST_GIT_REPO_CLONE = jn(TESTS_TMP_PATH, 'vcsgitclone%s' % uniq_suffix)
101 TEST_GIT_REPO_CLONE = jn(TESTS_TMP_PATH, 'vcsgitclone%s' % uniq_suffix)
96 TEST_GIT_REPO_PULL = jn(TESTS_TMP_PATH, 'vcsgitpull%s' % uniq_suffix)
102 TEST_GIT_REPO_PULL = jn(TESTS_TMP_PATH, 'vcsgitpull%s' % uniq_suffix)
97
103
98
104
99 HG_REMOTE_REPO = 'http://bitbucket.org/marcinkuzminski/vcs'
105 HG_REMOTE_REPO = 'http://bitbucket.org/marcinkuzminski/vcs'
100
106
101 TEST_HG_REPO = jn(TESTS_TMP_PATH, HG_REPO)
107 TEST_HG_REPO = jn(TESTS_TMP_PATH, HG_REPO)
102 TEST_HG_REPO_CLONE = jn(TESTS_TMP_PATH, 'vcshgclone%s' % uniq_suffix)
108 TEST_HG_REPO_CLONE = jn(TESTS_TMP_PATH, 'vcshgclone%s' % uniq_suffix)
103 TEST_HG_REPO_PULL = jn(TESTS_TMP_PATH, 'vcshgpull%s' % uniq_suffix)
109 TEST_HG_REPO_PULL = jn(TESTS_TMP_PATH, 'vcshgpull%s' % uniq_suffix)
104
110
105 TEST_DIR = tempfile.gettempdir()
111 TEST_DIR = tempfile.gettempdir()
106 TEST_REPO_PREFIX = 'vcs-test'
112 TEST_REPO_PREFIX = 'vcs-test'
107
113
108 # cached repos if any !
114 # cached repos if any !
109 # comment out to get some other repos from bb or github
115 # comment out to get some other repos from bb or github
110 GIT_REMOTE_REPO = jn(TESTS_TMP_PATH, GIT_REPO)
116 GIT_REMOTE_REPO = jn(TESTS_TMP_PATH, GIT_REPO)
111 HG_REMOTE_REPO = jn(TESTS_TMP_PATH, HG_REPO)
117 HG_REMOTE_REPO = jn(TESTS_TMP_PATH, HG_REPO)
112
118
113
119
114 def get_new_dir(title):
120 def get_new_dir(title):
115 """
121 """
116 Returns always new directory path.
122 Returns always new directory path.
117 """
123 """
118 from rhodecode.tests.vcs.utils import get_normalized_path
124 from rhodecode.tests.vcs.utils import get_normalized_path
119 name = TEST_REPO_PREFIX
125 name = TEST_REPO_PREFIX
120 if title:
126 if title:
121 name = '-'.join((name, title))
127 name = '-'.join((name, title))
122 hex = hashlib.sha1(str(time.time())).hexdigest()
128 hex = hashlib.sha1(str(time.time())).hexdigest()
123 name = '-'.join((name, hex))
129 name = '-'.join((name, hex))
124 path = os.path.join(TEST_DIR, name)
130 path = os.path.join(TEST_DIR, name)
125 return get_normalized_path(path)
131 return get_normalized_path(path)
126
132
127
133
128 class TestController(TestCase):
134 class TestController(TestCase):
129
135
130 def __init__(self, *args, **kwargs):
136 def __init__(self, *args, **kwargs):
131 wsgiapp = pylons.test.pylonsapp
137 wsgiapp = pylons.test.pylonsapp
132 config = wsgiapp.config
138 config = wsgiapp.config
133
139
134 self.app = TestApp(wsgiapp)
140 self.app = TestApp(wsgiapp)
135 url._push_object(URLGenerator(config['routes.map'], environ))
141 url._push_object(URLGenerator(config['routes.map'], environ))
136 self.Session = Session
142 self.Session = Session
137 self.index_location = config['app_conf']['index_dir']
143 self.index_location = config['app_conf']['index_dir']
138 TestCase.__init__(self, *args, **kwargs)
144 TestCase.__init__(self, *args, **kwargs)
139
145
140 def log_user(self, username=TEST_USER_ADMIN_LOGIN,
146 def log_user(self, username=TEST_USER_ADMIN_LOGIN,
141 password=TEST_USER_ADMIN_PASS):
147 password=TEST_USER_ADMIN_PASS):
142 self._logged_username = username
148 self._logged_username = username
143 response = self.app.post(url(controller='login', action='index'),
149 response = self.app.post(url(controller='login', action='index'),
144 {'username': username,
150 {'username': username,
145 'password': password})
151 'password': password})
146
152
147 if 'invalid user name' in response.body:
153 if 'invalid user name' in response.body:
148 self.fail('could not login using %s %s' % (username, password))
154 self.fail('could not login using %s %s' % (username, password))
149
155
150 self.assertEqual(response.status, '302 Found')
156 self.assertEqual(response.status, '302 Found')
151 ses = response.session['rhodecode_user']
157 ses = response.session['rhodecode_user']
152 self.assertEqual(ses.get('username'), username)
158 self.assertEqual(ses.get('username'), username)
153 response = response.follow()
159 response = response.follow()
154 self.assertEqual(ses.get('is_authenticated'), True)
160 self.assertEqual(ses.get('is_authenticated'), True)
155
161
156 return response.session['rhodecode_user']
162 return response.session['rhodecode_user']
157
163
158 def _get_logged_user(self):
164 def _get_logged_user(self):
159 return User.get_by_username(self._logged_username)
165 return User.get_by_username(self._logged_username)
160
166
161 def checkSessionFlash(self, response, msg):
167 def checkSessionFlash(self, response, msg):
162 self.assertTrue('flash' in response.session,
168 self.assertTrue('flash' in response.session,
163 msg='Response session:%r have no flash'
169 msg='Response session:%r have no flash'
164 % response.session)
170 % response.session)
165 if not msg in response.session['flash'][0][1]:
171 if not msg in response.session['flash'][0][1]:
166 msg = u'msg `%s` not found in session flash: got `%s` instead' % (
172 msg = u'msg `%s` not found in session flash: got `%s` instead' % (
167 msg, response.session['flash'][0][1])
173 msg, response.session['flash'][0][1])
168 self.fail(safe_str(msg))
174 self.fail(safe_str(msg))
169
175
170
176
171 ## HELPERS ##
177 ## HELPERS ##
172
178
173 def _get_repo_create_params(**custom):
179 def _get_repo_create_params(**custom):
174 defs = {
180 defs = {
175 'repo_name': None,
181 'repo_name': None,
176 'repo_type': 'hg',
182 'repo_type': 'hg',
177 'clone_uri': '',
183 'clone_uri': '',
178 'repo_group': '',
184 'repo_group': '',
179 'repo_description': 'DESC',
185 'repo_description': 'DESC',
180 'repo_private': False,
186 'repo_private': False,
181 'repo_landing_rev': 'tip'
187 'repo_landing_rev': 'tip'
182 }
188 }
183 defs.update(custom)
189 defs.update(custom)
184 if 'repo_name_full' not in custom:
190 if 'repo_name_full' not in custom:
185 defs.update({'repo_name_full': defs['repo_name']})
191 defs.update({'repo_name_full': defs['repo_name']})
186
192
187 return defs
193 return defs
188
194
189
195
190 def _get_group_create_params(**custom):
196 def _get_group_create_params(**custom):
191 defs = dict(
197 defs = dict(
192 group_name=None,
198 group_name=None,
193 group_description='DESC',
199 group_description='DESC',
194 group_parent_id=None,
200 group_parent_id=None,
195 perms_updates=[],
201 perms_updates=[],
196 perms_new=[],
202 perms_new=[],
197 enable_locking=False,
203 enable_locking=False,
198 recursive=False
204 recursive=False
199 )
205 )
200 defs.update(custom)
206 defs.update(custom)
201
207
202 return defs
208 return defs
General Comments 0
You need to be logged in to leave comments. Login now