Show More
@@ -1,139 +1,141 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import colander |
|
22 | 22 | import pytest |
|
23 | 23 | |
|
24 | 24 | from rhodecode.model import validation_schema |
|
25 | from rhodecode.model.db import Session | |
|
25 | 26 | from rhodecode.model.user import UserModel |
|
26 | 27 | from rhodecode.model.user_group import UserGroupModel |
|
27 | 28 | from rhodecode.model.validation_schema.types import ( |
|
28 | 29 | UserOrUserGroupType, UserType, UserGroupType |
|
29 | 30 | ) |
|
30 | 31 | |
|
31 | 32 | |
|
32 | 33 | class TestUserAndUserGroupSchemaType(object): |
|
33 | 34 | |
|
34 | 35 | class Schema(colander.Schema): |
|
35 | 36 | user_or_usergroup = colander.SchemaNode(UserOrUserGroupType()) |
|
36 | 37 | |
|
37 | 38 | def test_serialize(self, user_regular, test_user_group): |
|
38 | 39 | schema = self.Schema() |
|
39 | 40 | |
|
40 | 41 | assert schema.serialize({'user_or_usergroup': user_regular}) == ( |
|
41 | 42 | {'user_or_usergroup': 'user:' + user_regular.username}) |
|
42 | 43 | assert schema.serialize({'user_or_usergroup': test_user_group}) == ( |
|
43 | 44 | {'user_or_usergroup': 'usergroup:' + test_user_group.users_group_name}) |
|
44 | 45 | |
|
45 | 46 | with pytest.raises(colander.Invalid): |
|
46 | 47 | schema.serialize({'user_or_usergroup': 'invalidusername'}) |
|
47 | 48 | |
|
48 | 49 | def test_deserialize(self, test_user_group, user_regular): |
|
49 | 50 | schema = self.Schema() |
|
50 | 51 | |
|
51 | 52 | assert schema.deserialize( |
|
52 | 53 | {'user_or_usergroup': test_user_group.users_group_name} |
|
53 | 54 | ) == {'user_or_usergroup': test_user_group} |
|
54 | 55 | |
|
55 | 56 | assert schema.deserialize( |
|
56 | 57 | {'user_or_usergroup': user_regular.username} |
|
57 | 58 | ) == {'user_or_usergroup': user_regular} |
|
58 | 59 | |
|
59 | 60 | def test_deserialize_user_user_group_with_same_name(self): |
|
60 | 61 | schema = self.Schema() |
|
61 | 62 | try: |
|
62 | 63 | user = UserModel().create_or_update( |
|
63 | 64 | 'test_user_usergroup', 'nopass', 'test_user_usergroup') |
|
64 | 65 | usergroup = UserGroupModel().create( |
|
65 | 66 | 'test_user_usergroup', 'test usergroup', user) |
|
66 | 67 | |
|
67 | 68 | with pytest.raises(colander.Invalid) as exc_info: |
|
68 | 69 | schema.deserialize( |
|
69 | 70 | {'user_or_usergroup': user.username} |
|
70 | 71 | ) == {'user_or_usergroup': user} |
|
71 | 72 | |
|
72 | 73 | err = exc_info.value.asdict() |
|
73 | 74 | assert 'is both a user and usergroup' in err['user_or_usergroup'] |
|
74 | 75 | finally: |
|
76 | UserGroupModel().delete(usergroup) | |
|
77 | Session().commit() | |
|
75 | 78 | UserModel().delete(user) |
|
76 | UserGroupModel().delete(usergroup) | |
|
77 | 79 | |
|
78 | 80 | |
|
79 | 81 | class TestUserType(object): |
|
80 | 82 | |
|
81 | 83 | class Schema(colander.Schema): |
|
82 | 84 | user = colander.SchemaNode(UserType()) |
|
83 | 85 | |
|
84 | 86 | def test_serialize(self, user_regular, test_user_group): |
|
85 | 87 | schema = self.Schema() |
|
86 | 88 | |
|
87 | 89 | assert schema.serialize({'user': user_regular}) == ( |
|
88 | 90 | {'user': user_regular.username}) |
|
89 | 91 | |
|
90 | 92 | with pytest.raises(colander.Invalid): |
|
91 | 93 | schema.serialize({'user': test_user_group}) |
|
92 | 94 | |
|
93 | 95 | with pytest.raises(colander.Invalid): |
|
94 | 96 | schema.serialize({'user': 'invaliduser'}) |
|
95 | 97 | |
|
96 | 98 | def test_deserialize(self, user_regular, test_user_group): |
|
97 | 99 | schema = self.Schema() |
|
98 | 100 | |
|
99 | 101 | assert schema.deserialize( |
|
100 | 102 | {'user': user_regular.username}) == {'user': user_regular} |
|
101 | 103 | |
|
102 | 104 | with pytest.raises(colander.Invalid): |
|
103 | 105 | schema.deserialize({'user': test_user_group.users_group_name}) |
|
104 | 106 | |
|
105 | 107 | with pytest.raises(colander.Invalid): |
|
106 | 108 | schema.deserialize({'user': 'invaliduser'}) |
|
107 | 109 | |
|
108 | 110 | |
|
109 | 111 | class TestUserGroupType(object): |
|
110 | 112 | |
|
111 | 113 | class Schema(colander.Schema): |
|
112 | 114 | usergroup = colander.SchemaNode( |
|
113 | 115 | UserGroupType() |
|
114 | 116 | ) |
|
115 | 117 | |
|
116 | 118 | def test_serialize(self, user_regular, test_user_group): |
|
117 | 119 | schema = self.Schema() |
|
118 | 120 | |
|
119 | 121 | assert schema.serialize({'usergroup': test_user_group}) == ( |
|
120 | 122 | {'usergroup': test_user_group.users_group_name}) |
|
121 | 123 | |
|
122 | 124 | with pytest.raises(colander.Invalid): |
|
123 | 125 | schema.serialize({'usergroup': user_regular}) |
|
124 | 126 | |
|
125 | 127 | with pytest.raises(colander.Invalid): |
|
126 | 128 | schema.serialize({'usergroup': 'invalidusergroup'}) |
|
127 | 129 | |
|
128 | 130 | def test_deserialize(self, user_regular, test_user_group): |
|
129 | 131 | schema = self.Schema() |
|
130 | 132 | |
|
131 | 133 | assert schema.deserialize({ |
|
132 | 134 | 'usergroup': test_user_group.users_group_name |
|
133 | 135 | }) == {'usergroup': test_user_group} |
|
134 | 136 | |
|
135 | 137 | with pytest.raises(colander.Invalid): |
|
136 | 138 | schema.deserialize({'usergroup': user_regular.username}) |
|
137 | 139 | |
|
138 | 140 | with pytest.raises(colander.Invalid): |
|
139 | 141 | schema.deserialize({'usergroup': 'invaliduser'}) |
General Comments 0
You need to be logged in to leave comments.
Login now