Mercurial > kallithea
annotate rhodecode/tests/models/test_users.py @ 3647:8a86836fad64 beta
more usage of fixture tools
in tests
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Mon, 01 Apr 2013 23:45:25 +0200 |
parents | fa6ba6727475 |
children | 7e3d89d9d3a2 |
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 |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3415
diff
changeset
|
4 from rhodecode.model.db import User, UserGroup, UserGroupMember, UserEmailMap,\ |
2527
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 |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3415
diff
changeset
|
9 from rhodecode.model.users_group import UserGroupModel |
2527
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 |
3647
8a86836fad64
more usage of fixture tools
Marcin Kuzminski <marcin@python-works.com>
parents:
3417
diff
changeset
|
17 def tearDown(self): |
8a86836fad64
more usage of fixture tools
Marcin Kuzminski <marcin@python-works.com>
parents:
3417
diff
changeset
|
18 Session.remove() |
8a86836fad64
more usage of fixture tools
Marcin Kuzminski <marcin@python-works.com>
parents:
3417
diff
changeset
|
19 |
2527
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 def test_create_and_remove(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 usr = UserModel().create_or_update(username=u'test_user', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 password=u'qweqwe', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 email=u'u232@rhodecode.org', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 firstname=u'u1', lastname=u'u1') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 self.assertEqual(User.get_by_username(u'test_user'), usr) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 |
3415
b8f929bff7e3
fixed tests and missing replacements from 5f1850e4712a
Marcin Kuzminski <marcin@python-works.com>
parents:
2527
diff
changeset
|
28 # make user group |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3415
diff
changeset
|
29 users_group = UserGroupModel().create('some_example_group') |
2527
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 |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3415
diff
changeset
|
32 UserGroupModel().add_user_to_group(users_group, usr) |
2527
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3415
diff
changeset
|
35 self.assertEqual(UserGroup.get(users_group.users_group_id), users_group) |
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3415
diff
changeset
|
36 self.assertEqual(UserGroupMember.query().count(), 1) |
2527
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 UserModel().delete(usr.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3415
diff
changeset
|
40 self.assertEqual(UserGroupMember.query().all(), []) |
2527
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 def test_additonal_email_as_main(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 usr = UserModel().create_or_update(username=u'test_user', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 password=u'qweqwe', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 email=u'main_email@rhodecode.org', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 firstname=u'u1', lastname=u'u1') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 def do(): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 m = UserEmailMap() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 m.email = u'main_email@rhodecode.org' |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 m.user = usr |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 Session().add(m) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 self.assertRaises(AttributeError, do) |
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 UserModel().delete(usr.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 def test_extra_email_map(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 usr = UserModel().create_or_update(username=u'test_user', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 password=u'qweqwe', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 email=u'main_email@rhodecode.org', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 firstname=u'u1', lastname=u'u1') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
65 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 m = UserEmailMap() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 m.email = u'main_email2@rhodecode.org' |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
69 m.user = usr |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 Session().add(m) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 u = User.get_by_email(email='main_email@rhodecode.org') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 self.assertEqual(usr.user_id, u.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 self.assertEqual(usr.username, u.username) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 u = User.get_by_email(email='main_email2@rhodecode.org') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 self.assertEqual(usr.user_id, u.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 self.assertEqual(usr.username, u.username) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
80 u = User.get_by_email(email='main_email3@rhodecode.org') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
81 self.assertEqual(None, u) |
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 UserModel().delete(usr.user_id) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 Session().commit() |
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 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 class TestUsers(unittest.TestCase): |
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 __init__(self, methodName='runTest'): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 super(TestUsers, self).__init__(methodName=methodName) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 def setUp(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 self.u1 = UserModel().create_or_update(username=u'u1', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 password=u'qweqwe', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
95 email=u'u1@rhodecode.org', |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
96 firstname=u'u1', lastname=u'u1') |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
98 def tearDown(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 perm = Permission.query().all() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 for p in perm: |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 UserModel().revoke_perm(self.u1, p) |
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 UserModel().delete(self.u1) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
104 Session().commit() |
3647
8a86836fad64
more usage of fixture tools
Marcin Kuzminski <marcin@python-works.com>
parents:
3417
diff
changeset
|
105 Session.remove() |
2527
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
106 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
107 def test_add_perm(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 perm = Permission.query().all()[0] |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 UserModel().grant_perm(self.u1, perm) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 self.assertEqual(UserModel().has_perm(self.u1, perm), True) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 def test_has_perm(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 perm = Permission.query().all() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 for p in perm: |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 has_p = UserModel().has_perm(self.u1, p) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 self.assertEqual(False, has_p) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 def test_revoke_perm(self): |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 perm = Permission.query().all()[0] |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 UserModel().grant_perm(self.u1, perm) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 self.assertEqual(UserModel().has_perm(self.u1, perm), True) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
125 #revoke |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
126 UserModel().revoke_perm(self.u1, perm) |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 Session().commit() |
95624ce4465f
orginized test module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 self.assertEqual(UserModel().has_perm(self.u1, perm), False) |