##// END OF EJS Templates
Use generated groups....
Jean-Philippe Lang -
r14328:c63da2c0a5b5
parent child
Show More
@@ -1,209 +1,216
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
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 General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../../test_helper', __FILE__)
18 require File.expand_path('../../../test_helper', __FILE__)
19
19
20 class Redmine::ApiTest::GroupsTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::GroupsTest < Redmine::ApiTest::Base
21 fixtures :users, :groups_users, :email_addresses
21 fixtures :users, :groups_users, :email_addresses
22
22
23 test "GET /groups.xml should require authentication" do
23 test "GET /groups.xml should require authentication" do
24 get '/groups.xml'
24 get '/groups.xml'
25 assert_response 401
25 assert_response 401
26 end
26 end
27
27
28 test "GET /groups.xml should return givable groups" do
28 test "GET /groups.xml should return givable groups" do
29 get '/groups.xml', {}, credentials('admin')
29 get '/groups.xml', {}, credentials('admin')
30 assert_response :success
30 assert_response :success
31 assert_equal 'application/xml', response.content_type
31 assert_equal 'application/xml', response.content_type
32
32
33 assert_select 'groups' do
33 assert_select 'groups' do
34 assert_select 'group', Group.givable.count
34 assert_select 'group', Group.givable.count
35 assert_select 'group' do
35 assert_select 'group' do
36 assert_select 'name', :text => 'A Team'
36 assert_select 'name', :text => 'A Team'
37 assert_select 'id', :text => '10'
37 assert_select 'id', :text => '10'
38 end
38 end
39 end
39 end
40 end
40 end
41
41
42 test "GET /groups.xml?builtin=1 should return all groups" do
42 test "GET /groups.xml?builtin=1 should return all groups" do
43 get '/groups.xml?builtin=1', {}, credentials('admin')
43 get '/groups.xml?builtin=1', {}, credentials('admin')
44 assert_response :success
44 assert_response :success
45 assert_equal 'application/xml', response.content_type
45 assert_equal 'application/xml', response.content_type
46
46
47 assert_select 'groups' do
47 assert_select 'groups' do
48 assert_select 'group', Group.givable.count + 2
48 assert_select 'group', Group.givable.count + 2
49 assert_select 'group' do
49 assert_select 'group' do
50 assert_select 'builtin', :text => 'non_member'
50 assert_select 'builtin', :text => 'non_member'
51 assert_select 'id', :text => '12'
51 assert_select 'id', :text => '12'
52 end
52 end
53 assert_select 'group' do
53 assert_select 'group' do
54 assert_select 'builtin', :text => 'anonymous'
54 assert_select 'builtin', :text => 'anonymous'
55 assert_select 'id', :text => '13'
55 assert_select 'id', :text => '13'
56 end
56 end
57 end
57 end
58 end
58 end
59
59
60 test "GET /groups.json should require authentication" do
60 test "GET /groups.json should require authentication" do
61 get '/groups.json'
61 get '/groups.json'
62 assert_response 401
62 assert_response 401
63 end
63 end
64
64
65 test "GET /groups.json should return groups" do
65 test "GET /groups.json should return groups" do
66 get '/groups.json', {}, credentials('admin')
66 get '/groups.json', {}, credentials('admin')
67 assert_response :success
67 assert_response :success
68 assert_equal 'application/json', response.content_type
68 assert_equal 'application/json', response.content_type
69
69
70 json = MultiJson.load(response.body)
70 json = MultiJson.load(response.body)
71 groups = json['groups']
71 groups = json['groups']
72 assert_kind_of Array, groups
72 assert_kind_of Array, groups
73 group = groups.detect {|g| g['name'] == 'A Team'}
73 group = groups.detect {|g| g['name'] == 'A Team'}
74 assert_not_nil group
74 assert_not_nil group
75 assert_equal({'id' => 10, 'name' => 'A Team'}, group)
75 assert_equal({'id' => 10, 'name' => 'A Team'}, group)
76 end
76 end
77
77
78 test "GET /groups/:id.xml should return the group" do
78 test "GET /groups/:id.xml should return the group" do
79 get '/groups/10.xml', {}, credentials('admin')
79 get '/groups/10.xml', {}, credentials('admin')
80 assert_response :success
80 assert_response :success
81 assert_equal 'application/xml', response.content_type
81 assert_equal 'application/xml', response.content_type
82
82
83 assert_select 'group' do
83 assert_select 'group' do
84 assert_select 'name', :text => 'A Team'
84 assert_select 'name', :text => 'A Team'
85 assert_select 'id', :text => '10'
85 assert_select 'id', :text => '10'
86 end
86 end
87 end
87 end
88
88
89 test "GET /groups/:id.xml should return the builtin group" do
89 test "GET /groups/:id.xml should return the builtin group" do
90 get '/groups/12.xml', {}, credentials('admin')
90 get '/groups/12.xml', {}, credentials('admin')
91 assert_response :success
91 assert_response :success
92 assert_equal 'application/xml', response.content_type
92 assert_equal 'application/xml', response.content_type
93
93
94 assert_select 'group' do
94 assert_select 'group' do
95 assert_select 'builtin', :text => 'non_member'
95 assert_select 'builtin', :text => 'non_member'
96 assert_select 'id', :text => '12'
96 assert_select 'id', :text => '12'
97 end
97 end
98 end
98 end
99
99
100 test "GET /groups/:id.xml should include users if requested" do
100 test "GET /groups/:id.xml should include users if requested" do
101 get '/groups/10.xml?include=users', {}, credentials('admin')
101 get '/groups/10.xml?include=users', {}, credentials('admin')
102 assert_response :success
102 assert_response :success
103 assert_equal 'application/xml', response.content_type
103 assert_equal 'application/xml', response.content_type
104
104
105 assert_select 'group' do
105 assert_select 'group' do
106 assert_select 'users' do
106 assert_select 'users' do
107 assert_select 'user', Group.find(10).users.count
107 assert_select 'user', Group.find(10).users.count
108 assert_select 'user[id="8"]'
108 assert_select 'user[id="8"]'
109 end
109 end
110 end
110 end
111 end
111 end
112
112
113 test "GET /groups/:id.xml include memberships if requested" do
113 test "GET /groups/:id.xml include memberships if requested" do
114 get '/groups/10.xml?include=memberships', {}, credentials('admin')
114 get '/groups/10.xml?include=memberships', {}, credentials('admin')
115 assert_response :success
115 assert_response :success
116 assert_equal 'application/xml', response.content_type
116 assert_equal 'application/xml', response.content_type
117
117
118 assert_select 'group' do
118 assert_select 'group' do
119 assert_select 'memberships'
119 assert_select 'memberships'
120 end
120 end
121 end
121 end
122
122
123 test "POST /groups.xml with valid parameters should create the group" do
123 test "POST /groups.xml with valid parameters should create the group" do
124 assert_difference('Group.count') do
124 assert_difference('Group.count') do
125 post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
125 post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
126 assert_response :created
126 assert_response :created
127 assert_equal 'application/xml', response.content_type
127 assert_equal 'application/xml', response.content_type
128 end
128 end
129
129
130 group = Group.order('id DESC').first
130 group = Group.order('id DESC').first
131 assert_equal 'Test', group.name
131 assert_equal 'Test', group.name
132 assert_equal [2, 3], group.users.map(&:id).sort
132 assert_equal [2, 3], group.users.map(&:id).sort
133
133
134 assert_select 'group' do
134 assert_select 'group' do
135 assert_select 'name', :text => 'Test'
135 assert_select 'name', :text => 'Test'
136 end
136 end
137 end
137 end
138
138
139 test "POST /groups.xml with invalid parameters should return errors" do
139 test "POST /groups.xml with invalid parameters should return errors" do
140 assert_no_difference('Group.count') do
140 assert_no_difference('Group.count') do
141 post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
141 post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
142 end
142 end
143 assert_response :unprocessable_entity
143 assert_response :unprocessable_entity
144 assert_equal 'application/xml', response.content_type
144 assert_equal 'application/xml', response.content_type
145
145
146 assert_select 'errors' do
146 assert_select 'errors' do
147 assert_select 'error', :text => /Name cannot be blank/
147 assert_select 'error', :text => /Name cannot be blank/
148 end
148 end
149 end
149 end
150
150
151 test "PUT /groups/:id.xml with valid parameters should update the group" do
151 test "PUT /groups/:id.xml with valid parameters should update the group" do
152 put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
152 group = Group.generate!
153 put "/groups/#{group.id}.xml", {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
153 assert_response :ok
154 assert_response :ok
154 assert_equal '', @response.body
155 assert_equal '', @response.body
155
156
156 group = Group.find(10)
157 assert_equal 'New name', group.reload.name
157 assert_equal 'New name', group.name
158 assert_equal [2, 3], group.users.map(&:id).sort
158 assert_equal [2, 3], group.users.map(&:id).sort
159 end
159 end
160
160
161 test "PUT /groups/:id.xml with invalid parameters should return errors" do
161 test "PUT /groups/:id.xml with invalid parameters should return errors" do
162 put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
162 group = Group.generate!
163 put "/groups/#{group.id}.xml", {:group => {:name => ''}}, credentials('admin')
163 assert_response :unprocessable_entity
164 assert_response :unprocessable_entity
164 assert_equal 'application/xml', response.content_type
165 assert_equal 'application/xml', response.content_type
165
166
166 assert_select 'errors' do
167 assert_select 'errors' do
167 assert_select 'error', :text => /Name cannot be blank/
168 assert_select 'error', :text => /Name cannot be blank/
168 end
169 end
169 end
170 end
170
171
171 test "DELETE /groups/:id.xml should delete the group" do
172 test "DELETE /groups/:id.xml should delete the group" do
173 group = Group.generate!
172 assert_difference 'Group.count', -1 do
174 assert_difference 'Group.count', -1 do
173 delete '/groups/10.xml', {}, credentials('admin')
175 delete "/groups/#{group.id}.xml", {}, credentials('admin')
174 assert_response :ok
176 assert_response :ok
175 assert_equal '', @response.body
177 assert_equal '', @response.body
176 end
178 end
177 end
179 end
178
180
179 test "POST /groups/:id/users.xml should add user to the group" do
181 test "POST /groups/:id/users.xml should add user to the group" do
180 assert_difference 'Group.find(10).users.count' do
182 group = Group.generate!
181 post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
183 assert_difference 'group.reload.users.count' do
184 post "/groups/#{group.id}/users.xml", {:user_id => 5}, credentials('admin')
182 assert_response :ok
185 assert_response :ok
183 assert_equal '', @response.body
186 assert_equal '', @response.body
184 end
187 end
185 assert_include User.find(5), Group.find(10).users
188 assert_include User.find(5), group.reload.users
186 end
189 end
187
190
188 test "POST /groups/:id/users.xml should not add the user if already added" do
191 test "POST /groups/:id/users.xml should not add the user if already added" do
189 Group.find(10).users << User.find(5)
192 group = Group.generate!
193 group.users << User.find(5)
190
194
191 assert_no_difference 'Group.find(10).users.count' do
195 assert_no_difference 'group.reload.users.count' do
192 post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
196 post "/groups/#{group.id}/users.xml", {:user_id => 5}, credentials('admin')
193 assert_response :unprocessable_entity
197 assert_response :unprocessable_entity
194 end
198 end
195
199
196 assert_select 'errors' do
200 assert_select 'errors' do
197 assert_select 'error', :text => /User is invalid/
201 assert_select 'error', :text => /User is invalid/
198 end
202 end
199 end
203 end
200
204
201 test "DELETE /groups/:id/users/:user_id.xml should remove user from the group" do
205 test "DELETE /groups/:id/users/:user_id.xml should remove user from the group" do
202 assert_difference 'Group.find(10).users.count', -1 do
206 group = Group.generate!
203 delete '/groups/10/users/8.xml', {}, credentials('admin')
207 group.users << User.find(8)
208
209 assert_difference 'group.reload.users.count', -1 do
210 delete "/groups/#{group.id}/users/8.xml", {}, credentials('admin')
204 assert_response :ok
211 assert_response :ok
205 assert_equal '', @response.body
212 assert_equal '', @response.body
206 end
213 end
207 assert_not_include User.find(8), Group.find(10).users
214 assert_not_include User.find(8), group.reload.users
208 end
215 end
209 end
216 end
General Comments 0
You need to be logged in to leave comments. Login now