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