Mercurial > hg > soundsoftware-site
comparison .svn/pristine/58/583f59658b5b4c108e320b78c4ff88cab8b9c15d.svn-base @ 1517:dffacf8a6908 redmine-2.5
Update to Redmine SVN revision 13367 on 2.5-stable branch
author | Chris Cannam |
---|---|
date | Tue, 09 Sep 2014 09:29:00 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1516:b450a9d58aed | 1517:dffacf8a6908 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2014 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
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 | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 require File.expand_path('../../../test_helper', __FILE__) | |
19 | |
20 class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base | |
21 fixtures :users, :members, :member_roles, :roles, :projects | |
22 | |
23 def setup | |
24 Setting.rest_api_enabled = '1' | |
25 end | |
26 | |
27 should_allow_api_authentication(:get, "/users.xml") | |
28 should_allow_api_authentication(:get, "/users.json") | |
29 should_allow_api_authentication(:post, | |
30 '/users.xml', | |
31 {:user => { | |
32 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
33 :mail => 'foo@example.net', :password => 'secret123' | |
34 }}, | |
35 {:success_code => :created}) | |
36 should_allow_api_authentication(:post, | |
37 '/users.json', | |
38 {:user => { | |
39 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
40 :mail => 'foo@example.net' | |
41 }}, | |
42 {:success_code => :created}) | |
43 should_allow_api_authentication(:put, | |
44 '/users/2.xml', | |
45 {:user => { | |
46 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
47 :mail => 'jsmith@somenet.foo' | |
48 }}, | |
49 {:success_code => :ok}) | |
50 should_allow_api_authentication(:put, | |
51 '/users/2.json', | |
52 {:user => { | |
53 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
54 :mail => 'jsmith@somenet.foo' | |
55 }}, | |
56 {:success_code => :ok}) | |
57 should_allow_api_authentication(:delete, | |
58 '/users/2.xml', | |
59 {}, | |
60 {:success_code => :ok}) | |
61 should_allow_api_authentication(:delete, | |
62 '/users/2.xml', | |
63 {}, | |
64 {:success_code => :ok}) | |
65 | |
66 test "GET /users/:id.xml should return the user" do | |
67 get '/users/2.xml' | |
68 | |
69 assert_response :success | |
70 assert_tag :tag => 'user', | |
71 :child => {:tag => 'id', :content => '2'} | |
72 end | |
73 | |
74 test "GET /users/:id.json should return the user" do | |
75 get '/users/2.json' | |
76 | |
77 assert_response :success | |
78 json = ActiveSupport::JSON.decode(response.body) | |
79 assert_kind_of Hash, json | |
80 assert_kind_of Hash, json['user'] | |
81 assert_equal 2, json['user']['id'] | |
82 end | |
83 | |
84 test "GET /users/:id.xml with include=memberships should include memberships" do | |
85 get '/users/2.xml?include=memberships' | |
86 | |
87 assert_response :success | |
88 assert_tag :tag => 'memberships', | |
89 :parent => {:tag => 'user'}, | |
90 :children => {:count => 1} | |
91 end | |
92 | |
93 test "GET /users/:id.json with include=memberships should include memberships" do | |
94 get '/users/2.json?include=memberships' | |
95 | |
96 assert_response :success | |
97 json = ActiveSupport::JSON.decode(response.body) | |
98 assert_kind_of Array, json['user']['memberships'] | |
99 assert_equal [{ | |
100 "id"=>1, | |
101 "project"=>{"name"=>"eCookbook", "id"=>1}, | |
102 "roles"=>[{"name"=>"Manager", "id"=>1}] | |
103 }], json['user']['memberships'] | |
104 end | |
105 | |
106 test "GET /users/current.xml should require authentication" do | |
107 get '/users/current.xml' | |
108 | |
109 assert_response 401 | |
110 end | |
111 | |
112 test "GET /users/current.xml should return current user" do | |
113 get '/users/current.xml', {}, credentials('jsmith') | |
114 | |
115 assert_tag :tag => 'user', | |
116 :child => {:tag => 'id', :content => '2'} | |
117 end | |
118 | |
119 test "GET /users/:id should not return login for other user" do | |
120 get '/users/3.xml', {}, credentials('jsmith') | |
121 assert_response :success | |
122 assert_no_tag 'user', :child => {:tag => 'login'} | |
123 end | |
124 | |
125 test "GET /users/:id should return login for current user" do | |
126 get '/users/2.xml', {}, credentials('jsmith') | |
127 assert_response :success | |
128 assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'} | |
129 end | |
130 | |
131 test "GET /users/:id should not return api_key for other user" do | |
132 get '/users/3.xml', {}, credentials('jsmith') | |
133 assert_response :success | |
134 assert_no_tag 'user', :child => {:tag => 'api_key'} | |
135 end | |
136 | |
137 test "GET /users/:id should return api_key for current user" do | |
138 get '/users/2.xml', {}, credentials('jsmith') | |
139 assert_response :success | |
140 assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key} | |
141 end | |
142 | |
143 test "GET /users/:id should not return status for standard user" do | |
144 get '/users/3.xml', {}, credentials('jsmith') | |
145 assert_response :success | |
146 assert_no_tag 'user', :child => {:tag => 'status'} | |
147 end | |
148 | |
149 test "GET /users/:id should return status for administrators" do | |
150 get '/users/2.xml', {}, credentials('admin') | |
151 assert_response :success | |
152 assert_tag 'user', :child => {:tag => 'status', :content => User.find(1).status.to_s} | |
153 end | |
154 | |
155 test "POST /users.xml with valid parameters should create the user" do | |
156 assert_difference('User.count') do | |
157 post '/users.xml', { | |
158 :user => { | |
159 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
160 :mail => 'foo@example.net', :password => 'secret123', | |
161 :mail_notification => 'only_assigned'} | |
162 }, | |
163 credentials('admin') | |
164 end | |
165 | |
166 user = User.order('id DESC').first | |
167 assert_equal 'foo', user.login | |
168 assert_equal 'Firstname', user.firstname | |
169 assert_equal 'Lastname', user.lastname | |
170 assert_equal 'foo@example.net', user.mail | |
171 assert_equal 'only_assigned', user.mail_notification | |
172 assert !user.admin? | |
173 assert user.check_password?('secret123') | |
174 | |
175 assert_response :created | |
176 assert_equal 'application/xml', @response.content_type | |
177 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} | |
178 end | |
179 | |
180 test "POST /users.json with valid parameters should create the user" do | |
181 assert_difference('User.count') do | |
182 post '/users.json', { | |
183 :user => { | |
184 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
185 :mail => 'foo@example.net', :password => 'secret123', | |
186 :mail_notification => 'only_assigned'} | |
187 }, | |
188 credentials('admin') | |
189 end | |
190 | |
191 user = User.order('id DESC').first | |
192 assert_equal 'foo', user.login | |
193 assert_equal 'Firstname', user.firstname | |
194 assert_equal 'Lastname', user.lastname | |
195 assert_equal 'foo@example.net', user.mail | |
196 assert !user.admin? | |
197 | |
198 assert_response :created | |
199 assert_equal 'application/json', @response.content_type | |
200 json = ActiveSupport::JSON.decode(response.body) | |
201 assert_kind_of Hash, json | |
202 assert_kind_of Hash, json['user'] | |
203 assert_equal user.id, json['user']['id'] | |
204 end | |
205 | |
206 test "POST /users.xml with with invalid parameters should return errors" do | |
207 assert_no_difference('User.count') do | |
208 post '/users.xml', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin') | |
209 end | |
210 | |
211 assert_response :unprocessable_entity | |
212 assert_equal 'application/xml', @response.content_type | |
213 assert_tag 'errors', :child => { | |
214 :tag => 'error', | |
215 :content => "First name can't be blank" | |
216 } | |
217 end | |
218 | |
219 test "POST /users.json with with invalid parameters should return errors" do | |
220 assert_no_difference('User.count') do | |
221 post '/users.json', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin') | |
222 end | |
223 | |
224 assert_response :unprocessable_entity | |
225 assert_equal 'application/json', @response.content_type | |
226 json = ActiveSupport::JSON.decode(response.body) | |
227 assert_kind_of Hash, json | |
228 assert json.has_key?('errors') | |
229 assert_kind_of Array, json['errors'] | |
230 end | |
231 | |
232 test "PUT /users/:id.xml with valid parameters should update the user" do | |
233 assert_no_difference('User.count') do | |
234 put '/users/2.xml', { | |
235 :user => { | |
236 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
237 :mail => 'jsmith@somenet.foo'} | |
238 }, | |
239 credentials('admin') | |
240 end | |
241 | |
242 user = User.find(2) | |
243 assert_equal 'jsmith', user.login | |
244 assert_equal 'John', user.firstname | |
245 assert_equal 'Renamed', user.lastname | |
246 assert_equal 'jsmith@somenet.foo', user.mail | |
247 assert !user.admin? | |
248 | |
249 assert_response :ok | |
250 assert_equal '', @response.body | |
251 end | |
252 | |
253 test "PUT /users/:id.json with valid parameters should update the user" do | |
254 assert_no_difference('User.count') do | |
255 put '/users/2.json', { | |
256 :user => { | |
257 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
258 :mail => 'jsmith@somenet.foo'} | |
259 }, | |
260 credentials('admin') | |
261 end | |
262 | |
263 user = User.find(2) | |
264 assert_equal 'jsmith', user.login | |
265 assert_equal 'John', user.firstname | |
266 assert_equal 'Renamed', user.lastname | |
267 assert_equal 'jsmith@somenet.foo', user.mail | |
268 assert !user.admin? | |
269 | |
270 assert_response :ok | |
271 assert_equal '', @response.body | |
272 end | |
273 | |
274 test "PUT /users/:id.xml with invalid parameters" do | |
275 assert_no_difference('User.count') do | |
276 put '/users/2.xml', { | |
277 :user => { | |
278 :login => 'jsmith', :firstname => '', :lastname => 'Lastname', | |
279 :mail => 'foo'} | |
280 }, | |
281 credentials('admin') | |
282 end | |
283 | |
284 assert_response :unprocessable_entity | |
285 assert_equal 'application/xml', @response.content_type | |
286 assert_tag 'errors', :child => { | |
287 :tag => 'error', | |
288 :content => "First name can't be blank" | |
289 } | |
290 end | |
291 | |
292 test "PUT /users/:id.json with invalid parameters" do | |
293 assert_no_difference('User.count') do | |
294 put '/users/2.json', { | |
295 :user => { | |
296 :login => 'jsmith', :firstname => '', :lastname => 'Lastname', | |
297 :mail => 'foo'} | |
298 }, | |
299 credentials('admin') | |
300 end | |
301 | |
302 assert_response :unprocessable_entity | |
303 assert_equal 'application/json', @response.content_type | |
304 json = ActiveSupport::JSON.decode(response.body) | |
305 assert_kind_of Hash, json | |
306 assert json.has_key?('errors') | |
307 assert_kind_of Array, json['errors'] | |
308 end | |
309 | |
310 test "DELETE /users/:id.xml should delete the user" do | |
311 assert_difference('User.count', -1) do | |
312 delete '/users/2.xml', {}, credentials('admin') | |
313 end | |
314 | |
315 assert_response :ok | |
316 assert_equal '', @response.body | |
317 end | |
318 | |
319 test "DELETE /users/:id.json should delete the user" do | |
320 assert_difference('User.count', -1) do | |
321 delete '/users/2.json', {}, credentials('admin') | |
322 end | |
323 | |
324 assert_response :ok | |
325 assert_equal '', @response.body | |
326 end | |
327 end |