Show More
@@ -1,19 +1,19 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Email message and email sending related helper functions. |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | import socket |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | # Cache the hostname, but do it lazily: socket.getfqdn() can take a couple of |
|
9 | 9 | # seconds, which slows down the restart of the server. |
|
10 | 10 | class CachedDnsName(object): |
|
11 | 11 | def __str__(self): |
|
12 | 12 | return self.get_fqdn() |
|
13 | 13 | |
|
14 | 14 | def get_fqdn(self): |
|
15 | 15 | if not hasattr(self, '_fqdn'): |
|
16 | 16 | self._fqdn = socket.getfqdn() |
|
17 | 17 | return self._fqdn |
|
18 | 18 | |
|
19 | DNS_NAME = CachedDnsName() No newline at end of file | |
|
19 | DNS_NAME = CachedDnsName() |
@@ -1,46 +1,46 b'' | |||
|
1 | 1 | <div id='repos_list_wrap' class="yui-skin-sam"> |
|
2 | 2 | <table id="repos_list"> |
|
3 | 3 | <thead> |
|
4 | 4 | <tr> |
|
5 | 5 | <th></th> |
|
6 | 6 | <th class="left">${_('Name')}</th> |
|
7 | 7 | <th class="left">${_('Revision')}</th> |
|
8 | 8 | <th class="left">${_('Action')}</th> |
|
9 | 9 | <th class="left">${_('Action')}</th> |
|
10 | 10 | </thead> |
|
11 | 11 | <tbody> |
|
12 | 12 | <%namespace name="dt" file="/data_table/_dt_elements.html"/> |
|
13 | 13 | %if c.user_repos: |
|
14 | 14 | %for repo in c.user_repos: |
|
15 | 15 | <tr> |
|
16 | 16 | ##QUICK MENU |
|
17 | 17 | <td class="quick_repo_menu"> |
|
18 | 18 | ${dt.quick_menu(repo['name'])} |
|
19 | 19 | </td> |
|
20 | 20 | ##REPO NAME AND ICONS |
|
21 | 21 | <td class="reponame"> |
|
22 | 22 | ${dt.repo_name(repo['name'],repo['dbrepo']['repo_type'],repo['dbrepo']['private'],repo['dbrepo_fork'].get('repo_name'))} |
|
23 | 23 | </td> |
|
24 | 24 | ##LAST REVISION |
|
25 | 25 | <td> |
|
26 | 26 | ${dt.revision(repo['name'],repo['rev'],repo['tip'],repo['author'],repo['last_msg'])} |
|
27 | 27 | </td> |
|
28 | 28 | <td><a href="${h.url('repo_settings_home',repo_name=repo['name'])}" title="${_('edit')}"><img class="icon" alt="${_('private')}" src="${h.url('/images/icons/application_form_edit.png')}"/></a></td> |
|
29 | 29 | <td> |
|
30 | 30 | ${h.form(url('repo_settings_delete', repo_name=repo['name']),method='delete')} |
|
31 | 31 | ${h.submit('remove_%s' % repo['name'],'',class_="delete_icon action_button",onclick="return confirm('"+_('Confirm to delete this repository: %s') % repo['name']+"');")} |
|
32 | 32 | ${h.end_form()} |
|
33 | 33 | </td> |
|
34 | 34 | </tr> |
|
35 | 35 | %endfor |
|
36 | 36 | %else: |
|
37 | 37 | <div style="padding:5px 0px 10px 0px;"> |
|
38 | 38 | ${_('No repositories yet')} |
|
39 | 39 | %if h.HasPermissionAny('hg.admin','hg.create.repository')(): |
|
40 | 40 | ${h.link_to(_('create one now'),h.url('admin_settings_create_repository'),class_="ui-btn")} |
|
41 | 41 | %endif |
|
42 | 42 | </div> |
|
43 | 43 | %endif |
|
44 | 44 | </tbody> |
|
45 | 45 | </table> |
|
46 | </div> No newline at end of file | |
|
46 | </div> |
@@ -1,991 +1,990 b'' | |||
|
1 | 1 | from __future__ import with_statement |
|
2 | 2 | import random |
|
3 | 3 | import mock |
|
4 | 4 | |
|
5 | 5 | from rhodecode.tests import * |
|
6 | 6 | from rhodecode.lib.compat import json |
|
7 | 7 | from rhodecode.lib.auth import AuthUser |
|
8 | 8 | from rhodecode.model.user import UserModel |
|
9 | 9 | from rhodecode.model.users_group import UsersGroupModel |
|
10 | 10 | from rhodecode.model.repo import RepoModel |
|
11 | 11 | from rhodecode.model.meta import Session |
|
12 | 12 | |
|
13 | 13 | API_URL = '/_admin/api' |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | def _build_data(apikey, method, **kw): |
|
17 | 17 | """ |
|
18 | 18 | Builds API data with given random ID |
|
19 | 19 | |
|
20 | 20 | :param random_id: |
|
21 | 21 | :type random_id: |
|
22 | 22 | """ |
|
23 | 23 | random_id = random.randrange(1, 9999) |
|
24 | 24 | return random_id, json.dumps({ |
|
25 | 25 | "id": random_id, |
|
26 | 26 | "api_key": apikey, |
|
27 | 27 | "method": method, |
|
28 | 28 | "args": kw |
|
29 | 29 | }) |
|
30 | 30 | |
|
31 | 31 | jsonify = lambda obj: json.loads(json.dumps(obj)) |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | def crash(*args, **kwargs): |
|
35 | 35 | raise Exception('Total Crash !') |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | TEST_USERS_GROUP = 'test_users_group' |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | def make_users_group(name=TEST_USERS_GROUP): |
|
42 | 42 | gr = UsersGroupModel().create(name=name) |
|
43 | 43 | UsersGroupModel().add_user_to_group(users_group=gr, |
|
44 | 44 | user=TEST_USER_ADMIN_LOGIN) |
|
45 | 45 | Session().commit() |
|
46 | 46 | return gr |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | def destroy_users_group(name=TEST_USERS_GROUP): |
|
50 | 50 | UsersGroupModel().delete(users_group=name, force=True) |
|
51 | 51 | Session().commit() |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | def create_repo(repo_name, repo_type): |
|
55 | 55 | # create new repo |
|
56 | 56 | form_data = dict(repo_name=repo_name, |
|
57 | 57 | repo_name_full=repo_name, |
|
58 | 58 | fork_name=None, |
|
59 | 59 | description='description %s' % repo_name, |
|
60 | 60 | repo_group=None, |
|
61 | 61 | private=False, |
|
62 | 62 | repo_type=repo_type, |
|
63 | 63 | clone_uri=None, |
|
64 | 64 | landing_rev='tip') |
|
65 | 65 | cur_user = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
|
66 | 66 | r = RepoModel().create(form_data, cur_user) |
|
67 | 67 | Session().commit() |
|
68 | 68 | return r |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | def create_fork(fork_name, fork_type, fork_of): |
|
72 | 72 | fork = RepoModel(Session())._get_repo(fork_of) |
|
73 | 73 | r = create_repo(fork_name, fork_type) |
|
74 | 74 | r.fork = fork |
|
75 | 75 | Session().add(r) |
|
76 | 76 | Session().commit() |
|
77 | 77 | return r |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | def destroy_repo(repo_name): |
|
81 | 81 | RepoModel().delete(repo_name) |
|
82 | 82 | Session().commit() |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | class BaseTestApi(object): |
|
86 | 86 | REPO = None |
|
87 | 87 | REPO_TYPE = None |
|
88 | 88 | |
|
89 | 89 | @classmethod |
|
90 | 90 | def setUpClass(self): |
|
91 | 91 | self.usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
|
92 | 92 | self.apikey = self.usr.api_key |
|
93 | 93 | self.TEST_USER = UserModel().create_or_update( |
|
94 | 94 | username='test-api', |
|
95 | 95 | password='test', |
|
96 | 96 | email='test@api.rhodecode.org', |
|
97 | 97 | firstname='first', |
|
98 | 98 | lastname='last' |
|
99 | 99 | ) |
|
100 | 100 | Session().commit() |
|
101 | 101 | self.TEST_USER_LOGIN = self.TEST_USER.username |
|
102 | 102 | |
|
103 | 103 | @classmethod |
|
104 | 104 | def teardownClass(self): |
|
105 | 105 | pass |
|
106 | 106 | |
|
107 | 107 | def setUp(self): |
|
108 | 108 | self.maxDiff = None |
|
109 | 109 | make_users_group() |
|
110 | 110 | |
|
111 | 111 | def tearDown(self): |
|
112 | 112 | destroy_users_group() |
|
113 | 113 | |
|
114 | 114 | def _compare_ok(self, id_, expected, given): |
|
115 | 115 | expected = jsonify({ |
|
116 | 116 | 'id': id_, |
|
117 | 117 | 'error': None, |
|
118 | 118 | 'result': expected |
|
119 | 119 | }) |
|
120 | 120 | given = json.loads(given) |
|
121 | 121 | self.assertEqual(expected, given) |
|
122 | 122 | |
|
123 | 123 | def _compare_error(self, id_, expected, given): |
|
124 | 124 | expected = jsonify({ |
|
125 | 125 | 'id': id_, |
|
126 | 126 | 'error': expected, |
|
127 | 127 | 'result': None |
|
128 | 128 | }) |
|
129 | 129 | given = json.loads(given) |
|
130 | 130 | self.assertEqual(expected, given) |
|
131 | 131 | |
|
132 | 132 | # def test_Optional(self): |
|
133 | 133 | # from rhodecode.controllers.api.api import Optional |
|
134 | 134 | # option1 = Optional(None) |
|
135 | 135 | # self.assertEqual('<Optional:%s>' % None, repr(option1)) |
|
136 | 136 | # |
|
137 | 137 | # self.assertEqual(1, Optional.extract(Optional(1))) |
|
138 | 138 | # self.assertEqual('trololo', Optional.extract('trololo')) |
|
139 | 139 | |
|
140 | 140 | def test_api_wrong_key(self): |
|
141 | 141 | id_, params = _build_data('trololo', 'get_user') |
|
142 | 142 | response = self.app.post(API_URL, content_type='application/json', |
|
143 | 143 | params=params) |
|
144 | 144 | |
|
145 | 145 | expected = 'Invalid API KEY' |
|
146 | 146 | self._compare_error(id_, expected, given=response.body) |
|
147 | 147 | |
|
148 | 148 | def test_api_missing_non_optional_param(self): |
|
149 | 149 | id_, params = _build_data(self.apikey, 'get_user') |
|
150 | 150 | response = self.app.post(API_URL, content_type='application/json', |
|
151 | 151 | params=params) |
|
152 | 152 | |
|
153 | 153 | expected = 'Missing non optional `userid` arg in JSON DATA' |
|
154 | 154 | self._compare_error(id_, expected, given=response.body) |
|
155 | 155 | |
|
156 | 156 | def test_api_get_users(self): |
|
157 | 157 | id_, params = _build_data(self.apikey, 'get_users',) |
|
158 | 158 | response = self.app.post(API_URL, content_type='application/json', |
|
159 | 159 | params=params) |
|
160 | 160 | ret_all = [] |
|
161 | 161 | for usr in UserModel().get_all(): |
|
162 | 162 | ret = usr.get_api_data() |
|
163 | 163 | ret_all.append(jsonify(ret)) |
|
164 | 164 | expected = ret_all |
|
165 | 165 | self._compare_ok(id_, expected, given=response.body) |
|
166 | 166 | |
|
167 | 167 | def test_api_get_user(self): |
|
168 | 168 | id_, params = _build_data(self.apikey, 'get_user', |
|
169 | 169 | userid=TEST_USER_ADMIN_LOGIN) |
|
170 | 170 | response = self.app.post(API_URL, content_type='application/json', |
|
171 | 171 | params=params) |
|
172 | 172 | |
|
173 | 173 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
|
174 | 174 | ret = usr.get_api_data() |
|
175 | 175 | ret['permissions'] = AuthUser(usr.user_id).permissions |
|
176 | 176 | |
|
177 | 177 | expected = ret |
|
178 | 178 | self._compare_ok(id_, expected, given=response.body) |
|
179 | 179 | |
|
180 | 180 | def test_api_get_user_that_does_not_exist(self): |
|
181 | 181 | id_, params = _build_data(self.apikey, 'get_user', |
|
182 | 182 | userid='trololo') |
|
183 | 183 | response = self.app.post(API_URL, content_type='application/json', |
|
184 | 184 | params=params) |
|
185 | 185 | |
|
186 | 186 | expected = "user `%s` does not exist" % 'trololo' |
|
187 | 187 | self._compare_error(id_, expected, given=response.body) |
|
188 | 188 | |
|
189 | 189 | def test_api_pull(self): |
|
190 | 190 | #TODO: issues with rhodecode_extras here.. not sure why ! |
|
191 | 191 | pass |
|
192 | 192 | |
|
193 | 193 | # repo_name = 'test_pull' |
|
194 | 194 | # r = create_repo(repo_name, self.REPO_TYPE) |
|
195 | 195 | # r.clone_uri = TEST_self.REPO |
|
196 | 196 | # Session.add(r) |
|
197 | 197 | # Session.commit() |
|
198 | 198 | # |
|
199 | 199 | # id_, params = _build_data(self.apikey, 'pull', |
|
200 | 200 | # repoid=repo_name,) |
|
201 | 201 | # response = self.app.post(API_URL, content_type='application/json', |
|
202 | 202 | # params=params) |
|
203 | 203 | # |
|
204 | 204 | # expected = 'Pulled from `%s`' % repo_name |
|
205 | 205 | # self._compare_ok(id_, expected, given=response.body) |
|
206 | 206 | # |
|
207 | 207 | # destroy_repo(repo_name) |
|
208 | 208 | |
|
209 | 209 | def test_api_pull_error(self): |
|
210 | 210 | id_, params = _build_data(self.apikey, 'pull', |
|
211 | 211 | repoid=self.REPO,) |
|
212 | 212 | response = self.app.post(API_URL, content_type='application/json', |
|
213 | 213 | params=params) |
|
214 | 214 | |
|
215 | 215 | expected = 'Unable to pull changes from `%s`' % self.REPO |
|
216 | 216 | self._compare_error(id_, expected, given=response.body) |
|
217 | 217 | |
|
218 | 218 | def test_api_create_existing_user(self): |
|
219 | 219 | id_, params = _build_data(self.apikey, 'create_user', |
|
220 | 220 | username=TEST_USER_ADMIN_LOGIN, |
|
221 | 221 | email='test@foo.com', |
|
222 | 222 | password='trololo') |
|
223 | 223 | response = self.app.post(API_URL, content_type='application/json', |
|
224 | 224 | params=params) |
|
225 | 225 | |
|
226 | 226 | expected = "user `%s` already exist" % TEST_USER_ADMIN_LOGIN |
|
227 | 227 | self._compare_error(id_, expected, given=response.body) |
|
228 | 228 | |
|
229 | 229 | def test_api_create_user_with_existing_email(self): |
|
230 | 230 | id_, params = _build_data(self.apikey, 'create_user', |
|
231 | 231 | username=TEST_USER_ADMIN_LOGIN + 'new', |
|
232 | 232 | email=TEST_USER_REGULAR_EMAIL, |
|
233 | 233 | password='trololo') |
|
234 | 234 | response = self.app.post(API_URL, content_type='application/json', |
|
235 | 235 | params=params) |
|
236 | 236 | |
|
237 | 237 | expected = "email `%s` already exist" % TEST_USER_REGULAR_EMAIL |
|
238 | 238 | self._compare_error(id_, expected, given=response.body) |
|
239 | 239 | |
|
240 | 240 | def test_api_create_user(self): |
|
241 | 241 | username = 'test_new_api_user' |
|
242 | 242 | email = username + "@foo.com" |
|
243 | 243 | |
|
244 | 244 | id_, params = _build_data(self.apikey, 'create_user', |
|
245 | 245 | username=username, |
|
246 | 246 | email=email, |
|
247 | 247 | password='trololo') |
|
248 | 248 | response = self.app.post(API_URL, content_type='application/json', |
|
249 | 249 | params=params) |
|
250 | 250 | |
|
251 | 251 | usr = UserModel().get_by_username(username) |
|
252 | 252 | ret = dict( |
|
253 | 253 | msg='created new user `%s`' % username, |
|
254 | 254 | user=jsonify(usr.get_api_data()) |
|
255 | 255 | ) |
|
256 | 256 | |
|
257 | 257 | expected = ret |
|
258 | 258 | self._compare_ok(id_, expected, given=response.body) |
|
259 | 259 | |
|
260 | 260 | UserModel().delete(usr.user_id) |
|
261 | 261 | self.Session().commit() |
|
262 | 262 | |
|
263 | 263 | @mock.patch.object(UserModel, 'create_or_update', crash) |
|
264 | 264 | def test_api_create_user_when_exception_happened(self): |
|
265 | 265 | |
|
266 | 266 | username = 'test_new_api_user' |
|
267 | 267 | email = username + "@foo.com" |
|
268 | 268 | |
|
269 | 269 | id_, params = _build_data(self.apikey, 'create_user', |
|
270 | 270 | username=username, |
|
271 | 271 | email=email, |
|
272 | 272 | password='trololo') |
|
273 | 273 | response = self.app.post(API_URL, content_type='application/json', |
|
274 | 274 | params=params) |
|
275 | 275 | expected = 'failed to create user `%s`' % username |
|
276 | 276 | self._compare_error(id_, expected, given=response.body) |
|
277 | 277 | |
|
278 | 278 | def test_api_delete_user(self): |
|
279 | 279 | usr = UserModel().create_or_update(username=u'test_user', |
|
280 | 280 | password=u'qweqwe', |
|
281 | 281 | email=u'u232@rhodecode.org', |
|
282 | 282 | firstname=u'u1', lastname=u'u1') |
|
283 | 283 | self.Session().commit() |
|
284 | 284 | username = usr.username |
|
285 | 285 | email = usr.email |
|
286 | 286 | usr_id = usr.user_id |
|
287 | 287 | ## DELETE THIS USER NOW |
|
288 | 288 | |
|
289 | 289 | id_, params = _build_data(self.apikey, 'delete_user', |
|
290 | 290 | userid=username,) |
|
291 | 291 | response = self.app.post(API_URL, content_type='application/json', |
|
292 | 292 | params=params) |
|
293 | 293 | |
|
294 | 294 | ret = {'msg': 'deleted user ID:%s %s' % (usr_id, username), |
|
295 | 295 | 'user': None} |
|
296 | 296 | expected = ret |
|
297 | 297 | self._compare_ok(id_, expected, given=response.body) |
|
298 | 298 | |
|
299 | 299 | @mock.patch.object(UserModel, 'delete', crash) |
|
300 | 300 | def test_api_delete_user_when_exception_happened(self): |
|
301 | 301 | usr = UserModel().create_or_update(username=u'test_user', |
|
302 | 302 | password=u'qweqwe', |
|
303 | 303 | email=u'u232@rhodecode.org', |
|
304 | 304 | firstname=u'u1', lastname=u'u1') |
|
305 | 305 | self.Session().commit() |
|
306 | 306 | username = usr.username |
|
307 | 307 | |
|
308 | 308 | id_, params = _build_data(self.apikey, 'delete_user', |
|
309 | 309 | userid=username,) |
|
310 | 310 | response = self.app.post(API_URL, content_type='application/json', |
|
311 | 311 | params=params) |
|
312 | 312 | ret = 'failed to delete ID:%s %s' % (usr.user_id, |
|
313 | 313 | usr.username) |
|
314 | 314 | expected = ret |
|
315 | 315 | self._compare_error(id_, expected, given=response.body) |
|
316 | 316 | |
|
317 | 317 | @parameterized.expand([('firstname', 'new_username'), |
|
318 | 318 | ('lastname', 'new_username'), |
|
319 | 319 | ('email', 'new_username'), |
|
320 | 320 | ('admin', True), |
|
321 | 321 | ('admin', False), |
|
322 | 322 | ('ldap_dn', 'test'), |
|
323 | 323 | ('ldap_dn', None), |
|
324 | 324 | ('active', False), |
|
325 | 325 | ('active', True), |
|
326 | 326 | ('password', 'newpass') |
|
327 | 327 | ]) |
|
328 | 328 | def test_api_update_user(self, name, expected): |
|
329 | 329 | usr = UserModel().get_by_username(self.TEST_USER_LOGIN) |
|
330 | 330 | kw = {name: expected, |
|
331 | 331 | 'userid': usr.user_id} |
|
332 | 332 | id_, params = _build_data(self.apikey, 'update_user', **kw) |
|
333 | 333 | response = self.app.post(API_URL, content_type='application/json', |
|
334 | 334 | params=params) |
|
335 | 335 | |
|
336 | 336 | ret = { |
|
337 | 337 | 'msg': 'updated user ID:%s %s' % (usr.user_id, self.TEST_USER_LOGIN), |
|
338 | 338 | 'user': jsonify(UserModel()\ |
|
339 | 339 | .get_by_username(self.TEST_USER_LOGIN)\ |
|
340 | 340 | .get_api_data()) |
|
341 | 341 | } |
|
342 | 342 | |
|
343 | 343 | expected = ret |
|
344 | 344 | self._compare_ok(id_, expected, given=response.body) |
|
345 | 345 | |
|
346 | 346 | def test_api_update_user_no_changed_params(self): |
|
347 | 347 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
|
348 | 348 | ret = jsonify(usr.get_api_data()) |
|
349 | 349 | id_, params = _build_data(self.apikey, 'update_user', |
|
350 | 350 | userid=TEST_USER_ADMIN_LOGIN) |
|
351 | 351 | |
|
352 | 352 | response = self.app.post(API_URL, content_type='application/json', |
|
353 | 353 | params=params) |
|
354 | 354 | ret = { |
|
355 | 355 | 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN), |
|
356 | 356 | 'user': ret |
|
357 | 357 | } |
|
358 | 358 | expected = ret |
|
359 | 359 | self._compare_ok(id_, expected, given=response.body) |
|
360 | 360 | |
|
361 | 361 | def test_api_update_user_by_user_id(self): |
|
362 | 362 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
|
363 | 363 | ret = jsonify(usr.get_api_data()) |
|
364 | 364 | id_, params = _build_data(self.apikey, 'update_user', |
|
365 | 365 | userid=usr.user_id) |
|
366 | 366 | |
|
367 | 367 | response = self.app.post(API_URL, content_type='application/json', |
|
368 | 368 | params=params) |
|
369 | 369 | ret = { |
|
370 | 370 | 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN), |
|
371 | 371 | 'user': ret |
|
372 | 372 | } |
|
373 | 373 | expected = ret |
|
374 | 374 | self._compare_ok(id_, expected, given=response.body) |
|
375 | 375 | |
|
376 | 376 | @mock.patch.object(UserModel, 'update_user', crash) |
|
377 | 377 | def test_api_update_user_when_exception_happens(self): |
|
378 | 378 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
|
379 | 379 | ret = jsonify(usr.get_api_data()) |
|
380 | 380 | id_, params = _build_data(self.apikey, 'update_user', |
|
381 | 381 | userid=usr.user_id) |
|
382 | 382 | |
|
383 | 383 | response = self.app.post(API_URL, content_type='application/json', |
|
384 | 384 | params=params) |
|
385 | 385 | ret = 'failed to update user `%s`' % usr.user_id |
|
386 | 386 | |
|
387 | 387 | expected = ret |
|
388 | 388 | self._compare_error(id_, expected, given=response.body) |
|
389 | 389 | |
|
390 | 390 | def test_api_get_repo(self): |
|
391 | 391 | new_group = 'some_new_group' |
|
392 | 392 | make_users_group(new_group) |
|
393 | 393 | RepoModel().grant_users_group_permission(repo=self.REPO, |
|
394 | 394 | group_name=new_group, |
|
395 | 395 | perm='repository.read') |
|
396 | 396 | self.Session().commit() |
|
397 | 397 | id_, params = _build_data(self.apikey, 'get_repo', |
|
398 | 398 | repoid=self.REPO) |
|
399 | 399 | response = self.app.post(API_URL, content_type='application/json', |
|
400 | 400 | params=params) |
|
401 | 401 | |
|
402 | 402 | repo = RepoModel().get_by_repo_name(self.REPO) |
|
403 | 403 | ret = repo.get_api_data() |
|
404 | 404 | |
|
405 | 405 | members = [] |
|
406 | 406 | for user in repo.repo_to_perm: |
|
407 | 407 | perm = user.permission.permission_name |
|
408 | 408 | user = user.user |
|
409 | 409 | user_data = user.get_api_data() |
|
410 | 410 | user_data['type'] = "user" |
|
411 | 411 | user_data['permission'] = perm |
|
412 | 412 | members.append(user_data) |
|
413 | 413 | |
|
414 | 414 | for users_group in repo.users_group_to_perm: |
|
415 | 415 | perm = users_group.permission.permission_name |
|
416 | 416 | users_group = users_group.users_group |
|
417 | 417 | users_group_data = users_group.get_api_data() |
|
418 | 418 | users_group_data['type'] = "users_group" |
|
419 | 419 | users_group_data['permission'] = perm |
|
420 | 420 | members.append(users_group_data) |
|
421 | 421 | |
|
422 | 422 | ret['members'] = members |
|
423 | 423 | |
|
424 | 424 | expected = ret |
|
425 | 425 | self._compare_ok(id_, expected, given=response.body) |
|
426 | 426 | destroy_users_group(new_group) |
|
427 | 427 | |
|
428 | 428 | def test_api_get_repo_that_doesn_not_exist(self): |
|
429 | 429 | id_, params = _build_data(self.apikey, 'get_repo', |
|
430 | 430 | repoid='no-such-repo') |
|
431 | 431 | response = self.app.post(API_URL, content_type='application/json', |
|
432 | 432 | params=params) |
|
433 | 433 | |
|
434 | 434 | ret = 'repository `%s` does not exist' % 'no-such-repo' |
|
435 | 435 | expected = ret |
|
436 | 436 | self._compare_error(id_, expected, given=response.body) |
|
437 | 437 | |
|
438 | 438 | def test_api_get_repos(self): |
|
439 | 439 | id_, params = _build_data(self.apikey, 'get_repos') |
|
440 | 440 | response = self.app.post(API_URL, content_type='application/json', |
|
441 | 441 | params=params) |
|
442 | 442 | |
|
443 | 443 | result = [] |
|
444 | 444 | for repo in RepoModel().get_all(): |
|
445 | 445 | result.append(repo.get_api_data()) |
|
446 | 446 | ret = jsonify(result) |
|
447 | 447 | |
|
448 | 448 | expected = ret |
|
449 | 449 | self._compare_ok(id_, expected, given=response.body) |
|
450 | 450 | |
|
451 | 451 | @parameterized.expand([('all', 'all'), |
|
452 | 452 | ('dirs', 'dirs'), |
|
453 | 453 | ('files', 'files'), ]) |
|
454 | 454 | def test_api_get_repo_nodes(self, name, ret_type): |
|
455 | 455 | rev = 'tip' |
|
456 | 456 | path = '/' |
|
457 | 457 | id_, params = _build_data(self.apikey, 'get_repo_nodes', |
|
458 | 458 | repoid=self.REPO, revision=rev, |
|
459 | 459 | root_path=path, |
|
460 | 460 | ret_type=ret_type) |
|
461 | 461 | response = self.app.post(API_URL, content_type='application/json', |
|
462 | 462 | params=params) |
|
463 | 463 | |
|
464 | 464 | # we don't the actual return types here since it's tested somewhere |
|
465 | 465 | # else |
|
466 | 466 | expected = json.loads(response.body)['result'] |
|
467 | 467 | self._compare_ok(id_, expected, given=response.body) |
|
468 | 468 | |
|
469 | 469 | def test_api_get_repo_nodes_bad_revisions(self): |
|
470 | 470 | rev = 'i-dont-exist' |
|
471 | 471 | path = '/' |
|
472 | 472 | id_, params = _build_data(self.apikey, 'get_repo_nodes', |
|
473 | 473 | repoid=self.REPO, revision=rev, |
|
474 | 474 | root_path=path,) |
|
475 | 475 | response = self.app.post(API_URL, content_type='application/json', |
|
476 | 476 | params=params) |
|
477 | 477 | |
|
478 | 478 | expected = 'failed to get repo: `%s` nodes' % self.REPO |
|
479 | 479 | self._compare_error(id_, expected, given=response.body) |
|
480 | 480 | |
|
481 | 481 | def test_api_get_repo_nodes_bad_path(self): |
|
482 | 482 | rev = 'tip' |
|
483 | 483 | path = '/idontexits' |
|
484 | 484 | id_, params = _build_data(self.apikey, 'get_repo_nodes', |
|
485 | 485 | repoid=self.REPO, revision=rev, |
|
486 | 486 | root_path=path,) |
|
487 | 487 | response = self.app.post(API_URL, content_type='application/json', |
|
488 | 488 | params=params) |
|
489 | 489 | |
|
490 | 490 | expected = 'failed to get repo: `%s` nodes' % self.REPO |
|
491 | 491 | self._compare_error(id_, expected, given=response.body) |
|
492 | 492 | |
|
493 | 493 | def test_api_get_repo_nodes_bad_ret_type(self): |
|
494 | 494 | rev = 'tip' |
|
495 | 495 | path = '/' |
|
496 | 496 | ret_type = 'error' |
|
497 | 497 | id_, params = _build_data(self.apikey, 'get_repo_nodes', |
|
498 | 498 | repoid=self.REPO, revision=rev, |
|
499 | 499 | root_path=path, |
|
500 | 500 | ret_type=ret_type) |
|
501 | 501 | response = self.app.post(API_URL, content_type='application/json', |
|
502 | 502 | params=params) |
|
503 | 503 | |
|
504 | 504 | expected = 'ret_type must be one of %s' % (['files', 'dirs', 'all']) |
|
505 | 505 | self._compare_error(id_, expected, given=response.body) |
|
506 | 506 | |
|
507 | 507 | def test_api_create_repo(self): |
|
508 | 508 | repo_name = 'api-repo' |
|
509 | 509 | id_, params = _build_data(self.apikey, 'create_repo', |
|
510 | 510 | repo_name=repo_name, |
|
511 | 511 | owner=TEST_USER_ADMIN_LOGIN, |
|
512 | 512 | repo_type='hg', |
|
513 | 513 | ) |
|
514 | 514 | response = self.app.post(API_URL, content_type='application/json', |
|
515 | 515 | params=params) |
|
516 | 516 | |
|
517 | 517 | repo = RepoModel().get_by_repo_name(repo_name) |
|
518 | 518 | ret = { |
|
519 | 519 | 'msg': 'Created new repository `%s`' % repo_name, |
|
520 | 520 | 'repo': jsonify(repo.get_api_data()) |
|
521 | 521 | } |
|
522 | 522 | expected = ret |
|
523 | 523 | self._compare_ok(id_, expected, given=response.body) |
|
524 | 524 | destroy_repo(repo_name) |
|
525 | 525 | |
|
526 | 526 | def test_api_create_repo_unknown_owner(self): |
|
527 | 527 | repo_name = 'api-repo' |
|
528 | 528 | owner = 'i-dont-exist' |
|
529 | 529 | id_, params = _build_data(self.apikey, 'create_repo', |
|
530 | 530 | repo_name=repo_name, |
|
531 | 531 | owner=owner, |
|
532 | 532 | repo_type='hg', |
|
533 | 533 | ) |
|
534 | 534 | response = self.app.post(API_URL, content_type='application/json', |
|
535 | 535 | params=params) |
|
536 | 536 | expected = 'user `%s` does not exist' % owner |
|
537 | 537 | self._compare_error(id_, expected, given=response.body) |
|
538 | 538 | |
|
539 | 539 | def test_api_create_repo_exists(self): |
|
540 | 540 | repo_name = self.REPO |
|
541 | 541 | id_, params = _build_data(self.apikey, 'create_repo', |
|
542 | 542 | repo_name=repo_name, |
|
543 | 543 | owner=TEST_USER_ADMIN_LOGIN, |
|
544 | 544 | repo_type='hg', |
|
545 | 545 | ) |
|
546 | 546 | response = self.app.post(API_URL, content_type='application/json', |
|
547 | 547 | params=params) |
|
548 | 548 | expected = "repo `%s` already exist" % repo_name |
|
549 | 549 | self._compare_error(id_, expected, given=response.body) |
|
550 | 550 | |
|
551 | 551 | @mock.patch.object(RepoModel, 'create_repo', crash) |
|
552 | 552 | def test_api_create_repo_exception_occurred(self): |
|
553 | 553 | repo_name = 'api-repo' |
|
554 | 554 | id_, params = _build_data(self.apikey, 'create_repo', |
|
555 | 555 | repo_name=repo_name, |
|
556 | 556 | owner=TEST_USER_ADMIN_LOGIN, |
|
557 | 557 | repo_type='hg', |
|
558 | 558 | ) |
|
559 | 559 | response = self.app.post(API_URL, content_type='application/json', |
|
560 | 560 | params=params) |
|
561 | 561 | expected = 'failed to create repository `%s`' % repo_name |
|
562 | 562 | self._compare_error(id_, expected, given=response.body) |
|
563 | 563 | |
|
564 | 564 | def test_api_delete_repo(self): |
|
565 | 565 | repo_name = 'api_delete_me' |
|
566 | 566 | create_repo(repo_name, self.REPO_TYPE) |
|
567 | 567 | |
|
568 | 568 | id_, params = _build_data(self.apikey, 'delete_repo', |
|
569 | 569 | repoid=repo_name,) |
|
570 | 570 | response = self.app.post(API_URL, content_type='application/json', |
|
571 | 571 | params=params) |
|
572 | 572 | |
|
573 | 573 | ret = { |
|
574 | 574 | 'msg': 'Deleted repository `%s`' % repo_name, |
|
575 | 575 | 'success': True |
|
576 | 576 | } |
|
577 | 577 | expected = ret |
|
578 | 578 | self._compare_ok(id_, expected, given=response.body) |
|
579 | 579 | |
|
580 | 580 | def test_api_delete_repo_exception_occurred(self): |
|
581 | 581 | repo_name = 'api_delete_me' |
|
582 | 582 | create_repo(repo_name, self.REPO_TYPE) |
|
583 | 583 | try: |
|
584 | 584 | with mock.patch.object(RepoModel, 'delete', crash): |
|
585 | 585 | id_, params = _build_data(self.apikey, 'delete_repo', |
|
586 | 586 | repoid=repo_name,) |
|
587 | 587 | response = self.app.post(API_URL, content_type='application/json', |
|
588 | 588 | params=params) |
|
589 | 589 | |
|
590 | 590 | expected = 'failed to delete repository `%s`' % repo_name |
|
591 | 591 | self._compare_error(id_, expected, given=response.body) |
|
592 | 592 | finally: |
|
593 | 593 | destroy_repo(repo_name) |
|
594 | 594 | |
|
595 | 595 | def test_api_fork_repo(self): |
|
596 | 596 | fork_name = 'api-repo-fork' |
|
597 | 597 | id_, params = _build_data(self.apikey, 'fork_repo', |
|
598 | 598 | repoid=self.REPO, |
|
599 | 599 | fork_name=fork_name, |
|
600 | 600 | owner=TEST_USER_ADMIN_LOGIN, |
|
601 | 601 | ) |
|
602 | 602 | response = self.app.post(API_URL, content_type='application/json', |
|
603 | 603 | params=params) |
|
604 | 604 | |
|
605 | 605 | ret = { |
|
606 | 606 | 'msg': 'Created fork of `%s` as `%s`' % (self.REPO, |
|
607 | 607 | fork_name), |
|
608 | 608 | 'success': True |
|
609 | 609 | } |
|
610 | 610 | expected = ret |
|
611 | 611 | self._compare_ok(id_, expected, given=response.body) |
|
612 | 612 | destroy_repo(fork_name) |
|
613 | 613 | |
|
614 | 614 | def test_api_fork_repo_unknown_owner(self): |
|
615 | 615 | fork_name = 'api-repo-fork' |
|
616 | 616 | owner = 'i-dont-exist' |
|
617 | 617 | id_, params = _build_data(self.apikey, 'fork_repo', |
|
618 | 618 | repoid=self.REPO, |
|
619 | 619 | fork_name=fork_name, |
|
620 | 620 | owner=owner, |
|
621 | 621 | ) |
|
622 | 622 | response = self.app.post(API_URL, content_type='application/json', |
|
623 | 623 | params=params) |
|
624 | 624 | expected = 'user `%s` does not exist' % owner |
|
625 | 625 | self._compare_error(id_, expected, given=response.body) |
|
626 | 626 | |
|
627 | 627 | def test_api_fork_repo_fork_exists(self): |
|
628 | 628 | fork_name = 'api-repo-fork' |
|
629 | 629 | create_fork(fork_name, self.REPO_TYPE, self.REPO) |
|
630 | 630 | |
|
631 | 631 | try: |
|
632 | 632 | fork_name = 'api-repo-fork' |
|
633 | 633 | |
|
634 | 634 | id_, params = _build_data(self.apikey, 'fork_repo', |
|
635 | 635 | repoid=self.REPO, |
|
636 | 636 | fork_name=fork_name, |
|
637 | 637 | owner=TEST_USER_ADMIN_LOGIN, |
|
638 | 638 | ) |
|
639 | 639 | response = self.app.post(API_URL, content_type='application/json', |
|
640 | 640 | params=params) |
|
641 | 641 | |
|
642 | 642 | expected = "fork `%s` already exist" % fork_name |
|
643 | 643 | self._compare_error(id_, expected, given=response.body) |
|
644 | 644 | finally: |
|
645 | 645 | destroy_repo(fork_name) |
|
646 | 646 | |
|
647 | 647 | def test_api_fork_repo_repo_exists(self): |
|
648 | 648 | fork_name = self.REPO |
|
649 | 649 | |
|
650 | 650 | id_, params = _build_data(self.apikey, 'fork_repo', |
|
651 | 651 | repoid=self.REPO, |
|
652 | 652 | fork_name=fork_name, |
|
653 | 653 | owner=TEST_USER_ADMIN_LOGIN, |
|
654 | 654 | ) |
|
655 | 655 | response = self.app.post(API_URL, content_type='application/json', |
|
656 | 656 | params=params) |
|
657 | 657 | |
|
658 | 658 | expected = "repo `%s` already exist" % fork_name |
|
659 | 659 | self._compare_error(id_, expected, given=response.body) |
|
660 | 660 | |
|
661 | 661 | @mock.patch.object(RepoModel, 'create_fork', crash) |
|
662 | 662 | def test_api_fork_repo_exception_occurred(self): |
|
663 | 663 | fork_name = 'api-repo-fork' |
|
664 | 664 | id_, params = _build_data(self.apikey, 'fork_repo', |
|
665 | 665 | repoid=self.REPO, |
|
666 | 666 | fork_name=fork_name, |
|
667 | 667 | owner=TEST_USER_ADMIN_LOGIN, |
|
668 | 668 | ) |
|
669 | 669 | response = self.app.post(API_URL, content_type='application/json', |
|
670 | 670 | params=params) |
|
671 | 671 | |
|
672 | 672 | expected = 'failed to fork repository `%s` as `%s`' % (self.REPO, |
|
673 | 673 | fork_name) |
|
674 | 674 | self._compare_error(id_, expected, given=response.body) |
|
675 | 675 | |
|
676 | 676 | def test_api_get_users_group(self): |
|
677 | 677 | id_, params = _build_data(self.apikey, 'get_users_group', |
|
678 | 678 | usersgroupid=TEST_USERS_GROUP) |
|
679 | 679 | response = self.app.post(API_URL, content_type='application/json', |
|
680 | 680 | params=params) |
|
681 | 681 | |
|
682 | 682 | users_group = UsersGroupModel().get_group(TEST_USERS_GROUP) |
|
683 | 683 | members = [] |
|
684 | 684 | for user in users_group.members: |
|
685 | 685 | user = user.user |
|
686 | 686 | members.append(user.get_api_data()) |
|
687 | 687 | |
|
688 | 688 | ret = users_group.get_api_data() |
|
689 | 689 | ret['members'] = members |
|
690 | 690 | expected = ret |
|
691 | 691 | self._compare_ok(id_, expected, given=response.body) |
|
692 | 692 | |
|
693 | 693 | def test_api_get_users_groups(self): |
|
694 | 694 | |
|
695 | 695 | make_users_group('test_users_group2') |
|
696 | 696 | |
|
697 | 697 | id_, params = _build_data(self.apikey, 'get_users_groups',) |
|
698 | 698 | response = self.app.post(API_URL, content_type='application/json', |
|
699 | 699 | params=params) |
|
700 | 700 | |
|
701 | 701 | expected = [] |
|
702 | 702 | for gr_name in [TEST_USERS_GROUP, 'test_users_group2']: |
|
703 | 703 | users_group = UsersGroupModel().get_group(gr_name) |
|
704 | 704 | ret = users_group.get_api_data() |
|
705 | 705 | expected.append(ret) |
|
706 | 706 | self._compare_ok(id_, expected, given=response.body) |
|
707 | 707 | |
|
708 | 708 | UsersGroupModel().delete(users_group='test_users_group2') |
|
709 | 709 | self.Session().commit() |
|
710 | 710 | |
|
711 | 711 | def test_api_create_users_group(self): |
|
712 | 712 | group_name = 'some_new_group' |
|
713 | 713 | id_, params = _build_data(self.apikey, 'create_users_group', |
|
714 | 714 | group_name=group_name) |
|
715 | 715 | response = self.app.post(API_URL, content_type='application/json', |
|
716 | 716 | params=params) |
|
717 | 717 | |
|
718 | 718 | ret = { |
|
719 | 719 | 'msg': 'created new users group `%s`' % group_name, |
|
720 | 720 | 'users_group': jsonify(UsersGroupModel()\ |
|
721 | 721 | .get_by_name(group_name)\ |
|
722 | 722 | .get_api_data()) |
|
723 | 723 | } |
|
724 | 724 | expected = ret |
|
725 | 725 | self._compare_ok(id_, expected, given=response.body) |
|
726 | 726 | |
|
727 | 727 | destroy_users_group(group_name) |
|
728 | 728 | |
|
729 | 729 | def test_api_get_users_group_that_exist(self): |
|
730 | 730 | id_, params = _build_data(self.apikey, 'create_users_group', |
|
731 | 731 | group_name=TEST_USERS_GROUP) |
|
732 | 732 | response = self.app.post(API_URL, content_type='application/json', |
|
733 | 733 | params=params) |
|
734 | 734 | |
|
735 | 735 | expected = "users group `%s` already exist" % TEST_USERS_GROUP |
|
736 | 736 | self._compare_error(id_, expected, given=response.body) |
|
737 | 737 | |
|
738 | 738 | @mock.patch.object(UsersGroupModel, 'create', crash) |
|
739 | 739 | def test_api_get_users_group_exception_occurred(self): |
|
740 | 740 | group_name = 'exception_happens' |
|
741 | 741 | id_, params = _build_data(self.apikey, 'create_users_group', |
|
742 | 742 | group_name=group_name) |
|
743 | 743 | response = self.app.post(API_URL, content_type='application/json', |
|
744 | 744 | params=params) |
|
745 | 745 | |
|
746 | 746 | expected = 'failed to create group `%s`' % group_name |
|
747 | 747 | self._compare_error(id_, expected, given=response.body) |
|
748 | 748 | |
|
749 | 749 | def test_api_add_user_to_users_group(self): |
|
750 | 750 | gr_name = 'test_group' |
|
751 | 751 | UsersGroupModel().create(gr_name) |
|
752 | 752 | self.Session().commit() |
|
753 | 753 | id_, params = _build_data(self.apikey, 'add_user_to_users_group', |
|
754 | 754 | usersgroupid=gr_name, |
|
755 | 755 | userid=TEST_USER_ADMIN_LOGIN) |
|
756 | 756 | response = self.app.post(API_URL, content_type='application/json', |
|
757 | 757 | params=params) |
|
758 | 758 | |
|
759 | 759 | expected = { |
|
760 | 760 | 'msg': 'added member `%s` to users group `%s`' % ( |
|
761 | 761 | TEST_USER_ADMIN_LOGIN, gr_name |
|
762 | 762 | ), |
|
763 | 763 | 'success': True} |
|
764 | 764 | self._compare_ok(id_, expected, given=response.body) |
|
765 | 765 | |
|
766 | 766 | UsersGroupModel().delete(users_group=gr_name) |
|
767 | 767 | self.Session().commit() |
|
768 | 768 | |
|
769 | 769 | def test_api_add_user_to_users_group_that_doesnt_exist(self): |
|
770 | 770 | id_, params = _build_data(self.apikey, 'add_user_to_users_group', |
|
771 | 771 | usersgroupid='false-group', |
|
772 | 772 | userid=TEST_USER_ADMIN_LOGIN) |
|
773 | 773 | response = self.app.post(API_URL, content_type='application/json', |
|
774 | 774 | params=params) |
|
775 | 775 | |
|
776 | 776 | expected = 'users group `%s` does not exist' % 'false-group' |
|
777 | 777 | self._compare_error(id_, expected, given=response.body) |
|
778 | 778 | |
|
779 | 779 | @mock.patch.object(UsersGroupModel, 'add_user_to_group', crash) |
|
780 | 780 | def test_api_add_user_to_users_group_exception_occurred(self): |
|
781 | 781 | gr_name = 'test_group' |
|
782 | 782 | UsersGroupModel().create(gr_name) |
|
783 | 783 | self.Session().commit() |
|
784 | 784 | id_, params = _build_data(self.apikey, 'add_user_to_users_group', |
|
785 | 785 | usersgroupid=gr_name, |
|
786 | 786 | userid=TEST_USER_ADMIN_LOGIN) |
|
787 | 787 | response = self.app.post(API_URL, content_type='application/json', |
|
788 | 788 | params=params) |
|
789 | 789 | |
|
790 | 790 | expected = 'failed to add member to users group `%s`' % gr_name |
|
791 | 791 | self._compare_error(id_, expected, given=response.body) |
|
792 | 792 | |
|
793 | 793 | UsersGroupModel().delete(users_group=gr_name) |
|
794 | 794 | self.Session().commit() |
|
795 | 795 | |
|
796 | 796 | def test_api_remove_user_from_users_group(self): |
|
797 | 797 | gr_name = 'test_group_3' |
|
798 | 798 | gr = UsersGroupModel().create(gr_name) |
|
799 | 799 | UsersGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN) |
|
800 | 800 | self.Session().commit() |
|
801 | 801 | id_, params = _build_data(self.apikey, 'remove_user_from_users_group', |
|
802 | 802 | usersgroupid=gr_name, |
|
803 | 803 | userid=TEST_USER_ADMIN_LOGIN) |
|
804 | 804 | response = self.app.post(API_URL, content_type='application/json', |
|
805 | 805 | params=params) |
|
806 | 806 | |
|
807 | 807 | expected = { |
|
808 | 808 | 'msg': 'removed member `%s` from users group `%s`' % ( |
|
809 | 809 | TEST_USER_ADMIN_LOGIN, gr_name |
|
810 | 810 | ), |
|
811 | 811 | 'success': True} |
|
812 | 812 | self._compare_ok(id_, expected, given=response.body) |
|
813 | 813 | |
|
814 | 814 | UsersGroupModel().delete(users_group=gr_name) |
|
815 | 815 | self.Session().commit() |
|
816 | 816 | |
|
817 | 817 | @mock.patch.object(UsersGroupModel, 'remove_user_from_group', crash) |
|
818 | 818 | def test_api_remove_user_from_users_group_exception_occurred(self): |
|
819 | 819 | gr_name = 'test_group_3' |
|
820 | 820 | gr = UsersGroupModel().create(gr_name) |
|
821 | 821 | UsersGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN) |
|
822 | 822 | self.Session().commit() |
|
823 | 823 | id_, params = _build_data(self.apikey, 'remove_user_from_users_group', |
|
824 | 824 | usersgroupid=gr_name, |
|
825 | 825 | userid=TEST_USER_ADMIN_LOGIN) |
|
826 | 826 | response = self.app.post(API_URL, content_type='application/json', |
|
827 | 827 | params=params) |
|
828 | 828 | |
|
829 | 829 | expected = 'failed to remove member from users group `%s`' % gr_name |
|
830 | 830 | self._compare_error(id_, expected, given=response.body) |
|
831 | 831 | |
|
832 | 832 | UsersGroupModel().delete(users_group=gr_name) |
|
833 | 833 | self.Session().commit() |
|
834 | 834 | |
|
835 | 835 | @parameterized.expand([('none', 'repository.none'), |
|
836 | 836 | ('read', 'repository.read'), |
|
837 | 837 | ('write', 'repository.write'), |
|
838 | 838 | ('admin', 'repository.admin')]) |
|
839 | 839 | def test_api_grant_user_permission(self, name, perm): |
|
840 | 840 | id_, params = _build_data(self.apikey, 'grant_user_permission', |
|
841 | 841 | repoid=self.REPO, |
|
842 | 842 | userid=TEST_USER_ADMIN_LOGIN, |
|
843 | 843 | perm=perm) |
|
844 | 844 | response = self.app.post(API_URL, content_type='application/json', |
|
845 | 845 | params=params) |
|
846 | 846 | |
|
847 | 847 | ret = { |
|
848 | 848 | 'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % ( |
|
849 | 849 | perm, TEST_USER_ADMIN_LOGIN, self.REPO |
|
850 | 850 | ), |
|
851 | 851 | 'success': True |
|
852 | 852 | } |
|
853 | 853 | expected = ret |
|
854 | 854 | self._compare_ok(id_, expected, given=response.body) |
|
855 | 855 | |
|
856 | 856 | def test_api_grant_user_permission_wrong_permission(self): |
|
857 | 857 | perm = 'haha.no.permission' |
|
858 | 858 | id_, params = _build_data(self.apikey, 'grant_user_permission', |
|
859 | 859 | repoid=self.REPO, |
|
860 | 860 | userid=TEST_USER_ADMIN_LOGIN, |
|
861 | 861 | perm=perm) |
|
862 | 862 | response = self.app.post(API_URL, content_type='application/json', |
|
863 | 863 | params=params) |
|
864 | 864 | |
|
865 | 865 | expected = 'permission `%s` does not exist' % perm |
|
866 | 866 | self._compare_error(id_, expected, given=response.body) |
|
867 | 867 | |
|
868 | 868 | @mock.patch.object(RepoModel, 'grant_user_permission', crash) |
|
869 | 869 | def test_api_grant_user_permission_exception_when_adding(self): |
|
870 | 870 | perm = 'repository.read' |
|
871 | 871 | id_, params = _build_data(self.apikey, 'grant_user_permission', |
|
872 | 872 | repoid=self.REPO, |
|
873 | 873 | userid=TEST_USER_ADMIN_LOGIN, |
|
874 | 874 | perm=perm) |
|
875 | 875 | response = self.app.post(API_URL, content_type='application/json', |
|
876 | 876 | params=params) |
|
877 | 877 | |
|
878 | 878 | expected = 'failed to edit permission for user: `%s` in repo: `%s`' % ( |
|
879 | 879 | TEST_USER_ADMIN_LOGIN, self.REPO |
|
880 | 880 | ) |
|
881 | 881 | self._compare_error(id_, expected, given=response.body) |
|
882 | 882 | |
|
883 | 883 | def test_api_revoke_user_permission(self): |
|
884 | 884 | id_, params = _build_data(self.apikey, 'revoke_user_permission', |
|
885 | 885 | repoid=self.REPO, |
|
886 | 886 | userid=TEST_USER_ADMIN_LOGIN,) |
|
887 | 887 | response = self.app.post(API_URL, content_type='application/json', |
|
888 | 888 | params=params) |
|
889 | 889 | |
|
890 | 890 | expected = { |
|
891 | 891 | 'msg': 'Revoked perm for user: `%s` in repo: `%s`' % ( |
|
892 | 892 | TEST_USER_ADMIN_LOGIN, self.REPO |
|
893 | 893 | ), |
|
894 | 894 | 'success': True |
|
895 | 895 | } |
|
896 | 896 | self._compare_ok(id_, expected, given=response.body) |
|
897 | 897 | |
|
898 | 898 | @mock.patch.object(RepoModel, 'revoke_user_permission', crash) |
|
899 | 899 | def test_api_revoke_user_permission_exception_when_adding(self): |
|
900 | 900 | id_, params = _build_data(self.apikey, 'revoke_user_permission', |
|
901 | 901 | repoid=self.REPO, |
|
902 | 902 | userid=TEST_USER_ADMIN_LOGIN,) |
|
903 | 903 | response = self.app.post(API_URL, content_type='application/json', |
|
904 | 904 | params=params) |
|
905 | 905 | |
|
906 | 906 | expected = 'failed to edit permission for user: `%s` in repo: `%s`' % ( |
|
907 | 907 | TEST_USER_ADMIN_LOGIN, self.REPO |
|
908 | 908 | ) |
|
909 | 909 | self._compare_error(id_, expected, given=response.body) |
|
910 | 910 | |
|
911 | 911 | @parameterized.expand([('none', 'repository.none'), |
|
912 | 912 | ('read', 'repository.read'), |
|
913 | 913 | ('write', 'repository.write'), |
|
914 | 914 | ('admin', 'repository.admin')]) |
|
915 | 915 | def test_api_grant_users_group_permission(self, name, perm): |
|
916 | 916 | id_, params = _build_data(self.apikey, 'grant_users_group_permission', |
|
917 | 917 | repoid=self.REPO, |
|
918 | 918 | usersgroupid=TEST_USERS_GROUP, |
|
919 | 919 | perm=perm) |
|
920 | 920 | response = self.app.post(API_URL, content_type='application/json', |
|
921 | 921 | params=params) |
|
922 | 922 | |
|
923 | 923 | ret = { |
|
924 | 924 | 'msg': 'Granted perm: `%s` for users group: `%s` in repo: `%s`' % ( |
|
925 | 925 | perm, TEST_USERS_GROUP, self.REPO |
|
926 | 926 | ), |
|
927 | 927 | 'success': True |
|
928 | 928 | } |
|
929 | 929 | expected = ret |
|
930 | 930 | self._compare_ok(id_, expected, given=response.body) |
|
931 | 931 | |
|
932 | 932 | def test_api_grant_users_group_permission_wrong_permission(self): |
|
933 | 933 | perm = 'haha.no.permission' |
|
934 | 934 | id_, params = _build_data(self.apikey, 'grant_users_group_permission', |
|
935 | 935 | repoid=self.REPO, |
|
936 | 936 | usersgroupid=TEST_USERS_GROUP, |
|
937 | 937 | perm=perm) |
|
938 | 938 | response = self.app.post(API_URL, content_type='application/json', |
|
939 | 939 | params=params) |
|
940 | 940 | |
|
941 | 941 | expected = 'permission `%s` does not exist' % perm |
|
942 | 942 | self._compare_error(id_, expected, given=response.body) |
|
943 | 943 | |
|
944 | 944 | @mock.patch.object(RepoModel, 'grant_users_group_permission', crash) |
|
945 | 945 | def test_api_grant_users_group_permission_exception_when_adding(self): |
|
946 | 946 | perm = 'repository.read' |
|
947 | 947 | id_, params = _build_data(self.apikey, 'grant_users_group_permission', |
|
948 | 948 | repoid=self.REPO, |
|
949 | 949 | usersgroupid=TEST_USERS_GROUP, |
|
950 | 950 | perm=perm) |
|
951 | 951 | response = self.app.post(API_URL, content_type='application/json', |
|
952 | 952 | params=params) |
|
953 | 953 | |
|
954 | 954 | expected = 'failed to edit permission for users group: `%s` in repo: `%s`' % ( |
|
955 | 955 | TEST_USERS_GROUP, self.REPO |
|
956 | 956 | ) |
|
957 | 957 | self._compare_error(id_, expected, given=response.body) |
|
958 | 958 | |
|
959 | 959 | def test_api_revoke_users_group_permission(self): |
|
960 | 960 | RepoModel().grant_users_group_permission(repo=self.REPO, |
|
961 | 961 | group_name=TEST_USERS_GROUP, |
|
962 | 962 | perm='repository.read') |
|
963 | 963 | self.Session().commit() |
|
964 | 964 | id_, params = _build_data(self.apikey, 'revoke_users_group_permission', |
|
965 | 965 | repoid=self.REPO, |
|
966 | 966 | usersgroupid=TEST_USERS_GROUP,) |
|
967 | 967 | response = self.app.post(API_URL, content_type='application/json', |
|
968 | 968 | params=params) |
|
969 | 969 | |
|
970 | 970 | expected = { |
|
971 | 971 | 'msg': 'Revoked perm for users group: `%s` in repo: `%s`' % ( |
|
972 | 972 | TEST_USERS_GROUP, self.REPO |
|
973 | 973 | ), |
|
974 | 974 | 'success': True |
|
975 | 975 | } |
|
976 | 976 | self._compare_ok(id_, expected, given=response.body) |
|
977 | 977 | |
|
978 | 978 | @mock.patch.object(RepoModel, 'revoke_users_group_permission', crash) |
|
979 | 979 | def test_api_revoke_users_group_permission_exception_when_adding(self): |
|
980 | 980 | |
|
981 | 981 | id_, params = _build_data(self.apikey, 'revoke_users_group_permission', |
|
982 | 982 | repoid=self.REPO, |
|
983 | 983 | usersgroupid=TEST_USERS_GROUP,) |
|
984 | 984 | response = self.app.post(API_URL, content_type='application/json', |
|
985 | 985 | params=params) |
|
986 | 986 | |
|
987 | 987 | expected = 'failed to edit permission for users group: `%s` in repo: `%s`' % ( |
|
988 | 988 | TEST_USERS_GROUP, self.REPO |
|
989 | 989 | ) |
|
990 | 990 | self._compare_error(id_, expected, given=response.body) |
|
991 |
@@ -1,7 +1,7 b'' | |||
|
1 | 1 | from rhodecode.tests import * |
|
2 | 2 | from rhodecode.tests.api.api_base import BaseTestApi |
|
3 | 3 | |
|
4 | 4 | |
|
5 | 5 | class TestHgApi(BaseTestApi, TestController): |
|
6 | 6 | REPO = HG_REPO |
|
7 | REPO_TYPE = 'hg' No newline at end of file | |
|
7 | REPO_TYPE = 'hg' |
@@ -1,266 +1,266 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | from rhodecode.lib.auth import get_crypt_password, check_password |
|
4 | 4 | from rhodecode.model.db import User, RhodeCodeSetting, Repository |
|
5 | 5 | from rhodecode.tests import * |
|
6 | 6 | from rhodecode.lib import helpers as h |
|
7 | 7 | from rhodecode.model.user import UserModel |
|
8 | 8 | from rhodecode.model.scm import ScmModel |
|
9 | 9 | |
|
10 | 10 | |
|
11 | 11 | class TestAdminSettingsController(TestController): |
|
12 | 12 | |
|
13 | 13 | def test_index(self): |
|
14 | 14 | response = self.app.get(url('admin_settings')) |
|
15 | 15 | # Test response... |
|
16 | 16 | |
|
17 | 17 | def test_index_as_xml(self): |
|
18 | 18 | response = self.app.get(url('formatted_admin_settings', format='xml')) |
|
19 | 19 | |
|
20 | 20 | def test_create(self): |
|
21 | 21 | response = self.app.post(url('admin_settings')) |
|
22 | 22 | |
|
23 | 23 | def test_new(self): |
|
24 | 24 | response = self.app.get(url('admin_new_setting')) |
|
25 | 25 | |
|
26 | 26 | def test_new_as_xml(self): |
|
27 | 27 | response = self.app.get(url('formatted_admin_new_setting', format='xml')) |
|
28 | 28 | |
|
29 | 29 | def test_update(self): |
|
30 | 30 | response = self.app.put(url('admin_setting', setting_id=1)) |
|
31 | 31 | |
|
32 | 32 | def test_update_browser_fakeout(self): |
|
33 | 33 | response = self.app.post(url('admin_setting', setting_id=1), params=dict(_method='put')) |
|
34 | 34 | |
|
35 | 35 | def test_delete(self): |
|
36 | 36 | response = self.app.delete(url('admin_setting', setting_id=1)) |
|
37 | 37 | |
|
38 | 38 | def test_delete_browser_fakeout(self): |
|
39 | 39 | response = self.app.post(url('admin_setting', setting_id=1), params=dict(_method='delete')) |
|
40 | 40 | |
|
41 | 41 | def test_show(self): |
|
42 | 42 | response = self.app.get(url('admin_setting', setting_id=1)) |
|
43 | 43 | |
|
44 | 44 | def test_show_as_xml(self): |
|
45 | 45 | response = self.app.get(url('formatted_admin_setting', setting_id=1, format='xml')) |
|
46 | 46 | |
|
47 | 47 | def test_edit(self): |
|
48 | 48 | response = self.app.get(url('admin_edit_setting', setting_id=1)) |
|
49 | 49 | |
|
50 | 50 | def test_edit_as_xml(self): |
|
51 | 51 | response = self.app.get(url('formatted_admin_edit_setting', |
|
52 | 52 | setting_id=1, format='xml')) |
|
53 | 53 | |
|
54 | 54 | def test_ga_code_active(self): |
|
55 | 55 | self.log_user() |
|
56 | 56 | old_title = 'RhodeCode' |
|
57 | 57 | old_realm = 'RhodeCode authentication' |
|
58 | 58 | new_ga_code = 'ga-test-123456789' |
|
59 | 59 | response = self.app.post(url('admin_setting', setting_id='global'), |
|
60 | 60 | params=dict( |
|
61 | 61 | _method='put', |
|
62 | 62 | rhodecode_title=old_title, |
|
63 | 63 | rhodecode_realm=old_realm, |
|
64 | 64 | rhodecode_ga_code=new_ga_code |
|
65 | 65 | )) |
|
66 | 66 | |
|
67 | 67 | self.checkSessionFlash(response, 'Updated application settings') |
|
68 | 68 | |
|
69 | 69 | self.assertEqual(RhodeCodeSetting |
|
70 | 70 | .get_app_settings()['rhodecode_ga_code'], new_ga_code) |
|
71 | 71 | |
|
72 | 72 | response = response.follow() |
|
73 | 73 | response.mustcontain("""_gaq.push(['_setAccount', '%s']);""" % new_ga_code) |
|
74 | 74 | |
|
75 | 75 | def test_ga_code_inactive(self): |
|
76 | 76 | self.log_user() |
|
77 | 77 | old_title = 'RhodeCode' |
|
78 | 78 | old_realm = 'RhodeCode authentication' |
|
79 | 79 | new_ga_code = '' |
|
80 | 80 | response = self.app.post(url('admin_setting', setting_id='global'), |
|
81 | 81 | params=dict( |
|
82 | 82 | _method='put', |
|
83 | 83 | rhodecode_title=old_title, |
|
84 | 84 | rhodecode_realm=old_realm, |
|
85 | 85 | rhodecode_ga_code=new_ga_code |
|
86 | 86 | )) |
|
87 | 87 | |
|
88 | 88 | self.assertTrue('Updated application settings' in |
|
89 | 89 | response.session['flash'][0][1]) |
|
90 | 90 | self.assertEqual(RhodeCodeSetting |
|
91 | 91 | .get_app_settings()['rhodecode_ga_code'], new_ga_code) |
|
92 | 92 | |
|
93 | 93 | response = response.follow() |
|
94 | 94 | self.assertFalse("""_gaq.push(['_setAccount', '%s']);""" % new_ga_code |
|
95 | 95 | in response.body) |
|
96 | 96 | |
|
97 | 97 | def test_title_change(self): |
|
98 | 98 | self.log_user() |
|
99 | 99 | old_title = 'RhodeCode' |
|
100 | 100 | new_title = old_title + '_changed' |
|
101 | 101 | old_realm = 'RhodeCode authentication' |
|
102 | 102 | |
|
103 | 103 | for new_title in ['Changed', 'Ε»Γ³Εwik', old_title]: |
|
104 | 104 | response = self.app.post(url('admin_setting', setting_id='global'), |
|
105 | 105 | params=dict( |
|
106 | 106 | _method='put', |
|
107 | 107 | rhodecode_title=new_title, |
|
108 | 108 | rhodecode_realm=old_realm, |
|
109 | 109 | rhodecode_ga_code='' |
|
110 | 110 | )) |
|
111 | 111 | |
|
112 | 112 | self.checkSessionFlash(response, 'Updated application settings') |
|
113 | 113 | self.assertEqual(RhodeCodeSetting |
|
114 | 114 | .get_app_settings()['rhodecode_title'], |
|
115 | 115 | new_title.decode('utf-8')) |
|
116 | 116 | |
|
117 | 117 | response = response.follow() |
|
118 | 118 | response.mustcontain("""<h1><a href="/">%s</a></h1>""" % new_title) |
|
119 | 119 | |
|
120 | 120 | def test_my_account(self): |
|
121 | 121 | self.log_user() |
|
122 | 122 | response = self.app.get(url('admin_settings_my_account')) |
|
123 | 123 | |
|
124 | 124 | self.assertTrue('value="test_admin' in response.body) |
|
125 | 125 | |
|
126 | 126 | @parameterized.expand([('firstname', 'new_username'), |
|
127 | 127 | ('lastname', 'new_username'), |
|
128 | 128 | ('admin', True), |
|
129 | 129 | ('admin', False), |
|
130 | 130 | ('ldap_dn', 'test'), |
|
131 | 131 | ('ldap_dn', None), |
|
132 | 132 | ('active', False), |
|
133 | 133 | ('active', True), |
|
134 | 134 | ('email', 'some@email.com'), |
|
135 | 135 | ]) |
|
136 | 136 | def test_my_account_update(self, name, expected): |
|
137 | 137 | uname = 'testme' |
|
138 | 138 | usr = UserModel().create_or_update(username=uname, password='qweqwe', |
|
139 | 139 | email='testme@rhodecod.org') |
|
140 | 140 | self.Session().commit() |
|
141 | 141 | params = usr.get_api_data() |
|
142 | 142 | user_id = usr.user_id |
|
143 | 143 | self.log_user(username=uname, password='qweqwe') |
|
144 | 144 | params.update({name: expected}) |
|
145 | 145 | params.update({'password_confirmation': ''}) |
|
146 | 146 | params.update({'new_password': ''}) |
|
147 | 147 | |
|
148 | 148 | try: |
|
149 | 149 | response = self.app.put(url('admin_settings_my_account_update', |
|
150 | 150 | id=user_id), params) |
|
151 | 151 | |
|
152 | 152 | self.checkSessionFlash(response, |
|
153 | 153 | 'Your account was updated successfully') |
|
154 | 154 | |
|
155 | 155 | updated_user = User.get_by_username(uname) |
|
156 | 156 | updated_params = updated_user.get_api_data() |
|
157 | 157 | updated_params.update({'password_confirmation': ''}) |
|
158 | 158 | updated_params.update({'new_password': ''}) |
|
159 | 159 | |
|
160 | 160 | params['last_login'] = updated_params['last_login'] |
|
161 | 161 | if name == 'email': |
|
162 | 162 | params['emails'] = [expected] |
|
163 | 163 | if name == 'ldap_dn': |
|
164 | 164 | #cannot update this via form |
|
165 | 165 | params['ldap_dn'] = None |
|
166 | 166 | if name == 'active': |
|
167 | 167 | #my account cannot deactivate account |
|
168 | 168 | params['active'] = True |
|
169 | 169 | if name == 'admin': |
|
170 | 170 | #my account cannot make you an admin ! |
|
171 | 171 | params['admin'] = False |
|
172 | 172 | |
|
173 | 173 | self.assertEqual(params, updated_params) |
|
174 | 174 | |
|
175 | 175 | finally: |
|
176 | 176 | UserModel().delete('testme') |
|
177 | 177 | |
|
178 | 178 | def test_my_account_update_err_email_exists(self): |
|
179 | 179 | self.log_user() |
|
180 | 180 | |
|
181 | 181 | new_email = 'test_regular@mail.com' # already exisitn email |
|
182 | 182 | response = self.app.put(url('admin_settings_my_account_update'), |
|
183 | 183 | params=dict( |
|
184 | 184 | username='test_admin', |
|
185 | 185 | new_password='test12', |
|
186 | 186 | password_confirmation='test122', |
|
187 | 187 | firstname='NewName', |
|
188 | 188 | lastname='NewLastname', |
|
189 | 189 | email=new_email,) |
|
190 | 190 | ) |
|
191 | 191 | |
|
192 | 192 | response.mustcontain('This e-mail address is already taken') |
|
193 | 193 | |
|
194 | 194 | def test_my_account_update_err(self): |
|
195 | 195 | self.log_user('test_regular2', 'test12') |
|
196 | 196 | |
|
197 | 197 | new_email = 'newmail.pl' |
|
198 | 198 | response = self.app.post(url('admin_settings_my_account_update'), |
|
199 | 199 | params=dict( |
|
200 | 200 | _method='put', |
|
201 | 201 | username='test_admin', |
|
202 | 202 | new_password='test12', |
|
203 | 203 | password_confirmation='test122', |
|
204 | 204 | firstname='NewName', |
|
205 | 205 | lastname='NewLastname', |
|
206 | 206 | email=new_email,) |
|
207 | 207 | ) |
|
208 | 208 | |
|
209 | 209 | response.mustcontain('An email address must contain a single @') |
|
210 | 210 | from rhodecode.model import validators |
|
211 | 211 | msg = validators.ValidUsername(edit=False, |
|
212 | 212 | old_data={})._messages['username_exists'] |
|
213 | 213 | msg = h.html_escape(msg % {'username': 'test_admin'}) |
|
214 | 214 | response.mustcontain(u"%s" % msg) |
|
215 | 215 | |
|
216 | 216 | def test_set_repo_fork_has_no_self_id(self): |
|
217 | 217 | self.log_user() |
|
218 | 218 | repo = Repository.get_by_repo_name(HG_REPO) |
|
219 | 219 | response = self.app.get(url('edit_repo', repo_name=HG_REPO)) |
|
220 | 220 | opt = """<option value="%s">vcs_test_git</option>""" % repo.repo_id |
|
221 | 221 | assert opt not in response.body |
|
222 | 222 | |
|
223 | 223 | def test_set_fork_of_repo(self): |
|
224 | 224 | self.log_user() |
|
225 | 225 | repo = Repository.get_by_repo_name(HG_REPO) |
|
226 | 226 | repo2 = Repository.get_by_repo_name(GIT_REPO) |
|
227 | 227 | response = self.app.put(url('repo_as_fork', repo_name=HG_REPO), |
|
228 | 228 | params=dict( |
|
229 | 229 | id_fork_of=repo2.repo_id |
|
230 | 230 | )) |
|
231 | 231 | repo = Repository.get_by_repo_name(HG_REPO) |
|
232 | 232 | repo2 = Repository.get_by_repo_name(GIT_REPO) |
|
233 | 233 | self.checkSessionFlash(response, |
|
234 | 234 | 'Marked repo %s as fork of %s' % (repo.repo_name, repo2.repo_name)) |
|
235 | 235 | |
|
236 | 236 | assert repo.fork == repo2 |
|
237 | 237 | response = response.follow() |
|
238 | 238 | # check if given repo is selected |
|
239 | 239 | |
|
240 | 240 | opt = """<option value="%s" selected="selected">%s</option>""" % ( |
|
241 | 241 | repo2.repo_id, repo2.repo_name) |
|
242 | 242 | response.mustcontain(opt) |
|
243 | 243 | |
|
244 | 244 | # clean session flash |
|
245 | 245 | #response = self.app.get(url('edit_repo', repo_name=HG_REPO)) |
|
246 | 246 | |
|
247 | 247 | ## mark it as None |
|
248 | 248 | response = self.app.put(url('repo_as_fork', repo_name=HG_REPO), |
|
249 | 249 | params=dict( |
|
250 | 250 | id_fork_of=None |
|
251 | 251 | )) |
|
252 | 252 | repo = Repository.get_by_repo_name(HG_REPO) |
|
253 | 253 | repo2 = Repository.get_by_repo_name(GIT_REPO) |
|
254 | 254 | self.checkSessionFlash(response, |
|
255 | 255 | 'Marked repo %s as fork of %s' % (repo.repo_name, "Nothing")) |
|
256 | 256 | assert repo.fork == None |
|
257 | 257 | |
|
258 | 258 | def test_set_fork_of_same_repo(self): |
|
259 | 259 | self.log_user() |
|
260 | 260 | repo = Repository.get_by_repo_name(HG_REPO) |
|
261 | 261 | response = self.app.put(url('repo_as_fork', repo_name=HG_REPO), |
|
262 | 262 | params=dict( |
|
263 | 263 | id_fork_of=repo.repo_id |
|
264 | 264 | )) |
|
265 | 265 | self.checkSessionFlash(response, |
|
266 | 'An error occurred during this operation') No newline at end of file | |
|
266 | 'An error occurred during this operation') |
@@ -1,191 +1,187 b'' | |||
|
1 | 1 | import os |
|
2 | 2 | import unittest |
|
3 | 3 | from rhodecode.tests import * |
|
4 | 4 | |
|
5 | 5 | from rhodecode.model.db import User, Notification, UserNotification |
|
6 | 6 | from rhodecode.model.user import UserModel |
|
7 | 7 | |
|
8 | 8 | from rhodecode.model.meta import Session |
|
9 | 9 | from rhodecode.model.notification import NotificationModel |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | class TestNotifications(unittest.TestCase): |
|
13 | 13 | |
|
14 | 14 | def __init__(self, methodName='runTest'): |
|
15 | 15 | Session.remove() |
|
16 | 16 | self.u1 = UserModel().create_or_update(username=u'u1', |
|
17 | 17 | password=u'qweqwe', |
|
18 | 18 | email=u'u1@rhodecode.org', |
|
19 | 19 | firstname=u'u1', lastname=u'u1') |
|
20 | 20 | Session().commit() |
|
21 | 21 | self.u1 = self.u1.user_id |
|
22 | 22 | |
|
23 | 23 | self.u2 = UserModel().create_or_update(username=u'u2', |
|
24 | 24 | password=u'qweqwe', |
|
25 | 25 | email=u'u2@rhodecode.org', |
|
26 | 26 | firstname=u'u2', lastname=u'u3') |
|
27 | 27 | Session().commit() |
|
28 | 28 | self.u2 = self.u2.user_id |
|
29 | 29 | |
|
30 | 30 | self.u3 = UserModel().create_or_update(username=u'u3', |
|
31 | 31 | password=u'qweqwe', |
|
32 | 32 | email=u'u3@rhodecode.org', |
|
33 | 33 | firstname=u'u3', lastname=u'u3') |
|
34 | 34 | Session().commit() |
|
35 | 35 | self.u3 = self.u3.user_id |
|
36 | 36 | |
|
37 | 37 | super(TestNotifications, self).__init__(methodName=methodName) |
|
38 | 38 | |
|
39 | 39 | def _clean_notifications(self): |
|
40 | 40 | for n in Notification.query().all(): |
|
41 | 41 | Session().delete(n) |
|
42 | 42 | |
|
43 | 43 | Session().commit() |
|
44 | 44 | self.assertEqual(Notification.query().all(), []) |
|
45 | 45 | |
|
46 | 46 | def tearDown(self): |
|
47 | 47 | self._clean_notifications() |
|
48 | 48 | |
|
49 | 49 | def test_create_notification(self): |
|
50 | 50 | self.assertEqual([], Notification.query().all()) |
|
51 | 51 | self.assertEqual([], UserNotification.query().all()) |
|
52 | 52 | |
|
53 | 53 | usrs = [self.u1, self.u2] |
|
54 | 54 | notification = NotificationModel().create(created_by=self.u1, |
|
55 | 55 | subject=u'subj', body=u'hi there', |
|
56 | 56 | recipients=usrs) |
|
57 | 57 | Session().commit() |
|
58 | 58 | u1 = User.get(self.u1) |
|
59 | 59 | u2 = User.get(self.u2) |
|
60 | 60 | u3 = User.get(self.u3) |
|
61 | 61 | notifications = Notification.query().all() |
|
62 | 62 | self.assertEqual(len(notifications), 1) |
|
63 | 63 | |
|
64 | 64 | self.assertEqual(notifications[0].recipients, [u1, u2]) |
|
65 | 65 | self.assertEqual(notification.notification_id, |
|
66 | 66 | notifications[0].notification_id) |
|
67 | 67 | |
|
68 | 68 | unotification = UserNotification.query()\ |
|
69 | 69 | .filter(UserNotification.notification == notification).all() |
|
70 | 70 | |
|
71 | 71 | self.assertEqual(len(unotification), len(usrs)) |
|
72 | 72 | self.assertEqual(set([x.user.user_id for x in unotification]), |
|
73 | 73 | set(usrs)) |
|
74 | 74 | |
|
75 | 75 | def test_user_notifications(self): |
|
76 | 76 | self.assertEqual([], Notification.query().all()) |
|
77 | 77 | self.assertEqual([], UserNotification.query().all()) |
|
78 | 78 | |
|
79 | 79 | notification1 = NotificationModel().create(created_by=self.u1, |
|
80 | 80 | subject=u'subj', body=u'hi there1', |
|
81 | 81 | recipients=[self.u3]) |
|
82 | 82 | Session().commit() |
|
83 | 83 | notification2 = NotificationModel().create(created_by=self.u1, |
|
84 | 84 | subject=u'subj', body=u'hi there2', |
|
85 | 85 | recipients=[self.u3]) |
|
86 | 86 | Session().commit() |
|
87 | 87 | u3 = Session().query(User).get(self.u3) |
|
88 | 88 | |
|
89 | 89 | self.assertEqual(sorted([x.notification for x in u3.notifications]), |
|
90 | 90 | sorted([notification2, notification1])) |
|
91 | 91 | |
|
92 | 92 | def test_delete_notifications(self): |
|
93 | 93 | self.assertEqual([], Notification.query().all()) |
|
94 | 94 | self.assertEqual([], UserNotification.query().all()) |
|
95 | 95 | |
|
96 | 96 | notification = NotificationModel().create(created_by=self.u1, |
|
97 | 97 | subject=u'title', body=u'hi there3', |
|
98 | 98 | recipients=[self.u3, self.u1, self.u2]) |
|
99 | 99 | Session().commit() |
|
100 | 100 | notifications = Notification.query().all() |
|
101 | 101 | self.assertTrue(notification in notifications) |
|
102 | 102 | |
|
103 | 103 | Notification.delete(notification.notification_id) |
|
104 | 104 | Session().commit() |
|
105 | 105 | |
|
106 | 106 | notifications = Notification.query().all() |
|
107 | 107 | self.assertFalse(notification in notifications) |
|
108 | 108 | |
|
109 | 109 | un = UserNotification.query().filter(UserNotification.notification |
|
110 | 110 | == notification).all() |
|
111 | 111 | self.assertEqual(un, []) |
|
112 | 112 | |
|
113 | 113 | def test_delete_association(self): |
|
114 | 114 | |
|
115 | 115 | self.assertEqual([], Notification.query().all()) |
|
116 | 116 | self.assertEqual([], UserNotification.query().all()) |
|
117 | 117 | |
|
118 | 118 | notification = NotificationModel().create(created_by=self.u1, |
|
119 | 119 | subject=u'title', body=u'hi there3', |
|
120 | 120 | recipients=[self.u3, self.u1, self.u2]) |
|
121 | 121 | Session().commit() |
|
122 | 122 | |
|
123 | 123 | unotification = UserNotification.query()\ |
|
124 | 124 | .filter(UserNotification.notification == |
|
125 | 125 | notification)\ |
|
126 | 126 | .filter(UserNotification.user_id == self.u3)\ |
|
127 | 127 | .scalar() |
|
128 | 128 | |
|
129 | 129 | self.assertEqual(unotification.user_id, self.u3) |
|
130 | 130 | |
|
131 | 131 | NotificationModel().delete(self.u3, |
|
132 | 132 | notification.notification_id) |
|
133 | 133 | Session().commit() |
|
134 | 134 | |
|
135 | 135 | u3notification = UserNotification.query()\ |
|
136 | 136 | .filter(UserNotification.notification == |
|
137 | 137 | notification)\ |
|
138 | 138 | .filter(UserNotification.user_id == self.u3)\ |
|
139 | 139 | .scalar() |
|
140 | 140 | |
|
141 | 141 | self.assertEqual(u3notification, None) |
|
142 | 142 | |
|
143 | 143 | # notification object is still there |
|
144 | 144 | self.assertEqual(Notification.query().all(), [notification]) |
|
145 | 145 | |
|
146 | 146 | #u1 and u2 still have assignments |
|
147 | 147 | u1notification = UserNotification.query()\ |
|
148 | 148 | .filter(UserNotification.notification == |
|
149 | 149 | notification)\ |
|
150 | 150 | .filter(UserNotification.user_id == self.u1)\ |
|
151 | 151 | .scalar() |
|
152 | 152 | self.assertNotEqual(u1notification, None) |
|
153 | 153 | u2notification = UserNotification.query()\ |
|
154 | 154 | .filter(UserNotification.notification == |
|
155 | 155 | notification)\ |
|
156 | 156 | .filter(UserNotification.user_id == self.u2)\ |
|
157 | 157 | .scalar() |
|
158 | 158 | self.assertNotEqual(u2notification, None) |
|
159 | 159 | |
|
160 | 160 | def test_notification_counter(self): |
|
161 | 161 | self._clean_notifications() |
|
162 | 162 | self.assertEqual([], Notification.query().all()) |
|
163 | 163 | self.assertEqual([], UserNotification.query().all()) |
|
164 | 164 | |
|
165 | 165 | NotificationModel().create(created_by=self.u1, |
|
166 | 166 | subject=u'title', body=u'hi there_delete', |
|
167 | 167 | recipients=[self.u3, self.u1]) |
|
168 | 168 | Session().commit() |
|
169 | 169 | |
|
170 | 170 | self.assertEqual(NotificationModel() |
|
171 | 171 | .get_unread_cnt_for_user(self.u1), 1) |
|
172 | 172 | self.assertEqual(NotificationModel() |
|
173 | 173 | .get_unread_cnt_for_user(self.u2), 0) |
|
174 | 174 | self.assertEqual(NotificationModel() |
|
175 | 175 | .get_unread_cnt_for_user(self.u3), 1) |
|
176 | 176 | |
|
177 | 177 | notification = NotificationModel().create(created_by=self.u1, |
|
178 | 178 | subject=u'title', body=u'hi there3', |
|
179 | 179 | recipients=[self.u3, self.u1, self.u2]) |
|
180 | 180 | Session().commit() |
|
181 | 181 | |
|
182 | 182 | self.assertEqual(NotificationModel() |
|
183 | 183 | .get_unread_cnt_for_user(self.u1), 2) |
|
184 | 184 | self.assertEqual(NotificationModel() |
|
185 | 185 | .get_unread_cnt_for_user(self.u2), 1) |
|
186 | 186 | self.assertEqual(NotificationModel() |
|
187 | 187 | .get_unread_cnt_for_user(self.u3), 2) |
|
188 | ||
|
189 | ||
|
190 | ||
|
191 |
@@ -1,170 +1,170 b'' | |||
|
1 | 1 | import os |
|
2 | 2 | import unittest |
|
3 | 3 | from rhodecode.tests import * |
|
4 | 4 | |
|
5 | 5 | from rhodecode.model.repos_group import ReposGroupModel |
|
6 | 6 | from rhodecode.model.repo import RepoModel |
|
7 | 7 | from rhodecode.model.db import RepoGroup, User |
|
8 | 8 | from rhodecode.model.meta import Session |
|
9 | 9 | from sqlalchemy.exc import IntegrityError |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | def _make_group(path, desc='desc', parent_id=None, |
|
13 | 13 | skip_if_exists=False): |
|
14 | 14 | |
|
15 | 15 | gr = RepoGroup.get_by_group_name(path) |
|
16 | 16 | if gr and skip_if_exists: |
|
17 | 17 | return gr |
|
18 | 18 | |
|
19 | 19 | gr = ReposGroupModel().create(path, desc, parent_id) |
|
20 | 20 | return gr |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | class TestReposGroups(unittest.TestCase): |
|
24 | 24 | |
|
25 | 25 | def setUp(self): |
|
26 | 26 | self.g1 = _make_group('test1', skip_if_exists=True) |
|
27 | 27 | Session().commit() |
|
28 | 28 | self.g2 = _make_group('test2', skip_if_exists=True) |
|
29 | 29 | Session().commit() |
|
30 | 30 | self.g3 = _make_group('test3', skip_if_exists=True) |
|
31 | 31 | Session().commit() |
|
32 | 32 | |
|
33 | 33 | def tearDown(self): |
|
34 | 34 | print 'out' |
|
35 | 35 | |
|
36 | 36 | def __check_path(self, *path): |
|
37 | 37 | """ |
|
38 | 38 | Checks the path for existance ! |
|
39 | 39 | """ |
|
40 | 40 | path = [TESTS_TMP_PATH] + list(path) |
|
41 | 41 | path = os.path.join(*path) |
|
42 | 42 | return os.path.isdir(path) |
|
43 | 43 | |
|
44 | 44 | def _check_folders(self): |
|
45 | 45 | print os.listdir(TESTS_TMP_PATH) |
|
46 | 46 | |
|
47 | 47 | def __delete_group(self, id_): |
|
48 | 48 | ReposGroupModel().delete(id_) |
|
49 | 49 | |
|
50 | 50 | def __update_group(self, id_, path, desc='desc', parent_id=None): |
|
51 | 51 | form_data = dict( |
|
52 | 52 | group_name=path, |
|
53 | 53 | group_description=desc, |
|
54 | 54 | group_parent_id=parent_id, |
|
55 | 55 | perms_updates=[], |
|
56 | 56 | perms_new=[] |
|
57 | 57 | ) |
|
58 | 58 | gr = ReposGroupModel().update(id_, form_data) |
|
59 | 59 | return gr |
|
60 | 60 | |
|
61 | 61 | def test_create_group(self): |
|
62 | 62 | g = _make_group('newGroup') |
|
63 | 63 | self.assertEqual(g.full_path, 'newGroup') |
|
64 | 64 | |
|
65 | 65 | self.assertTrue(self.__check_path('newGroup')) |
|
66 | 66 | |
|
67 | 67 | def test_create_same_name_group(self): |
|
68 | 68 | self.assertRaises(IntegrityError, lambda: _make_group('newGroup')) |
|
69 | 69 | Session().rollback() |
|
70 | 70 | |
|
71 | 71 | def test_same_subgroup(self): |
|
72 | 72 | sg1 = _make_group('sub1', parent_id=self.g1.group_id) |
|
73 | 73 | self.assertEqual(sg1.parent_group, self.g1) |
|
74 | 74 | self.assertEqual(sg1.full_path, 'test1/sub1') |
|
75 | 75 | self.assertTrue(self.__check_path('test1', 'sub1')) |
|
76 | 76 | |
|
77 | 77 | ssg1 = _make_group('subsub1', parent_id=sg1.group_id) |
|
78 | 78 | self.assertEqual(ssg1.parent_group, sg1) |
|
79 | 79 | self.assertEqual(ssg1.full_path, 'test1/sub1/subsub1') |
|
80 | 80 | self.assertTrue(self.__check_path('test1', 'sub1', 'subsub1')) |
|
81 | 81 | |
|
82 | 82 | def test_remove_group(self): |
|
83 | 83 | sg1 = _make_group('deleteme') |
|
84 | 84 | self.__delete_group(sg1.group_id) |
|
85 | 85 | |
|
86 | 86 | self.assertEqual(RepoGroup.get(sg1.group_id), None) |
|
87 | 87 | self.assertFalse(self.__check_path('deteteme')) |
|
88 | 88 | |
|
89 | 89 | sg1 = _make_group('deleteme', parent_id=self.g1.group_id) |
|
90 | 90 | self.__delete_group(sg1.group_id) |
|
91 | 91 | |
|
92 | 92 | self.assertEqual(RepoGroup.get(sg1.group_id), None) |
|
93 | 93 | self.assertFalse(self.__check_path('test1', 'deteteme')) |
|
94 | 94 | |
|
95 | 95 | def test_rename_single_group(self): |
|
96 | 96 | sg1 = _make_group('initial') |
|
97 | 97 | |
|
98 | 98 | new_sg1 = self.__update_group(sg1.group_id, 'after') |
|
99 | 99 | self.assertTrue(self.__check_path('after')) |
|
100 | 100 | self.assertEqual(RepoGroup.get_by_group_name('initial'), None) |
|
101 | 101 | |
|
102 | 102 | def test_update_group_parent(self): |
|
103 | 103 | |
|
104 | 104 | sg1 = _make_group('initial', parent_id=self.g1.group_id) |
|
105 | 105 | |
|
106 | 106 | new_sg1 = self.__update_group(sg1.group_id, 'after', parent_id=self.g1.group_id) |
|
107 | 107 | self.assertTrue(self.__check_path('test1', 'after')) |
|
108 | 108 | self.assertEqual(RepoGroup.get_by_group_name('test1/initial'), None) |
|
109 | 109 | |
|
110 | 110 | new_sg1 = self.__update_group(sg1.group_id, 'after', parent_id=self.g3.group_id) |
|
111 | 111 | self.assertTrue(self.__check_path('test3', 'after')) |
|
112 | 112 | self.assertEqual(RepoGroup.get_by_group_name('test3/initial'), None) |
|
113 | 113 | |
|
114 | 114 | new_sg1 = self.__update_group(sg1.group_id, 'hello') |
|
115 | 115 | self.assertTrue(self.__check_path('hello')) |
|
116 | 116 | |
|
117 | 117 | self.assertEqual(RepoGroup.get_by_group_name('hello'), new_sg1) |
|
118 | 118 | |
|
119 | 119 | def test_subgrouping_with_repo(self): |
|
120 | 120 | |
|
121 | 121 | g1 = _make_group('g1') |
|
122 | 122 | g2 = _make_group('g2') |
|
123 | 123 | |
|
124 | 124 | # create new repo |
|
125 | 125 | form_data = dict(repo_name='john', |
|
126 | 126 | repo_name_full='john', |
|
127 | 127 | fork_name=None, |
|
128 | 128 | description=None, |
|
129 | 129 | repo_group=None, |
|
130 | 130 | private=False, |
|
131 | 131 | repo_type='hg', |
|
132 | 132 | clone_uri=None, |
|
133 | 133 | landing_rev='tip') |
|
134 | 134 | cur_user = User.get_by_username(TEST_USER_ADMIN_LOGIN) |
|
135 | 135 | r = RepoModel().create(form_data, cur_user) |
|
136 | 136 | |
|
137 | 137 | self.assertEqual(r.repo_name, 'john') |
|
138 | 138 | |
|
139 | 139 | # put repo into group |
|
140 | 140 | form_data = form_data |
|
141 | 141 | form_data['repo_group'] = g1.group_id |
|
142 | 142 | form_data['perms_new'] = [] |
|
143 | 143 | form_data['perms_updates'] = [] |
|
144 | 144 | RepoModel().update(r.repo_name, form_data) |
|
145 | 145 | self.assertEqual(r.repo_name, 'g1/john') |
|
146 | 146 | |
|
147 | 147 | self.__update_group(g1.group_id, 'g1', parent_id=g2.group_id) |
|
148 | 148 | self.assertTrue(self.__check_path('g2', 'g1')) |
|
149 | 149 | |
|
150 | 150 | # test repo |
|
151 | 151 | self.assertEqual(r.repo_name, RepoGroup.url_sep().join(['g2', 'g1', |
|
152 | 152 | r.just_name])) |
|
153 | 153 | |
|
154 | 154 | def test_move_to_root(self): |
|
155 | 155 | g1 = _make_group('t11') |
|
156 | 156 | Session().commit() |
|
157 | 157 | g2 = _make_group('t22', parent_id=g1.group_id) |
|
158 | 158 | Session().commit() |
|
159 | 159 | |
|
160 | 160 | self.assertEqual(g2.full_path, 't11/t22') |
|
161 | 161 | self.assertTrue(self.__check_path('t11', 't22')) |
|
162 | 162 | |
|
163 | 163 | g2 = self.__update_group(g2.group_id, 'g22', parent_id=None) |
|
164 | 164 | Session().commit() |
|
165 | 165 | |
|
166 | 166 | self.assertEqual(g2.group_name, 'g22') |
|
167 | 167 | # we moved out group from t1 to '' so it's full path should be 'g2' |
|
168 | 168 | self.assertEqual(g2.full_path, 'g22') |
|
169 | 169 | self.assertFalse(self.__check_path('t11', 't22')) |
|
170 | self.assertTrue(self.__check_path('g22')) No newline at end of file | |
|
170 | self.assertTrue(self.__check_path('g22')) |
General Comments 0
You need to be logged in to leave comments.
Login now