@@ -1,35 +1,36 | |||||
1 | api.user do |
|
1 | api.user do | |
2 | api.id @user.id |
|
2 | api.id @user.id | |
3 | api.login @user.login if User.current.admin? || (User.current == @user) |
|
3 | api.login @user.login if User.current.admin? || (User.current == @user) | |
4 | api.firstname @user.firstname |
|
4 | api.firstname @user.firstname | |
5 | api.lastname @user.lastname |
|
5 | api.lastname @user.lastname | |
6 | api.mail @user.mail if User.current.admin? || !@user.pref.hide_mail |
|
6 | api.mail @user.mail if User.current.admin? || !@user.pref.hide_mail | |
7 | api.created_on @user.created_on |
|
7 | api.created_on @user.created_on | |
8 | api.last_login_on @user.last_login_on |
|
8 | api.last_login_on @user.last_login_on | |
|
9 | api.api_key @user.api_key if User.current.admin? || (User.current == @user) | |||
9 |
|
10 | |||
10 | render_api_custom_values @user.visible_custom_field_values, api |
|
11 | render_api_custom_values @user.visible_custom_field_values, api | |
11 |
|
12 | |||
12 | api.array :groups do |groups| |
|
13 | api.array :groups do |groups| | |
13 | @user.groups.each do |group| |
|
14 | @user.groups.each do |group| | |
14 | api.group :id => group.id, :name => group.name |
|
15 | api.group :id => group.id, :name => group.name | |
15 | end |
|
16 | end | |
16 | end if User.current.admin? && include_in_api_response?('groups') |
|
17 | end if User.current.admin? && include_in_api_response?('groups') | |
17 |
|
18 | |||
18 | api.array :memberships do |
|
19 | api.array :memberships do | |
19 | @memberships.each do |membership| |
|
20 | @memberships.each do |membership| | |
20 | api.membership do |
|
21 | api.membership do | |
21 | api.id membership.id |
|
22 | api.id membership.id | |
22 | api.project :id => membership.project.id, :name => membership.project.name |
|
23 | api.project :id => membership.project.id, :name => membership.project.name | |
23 | api.array :roles do |
|
24 | api.array :roles do | |
24 | membership.member_roles.each do |member_role| |
|
25 | membership.member_roles.each do |member_role| | |
25 | if member_role.role |
|
26 | if member_role.role | |
26 | attrs = {:id => member_role.role.id, :name => member_role.role.name} |
|
27 | attrs = {:id => member_role.role.id, :name => member_role.role.name} | |
27 | attrs.merge!(:inherited => true) if member_role.inherited_from.present? |
|
28 | attrs.merge!(:inherited => true) if member_role.inherited_from.present? | |
28 | api.role attrs |
|
29 | api.role attrs | |
29 | end |
|
30 | end | |
30 | end |
|
31 | end | |
31 | end |
|
32 | end | |
32 | end if membership.project |
|
33 | end if membership.project | |
33 | end |
|
34 | end | |
34 | end if include_in_api_response?('memberships') && @memberships |
|
35 | end if include_in_api_response?('memberships') && @memberships | |
35 | end |
|
36 | end |
@@ -1,359 +1,371 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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 Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base |
|
20 | class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base | |
21 | fixtures :users, :members, :member_roles, :roles, :projects |
|
21 | fixtures :users, :members, :member_roles, :roles, :projects | |
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 /users" do |
|
27 | context "GET /users" do | |
28 | should_allow_api_authentication(:get, "/users.xml") |
|
28 | should_allow_api_authentication(:get, "/users.xml") | |
29 | should_allow_api_authentication(:get, "/users.json") |
|
29 | should_allow_api_authentication(:get, "/users.json") | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
32 | context "GET /users/2" do |
|
32 | context "GET /users/2" do | |
33 | context ".xml" do |
|
33 | context ".xml" do | |
34 | should "return requested user" do |
|
34 | should "return requested user" do | |
35 | get '/users/2.xml' |
|
35 | get '/users/2.xml' | |
36 |
|
36 | |||
37 | assert_response :success |
|
37 | assert_response :success | |
38 | assert_tag :tag => 'user', |
|
38 | assert_tag :tag => 'user', | |
39 | :child => {:tag => 'id', :content => '2'} |
|
39 | :child => {:tag => 'id', :content => '2'} | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | context "with include=memberships" do |
|
42 | context "with include=memberships" do | |
43 | should "include memberships" do |
|
43 | should "include memberships" do | |
44 | get '/users/2.xml?include=memberships' |
|
44 | get '/users/2.xml?include=memberships' | |
45 |
|
45 | |||
46 | assert_response :success |
|
46 | assert_response :success | |
47 | assert_tag :tag => 'memberships', |
|
47 | assert_tag :tag => 'memberships', | |
48 | :parent => {:tag => 'user'}, |
|
48 | :parent => {:tag => 'user'}, | |
49 | :children => {:count => 1} |
|
49 | :children => {:count => 1} | |
50 | end |
|
50 | end | |
51 | end |
|
51 | end | |
52 | end |
|
52 | end | |
53 |
|
53 | |||
54 | context ".json" do |
|
54 | context ".json" do | |
55 | should "return requested user" do |
|
55 | should "return requested user" do | |
56 | get '/users/2.json' |
|
56 | get '/users/2.json' | |
57 |
|
57 | |||
58 | assert_response :success |
|
58 | assert_response :success | |
59 | json = ActiveSupport::JSON.decode(response.body) |
|
59 | json = ActiveSupport::JSON.decode(response.body) | |
60 | assert_kind_of Hash, json |
|
60 | assert_kind_of Hash, json | |
61 | assert_kind_of Hash, json['user'] |
|
61 | assert_kind_of Hash, json['user'] | |
62 | assert_equal 2, json['user']['id'] |
|
62 | assert_equal 2, json['user']['id'] | |
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | context "with include=memberships" do |
|
65 | context "with include=memberships" do | |
66 | should "include memberships" do |
|
66 | should "include memberships" do | |
67 | get '/users/2.json?include=memberships' |
|
67 | get '/users/2.json?include=memberships' | |
68 |
|
68 | |||
69 | assert_response :success |
|
69 | assert_response :success | |
70 | json = ActiveSupport::JSON.decode(response.body) |
|
70 | json = ActiveSupport::JSON.decode(response.body) | |
71 | assert_kind_of Array, json['user']['memberships'] |
|
71 | assert_kind_of Array, json['user']['memberships'] | |
72 | assert_equal [{ |
|
72 | assert_equal [{ | |
73 | "id"=>1, |
|
73 | "id"=>1, | |
74 | "project"=>{"name"=>"eCookbook", "id"=>1}, |
|
74 | "project"=>{"name"=>"eCookbook", "id"=>1}, | |
75 | "roles"=>[{"name"=>"Manager", "id"=>1}] |
|
75 | "roles"=>[{"name"=>"Manager", "id"=>1}] | |
76 | }], json['user']['memberships'] |
|
76 | }], json['user']['memberships'] | |
77 | end |
|
77 | end | |
78 | end |
|
78 | end | |
79 | end |
|
79 | end | |
80 | end |
|
80 | end | |
81 |
|
81 | |||
82 | context "GET /users/current" do |
|
82 | context "GET /users/current" do | |
83 | context ".xml" do |
|
83 | context ".xml" do | |
84 | should "require authentication" do |
|
84 | should "require authentication" do | |
85 | get '/users/current.xml' |
|
85 | get '/users/current.xml' | |
86 |
|
86 | |||
87 | assert_response 401 |
|
87 | assert_response 401 | |
88 | end |
|
88 | end | |
89 |
|
89 | |||
90 | should "return current user" do |
|
90 | should "return current user" do | |
91 | get '/users/current.xml', {}, credentials('jsmith') |
|
91 | get '/users/current.xml', {}, credentials('jsmith') | |
92 |
|
92 | |||
93 | assert_tag :tag => 'user', |
|
93 | assert_tag :tag => 'user', | |
94 | :child => {:tag => 'id', :content => '2'} |
|
94 | :child => {:tag => 'id', :content => '2'} | |
95 | end |
|
95 | end | |
96 | end |
|
96 | end | |
97 | end |
|
97 | end | |
98 |
|
98 | |||
99 | test "GET /users/:id should not return login for other user" do |
|
99 | test "GET /users/:id should not return login for other user" do | |
100 | get '/users/3.xml', {}, credentials('jsmith') |
|
100 | get '/users/3.xml', {}, credentials('jsmith') | |
101 | assert_response :success |
|
101 | assert_response :success | |
102 | assert_no_tag 'user', :child => {:tag => 'login'} |
|
102 | assert_no_tag 'user', :child => {:tag => 'login'} | |
103 | end |
|
103 | end | |
104 |
|
104 | |||
105 | test "GET /users/:id should return login for current user" do |
|
105 | test "GET /users/:id should return login for current user" do | |
106 | get '/users/2.xml', {}, credentials('jsmith') |
|
106 | get '/users/2.xml', {}, credentials('jsmith') | |
107 | assert_response :success |
|
107 | assert_response :success | |
108 | assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'} |
|
108 | assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'} | |
109 | end |
|
109 | end | |
110 |
|
110 | |||
|
111 | test "GET /users/:id should not return api_key for other user" do | |||
|
112 | get '/users/3.xml', {}, credentials('jsmith') | |||
|
113 | assert_response :success | |||
|
114 | assert_no_tag 'user', :child => {:tag => 'api_key'} | |||
|
115 | end | |||
|
116 | ||||
|
117 | test "GET /users/:id should return api_key for current user" do | |||
|
118 | get '/users/2.xml', {}, credentials('jsmith') | |||
|
119 | assert_response :success | |||
|
120 | assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key} | |||
|
121 | end | |||
|
122 | ||||
111 | context "POST /users" do |
|
123 | context "POST /users" do | |
112 | context "with valid parameters" do |
|
124 | context "with valid parameters" do | |
113 | setup do |
|
125 | setup do | |
114 | @parameters = { |
|
126 | @parameters = { | |
115 | :user => { |
|
127 | :user => { | |
116 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', |
|
128 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
117 | :mail => 'foo@example.net', :password => 'secret123', |
|
129 | :mail => 'foo@example.net', :password => 'secret123', | |
118 | :mail_notification => 'only_assigned' |
|
130 | :mail_notification => 'only_assigned' | |
119 | } |
|
131 | } | |
120 | } |
|
132 | } | |
121 | end |
|
133 | end | |
122 |
|
134 | |||
123 | context ".xml" do |
|
135 | context ".xml" do | |
124 | should_allow_api_authentication(:post, |
|
136 | should_allow_api_authentication(:post, | |
125 | '/users.xml', |
|
137 | '/users.xml', | |
126 | {:user => { |
|
138 | {:user => { | |
127 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', |
|
139 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
128 | :mail => 'foo@example.net', :password => 'secret123' |
|
140 | :mail => 'foo@example.net', :password => 'secret123' | |
129 | }}, |
|
141 | }}, | |
130 | {:success_code => :created}) |
|
142 | {:success_code => :created}) | |
131 |
|
143 | |||
132 | should "create a user with the attributes" do |
|
144 | should "create a user with the attributes" do | |
133 | assert_difference('User.count') do |
|
145 | assert_difference('User.count') do | |
134 | post '/users.xml', @parameters, credentials('admin') |
|
146 | post '/users.xml', @parameters, credentials('admin') | |
135 | end |
|
147 | end | |
136 |
|
148 | |||
137 | user = User.first(:order => 'id DESC') |
|
149 | user = User.first(:order => 'id DESC') | |
138 | assert_equal 'foo', user.login |
|
150 | assert_equal 'foo', user.login | |
139 | assert_equal 'Firstname', user.firstname |
|
151 | assert_equal 'Firstname', user.firstname | |
140 | assert_equal 'Lastname', user.lastname |
|
152 | assert_equal 'Lastname', user.lastname | |
141 | assert_equal 'foo@example.net', user.mail |
|
153 | assert_equal 'foo@example.net', user.mail | |
142 | assert_equal 'only_assigned', user.mail_notification |
|
154 | assert_equal 'only_assigned', user.mail_notification | |
143 | assert !user.admin? |
|
155 | assert !user.admin? | |
144 | assert user.check_password?('secret123') |
|
156 | assert user.check_password?('secret123') | |
145 |
|
157 | |||
146 | assert_response :created |
|
158 | assert_response :created | |
147 | assert_equal 'application/xml', @response.content_type |
|
159 | assert_equal 'application/xml', @response.content_type | |
148 | assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} |
|
160 | assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} | |
149 | end |
|
161 | end | |
150 | end |
|
162 | end | |
151 |
|
163 | |||
152 | context ".json" do |
|
164 | context ".json" do | |
153 | should_allow_api_authentication(:post, |
|
165 | should_allow_api_authentication(:post, | |
154 | '/users.json', |
|
166 | '/users.json', | |
155 | {:user => { |
|
167 | {:user => { | |
156 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', |
|
168 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
157 | :mail => 'foo@example.net' |
|
169 | :mail => 'foo@example.net' | |
158 | }}, |
|
170 | }}, | |
159 | {:success_code => :created}) |
|
171 | {:success_code => :created}) | |
160 |
|
172 | |||
161 | should "create a user with the attributes" do |
|
173 | should "create a user with the attributes" do | |
162 | assert_difference('User.count') do |
|
174 | assert_difference('User.count') do | |
163 | post '/users.json', @parameters, credentials('admin') |
|
175 | post '/users.json', @parameters, credentials('admin') | |
164 | end |
|
176 | end | |
165 |
|
177 | |||
166 | user = User.first(:order => 'id DESC') |
|
178 | user = User.first(:order => 'id DESC') | |
167 | assert_equal 'foo', user.login |
|
179 | assert_equal 'foo', user.login | |
168 | assert_equal 'Firstname', user.firstname |
|
180 | assert_equal 'Firstname', user.firstname | |
169 | assert_equal 'Lastname', user.lastname |
|
181 | assert_equal 'Lastname', user.lastname | |
170 | assert_equal 'foo@example.net', user.mail |
|
182 | assert_equal 'foo@example.net', user.mail | |
171 | assert !user.admin? |
|
183 | assert !user.admin? | |
172 |
|
184 | |||
173 | assert_response :created |
|
185 | assert_response :created | |
174 | assert_equal 'application/json', @response.content_type |
|
186 | assert_equal 'application/json', @response.content_type | |
175 | json = ActiveSupport::JSON.decode(response.body) |
|
187 | json = ActiveSupport::JSON.decode(response.body) | |
176 | assert_kind_of Hash, json |
|
188 | assert_kind_of Hash, json | |
177 | assert_kind_of Hash, json['user'] |
|
189 | assert_kind_of Hash, json['user'] | |
178 | assert_equal user.id, json['user']['id'] |
|
190 | assert_equal user.id, json['user']['id'] | |
179 | end |
|
191 | end | |
180 | end |
|
192 | end | |
181 | end |
|
193 | end | |
182 |
|
194 | |||
183 | context "with invalid parameters" do |
|
195 | context "with invalid parameters" do | |
184 | setup do |
|
196 | setup do | |
185 | @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}} |
|
197 | @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}} | |
186 | end |
|
198 | end | |
187 |
|
199 | |||
188 | context ".xml" do |
|
200 | context ".xml" do | |
189 | should "return errors" do |
|
201 | should "return errors" do | |
190 | assert_no_difference('User.count') do |
|
202 | assert_no_difference('User.count') do | |
191 | post '/users.xml', @parameters, credentials('admin') |
|
203 | post '/users.xml', @parameters, credentials('admin') | |
192 | end |
|
204 | end | |
193 |
|
205 | |||
194 | assert_response :unprocessable_entity |
|
206 | assert_response :unprocessable_entity | |
195 | assert_equal 'application/xml', @response.content_type |
|
207 | assert_equal 'application/xml', @response.content_type | |
196 | assert_tag 'errors', :child => { |
|
208 | assert_tag 'errors', :child => { | |
197 | :tag => 'error', |
|
209 | :tag => 'error', | |
198 | :content => "First name can't be blank" |
|
210 | :content => "First name can't be blank" | |
199 | } |
|
211 | } | |
200 | end |
|
212 | end | |
201 | end |
|
213 | end | |
202 |
|
214 | |||
203 | context ".json" do |
|
215 | context ".json" do | |
204 | should "return errors" do |
|
216 | should "return errors" do | |
205 | assert_no_difference('User.count') do |
|
217 | assert_no_difference('User.count') do | |
206 | post '/users.json', @parameters, credentials('admin') |
|
218 | post '/users.json', @parameters, credentials('admin') | |
207 | end |
|
219 | end | |
208 |
|
220 | |||
209 | assert_response :unprocessable_entity |
|
221 | assert_response :unprocessable_entity | |
210 | assert_equal 'application/json', @response.content_type |
|
222 | assert_equal 'application/json', @response.content_type | |
211 | json = ActiveSupport::JSON.decode(response.body) |
|
223 | json = ActiveSupport::JSON.decode(response.body) | |
212 | assert_kind_of Hash, json |
|
224 | assert_kind_of Hash, json | |
213 | assert json.has_key?('errors') |
|
225 | assert json.has_key?('errors') | |
214 | assert_kind_of Array, json['errors'] |
|
226 | assert_kind_of Array, json['errors'] | |
215 | end |
|
227 | end | |
216 | end |
|
228 | end | |
217 | end |
|
229 | end | |
218 | end |
|
230 | end | |
219 |
|
231 | |||
220 | context "PUT /users/2" do |
|
232 | context "PUT /users/2" do | |
221 | context "with valid parameters" do |
|
233 | context "with valid parameters" do | |
222 | setup do |
|
234 | setup do | |
223 | @parameters = { |
|
235 | @parameters = { | |
224 | :user => { |
|
236 | :user => { | |
225 | :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', |
|
237 | :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
226 | :mail => 'jsmith@somenet.foo' |
|
238 | :mail => 'jsmith@somenet.foo' | |
227 | } |
|
239 | } | |
228 | } |
|
240 | } | |
229 | end |
|
241 | end | |
230 |
|
242 | |||
231 | context ".xml" do |
|
243 | context ".xml" do | |
232 | should_allow_api_authentication(:put, |
|
244 | should_allow_api_authentication(:put, | |
233 | '/users/2.xml', |
|
245 | '/users/2.xml', | |
234 | {:user => { |
|
246 | {:user => { | |
235 | :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', |
|
247 | :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
236 | :mail => 'jsmith@somenet.foo' |
|
248 | :mail => 'jsmith@somenet.foo' | |
237 | }}, |
|
249 | }}, | |
238 | {:success_code => :ok}) |
|
250 | {:success_code => :ok}) | |
239 |
|
251 | |||
240 | should "update user with the attributes" do |
|
252 | should "update user with the attributes" do | |
241 | assert_no_difference('User.count') do |
|
253 | assert_no_difference('User.count') do | |
242 | put '/users/2.xml', @parameters, credentials('admin') |
|
254 | put '/users/2.xml', @parameters, credentials('admin') | |
243 | end |
|
255 | end | |
244 |
|
256 | |||
245 | user = User.find(2) |
|
257 | user = User.find(2) | |
246 | assert_equal 'jsmith', user.login |
|
258 | assert_equal 'jsmith', user.login | |
247 | assert_equal 'John', user.firstname |
|
259 | assert_equal 'John', user.firstname | |
248 | assert_equal 'Renamed', user.lastname |
|
260 | assert_equal 'Renamed', user.lastname | |
249 | assert_equal 'jsmith@somenet.foo', user.mail |
|
261 | assert_equal 'jsmith@somenet.foo', user.mail | |
250 | assert !user.admin? |
|
262 | assert !user.admin? | |
251 |
|
263 | |||
252 | assert_response :ok |
|
264 | assert_response :ok | |
253 | assert_equal '', @response.body |
|
265 | assert_equal '', @response.body | |
254 | end |
|
266 | end | |
255 | end |
|
267 | end | |
256 |
|
268 | |||
257 | context ".json" do |
|
269 | context ".json" do | |
258 | should_allow_api_authentication(:put, |
|
270 | should_allow_api_authentication(:put, | |
259 | '/users/2.json', |
|
271 | '/users/2.json', | |
260 | {:user => { |
|
272 | {:user => { | |
261 | :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', |
|
273 | :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
262 | :mail => 'jsmith@somenet.foo' |
|
274 | :mail => 'jsmith@somenet.foo' | |
263 | }}, |
|
275 | }}, | |
264 | {:success_code => :ok}) |
|
276 | {:success_code => :ok}) | |
265 |
|
277 | |||
266 | should "update user with the attributes" do |
|
278 | should "update user with the attributes" do | |
267 | assert_no_difference('User.count') do |
|
279 | assert_no_difference('User.count') do | |
268 | put '/users/2.json', @parameters, credentials('admin') |
|
280 | put '/users/2.json', @parameters, credentials('admin') | |
269 | end |
|
281 | end | |
270 |
|
282 | |||
271 | user = User.find(2) |
|
283 | user = User.find(2) | |
272 | assert_equal 'jsmith', user.login |
|
284 | assert_equal 'jsmith', user.login | |
273 | assert_equal 'John', user.firstname |
|
285 | assert_equal 'John', user.firstname | |
274 | assert_equal 'Renamed', user.lastname |
|
286 | assert_equal 'Renamed', user.lastname | |
275 | assert_equal 'jsmith@somenet.foo', user.mail |
|
287 | assert_equal 'jsmith@somenet.foo', user.mail | |
276 | assert !user.admin? |
|
288 | assert !user.admin? | |
277 |
|
289 | |||
278 | assert_response :ok |
|
290 | assert_response :ok | |
279 | assert_equal '', @response.body |
|
291 | assert_equal '', @response.body | |
280 | end |
|
292 | end | |
281 | end |
|
293 | end | |
282 | end |
|
294 | end | |
283 |
|
295 | |||
284 | context "with invalid parameters" do |
|
296 | context "with invalid parameters" do | |
285 | setup do |
|
297 | setup do | |
286 | @parameters = { |
|
298 | @parameters = { | |
287 | :user => { |
|
299 | :user => { | |
288 | :login => 'jsmith', :firstname => '', :lastname => 'Lastname', |
|
300 | :login => 'jsmith', :firstname => '', :lastname => 'Lastname', | |
289 | :mail => 'foo' |
|
301 | :mail => 'foo' | |
290 | } |
|
302 | } | |
291 | } |
|
303 | } | |
292 | end |
|
304 | end | |
293 |
|
305 | |||
294 | context ".xml" do |
|
306 | context ".xml" do | |
295 | should "return errors" do |
|
307 | should "return errors" do | |
296 | assert_no_difference('User.count') do |
|
308 | assert_no_difference('User.count') do | |
297 | put '/users/2.xml', @parameters, credentials('admin') |
|
309 | put '/users/2.xml', @parameters, credentials('admin') | |
298 | end |
|
310 | end | |
299 |
|
311 | |||
300 | assert_response :unprocessable_entity |
|
312 | assert_response :unprocessable_entity | |
301 | assert_equal 'application/xml', @response.content_type |
|
313 | assert_equal 'application/xml', @response.content_type | |
302 | assert_tag 'errors', :child => { |
|
314 | assert_tag 'errors', :child => { | |
303 | :tag => 'error', |
|
315 | :tag => 'error', | |
304 | :content => "First name can't be blank" |
|
316 | :content => "First name can't be blank" | |
305 | } |
|
317 | } | |
306 | end |
|
318 | end | |
307 | end |
|
319 | end | |
308 |
|
320 | |||
309 | context ".json" do |
|
321 | context ".json" do | |
310 | should "return errors" do |
|
322 | should "return errors" do | |
311 | assert_no_difference('User.count') do |
|
323 | assert_no_difference('User.count') do | |
312 | put '/users/2.json', @parameters, credentials('admin') |
|
324 | put '/users/2.json', @parameters, credentials('admin') | |
313 | end |
|
325 | end | |
314 |
|
326 | |||
315 | assert_response :unprocessable_entity |
|
327 | assert_response :unprocessable_entity | |
316 | assert_equal 'application/json', @response.content_type |
|
328 | assert_equal 'application/json', @response.content_type | |
317 | json = ActiveSupport::JSON.decode(response.body) |
|
329 | json = ActiveSupport::JSON.decode(response.body) | |
318 | assert_kind_of Hash, json |
|
330 | assert_kind_of Hash, json | |
319 | assert json.has_key?('errors') |
|
331 | assert json.has_key?('errors') | |
320 | assert_kind_of Array, json['errors'] |
|
332 | assert_kind_of Array, json['errors'] | |
321 | end |
|
333 | end | |
322 | end |
|
334 | end | |
323 | end |
|
335 | end | |
324 | end |
|
336 | end | |
325 |
|
337 | |||
326 | context "DELETE /users/2" do |
|
338 | context "DELETE /users/2" do | |
327 | context ".xml" do |
|
339 | context ".xml" do | |
328 | should_allow_api_authentication(:delete, |
|
340 | should_allow_api_authentication(:delete, | |
329 | '/users/2.xml', |
|
341 | '/users/2.xml', | |
330 | {}, |
|
342 | {}, | |
331 | {:success_code => :ok}) |
|
343 | {:success_code => :ok}) | |
332 |
|
344 | |||
333 | should "delete user" do |
|
345 | should "delete user" do | |
334 | assert_difference('User.count', -1) do |
|
346 | assert_difference('User.count', -1) do | |
335 | delete '/users/2.xml', {}, credentials('admin') |
|
347 | delete '/users/2.xml', {}, credentials('admin') | |
336 | end |
|
348 | end | |
337 |
|
349 | |||
338 | assert_response :ok |
|
350 | assert_response :ok | |
339 | assert_equal '', @response.body |
|
351 | assert_equal '', @response.body | |
340 | end |
|
352 | end | |
341 | end |
|
353 | end | |
342 |
|
354 | |||
343 | context ".json" do |
|
355 | context ".json" do | |
344 | should_allow_api_authentication(:delete, |
|
356 | should_allow_api_authentication(:delete, | |
345 | '/users/2.xml', |
|
357 | '/users/2.xml', | |
346 | {}, |
|
358 | {}, | |
347 | {:success_code => :ok}) |
|
359 | {:success_code => :ok}) | |
348 |
|
360 | |||
349 | should "delete user" do |
|
361 | should "delete user" do | |
350 | assert_difference('User.count', -1) do |
|
362 | assert_difference('User.count', -1) do | |
351 | delete '/users/2.json', {}, credentials('admin') |
|
363 | delete '/users/2.json', {}, credentials('admin') | |
352 | end |
|
364 | end | |
353 |
|
365 | |||
354 | assert_response :ok |
|
366 | assert_response :ok | |
355 | assert_equal '', @response.body |
|
367 | assert_equal '', @response.body | |
356 | end |
|
368 | end | |
357 | end |
|
369 | end | |
358 | end |
|
370 | end | |
359 | end |
|
371 | end |
General Comments 0
You need to be logged in to leave comments.
Login now