comparison test/integration/api_test/groups_test.rb @ 1526:404aa68d4227

Merge from live branch
author Chris Cannam
date Thu, 11 Sep 2014 12:46:20 +0100
parents e248c7af89ec
children
comparison
equal deleted inserted replaced
1493:a5f2bdf3b486 1526:404aa68d4227
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2014 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.
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 Redmine::ApiTest::GroupsTest < Redmine::ApiTest::Base
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 test "GET /groups.xml should require authentication" do
28 context ".xml" do 28 get '/groups.xml'
29 should "require authentication" do 29 assert_response 401
30 get '/groups.xml' 30 end
31 assert_response 401
32 end
33 31
34 should "return groups" do 32 test "GET /groups.xml should return groups" do
35 get '/groups.xml', {}, credentials('admin') 33 get '/groups.xml', {}, credentials('admin')
36 assert_response :success 34 assert_response :success
37 assert_equal 'application/xml', response.content_type 35 assert_equal 'application/xml', response.content_type
38 36
39 assert_select 'groups' do 37 assert_select 'groups' do
40 assert_select 'group' do 38 assert_select 'group' do
41 assert_select 'name', :text => 'A Team' 39 assert_select 'name', :text => 'A Team'
42 assert_select 'id', :text => '10' 40 assert_select 'id', :text => '10'
43 end
44 end
45 end
46 end
47
48 context ".json" do
49 should "require authentication" do
50 get '/groups.json'
51 assert_response 401
52 end
53
54 should "return groups" do
55 get '/groups.json', {}, credentials('admin')
56 assert_response :success
57 assert_equal 'application/json', response.content_type
58
59 json = MultiJson.load(response.body)
60 groups = json['groups']
61 assert_kind_of Array, groups
62 group = groups.detect {|g| g['name'] == 'A Team'}
63 assert_not_nil group
64 assert_equal({'id' => 10, 'name' => 'A Team'}, group)
65 end 41 end
66 end 42 end
67 end 43 end
68 44
69 context "GET /groups/:id" do 45 test "GET /groups.json should require authentication" do
70 context ".xml" do 46 get '/groups.json'
71 should "return the group with its users" do 47 assert_response 401
72 get '/groups/10.xml', {}, credentials('admin') 48 end
73 assert_response :success
74 assert_equal 'application/xml', response.content_type
75 49
76 assert_select 'group' do 50 test "GET /groups.json should return groups" do
77 assert_select 'name', :text => 'A Team' 51 get '/groups.json', {}, credentials('admin')
78 assert_select 'id', :text => '10' 52 assert_response :success
79 end 53 assert_equal 'application/json', response.content_type
80 end
81 54
82 should "include users if requested" do 55 json = MultiJson.load(response.body)
83 get '/groups/10.xml?include=users', {}, credentials('admin') 56 groups = json['groups']
84 assert_response :success 57 assert_kind_of Array, groups
85 assert_equal 'application/xml', response.content_type 58 group = groups.detect {|g| g['name'] == 'A Team'}
59 assert_not_nil group
60 assert_equal({'id' => 10, 'name' => 'A Team'}, group)
61 end
86 62
87 assert_select 'group' do 63 test "GET /groups/:id.xml should return the group with its users" do
88 assert_select 'users' do 64 get '/groups/10.xml', {}, credentials('admin')
89 assert_select 'user', Group.find(10).users.count 65 assert_response :success
90 assert_select 'user[id=8]' 66 assert_equal 'application/xml', response.content_type
91 end
92 end
93 end
94 67
95 should "include memberships if requested" do 68 assert_select 'group' do
96 get '/groups/10.xml?include=memberships', {}, credentials('admin') 69 assert_select 'name', :text => 'A Team'
97 assert_response :success 70 assert_select 'id', :text => '10'
98 assert_equal 'application/xml', response.content_type 71 end
72 end
99 73
100 assert_select 'group' do 74 test "GET /groups/:id.xml should include users if requested" do
101 assert_select 'memberships' 75 get '/groups/10.xml?include=users', {}, credentials('admin')
102 end 76 assert_response :success
77 assert_equal 'application/xml', response.content_type
78
79 assert_select 'group' do
80 assert_select 'users' do
81 assert_select 'user', Group.find(10).users.count
82 assert_select 'user[id=8]'
103 end 83 end
104 end 84 end
105 end 85 end
106 86
107 context "POST /groups" do 87 test "GET /groups/:id.xml include memberships if requested" do
108 context "with valid parameters" do 88 get '/groups/10.xml?include=memberships', {}, credentials('admin')
109 context ".xml" do 89 assert_response :success
110 should "create groups" do 90 assert_equal 'application/xml', response.content_type
111 assert_difference('Group.count') do
112 post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
113 assert_response :created
114 assert_equal 'application/xml', response.content_type
115 end
116
117 group = Group.order('id DESC').first
118 assert_equal 'Test', group.name
119 assert_equal [2, 3], group.users.map(&:id).sort
120 91
121 assert_select 'group' do 92 assert_select 'group' do
122 assert_select 'name', :text => 'Test' 93 assert_select 'memberships'
123 end
124 end
125 end
126 end
127
128 context "with invalid parameters" do
129 context ".xml" do
130 should "return errors" do
131 assert_no_difference('Group.count') do
132 post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
133 end
134 assert_response :unprocessable_entity
135 assert_equal 'application/xml', response.content_type
136
137 assert_select 'errors' do
138 assert_select 'error', :text => /Name can't be blank/
139 end
140 end
141 end
142 end 94 end
143 end 95 end
144 96
145 context "PUT /groups/:id" do 97 test "POST /groups.xml with valid parameters should create the group" do
146 context "with valid parameters" do 98 assert_difference('Group.count') do
147 context ".xml" do 99 post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
148 should "update the group" do 100 assert_response :created
149 put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin') 101 assert_equal 'application/xml', response.content_type
150 assert_response :ok
151 assert_equal '', @response.body
152
153 group = Group.find(10)
154 assert_equal 'New name', group.name
155 assert_equal [2, 3], group.users.map(&:id).sort
156 end
157 end
158 end 102 end
159 103
160 context "with invalid parameters" do 104 group = Group.order('id DESC').first
161 context ".xml" do 105 assert_equal 'Test', group.name
162 should "return errors" do 106 assert_equal [2, 3], group.users.map(&:id).sort
163 put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
164 assert_response :unprocessable_entity
165 assert_equal 'application/xml', response.content_type
166 107
167 assert_select 'errors' do 108 assert_select 'group' do
168 assert_select 'error', :text => /Name can't be blank/ 109 assert_select 'name', :text => 'Test'
169 end
170 end
171 end
172 end 110 end
173 end 111 end
174 112
175 context "DELETE /groups/:id" do 113 test "POST /groups.xml with invalid parameters should return errors" do
176 context ".xml" do 114 assert_no_difference('Group.count') do
177 should "delete the group" do 115 post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
178 assert_difference 'Group.count', -1 do 116 end
179 delete '/groups/10.xml', {}, credentials('admin') 117 assert_response :unprocessable_entity
180 assert_response :ok 118 assert_equal 'application/xml', response.content_type
181 assert_equal '', @response.body 119
182 end 120 assert_select 'errors' do
183 end 121 assert_select 'error', :text => /Name can't be blank/
184 end 122 end
185 end 123 end
186 124
187 context "POST /groups/:id/users" do 125 test "PUT /groups/:id.xml with valid parameters should update the group" do
188 context ".xml" do 126 put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
189 should "add user to the group" do 127 assert_response :ok
190 assert_difference 'Group.find(10).users.count' do 128 assert_equal '', @response.body
191 post '/groups/10/users.xml', {:user_id => 5}, credentials('admin') 129
192 assert_response :ok 130 group = Group.find(10)
193 assert_equal '', @response.body 131 assert_equal 'New name', group.name
194 end 132 assert_equal [2, 3], group.users.map(&:id).sort
195 assert_include User.find(5), Group.find(10).users 133 end
196 end 134
135 test "PUT /groups/:id.xml with invalid parameters should return errors" do
136 put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
137 assert_response :unprocessable_entity
138 assert_equal 'application/xml', response.content_type
139
140 assert_select 'errors' do
141 assert_select 'error', :text => /Name can't be blank/
197 end 142 end
198 end 143 end
199 144
200 context "DELETE /groups/:id/users/:user_id" do 145 test "DELETE /groups/:id.xml should delete the group" do
201 context ".xml" do 146 assert_difference 'Group.count', -1 do
202 should "remove user from the group" do 147 delete '/groups/10.xml', {}, credentials('admin')
203 assert_difference 'Group.find(10).users.count', -1 do 148 assert_response :ok
204 delete '/groups/10/users/8.xml', {}, credentials('admin') 149 assert_equal '', @response.body
205 assert_response :ok
206 assert_equal '', @response.body
207 end
208 assert_not_include User.find(8), Group.find(10).users
209 end
210 end 150 end
211 end 151 end
152
153 test "POST /groups/:id/users.xml should add user to the group" do
154 assert_difference 'Group.find(10).users.count' do
155 post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
156 assert_response :ok
157 assert_equal '', @response.body
158 end
159 assert_include User.find(5), Group.find(10).users
160 end
161
162 test "DELETE /groups/:id/users/:user_id.xml should remove user from the group" do
163 assert_difference 'Group.find(10).users.count', -1 do
164 delete '/groups/10/users/8.xml', {}, credentials('admin')
165 assert_response :ok
166 assert_equal '', @response.body
167 end
168 assert_not_include User.find(8), Group.find(10).users
169 end
212 end 170 end