##// END OF EJS Templates
Makes users optional in GET /groups/:id (#8981)....
Jean-Philippe Lang -
r9582:327d5d2132ac
parent child
Show More
@@ -1,30 +1,30
1 api.group do
1 api.group do
2 api.id @group.id
2 api.id @group.id
3 api.name @group.lastname
3 api.name @group.lastname
4
4
5 render_api_custom_values @group.visible_custom_field_values, api
5 render_api_custom_values @group.visible_custom_field_values, api
6
6
7 api.array :users do
7 api.array :users do
8 @group.users.each do |user|
8 @group.users.each do |user|
9 api.user :id => user.id, :name => user.name
9 api.user :id => user.id, :name => user.name
10 end
10 end
11 end
11 end if include_in_api_response?('users')
12
12
13 api.array :memberships do
13 api.array :memberships do
14 @group.memberships.each do |membership|
14 @group.memberships.each do |membership|
15 api.membership do
15 api.membership do
16 api.id membership.id
16 api.id membership.id
17 api.project :id => membership.project.id, :name => membership.project.name
17 api.project :id => membership.project.id, :name => membership.project.name
18 api.array :roles do
18 api.array :roles do
19 membership.member_roles.each do |member_role|
19 membership.member_roles.each do |member_role|
20 if member_role.role
20 if member_role.role
21 attrs = {:id => member_role.role.id, :name => member_role.role.name}
21 attrs = {:id => member_role.role.id, :name => member_role.role.name}
22 attrs.merge!(:inherited => true) if member_role.inherited_from.present?
22 attrs.merge!(:inherited => true) if member_role.inherited_from.present?
23 api.role attrs
23 api.role attrs
24 end
24 end
25 end
25 end
26 end
26 end
27 end if membership.project
27 end if membership.project
28 end
28 end
29 end if include_in_api_response?('memberships')
29 end if include_in_api_response?('memberships')
30 end
30 end
@@ -1,199 +1,208
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 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 ApiTest::GroupsTest < ActionController::IntegrationTest
20 class ApiTest::GroupsTest < ActionController::IntegrationTest
21 fixtures :users, :groups_users
21 fixtures :users, :groups_users
22
22
23 def setup
23 def setup
24 Setting.rest_api_enabled = '1'
24 Setting.rest_api_enabled = '1'
25 end
25 end
26
26
27 context "GET /groups" do
27 context "GET /groups" do
28 context ".xml" do
28 context ".xml" do
29 should "require authentication" do
29 should "require authentication" do
30 get '/groups.xml'
30 get '/groups.xml'
31 assert_response 401
31 assert_response 401
32 end
32 end
33
33
34 should "return groups" do
34 should "return groups" do
35 get '/groups.xml', {}, credentials('admin')
35 get '/groups.xml', {}, credentials('admin')
36 assert_response :success
36 assert_response :success
37 assert_equal 'application/xml', response.content_type
37 assert_equal 'application/xml', response.content_type
38
38
39 assert_select 'groups' do
39 assert_select 'groups' do
40 assert_select 'group' do
40 assert_select 'group' do
41 assert_select 'name', :text => 'A Team'
41 assert_select 'name', :text => 'A Team'
42 assert_select 'id', :text => '10'
42 assert_select 'id', :text => '10'
43 end
43 end
44 end
44 end
45 end
45 end
46 end
46 end
47
47
48 context ".json" do
48 context ".json" do
49 should "require authentication" do
49 should "require authentication" do
50 get '/groups.json'
50 get '/groups.json'
51 assert_response 401
51 assert_response 401
52 end
52 end
53
53
54 should "return groups" do
54 should "return groups" do
55 get '/groups.json', {}, credentials('admin')
55 get '/groups.json', {}, credentials('admin')
56 assert_response :success
56 assert_response :success
57 assert_equal 'application/json', response.content_type
57 assert_equal 'application/json', response.content_type
58
58
59 json = MultiJson.load(response.body)
59 json = MultiJson.load(response.body)
60 groups = json['groups']
60 groups = json['groups']
61 assert_kind_of Array, groups
61 assert_kind_of Array, groups
62 group = groups.detect {|g| g['name'] == 'A Team'}
62 group = groups.detect {|g| g['name'] == 'A Team'}
63 assert_not_nil group
63 assert_not_nil group
64 assert_equal({'id' => 10, 'name' => 'A Team'}, group)
64 assert_equal({'id' => 10, 'name' => 'A Team'}, group)
65 end
65 end
66 end
66 end
67 end
67 end
68
68
69 context "GET /groups/:id" do
69 context "GET /groups/:id" do
70 context ".xml" do
70 context ".xml" do
71 should "return the group with its users" do
71 should "return the group with its users" do
72 get '/groups/10.xml', {}, credentials('admin')
72 get '/groups/10.xml', {}, credentials('admin')
73 assert_response :success
73 assert_response :success
74 assert_equal 'application/xml', response.content_type
74 assert_equal 'application/xml', response.content_type
75
75
76 assert_select 'group' do
76 assert_select 'group' do
77 assert_select 'name', :text => 'A Team'
77 assert_select 'name', :text => 'A Team'
78 assert_select 'id', :text => '10'
78 assert_select 'id', :text => '10'
79 end
80 end
81
82 should "include users if requested" do
83 get '/groups/10.xml?include=users', {}, credentials('admin')
84 assert_response :success
85 assert_equal 'application/xml', response.content_type
86
87 assert_select 'group' do
79 assert_select 'users' do
88 assert_select 'users' do
80 assert_select 'user', Group.find(10).users.count
89 assert_select 'user', Group.find(10).users.count
81 assert_select 'user[id=8]'
90 assert_select 'user[id=8]'
82 end
91 end
83 end
92 end
84 end
93 end
85
94
86 should "include memberships if requested" do
95 should "include memberships if requested" do
87 get '/groups/10.xml?include=memberships', {}, credentials('admin')
96 get '/groups/10.xml?include=memberships', {}, credentials('admin')
88 assert_response :success
97 assert_response :success
89 assert_equal 'application/xml', response.content_type
98 assert_equal 'application/xml', response.content_type
90
99
91 assert_select 'group' do
100 assert_select 'group' do
92 assert_select 'memberships'
101 assert_select 'memberships'
93 end
102 end
94 end
103 end
95 end
104 end
96 end
105 end
97
106
98 context "POST /groups" do
107 context "POST /groups" do
99 context "with valid parameters" do
108 context "with valid parameters" do
100 context ".xml" do
109 context ".xml" do
101 should "create groups" do
110 should "create groups" do
102 assert_difference('Group.count') do
111 assert_difference('Group.count') do
103 post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
112 post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
104 assert_response :created
113 assert_response :created
105 assert_equal 'application/xml', response.content_type
114 assert_equal 'application/xml', response.content_type
106 end
115 end
107
116
108 group = Group.order('id DESC').first
117 group = Group.order('id DESC').first
109 assert_equal 'Test', group.name
118 assert_equal 'Test', group.name
110 assert_equal [2, 3], group.users.map(&:id).sort
119 assert_equal [2, 3], group.users.map(&:id).sort
111
120
112 assert_select 'group' do
121 assert_select 'group' do
113 assert_select 'name', :text => 'Test'
122 assert_select 'name', :text => 'Test'
114 end
123 end
115 end
124 end
116 end
125 end
117 end
126 end
118
127
119 context "with invalid parameters" do
128 context "with invalid parameters" do
120 context ".xml" do
129 context ".xml" do
121 should "return errors" do
130 should "return errors" do
122 assert_no_difference('Group.count') do
131 assert_no_difference('Group.count') do
123 post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
132 post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
124 end
133 end
125 assert_response :unprocessable_entity
134 assert_response :unprocessable_entity
126 assert_equal 'application/xml', response.content_type
135 assert_equal 'application/xml', response.content_type
127
136
128 assert_select 'errors' do
137 assert_select 'errors' do
129 assert_select 'error', :text => /Name can't be blank/
138 assert_select 'error', :text => /Name can't be blank/
130 end
139 end
131 end
140 end
132 end
141 end
133 end
142 end
134 end
143 end
135
144
136 context "PUT /groups/:id" do
145 context "PUT /groups/:id" do
137 context "with valid parameters" do
146 context "with valid parameters" do
138 context ".xml" do
147 context ".xml" do
139 should "update the group" do
148 should "update the group" do
140 put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
149 put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
141 assert_response :ok
150 assert_response :ok
142
151
143 group = Group.find(10)
152 group = Group.find(10)
144 assert_equal 'New name', group.name
153 assert_equal 'New name', group.name
145 assert_equal [2, 3], group.users.map(&:id).sort
154 assert_equal [2, 3], group.users.map(&:id).sort
146 end
155 end
147 end
156 end
148 end
157 end
149
158
150 context "with invalid parameters" do
159 context "with invalid parameters" do
151 context ".xml" do
160 context ".xml" do
152 should "return errors" do
161 should "return errors" do
153 put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
162 put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
154 assert_response :unprocessable_entity
163 assert_response :unprocessable_entity
155 assert_equal 'application/xml', response.content_type
164 assert_equal 'application/xml', response.content_type
156
165
157 assert_select 'errors' do
166 assert_select 'errors' do
158 assert_select 'error', :text => /Name can't be blank/
167 assert_select 'error', :text => /Name can't be blank/
159 end
168 end
160 end
169 end
161 end
170 end
162 end
171 end
163 end
172 end
164
173
165 context "DELETE /groups/:id" do
174 context "DELETE /groups/:id" do
166 context ".xml" do
175 context ".xml" do
167 should "delete the group" do
176 should "delete the group" do
168 assert_difference 'Group.count', -1 do
177 assert_difference 'Group.count', -1 do
169 delete '/groups/10.xml', {}, credentials('admin')
178 delete '/groups/10.xml', {}, credentials('admin')
170 assert_response :ok
179 assert_response :ok
171 end
180 end
172 end
181 end
173 end
182 end
174 end
183 end
175
184
176 context "POST /groups/:id/users" do
185 context "POST /groups/:id/users" do
177 context ".xml" do
186 context ".xml" do
178 should "add user to the group" do
187 should "add user to the group" do
179 assert_difference 'Group.find(10).users.count' do
188 assert_difference 'Group.find(10).users.count' do
180 post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
189 post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
181 assert_response :ok
190 assert_response :ok
182 end
191 end
183 assert_include User.find(5), Group.find(10).users
192 assert_include User.find(5), Group.find(10).users
184 end
193 end
185 end
194 end
186 end
195 end
187
196
188 context "DELETE /groups/:id/users/:user_id" do
197 context "DELETE /groups/:id/users/:user_id" do
189 context ".xml" do
198 context ".xml" do
190 should "remove user from the group" do
199 should "remove user from the group" do
191 assert_difference 'Group.find(10).users.count', -1 do
200 assert_difference 'Group.find(10).users.count', -1 do
192 delete '/groups/10/users/8.xml', {}, credentials('admin')
201 delete '/groups/10/users/8.xml', {}, credentials('admin')
193 assert_response :ok
202 assert_response :ok
194 end
203 end
195 assert_not_include User.find(8), Group.find(10).users
204 assert_not_include User.find(8), Group.find(10).users
196 end
205 end
197 end
206 end
198 end
207 end
199 end
208 end
General Comments 0
You need to be logged in to leave comments. Login now