##// END OF EJS Templates
fixed repo_create permission by adding missing commit statements...
marcink -
r1758:a87aa385 beta
parent child Browse files
Show More
@@ -197,7 +197,7 b' class UsersController(BaseController):'
197 user_model.grant_perm(id, perm)
197 user_model.grant_perm(id, perm)
198 h.flash(_("Granted 'repository create' permission to user"),
198 h.flash(_("Granted 'repository create' permission to user"),
199 category='success')
199 category='success')
200
200 Session.commit()
201 else:
201 else:
202 perm = Permission.get_by_key('hg.create.repository')
202 perm = Permission.get_by_key('hg.create.repository')
203 user_model.revoke_perm(id, perm)
203 user_model.revoke_perm(id, perm)
@@ -206,5 +206,5 b' class UsersController(BaseController):'
206 user_model.grant_perm(id, perm)
206 user_model.grant_perm(id, perm)
207 h.flash(_("Revoked 'repository create' permission to user"),
207 h.flash(_("Revoked 'repository create' permission to user"),
208 category='success')
208 category='success')
209
209 Session.commit()
210 return redirect(url('edit_user', id=id))
210 return redirect(url('edit_user', id=id))
@@ -245,12 +245,20 b' class DbManage(object):'
245 self.create_user(username, password, email, True)
245 self.create_user(username, password, email, True)
246 else:
246 else:
247 log.info('creating admin and regular test users')
247 log.info('creating admin and regular test users')
248 self.create_user('test_admin', 'test12',
248 from rhodecode.tests import TEST_USER_ADMIN_LOGIN,\
249 'test_admin@mail.com', True)
249 TEST_USER_ADMIN_PASS ,TEST_USER_ADMIN_EMAIL,TEST_USER_REGULAR_LOGIN,\
250 self.create_user('test_regular', 'test12',
250 TEST_USER_REGULAR_PASS,TEST_USER_REGULAR_EMAIL,\
251 'test_regular@mail.com', False)
251 TEST_USER_REGULAR2_LOGIN,TEST_USER_REGULAR2_PASS,\
252 self.create_user('test_regular2', 'test12',
252 TEST_USER_REGULAR2_EMAIL
253 'test_regular2@mail.com', False)
253
254 self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS,
255 TEST_USER_ADMIN_EMAIL, True)
256
257 self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS,
258 TEST_USER_REGULAR_EMAIL, False)
259
260 self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS,
261 TEST_USER_REGULAR2_EMAIL, False)
254
262
255 def create_ui_settings(self):
263 def create_ui_settings(self):
256 """Creates ui settings, fills out hooks
264 """Creates ui settings, fills out hooks
@@ -82,8 +82,8 b' class ModelSerializer(json.JSONEncoder):'
82 return json.JSONEncoder.default(self, obj)
82 return json.JSONEncoder.default(self, obj)
83
83
84 class BaseModel(object):
84 class BaseModel(object):
85 """Base Model for all classess
85 """
86
86 Base Model for all classess
87 """
87 """
88
88
89 @classmethod
89 @classmethod
@@ -91,13 +91,20 b' class BaseModel(object):'
91 """return column names for this model """
91 """return column names for this model """
92 return class_mapper(cls).c.keys()
92 return class_mapper(cls).c.keys()
93
93
94 def get_dict(self):
94 def get_dict(self, serialized=False):
95 """return dict with keys and values corresponding
95 """
96 to this model data """
96 return dict with keys and values corresponding
97 to this model data
98 """
97
99
98 d = {}
100 d = {}
99 for k in self._get_keys():
101 for k in self._get_keys():
100 d[k] = getattr(self, k)
102 d[k] = getattr(self, k)
103
104 # also use __json__() if present to get additional fields
105 if hasattr(self, '__json__'):
106 for k,val in self.__json__().iteritems():
107 d[k] = val
101 return d
108 return d
102
109
103 def get_appstruct(self):
110 def get_appstruct(self):
@@ -350,6 +357,11 b' class User(Base, BaseModel):'
350 log.debug('updated user %s lastlogin', self.username)
357 log.debug('updated user %s lastlogin', self.username)
351
358
352
359
360 def __json__(self):
361 return dict(email=self.email,
362 full_name=self.full_name)
363
364
353 class UserLog(Base, BaseModel):
365 class UserLog(Base, BaseModel):
354 __tablename__ = 'user_logs'
366 __tablename__ = 'user_logs'
355 __table_args__ = {'extend_existing':True}
367 __table_args__ = {'extend_existing':True}
@@ -281,9 +281,10 b' class UserModel(BaseModel):'
281 log.error(traceback.format_exc())
281 log.error(traceback.format_exc())
282 raise
282 raise
283
283
284 def delete(self, user_id):
284 def delete(self, user):
285 user = self.__get_user(user)
286
285 try:
287 try:
286 user = self.get(user_id, cache=False)
287 if user.username == 'default':
288 if user.username == 'default':
288 raise DefaultUserException(
289 raise DefaultUserException(
289 _("You can't remove this user since it's"
290 _("You can't remove this user since it's"
@@ -470,35 +471,37 b' class UserModel(BaseModel):'
470
471
471 return user
472 return user
472
473
473
474
475 def has_perm(self, user, perm):
474 def has_perm(self, user, perm):
476 if not isinstance(perm, Permission):
475 if not isinstance(perm, Permission):
477 raise Exception('perm needs to be an instance of Permission class')
476 raise Exception('perm needs to be an instance of Permission class '
477 'got %s instead' % type(perm))
478
478
479 user = self.__get_user(user)
479 user = self.__get_user(user)
480
480
481 return UserToPerm.query().filter(UserToPerm.user == user.user)\
481 return UserToPerm.query().filter(UserToPerm.user == user)\
482 .filter(UserToPerm.permission == perm).scalar() is not None
482 .filter(UserToPerm.permission == perm).scalar() is not None
483
483
484 def grant_perm(self, user, perm):
484 def grant_perm(self, user, perm):
485 if not isinstance(perm, Permission):
485 if not isinstance(perm, Permission):
486 raise Exception('perm needs to be an instance of Permission class')
486 raise Exception('perm needs to be an instance of Permission class '
487 'got %s instead' % type(perm))
487
488
488 user = self.__get_user(user)
489 user = self.__get_user(user)
489
490
490 new = UserToPerm()
491 new = UserToPerm()
491 new.user = user.user
492 new.user = user
492 new.permission = perm
493 new.permission = perm
493 self.sa.add(new)
494 self.sa.add(new)
494
495
495
496
496 def revoke_perm(self, user, perm):
497 def revoke_perm(self, user, perm):
497 if not isinstance(perm, Permission):
498 if not isinstance(perm, Permission):
498 raise Exception('perm needs to be an instance of Permission class')
499 raise Exception('perm needs to be an instance of Permission class '
500 'got %s instead' % type(perm))
499
501
500 user = self.__get_user(user)
502 user = self.__get_user(user)
501
503
502 obj = UserToPerm.query().filter(UserToPerm.user == user.user)\
504 obj = UserToPerm.query().filter(UserToPerm.user == user)\
503 .filter(UserToPerm.permission == perm).one()
505 .filter(UserToPerm.permission == perm).scalar()
504 self.sa.delete(obj)
506 if obj:
507 self.sa.delete(obj)
@@ -31,9 +31,13 b' time.tzset()'
31
31
32 log = logging.getLogger(__name__)
32 log = logging.getLogger(__name__)
33
33
34 __all__ = ['environ', 'url', 'TestController', 'TESTS_TMP_PATH', 'HG_REPO',
34 __all__ = [
35 'GIT_REPO', 'NEW_HG_REPO', 'NEW_GIT_REPO', 'HG_FORK', 'GIT_FORK',
35 'environ', 'url', 'TestController', 'TESTS_TMP_PATH', 'HG_REPO',
36 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_ADMIN_PASS' ]
36 'GIT_REPO', 'NEW_HG_REPO', 'NEW_GIT_REPO', 'HG_FORK', 'GIT_FORK',
37 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_REGULAR_LOGIN', 'TEST_USER_REGULAR_PASS',
38 'TEST_USER_REGULAR_EMAIL', 'TEST_USER_REGULAR2_LOGIN',
39 'TEST_USER_REGULAR2_PASS', 'TEST_USER_REGULAR2_EMAIL'
40 ]
37
41
38 # Invoke websetup with the current config file
42 # Invoke websetup with the current config file
39 # SetupCommand('setup-app').run([config_file])
43 # SetupCommand('setup-app').run([config_file])
@@ -48,6 +52,16 b' environ = {}'
48 TESTS_TMP_PATH = jn('/', 'tmp', 'rc_test_%s' % _RandomNameSequence().next())
52 TESTS_TMP_PATH = jn('/', 'tmp', 'rc_test_%s' % _RandomNameSequence().next())
49 TEST_USER_ADMIN_LOGIN = 'test_admin'
53 TEST_USER_ADMIN_LOGIN = 'test_admin'
50 TEST_USER_ADMIN_PASS = 'test12'
54 TEST_USER_ADMIN_PASS = 'test12'
55 TEST_USER_ADMIN_EMAIL = 'test_admin@mail.com'
56
57 TEST_USER_REGULAR_LOGIN = 'test_regular'
58 TEST_USER_REGULAR_PASS = 'test12'
59 TEST_USER_REGULAR_EMAIL = 'test_regular@mail.com'
60
61 TEST_USER_REGULAR2_LOGIN = 'test_regular2'
62 TEST_USER_REGULAR2_PASS = 'test12'
63 TEST_USER_REGULAR2_EMAIL = 'test_regular2@mail.com'
64
51 HG_REPO = 'vcs_test_hg'
65 HG_REPO = 'vcs_test_hg'
52 GIT_REPO = 'vcs_test_git'
66 GIT_REPO = 'vcs_test_git'
53
67
@@ -1,11 +1,13 b''
1 from rhodecode.tests import *
1 from rhodecode.tests import *
2 from rhodecode.model.db import User
2 from rhodecode.model.db import User, Permission
3 from rhodecode.lib.auth import check_password
3 from rhodecode.lib.auth import check_password
4 from sqlalchemy.orm.exc import NoResultFound
4 from sqlalchemy.orm.exc import NoResultFound
5 from rhodecode.model.user import UserModel
5
6
6 class TestAdminUsersController(TestController):
7 class TestAdminUsersController(TestController):
7
8
8 def test_index(self):
9 def test_index(self):
10 self.log_user()
9 response = self.app.get(url('users'))
11 response = self.app.get(url('users'))
10 # Test response...
12 # Test response...
11
13
@@ -21,30 +23,31 b' class TestAdminUsersController(TestContr'
21 lastname = 'lastname'
23 lastname = 'lastname'
22 email = 'mail@mail.com'
24 email = 'mail@mail.com'
23
25
24 response = self.app.post(url('users'), {'username':username,
26 response = self.app.post(url('users'),
25 'password':password,
27 {'username':username,
26 'password_confirmation':password_confirmation,
28 'password':password,
27 'name':name,
29 'password_confirmation':password_confirmation,
28 'active':True,
30 'name':name,
29 'lastname':lastname,
31 'active':True,
30 'email':email})
32 'lastname':lastname,
33 'email':email})
31
34
32
35
33 assert '''created user %s''' % (username) in response.session['flash'][0], 'No flash message about new user'
36 self.assertTrue('''created user %s''' % (username) in
37 response.session['flash'][0])
34
38
35 new_user = self.Session.query(User).filter(User.username == username).one()
39 new_user = self.Session.query(User).\
36
40 filter(User.username == username).one()
37
41
38 assert new_user.username == username, 'wrong info about username'
42 self.assertEqual(new_user.username,username)
39 assert check_password(password, new_user.password) == True , 'wrong info about password'
43 self.assertEqual(check_password(password, new_user.password),True)
40 assert new_user.name == name, 'wrong info about name'
44 self.assertEqual(new_user.name,name)
41 assert new_user.lastname == lastname, 'wrong info about lastname'
45 self.assertEqual(new_user.lastname,lastname)
42 assert new_user.email == email, 'wrong info about email'
46 self.assertEqual(new_user.email,email)
43
44
47
45 response.follow()
48 response.follow()
46 response = response.follow()
49 response = response.follow()
47 assert """edit">newtestuser</a>""" in response.body
50 self.assertTrue("""edit">newtestuser</a>""" in response.body)
48
51
49 def test_create_err(self):
52 def test_create_err(self):
50 self.log_user()
53 self.log_user()
@@ -61,9 +64,9 b' class TestAdminUsersController(TestContr'
61 'lastname':lastname,
64 'lastname':lastname,
62 'email':email})
65 'email':email})
63
66
64 assert """<span class="error-message">Invalid username</span>""" in response.body
67 self.assertTrue("""<span class="error-message">Invalid username</span>""" in response.body)
65 assert """<span class="error-message">Please enter a value</span>""" in response.body
68 self.assertTrue("""<span class="error-message">Please enter a value</span>""" in response.body)
66 assert """<span class="error-message">An email address must contain a single @</span>""" in response.body
69 self.assertTrue("""<span class="error-message">An email address must contain a single @</span>""" in response.body)
67
70
68 def get_user():
71 def get_user():
69 self.Session.query(User).filter(User.username == username).one()
72 self.Session.query(User).filter(User.username == username).one()
@@ -71,6 +74,7 b' class TestAdminUsersController(TestContr'
71 self.assertRaises(NoResultFound, get_user), 'found user in database'
74 self.assertRaises(NoResultFound, get_user), 'found user in database'
72
75
73 def test_new(self):
76 def test_new(self):
77 self.log_user()
74 response = self.app.get(url('new_user'))
78 response = self.app.get(url('new_user'))
75
79
76 def test_new_as_xml(self):
80 def test_new_as_xml(self):
@@ -100,14 +104,17 b' class TestAdminUsersController(TestContr'
100
104
101 response = response.follow()
105 response = response.follow()
102
106
103 new_user = self.Session.query(User).filter(User.username == username).one()
107 new_user = self.Session.query(User)\
108 .filter(User.username == username).one()
104 response = self.app.delete(url('user', id=new_user.user_id))
109 response = self.app.delete(url('user', id=new_user.user_id))
105
110
106 assert """successfully deleted user""" in response.session['flash'][0], 'No info about user deletion'
111 self.assertTrue("""successfully deleted user""" in
112 response.session['flash'][0])
107
113
108
114
109 def test_delete_browser_fakeout(self):
115 def test_delete_browser_fakeout(self):
110 response = self.app.post(url('user', id=1), params=dict(_method='delete'))
116 response = self.app.post(url('user', id=1),
117 params=dict(_method='delete'))
111
118
112 def test_show(self):
119 def test_show(self):
113 response = self.app.get(url('user', id=1))
120 response = self.app.get(url('user', id=1))
@@ -116,7 +123,57 b' class TestAdminUsersController(TestContr'
116 response = self.app.get(url('formatted_user', id=1, format='xml'))
123 response = self.app.get(url('formatted_user', id=1, format='xml'))
117
124
118 def test_edit(self):
125 def test_edit(self):
119 response = self.app.get(url('edit_user', id=1))
126 self.log_user()
127 user = User.get_by_username(TEST_USER_ADMIN_LOGIN)
128 response = self.app.get(url('edit_user', id=user.user_id))
129
130
131 def test_add_perm_create_repo(self):
132 self.log_user()
133 perm_none = Permission.get_by_key('hg.create.none')
134 perm_create = Permission.get_by_key('hg.create.repository')
135
136 user = User.get_by_username(TEST_USER_REGULAR_LOGIN)
137
138
139 #User should have None permission on creation repository
140 self.assertEqual(UserModel().has_perm(user, perm_none), False)
141 self.assertEqual(UserModel().has_perm(user, perm_create), False)
142
143 response = self.app.post(url('user_perm', id=user.user_id),
144 params=dict(_method='put',
145 create_repo_perm=True))
146
147 perm_none = Permission.get_by_key('hg.create.none')
148 perm_create = Permission.get_by_key('hg.create.repository')
149
150 user = User.get_by_username(TEST_USER_REGULAR_LOGIN)
151 #User should have None permission on creation repository
152 self.assertEqual(UserModel().has_perm(user, perm_none), False)
153 self.assertEqual(UserModel().has_perm(user, perm_create), True)
154
155 def test_revoke_perm_create_repo(self):
156 self.log_user()
157 perm_none = Permission.get_by_key('hg.create.none')
158 perm_create = Permission.get_by_key('hg.create.repository')
159
160 user = User.get_by_username(TEST_USER_REGULAR2_LOGIN)
161
162
163 #User should have None permission on creation repository
164 self.assertEqual(UserModel().has_perm(user, perm_none), False)
165 self.assertEqual(UserModel().has_perm(user, perm_create), False)
166
167 response = self.app.post(url('user_perm', id=user.user_id),
168 params=dict(_method='put'))
169
170 perm_none = Permission.get_by_key('hg.create.none')
171 perm_create = Permission.get_by_key('hg.create.repository')
172
173 user = User.get_by_username(TEST_USER_REGULAR2_LOGIN)
174 #User should have None permission on creation repository
175 self.assertEqual(UserModel().has_perm(user, perm_none), True)
176 self.assertEqual(UserModel().has_perm(user, perm_create), False)
120
177
121 def test_edit_as_xml(self):
178 def test_edit_as_xml(self):
122 response = self.app.get(url('formatted_edit_user', id=1, format='xml'))
179 response = self.app.get(url('formatted_edit_user', id=1, format='xml'))
@@ -5,7 +5,7 b' from rhodecode.tests import *'
5 from rhodecode.model.repos_group import ReposGroupModel
5 from rhodecode.model.repos_group import ReposGroupModel
6 from rhodecode.model.repo import RepoModel
6 from rhodecode.model.repo import RepoModel
7 from rhodecode.model.db import RepoGroup, User, Notification, UserNotification, \
7 from rhodecode.model.db import RepoGroup, User, Notification, UserNotification, \
8 UsersGroup, UsersGroupMember
8 UsersGroup, UsersGroupMember, Permission
9 from sqlalchemy.exc import IntegrityError
9 from sqlalchemy.exc import IntegrityError
10 from rhodecode.model.user import UserModel
10 from rhodecode.model.user import UserModel
11
11
@@ -158,7 +158,10 b' class TestReposGroups(unittest.TestCase)'
158 self.assertEqual(r.repo_name, os.path.join('g2', 'g1', r.just_name))
158 self.assertEqual(r.repo_name, os.path.join('g2', 'g1', r.just_name))
159
159
160 class TestUser(unittest.TestCase):
160 class TestUser(unittest.TestCase):
161
161 def __init__(self, methodName='runTest'):
162 Session.remove()
163 super(TestUser, self).__init__(methodName=methodName)
164
162 def test_create_and_remove(self):
165 def test_create_and_remove(self):
163 usr = UserModel().create_or_update(username=u'test_user', password=u'qweqwe',
166 usr = UserModel().create_or_update(username=u'test_user', password=u'qweqwe',
164 email=u'u232@rhodecode.org',
167 email=u'u232@rhodecode.org',
@@ -184,6 +187,7 b' class TestUser(unittest.TestCase):'
184 class TestNotifications(unittest.TestCase):
187 class TestNotifications(unittest.TestCase):
185
188
186 def __init__(self, methodName='runTest'):
189 def __init__(self, methodName='runTest'):
190 Session.remove()
187 self.u1 = UserModel().create_or_update(username=u'u1',
191 self.u1 = UserModel().create_or_update(username=u'u1',
188 password=u'qweqwe',
192 password=u'qweqwe',
189 email=u'u1@rhodecode.org',
193 email=u'u1@rhodecode.org',
@@ -214,6 +218,8 b' class TestNotifications(unittest.TestCas'
214 Session.commit()
218 Session.commit()
215 self.assertEqual(Notification.query().all(), [])
219 self.assertEqual(Notification.query().all(), [])
216
220
221 def tearDown(self):
222 self._clean_notifications()
217
223
218 def test_create_notification(self):
224 def test_create_notification(self):
219 self.assertEqual([], Notification.query().all())
225 self.assertEqual([], Notification.query().all())
@@ -239,7 +245,6 b' class TestNotifications(unittest.TestCas'
239 self.assertEqual(len(unotification), len(usrs))
245 self.assertEqual(len(unotification), len(usrs))
240 self.assertEqual([x.user.user_id for x in unotification], usrs)
246 self.assertEqual([x.user.user_id for x in unotification], usrs)
241
247
242 self._clean_notifications()
243
248
244 def test_user_notifications(self):
249 def test_user_notifications(self):
245 self.assertEqual([], Notification.query().all())
250 self.assertEqual([], Notification.query().all())
@@ -257,7 +262,6 b' class TestNotifications(unittest.TestCas'
257
262
258 self.assertEqual(sorted([x.notification for x in u3.notifications]),
263 self.assertEqual(sorted([x.notification for x in u3.notifications]),
259 sorted([notification2, notification1]))
264 sorted([notification2, notification1]))
260 self._clean_notifications()
261
265
262 def test_delete_notifications(self):
266 def test_delete_notifications(self):
263 self.assertEqual([], Notification.query().all())
267 self.assertEqual([], Notification.query().all())
@@ -280,7 +284,6 b' class TestNotifications(unittest.TestCas'
280 == notification).all()
284 == notification).all()
281 self.assertEqual(un, [])
285 self.assertEqual(un, [])
282
286
283 self._clean_notifications()
284
287
285 def test_delete_association(self):
288 def test_delete_association(self):
286
289
@@ -329,8 +332,6 b' class TestNotifications(unittest.TestCas'
329 .scalar()
332 .scalar()
330 self.assertNotEqual(u2notification, None)
333 self.assertNotEqual(u2notification, None)
331
334
332 self._clean_notifications()
333
334 def test_notification_counter(self):
335 def test_notification_counter(self):
335 self._clean_notifications()
336 self._clean_notifications()
336 self.assertEqual([], Notification.query().all())
337 self.assertEqual([], Notification.query().all())
@@ -359,4 +360,51 b' class TestNotifications(unittest.TestCas'
359 .get_unread_cnt_for_user(self.u2), 1)
360 .get_unread_cnt_for_user(self.u2), 1)
360 self.assertEqual(NotificationModel()
361 self.assertEqual(NotificationModel()
361 .get_unread_cnt_for_user(self.u3), 2)
362 .get_unread_cnt_for_user(self.u3), 2)
362 self._clean_notifications()
363
364 class TestUsers(unittest.TestCase):
365
366 def __init__(self, methodName='runTest'):
367 super(TestUsers, self).__init__(methodName=methodName)
368
369 def setUp(self):
370 self.u1 = UserModel().create_or_update(username=u'u1',
371 password=u'qweqwe',
372 email=u'u1@rhodecode.org',
373 name=u'u1', lastname=u'u1')
374
375 def tearDown(self):
376 perm = Permission.query().all()
377 for p in perm:
378 UserModel().revoke_perm(self.u1, p)
379
380 UserModel().delete(self.u1)
381 Session.commit()
382
383 def test_add_perm(self):
384 perm = Permission.query().all()[0]
385 UserModel().grant_perm(self.u1, perm)
386 Session.commit()
387 self.assertEqual(UserModel().has_perm(self.u1, perm), True)
388
389 def test_has_perm(self):
390 perm = Permission.query().all()
391 for p in perm:
392 has_p = UserModel().has_perm(self.u1, p)
393 self.assertEqual(False, has_p)
394
395 def test_revoke_perm(self):
396 perm = Permission.query().all()[0]
397 UserModel().grant_perm(self.u1, perm)
398 Session.commit()
399 self.assertEqual(UserModel().has_perm(self.u1, perm), True)
400
401 #revoke
402 UserModel().revoke_perm(self.u1, perm)
403 Session.commit()
404 self.assertEqual(UserModel().has_perm(self.u1, perm),False)
405
406
407
408
409
410
General Comments 0
You need to be logged in to leave comments. Login now