Mercurial > kallithea
annotate rhodecode/tests/models/test_users.py @ 2963:742d1b8ca263 beta
use str() on os.walk passing unicode can lead to UnicodeDecode errors when iterating
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 26 Oct 2012 00:26:20 +0200 |
parents | 95624ce4465f |
children | b8f929bff7e3 |
rev | line source |
---|---|
2527
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 import unittest |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
2 from rhodecode.tests import * |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
3 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
4 from rhodecode.model.db import User, UsersGroup, UsersGroupMember, UserEmailMap,\ |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
5 Permission |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
6 from rhodecode.model.user import UserModel |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
8 from rhodecode.model.meta import Session |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 from rhodecode.model.users_group import UsersGroupModel |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
11 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 class TestUser(unittest.TestCase): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 def __init__(self, methodName='runTest'): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 Session.remove() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 super(TestUser, self).__init__(methodName=methodName) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 def test_create_and_remove(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 usr = UserModel().create_or_update(username=u'test_user', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 password=u'qweqwe', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 email=u'u232@rhodecode.org', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 firstname=u'u1', lastname=u'u1') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 self.assertEqual(User.get_by_username(u'test_user'), usr) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 # make users group |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 users_group = UsersGroupModel().create('some_example_group') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 UsersGroupModel().add_user_to_group(users_group, usr) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 self.assertEqual(UsersGroup.get(users_group.users_group_id), users_group) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 self.assertEqual(UsersGroupMember.query().count(), 1) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 UserModel().delete(usr.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 self.assertEqual(UsersGroupMember.query().all(), []) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 def test_additonal_email_as_main(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 usr = UserModel().create_or_update(username=u'test_user', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 password=u'qweqwe', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 email=u'main_email@rhodecode.org', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 firstname=u'u1', lastname=u'u1') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 def do(): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 m = UserEmailMap() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 m.email = u'main_email@rhodecode.org' |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 m.user = usr |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 Session().add(m) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 self.assertRaises(AttributeError, do) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 UserModel().delete(usr.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 def test_extra_email_map(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 usr = UserModel().create_or_update(username=u'test_user', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 password=u'qweqwe', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 email=u'main_email@rhodecode.org', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 firstname=u'u1', lastname=u'u1') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 m = UserEmailMap() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
65 m.email = u'main_email2@rhodecode.org' |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 m.user = usr |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 Session().add(m) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
69 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 u = User.get_by_email(email='main_email@rhodecode.org') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 self.assertEqual(usr.user_id, u.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 self.assertEqual(usr.username, u.username) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 u = User.get_by_email(email='main_email2@rhodecode.org') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 self.assertEqual(usr.user_id, u.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 self.assertEqual(usr.username, u.username) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 u = User.get_by_email(email='main_email3@rhodecode.org') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 self.assertEqual(None, u) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
80 UserModel().delete(usr.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
81 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
82 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
83 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 class TestUsers(unittest.TestCase): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
85 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
86 def __init__(self, methodName='runTest'): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 super(TestUsers, self).__init__(methodName=methodName) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
88 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 def setUp(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 self.u1 = UserModel().create_or_update(username=u'u1', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 password=u'qweqwe', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 email=u'u1@rhodecode.org', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 firstname=u'u1', lastname=u'u1') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
95 def tearDown(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
96 perm = Permission.query().all() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 for p in perm: |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
98 UserModel().revoke_perm(self.u1, p) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 UserModel().delete(self.u1) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
103 def test_add_perm(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
104 perm = Permission.query().all()[0] |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
105 UserModel().grant_perm(self.u1, perm) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
106 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
107 self.assertEqual(UserModel().has_perm(self.u1, perm), True) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 def test_has_perm(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 perm = Permission.query().all() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 for p in perm: |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 has_p = UserModel().has_perm(self.u1, p) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 self.assertEqual(False, has_p) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 def test_revoke_perm(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 perm = Permission.query().all()[0] |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 UserModel().grant_perm(self.u1, perm) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 self.assertEqual(UserModel().has_perm(self.u1, perm), True) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 #revoke |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 UserModel().revoke_perm(self.u1, perm) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 self.assertEqual(UserModel().has_perm(self.u1, perm), False) |