@@ -1,285 +1,319 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | require 'pp' |
|
20 | 20 | class ApiTest::UsersTest < ActionController::IntegrationTest |
|
21 | 21 | fixtures :users |
|
22 | 22 | |
|
23 | 23 | def setup |
|
24 | 24 | Setting.rest_api_enabled = '1' |
|
25 | 25 | end |
|
26 | 26 | |
|
27 | 27 | context "GET /users" do |
|
28 | 28 | should_allow_api_authentication(:get, "/users.xml") |
|
29 | 29 | should_allow_api_authentication(:get, "/users.json") |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | context "GET /users/2" do |
|
33 | 33 | context ".xml" do |
|
34 | 34 | should "return requested user" do |
|
35 | 35 | get '/users/2.xml' |
|
36 | 36 | |
|
37 | 37 | assert_tag :tag => 'user', |
|
38 | 38 | :child => {:tag => 'id', :content => '2'} |
|
39 | 39 | end |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | context ".json" do |
|
43 | 43 | should "return requested user" do |
|
44 | 44 | get '/users/2.json' |
|
45 | 45 | |
|
46 | 46 | json = ActiveSupport::JSON.decode(response.body) |
|
47 | 47 | assert_kind_of Hash, json |
|
48 | 48 | assert_kind_of Hash, json['user'] |
|
49 | 49 | assert_equal 2, json['user']['id'] |
|
50 | 50 | end |
|
51 | 51 | end |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | context "GET /users/current" do |
|
55 | 55 | context ".xml" do |
|
56 | 56 | should "require authentication" do |
|
57 | 57 | get '/users/current.xml' |
|
58 | 58 | |
|
59 | 59 | assert_response 401 |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | should "return current user" do |
|
63 | 63 | get '/users/current.xml', {}, :authorization => credentials('jsmith') |
|
64 | 64 | |
|
65 | 65 | assert_tag :tag => 'user', |
|
66 | 66 | :child => {:tag => 'id', :content => '2'} |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | context "POST /users" do |
|
72 | 72 | context "with valid parameters" do |
|
73 | 73 | setup do |
|
74 | @parameters = {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret', :mail_notification => 'only_assigned'}} | |
|
74 | @parameters = { | |
|
75 | :user => { | |
|
76 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
|
77 | :mail => 'foo@example.net', :password => 'secret', | |
|
78 | :mail_notification => 'only_assigned' | |
|
79 | } | |
|
80 | } | |
|
75 | 81 | end |
|
76 | 82 | |
|
77 | 83 | context ".xml" do |
|
78 | 84 | should_allow_api_authentication(:post, |
|
79 | 85 | '/users.xml', |
|
80 | {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret'}}, | |
|
86 | {:user => { | |
|
87 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
|
88 | :mail => 'foo@example.net', :password => 'secret' | |
|
89 | }}, | |
|
81 | 90 | {:success_code => :created}) |
|
82 | 91 | |
|
83 | 92 | should "create a user with the attributes" do |
|
84 | 93 | assert_difference('User.count') do |
|
85 | 94 | post '/users.xml', @parameters, :authorization => credentials('admin') |
|
86 | 95 | end |
|
87 | 96 | |
|
88 | 97 | user = User.first(:order => 'id DESC') |
|
89 | 98 | assert_equal 'foo', user.login |
|
90 | 99 | assert_equal 'Firstname', user.firstname |
|
91 | 100 | assert_equal 'Lastname', user.lastname |
|
92 | 101 | assert_equal 'foo@example.net', user.mail |
|
93 | 102 | assert_equal 'only_assigned', user.mail_notification |
|
94 | 103 | assert !user.admin? |
|
95 | 104 | assert user.check_password?('secret') |
|
96 | 105 | |
|
97 | 106 | assert_response :created |
|
98 | 107 | assert_equal 'application/xml', @response.content_type |
|
99 | 108 | assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} |
|
100 | 109 | end |
|
101 | 110 | end |
|
102 | 111 | |
|
103 | 112 | context ".json" do |
|
104 | 113 | should_allow_api_authentication(:post, |
|
105 | 114 | '/users.json', |
|
106 | {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}}, | |
|
115 | {:user => { | |
|
116 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
|
117 | :mail => 'foo@example.net' | |
|
118 | }}, | |
|
107 | 119 | {:success_code => :created}) |
|
108 | 120 | |
|
109 | 121 | should "create a user with the attributes" do |
|
110 | 122 | assert_difference('User.count') do |
|
111 | 123 | post '/users.json', @parameters, :authorization => credentials('admin') |
|
112 | 124 | end |
|
113 | 125 | |
|
114 | 126 | user = User.first(:order => 'id DESC') |
|
115 | 127 | assert_equal 'foo', user.login |
|
116 | 128 | assert_equal 'Firstname', user.firstname |
|
117 | 129 | assert_equal 'Lastname', user.lastname |
|
118 | 130 | assert_equal 'foo@example.net', user.mail |
|
119 | 131 | assert !user.admin? |
|
120 | 132 | |
|
121 | 133 | assert_response :created |
|
122 | 134 | assert_equal 'application/json', @response.content_type |
|
123 | 135 | json = ActiveSupport::JSON.decode(response.body) |
|
124 | 136 | assert_kind_of Hash, json |
|
125 | 137 | assert_kind_of Hash, json['user'] |
|
126 | 138 | assert_equal user.id, json['user']['id'] |
|
127 | 139 | end |
|
128 | 140 | end |
|
129 | 141 | end |
|
130 | 142 | |
|
131 | 143 | context "with invalid parameters" do |
|
132 | 144 | setup do |
|
133 | 145 | @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}} |
|
134 | 146 | end |
|
135 | 147 | |
|
136 | 148 | context ".xml" do |
|
137 | 149 | should "return errors" do |
|
138 | 150 | assert_no_difference('User.count') do |
|
139 | 151 | post '/users.xml', @parameters, :authorization => credentials('admin') |
|
140 | 152 | end |
|
141 | 153 | |
|
142 | 154 | assert_response :unprocessable_entity |
|
143 | 155 | assert_equal 'application/xml', @response.content_type |
|
144 | assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"} | |
|
156 | assert_tag 'errors', :child => { | |
|
157 | :tag => 'error', | |
|
158 | :content => "First name can't be blank" | |
|
159 | } | |
|
145 | 160 | end |
|
146 | 161 | end |
|
147 | 162 | |
|
148 | 163 | context ".json" do |
|
149 | 164 | should "return errors" do |
|
150 | 165 | assert_no_difference('User.count') do |
|
151 | 166 | post '/users.json', @parameters, :authorization => credentials('admin') |
|
152 | 167 | end |
|
153 | 168 | |
|
154 | 169 | assert_response :unprocessable_entity |
|
155 | 170 | assert_equal 'application/json', @response.content_type |
|
156 | 171 | json = ActiveSupport::JSON.decode(response.body) |
|
157 | 172 | assert_kind_of Hash, json |
|
158 | 173 | assert json.has_key?('errors') |
|
159 | 174 | assert_kind_of Array, json['errors'] |
|
160 | 175 | end |
|
161 | 176 | end |
|
162 | 177 | end |
|
163 | 178 | end |
|
164 | 179 | |
|
165 | 180 | context "PUT /users/2" do |
|
166 | 181 | context "with valid parameters" do |
|
167 | 182 | setup do |
|
168 | @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}} | |
|
183 | @parameters = { | |
|
184 | :user => { | |
|
185 | :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
|
186 | :mail => 'jsmith@somenet.foo' | |
|
187 | } | |
|
188 | } | |
|
169 | 189 | end |
|
170 | 190 | |
|
171 | 191 | context ".xml" do |
|
172 | 192 | should_allow_api_authentication(:put, |
|
173 | 193 | '/users/2.xml', |
|
174 | {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, | |
|
194 | {:user => { | |
|
195 | :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
|
196 | :mail => 'jsmith@somenet.foo' | |
|
197 | }}, | |
|
175 | 198 | {:success_code => :ok}) |
|
176 | 199 | |
|
177 | 200 | should "update user with the attributes" do |
|
178 | 201 | assert_no_difference('User.count') do |
|
179 | 202 | put '/users/2.xml', @parameters, :authorization => credentials('admin') |
|
180 | 203 | end |
|
181 | 204 | |
|
182 | 205 | user = User.find(2) |
|
183 | 206 | assert_equal 'jsmith', user.login |
|
184 | 207 | assert_equal 'John', user.firstname |
|
185 | 208 | assert_equal 'Renamed', user.lastname |
|
186 | 209 | assert_equal 'jsmith@somenet.foo', user.mail |
|
187 | 210 | assert !user.admin? |
|
188 | 211 | |
|
189 | 212 | assert_response :ok |
|
190 | 213 | end |
|
191 | 214 | end |
|
192 | 215 | |
|
193 | 216 | context ".json" do |
|
194 | 217 | should_allow_api_authentication(:put, |
|
195 | 218 | '/users/2.json', |
|
196 | {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, | |
|
219 | {:user => { | |
|
220 | :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
|
221 | :mail => 'jsmith@somenet.foo' | |
|
222 | }}, | |
|
197 | 223 | {:success_code => :ok}) |
|
198 | 224 | |
|
199 | 225 | should "update user with the attributes" do |
|
200 | 226 | assert_no_difference('User.count') do |
|
201 | 227 | put '/users/2.json', @parameters, :authorization => credentials('admin') |
|
202 | 228 | end |
|
203 | 229 | |
|
204 | 230 | user = User.find(2) |
|
205 | 231 | assert_equal 'jsmith', user.login |
|
206 | 232 | assert_equal 'John', user.firstname |
|
207 | 233 | assert_equal 'Renamed', user.lastname |
|
208 | 234 | assert_equal 'jsmith@somenet.foo', user.mail |
|
209 | 235 | assert !user.admin? |
|
210 | 236 | |
|
211 | 237 | assert_response :ok |
|
212 | 238 | end |
|
213 | 239 | end |
|
214 | 240 | end |
|
215 | 241 | |
|
216 | 242 | context "with invalid parameters" do |
|
217 | 243 | setup do |
|
218 | @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}} | |
|
244 | @parameters = { | |
|
245 | :user => { | |
|
246 | :login => 'jsmith', :firstname => '', :lastname => 'Lastname', | |
|
247 | :mail => 'foo' | |
|
248 | } | |
|
249 | } | |
|
219 | 250 | end |
|
220 | 251 | |
|
221 | 252 | context ".xml" do |
|
222 | 253 | should "return errors" do |
|
223 | 254 | assert_no_difference('User.count') do |
|
224 | 255 | put '/users/2.xml', @parameters, :authorization => credentials('admin') |
|
225 | 256 | end |
|
226 | 257 | |
|
227 | 258 | assert_response :unprocessable_entity |
|
228 | 259 | assert_equal 'application/xml', @response.content_type |
|
229 | assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"} | |
|
260 | assert_tag 'errors', :child => { | |
|
261 | :tag => 'error', | |
|
262 | :content => "First name can't be blank" | |
|
263 | } | |
|
230 | 264 | end |
|
231 | 265 | end |
|
232 | 266 | |
|
233 | 267 | context ".json" do |
|
234 | 268 | should "return errors" do |
|
235 | 269 | assert_no_difference('User.count') do |
|
236 | 270 | put '/users/2.json', @parameters, :authorization => credentials('admin') |
|
237 | 271 | end |
|
238 | 272 | |
|
239 | 273 | assert_response :unprocessable_entity |
|
240 | 274 | assert_equal 'application/json', @response.content_type |
|
241 | 275 | json = ActiveSupport::JSON.decode(response.body) |
|
242 | 276 | assert_kind_of Hash, json |
|
243 | 277 | assert json.has_key?('errors') |
|
244 | 278 | assert_kind_of Array, json['errors'] |
|
245 | 279 | end |
|
246 | 280 | end |
|
247 | 281 | end |
|
248 | 282 | end |
|
249 | 283 | |
|
250 | 284 | context "DELETE /users/2" do |
|
251 | 285 | context ".xml" do |
|
252 | 286 | should_allow_api_authentication(:delete, |
|
253 | 287 | '/users/2.xml', |
|
254 | 288 | {}, |
|
255 | 289 | {:success_code => :ok}) |
|
256 | 290 | |
|
257 | 291 | should "delete user" do |
|
258 | 292 | assert_difference('User.count', -1) do |
|
259 | 293 | delete '/users/2.xml', {}, :authorization => credentials('admin') |
|
260 | 294 | end |
|
261 | 295 | |
|
262 | 296 | assert_response :ok |
|
263 | 297 | end |
|
264 | 298 | end |
|
265 | 299 | |
|
266 | 300 | context ".json" do |
|
267 | 301 | should_allow_api_authentication(:delete, |
|
268 | 302 | '/users/2.xml', |
|
269 | 303 | {}, |
|
270 | 304 | {:success_code => :ok}) |
|
271 | 305 | |
|
272 | 306 | should "delete user" do |
|
273 | 307 | assert_difference('User.count', -1) do |
|
274 | 308 | delete '/users/2.json', {}, :authorization => credentials('admin') |
|
275 | 309 | end |
|
276 | 310 | |
|
277 | 311 | assert_response :ok |
|
278 | 312 | end |
|
279 | 313 | end |
|
280 | 314 | end |
|
281 | 315 | |
|
282 | 316 | def credentials(user, password=nil) |
|
283 | 317 | ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) |
|
284 | 318 | end |
|
285 | 319 | end |
General Comments 0
You need to be logged in to leave comments.
Login now