Mercurial > hg > soundsoftware-site
comparison test/integration/api_test/.svn/text-base/users_test.rb.svn-base @ 117:af80e5618e9b redmine-1.1
* Update to Redmine 1.1-stable branch (Redmine SVN rev 4707)
author | Chris Cannam |
---|---|
date | Thu, 13 Jan 2011 12:53:21 +0000 |
parents | |
children | 07fa8a8b56a8 |
comparison
equal
deleted
inserted
replaced
39:150ceac17a8d | 117:af80e5618e9b |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2010 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 require 'pp' | |
20 class ApiTest::UsersTest < ActionController::IntegrationTest | |
21 fixtures :users | |
22 | |
23 def setup | |
24 Setting.rest_api_enabled = '1' | |
25 end | |
26 | |
27 context "GET /users" do | |
28 should_allow_api_authentication(:get, "/users.xml") | |
29 should_allow_api_authentication(:get, "/users.json") | |
30 end | |
31 | |
32 context "GET /users/2" do | |
33 context ".xml" do | |
34 should "return requested user" do | |
35 get '/users/2.xml' | |
36 | |
37 assert_tag :tag => 'user', | |
38 :child => {:tag => 'id', :content => '2'} | |
39 end | |
40 end | |
41 | |
42 context ".json" do | |
43 should "return requested user" do | |
44 get '/users/2.json' | |
45 | |
46 json = ActiveSupport::JSON.decode(response.body) | |
47 assert_kind_of Hash, json | |
48 assert_kind_of Hash, json['user'] | |
49 assert_equal 2, json['user']['id'] | |
50 end | |
51 end | |
52 end | |
53 | |
54 context "GET /users/current" do | |
55 context ".xml" do | |
56 should "require authentication" do | |
57 get '/users/current.xml' | |
58 | |
59 assert_response 401 | |
60 end | |
61 | |
62 should "return current user" do | |
63 get '/users/current.xml', {}, :authorization => credentials('jsmith') | |
64 | |
65 assert_tag :tag => 'user', | |
66 :child => {:tag => 'id', :content => '2'} | |
67 end | |
68 end | |
69 end | |
70 | |
71 context "POST /users" do | |
72 context "with valid parameters" do | |
73 setup do | |
74 @parameters = {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret', :mail_notification => 'only_assigned'}} | |
75 end | |
76 | |
77 context ".xml" do | |
78 should_allow_api_authentication(:post, | |
79 '/users.xml', | |
80 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret'}}, | |
81 {:success_code => :created}) | |
82 | |
83 should "create a user with the attributes" do | |
84 assert_difference('User.count') do | |
85 post '/users.xml', @parameters, :authorization => credentials('admin') | |
86 end | |
87 | |
88 user = User.first(:order => 'id DESC') | |
89 assert_equal 'foo', user.login | |
90 assert_equal 'Firstname', user.firstname | |
91 assert_equal 'Lastname', user.lastname | |
92 assert_equal 'foo@example.net', user.mail | |
93 assert_equal 'only_assigned', user.mail_notification | |
94 assert !user.admin? | |
95 assert user.check_password?('secret') | |
96 | |
97 assert_response :created | |
98 assert_equal 'application/xml', @response.content_type | |
99 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} | |
100 end | |
101 end | |
102 | |
103 context ".json" do | |
104 should_allow_api_authentication(:post, | |
105 '/users.json', | |
106 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}}, | |
107 {:success_code => :created}) | |
108 | |
109 should "create a user with the attributes" do | |
110 assert_difference('User.count') do | |
111 post '/users.json', @parameters, :authorization => credentials('admin') | |
112 end | |
113 | |
114 user = User.first(:order => 'id DESC') | |
115 assert_equal 'foo', user.login | |
116 assert_equal 'Firstname', user.firstname | |
117 assert_equal 'Lastname', user.lastname | |
118 assert_equal 'foo@example.net', user.mail | |
119 assert !user.admin? | |
120 | |
121 assert_response :created | |
122 assert_equal 'application/json', @response.content_type | |
123 json = ActiveSupport::JSON.decode(response.body) | |
124 assert_kind_of Hash, json | |
125 assert_kind_of Hash, json['user'] | |
126 assert_equal user.id, json['user']['id'] | |
127 end | |
128 end | |
129 end | |
130 | |
131 context "with invalid parameters" do | |
132 setup do | |
133 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}} | |
134 end | |
135 | |
136 context ".xml" do | |
137 should "return errors" do | |
138 assert_no_difference('User.count') do | |
139 post '/users.xml', @parameters, :authorization => credentials('admin') | |
140 end | |
141 | |
142 assert_response :unprocessable_entity | |
143 assert_equal 'application/xml', @response.content_type | |
144 assert_tag 'errors', :child => {:tag => 'error', :content => "Firstname can't be blank"} | |
145 end | |
146 end | |
147 | |
148 context ".json" do | |
149 should "return errors" do | |
150 assert_no_difference('User.count') do | |
151 post '/users.json', @parameters, :authorization => credentials('admin') | |
152 end | |
153 | |
154 assert_response :unprocessable_entity | |
155 assert_equal 'application/json', @response.content_type | |
156 json = ActiveSupport::JSON.decode(response.body) | |
157 assert_kind_of Hash, json | |
158 assert json.has_key?('errors') | |
159 assert_kind_of Array, json['errors'] | |
160 end | |
161 end | |
162 end | |
163 end | |
164 | |
165 context "PUT /users/2" do | |
166 context "with valid parameters" do | |
167 setup do | |
168 @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}} | |
169 end | |
170 | |
171 context ".xml" do | |
172 should_allow_api_authentication(:put, | |
173 '/users/2.xml', | |
174 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, | |
175 {:success_code => :ok}) | |
176 | |
177 should "update user with the attributes" do | |
178 assert_no_difference('User.count') do | |
179 put '/users/2.xml', @parameters, :authorization => credentials('admin') | |
180 end | |
181 | |
182 user = User.find(2) | |
183 assert_equal 'jsmith', user.login | |
184 assert_equal 'John', user.firstname | |
185 assert_equal 'Renamed', user.lastname | |
186 assert_equal 'jsmith@somenet.foo', user.mail | |
187 assert !user.admin? | |
188 | |
189 assert_response :ok | |
190 end | |
191 end | |
192 | |
193 context ".json" do | |
194 should_allow_api_authentication(:put, | |
195 '/users/2.json', | |
196 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, | |
197 {:success_code => :ok}) | |
198 | |
199 should "update user with the attributes" do | |
200 assert_no_difference('User.count') do | |
201 put '/users/2.json', @parameters, :authorization => credentials('admin') | |
202 end | |
203 | |
204 user = User.find(2) | |
205 assert_equal 'jsmith', user.login | |
206 assert_equal 'John', user.firstname | |
207 assert_equal 'Renamed', user.lastname | |
208 assert_equal 'jsmith@somenet.foo', user.mail | |
209 assert !user.admin? | |
210 | |
211 assert_response :ok | |
212 end | |
213 end | |
214 end | |
215 | |
216 context "with invalid parameters" do | |
217 setup do | |
218 @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}} | |
219 end | |
220 | |
221 context ".xml" do | |
222 should "return errors" do | |
223 assert_no_difference('User.count') do | |
224 put '/users/2.xml', @parameters, :authorization => credentials('admin') | |
225 end | |
226 | |
227 assert_response :unprocessable_entity | |
228 assert_equal 'application/xml', @response.content_type | |
229 assert_tag 'errors', :child => {:tag => 'error', :content => "Firstname can't be blank"} | |
230 end | |
231 end | |
232 | |
233 context ".json" do | |
234 should "return errors" do | |
235 assert_no_difference('User.count') do | |
236 put '/users/2.json', @parameters, :authorization => credentials('admin') | |
237 end | |
238 | |
239 assert_response :unprocessable_entity | |
240 assert_equal 'application/json', @response.content_type | |
241 json = ActiveSupport::JSON.decode(response.body) | |
242 assert_kind_of Hash, json | |
243 assert json.has_key?('errors') | |
244 assert_kind_of Array, json['errors'] | |
245 end | |
246 end | |
247 end | |
248 | |
249 context "DELETE /users/2" do | |
250 context ".xml" do | |
251 should "not be allowed" do | |
252 assert_no_difference('User.count') do | |
253 delete '/users/2.xml' | |
254 end | |
255 | |
256 assert_response :method_not_allowed | |
257 end | |
258 end | |
259 | |
260 context ".json" do | |
261 should "not be allowed" do | |
262 assert_no_difference('User.count') do | |
263 delete '/users/2.json' | |
264 end | |
265 | |
266 assert_response :method_not_allowed | |
267 end | |
268 end | |
269 end | |
270 end | |
271 | |
272 def credentials(user, password=nil) | |
273 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) | |
274 end | |
275 end |