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