@@ -1,285 +1,285 | |||
|
1 | 1 | # Redmine - project management software |
|
2 |
# Copyright (C) 2006-201 |
|
|
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 | 74 | @parameters = {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret', :mail_notification => 'only_assigned'}} |
|
75 | 75 | end |
|
76 | ||
|
76 | ||
|
77 | 77 | context ".xml" do |
|
78 | 78 | should_allow_api_authentication(:post, |
|
79 | 79 | '/users.xml', |
|
80 | 80 | {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret'}}, |
|
81 | 81 | {:success_code => :created}) |
|
82 | ||
|
82 | ||
|
83 | 83 | should "create a user with the attributes" do |
|
84 | 84 | assert_difference('User.count') do |
|
85 | 85 | post '/users.xml', @parameters, :authorization => credentials('admin') |
|
86 | 86 | end |
|
87 | ||
|
87 | ||
|
88 | 88 | user = User.first(:order => 'id DESC') |
|
89 | 89 | assert_equal 'foo', user.login |
|
90 | 90 | assert_equal 'Firstname', user.firstname |
|
91 | 91 | assert_equal 'Lastname', user.lastname |
|
92 | 92 | assert_equal 'foo@example.net', user.mail |
|
93 | 93 | assert_equal 'only_assigned', user.mail_notification |
|
94 | 94 | assert !user.admin? |
|
95 | 95 | assert user.check_password?('secret') |
|
96 | ||
|
96 | ||
|
97 | 97 | assert_response :created |
|
98 | 98 | assert_equal 'application/xml', @response.content_type |
|
99 | 99 | assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} |
|
100 | 100 | end |
|
101 | 101 | end |
|
102 | ||
|
102 | ||
|
103 | 103 | context ".json" do |
|
104 | 104 | should_allow_api_authentication(:post, |
|
105 | 105 | '/users.json', |
|
106 | 106 | {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}}, |
|
107 | 107 | {:success_code => :created}) |
|
108 | ||
|
108 | ||
|
109 | 109 | should "create a user with the attributes" do |
|
110 | 110 | assert_difference('User.count') do |
|
111 | 111 | post '/users.json', @parameters, :authorization => credentials('admin') |
|
112 | 112 | end |
|
113 | ||
|
113 | ||
|
114 | 114 | user = User.first(:order => 'id DESC') |
|
115 | 115 | assert_equal 'foo', user.login |
|
116 | 116 | assert_equal 'Firstname', user.firstname |
|
117 | 117 | assert_equal 'Lastname', user.lastname |
|
118 | 118 | assert_equal 'foo@example.net', user.mail |
|
119 | 119 | assert !user.admin? |
|
120 | ||
|
120 | ||
|
121 | 121 | assert_response :created |
|
122 | 122 | assert_equal 'application/json', @response.content_type |
|
123 | 123 | json = ActiveSupport::JSON.decode(response.body) |
|
124 | 124 | assert_kind_of Hash, json |
|
125 | 125 | assert_kind_of Hash, json['user'] |
|
126 | 126 | assert_equal user.id, json['user']['id'] |
|
127 | 127 | end |
|
128 | 128 | end |
|
129 | 129 | end |
|
130 | ||
|
130 | ||
|
131 | 131 | context "with invalid parameters" do |
|
132 | 132 | setup do |
|
133 | 133 | @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}} |
|
134 | 134 | end |
|
135 | ||
|
135 | ||
|
136 | 136 | context ".xml" do |
|
137 | 137 | should "return errors" do |
|
138 | 138 | assert_no_difference('User.count') do |
|
139 | 139 | post '/users.xml', @parameters, :authorization => credentials('admin') |
|
140 | 140 | end |
|
141 | ||
|
141 | ||
|
142 | 142 | assert_response :unprocessable_entity |
|
143 | 143 | assert_equal 'application/xml', @response.content_type |
|
144 | 144 | assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"} |
|
145 | 145 | end |
|
146 | 146 | end |
|
147 | ||
|
147 | ||
|
148 | 148 | context ".json" do |
|
149 | 149 | should "return errors" do |
|
150 | 150 | assert_no_difference('User.count') do |
|
151 | 151 | post '/users.json', @parameters, :authorization => credentials('admin') |
|
152 | 152 | end |
|
153 | ||
|
153 | ||
|
154 | 154 | assert_response :unprocessable_entity |
|
155 | 155 | assert_equal 'application/json', @response.content_type |
|
156 | 156 | json = ActiveSupport::JSON.decode(response.body) |
|
157 | 157 | assert_kind_of Hash, json |
|
158 | 158 | assert json.has_key?('errors') |
|
159 | 159 | assert_kind_of Array, json['errors'] |
|
160 | 160 | end |
|
161 | 161 | end |
|
162 | 162 | end |
|
163 | 163 | end |
|
164 | 164 | |
|
165 | 165 | context "PUT /users/2" do |
|
166 | 166 | context "with valid parameters" do |
|
167 | 167 | setup do |
|
168 | 168 | @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}} |
|
169 | 169 | end |
|
170 | ||
|
170 | ||
|
171 | 171 | context ".xml" do |
|
172 | 172 | should_allow_api_authentication(:put, |
|
173 | 173 | '/users/2.xml', |
|
174 | 174 | {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, |
|
175 | 175 | {:success_code => :ok}) |
|
176 | ||
|
176 | ||
|
177 | 177 | should "update user with the attributes" do |
|
178 | 178 | assert_no_difference('User.count') do |
|
179 | 179 | put '/users/2.xml', @parameters, :authorization => credentials('admin') |
|
180 | 180 | end |
|
181 | ||
|
181 | ||
|
182 | 182 | user = User.find(2) |
|
183 | 183 | assert_equal 'jsmith', user.login |
|
184 | 184 | assert_equal 'John', user.firstname |
|
185 | 185 | assert_equal 'Renamed', user.lastname |
|
186 | 186 | assert_equal 'jsmith@somenet.foo', user.mail |
|
187 | 187 | assert !user.admin? |
|
188 | ||
|
188 | ||
|
189 | 189 | assert_response :ok |
|
190 | 190 | end |
|
191 | 191 | end |
|
192 | ||
|
192 | ||
|
193 | 193 | context ".json" do |
|
194 | 194 | should_allow_api_authentication(:put, |
|
195 | 195 | '/users/2.json', |
|
196 | 196 | {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, |
|
197 | 197 | {:success_code => :ok}) |
|
198 | ||
|
198 | ||
|
199 | 199 | should "update user with the attributes" do |
|
200 | 200 | assert_no_difference('User.count') do |
|
201 | 201 | put '/users/2.json', @parameters, :authorization => credentials('admin') |
|
202 | 202 | end |
|
203 | ||
|
203 | ||
|
204 | 204 | user = User.find(2) |
|
205 | 205 | assert_equal 'jsmith', user.login |
|
206 | 206 | assert_equal 'John', user.firstname |
|
207 | 207 | assert_equal 'Renamed', user.lastname |
|
208 | 208 | assert_equal 'jsmith@somenet.foo', user.mail |
|
209 | 209 | assert !user.admin? |
|
210 | ||
|
210 | ||
|
211 | 211 | assert_response :ok |
|
212 | 212 | end |
|
213 | 213 | end |
|
214 | 214 | end |
|
215 | ||
|
215 | ||
|
216 | 216 | context "with invalid parameters" do |
|
217 | 217 | setup do |
|
218 | 218 | @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}} |
|
219 | 219 | end |
|
220 | ||
|
220 | ||
|
221 | 221 | context ".xml" do |
|
222 | 222 | should "return errors" do |
|
223 | 223 | assert_no_difference('User.count') do |
|
224 | 224 | put '/users/2.xml', @parameters, :authorization => credentials('admin') |
|
225 | 225 | end |
|
226 | ||
|
226 | ||
|
227 | 227 | assert_response :unprocessable_entity |
|
228 | 228 | assert_equal 'application/xml', @response.content_type |
|
229 | 229 | assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"} |
|
230 | 230 | end |
|
231 | 231 | end |
|
232 | ||
|
232 | ||
|
233 | 233 | context ".json" do |
|
234 | 234 | should "return errors" do |
|
235 | 235 | assert_no_difference('User.count') do |
|
236 | 236 | put '/users/2.json', @parameters, :authorization => credentials('admin') |
|
237 | 237 | end |
|
238 | ||
|
238 | ||
|
239 | 239 | assert_response :unprocessable_entity |
|
240 | 240 | assert_equal 'application/json', @response.content_type |
|
241 | 241 | json = ActiveSupport::JSON.decode(response.body) |
|
242 | 242 | assert_kind_of Hash, json |
|
243 | 243 | assert json.has_key?('errors') |
|
244 | 244 | assert_kind_of Array, json['errors'] |
|
245 | 245 | end |
|
246 | 246 | end |
|
247 | 247 | end |
|
248 | 248 | end |
|
249 | ||
|
249 | ||
|
250 | 250 | context "DELETE /users/2" do |
|
251 | 251 | context ".xml" do |
|
252 | 252 | should_allow_api_authentication(:delete, |
|
253 | 253 | '/users/2.xml', |
|
254 | 254 | {}, |
|
255 | 255 | {:success_code => :ok}) |
|
256 | ||
|
256 | ||
|
257 | 257 | should "delete user" do |
|
258 | 258 | assert_difference('User.count', -1) do |
|
259 | 259 | delete '/users/2.xml', {}, :authorization => credentials('admin') |
|
260 | 260 | end |
|
261 | ||
|
261 | ||
|
262 | 262 | assert_response :ok |
|
263 | 263 | end |
|
264 | 264 | end |
|
265 | ||
|
265 | ||
|
266 | 266 | context ".json" do |
|
267 | 267 | should_allow_api_authentication(:delete, |
|
268 | 268 | '/users/2.xml', |
|
269 | 269 | {}, |
|
270 | 270 | {:success_code => :ok}) |
|
271 | ||
|
271 | ||
|
272 | 272 | should "delete user" do |
|
273 | 273 | assert_difference('User.count', -1) do |
|
274 | 274 | delete '/users/2.json', {}, :authorization => credentials('admin') |
|
275 | 275 | end |
|
276 | ||
|
276 | ||
|
277 | 277 | assert_response :ok |
|
278 | 278 | end |
|
279 | 279 | end |
|
280 | 280 | end |
|
281 | ||
|
281 | ||
|
282 | 282 | def credentials(user, password=nil) |
|
283 | 283 | ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) |
|
284 | 284 | end |
|
285 | 285 | end |
General Comments 0
You need to be logged in to leave comments.
Login now