##// END OF EJS Templates
Allow key authentication when updating issues (with tests) #6447...
Eric Davis -
r4252:7d934c984ae8
parent child
Show More
@@ -27,7 +27,7 class IssuesController < ApplicationController
27 before_filter :find_optional_project, :only => [:index]
27 before_filter :find_optional_project, :only => [:index]
28 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 before_filter :check_for_default_issue_status, :only => [:new, :create]
29 before_filter :build_new_issue_from_params, :only => [:new, :create]
29 before_filter :build_new_issue_from_params, :only => [:new, :create]
30 accept_key_auth :index, :show, :create
30 accept_key_auth :index, :show, :create, :update
31
31
32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33
33
@@ -160,120 +160,141 class ApiTest::IssuesTest < ActionController::IntegrationTest
160 end
160 end
161 end
161 end
162
162
163 context "PUT /issues/1.xml" do
163 # Issue 6 is on a private project
164 context "PUT /issues/6.xml" do
164 setup do
165 setup do
165 @issue_count = Issue.count
166 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
166 @journal_count = Journal.count
167 @headers = { :authorization => credentials('jsmith') }
167 @attributes = {:subject => 'API update', :notes => 'A new note'}
168
169 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
170 end
168 end
171
169
172 should_respond_with :ok
170 should_allow_api_authentication(:put,
173 should_respond_with_content_type 'application/xml'
171 '/issues/6.xml',
172 {:issue => {:subject => 'API update', :notes => 'A new note'}},
173 {:success_code => :ok})
174
174
175 should "not create a new issue" do
175 should "not create a new issue" do
176 assert_equal Issue.count, @issue_count
176 assert_no_difference('Issue.count') do
177 put '/issues/6.xml', @parameters, @headers
178 end
177 end
179 end
178
180
179 should "create a new journal" do
181 should "create a new journal" do
180 assert_equal Journal.count, @journal_count + 1
182 assert_difference('Journal.count') do
183 put '/issues/6.xml', @parameters, @headers
184 end
181 end
185 end
182
186
183 should "add the note to the journal" do
187 should "add the note to the journal" do
188 put '/issues/6.xml', @parameters, @headers
189
184 journal = Journal.last
190 journal = Journal.last
185 assert_equal "A new note", journal.notes
191 assert_equal "A new note", journal.notes
186 end
192 end
187
193
188 should "update the issue" do
194 should "update the issue" do
189 issue = Issue.find(1)
195 put '/issues/6.xml', @parameters, @headers
190 @attributes.each do |attribute, value|
196
191 assert_equal value, issue.send(attribute) unless attribute == :notes
197 issue = Issue.find(6)
192 end
198 assert_equal "API update", issue.subject
193 end
199 end
194
200
195 end
201 end
196
202
197 context "PUT /issues/1.xml with failed update" do
203 context "PUT /issues/6.xml with failed update" do
198 setup do
204 setup do
199 @attributes = {:subject => ''}
205 @parameters = {:issue => {:subject => ''}}
200 @issue_count = Issue.count
206 @headers = { :authorization => credentials('jsmith') }
201 @journal_count = Journal.count
202
203 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
204 end
207 end
205
208
206 should_respond_with :unprocessable_entity
209 should_allow_api_authentication(:put,
207 should_respond_with_content_type 'application/xml'
210 '/issues/6.xml',
208
211 {:issue => {:subject => ''}}, # Missing subject should fail
212 {:success_code => :unprocessable_entity})
213
209 should "not create a new issue" do
214 should "not create a new issue" do
210 assert_equal Issue.count, @issue_count
215 assert_no_difference('Issue.count') do
216 put '/issues/6.xml', @parameters, @headers
217 end
211 end
218 end
212
219
213 should "not create a new journal" do
220 should "not create a new journal" do
214 assert_equal Journal.count, @journal_count
221 assert_no_difference('Journal.count') do
222 put '/issues/6.xml', @parameters, @headers
223 end
215 end
224 end
216
225
217 should "have an errors tag" do
226 should "have an errors tag" do
227 put '/issues/6.xml', @parameters, @headers
228
218 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
229 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
219 end
230 end
220 end
231 end
221
232
222 context "PUT /issues/1.json" do
233 context "PUT /issues/6.json" do
223 setup do
234 setup do
224 @issue_count = Issue.count
235 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
225 @journal_count = Journal.count
236 @headers = { :authorization => credentials('jsmith') }
226 @attributes = {:subject => 'API update', :notes => 'A new note'}
227
228 put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
229 end
237 end
230
238
231 should_respond_with :ok
239 should_allow_api_authentication(:put,
232 should_respond_with_content_type 'application/json'
240 '/issues/6.json',
241 {:issue => {:subject => 'API update', :notes => 'A new note'}},
242 {:success_code => :ok})
233
243
234 should "not create a new issue" do
244 should "not create a new issue" do
235 assert_equal Issue.count, @issue_count
245 assert_no_difference('Issue.count') do
246 put '/issues/6.json', @parameters, @headers
247 end
236 end
248 end
237
249
238 should "create a new journal" do
250 should "create a new journal" do
239 assert_equal Journal.count, @journal_count + 1
251 assert_difference('Journal.count') do
252 put '/issues/6.json', @parameters, @headers
253 end
240 end
254 end
241
255
242 should "add the note to the journal" do
256 should "add the note to the journal" do
257 put '/issues/6.json', @parameters, @headers
258
243 journal = Journal.last
259 journal = Journal.last
244 assert_equal "A new note", journal.notes
260 assert_equal "A new note", journal.notes
245 end
261 end
246
262
247 should "update the issue" do
263 should "update the issue" do
248 issue = Issue.find(1)
264 put '/issues/6.json', @parameters, @headers
249 @attributes.each do |attribute, value|
265
250 assert_equal value, issue.send(attribute) unless attribute == :notes
266 issue = Issue.find(6)
251 end
267 assert_equal "API update", issue.subject
252 end
268 end
253
269
254 end
270 end
255
271
256 context "PUT /issues/1.json with failed update" do
272 context "PUT /issues/6.json with failed update" do
257 setup do
273 setup do
258 @attributes = {:subject => ''}
274 @parameters = {:issue => {:subject => ''}}
259 @issue_count = Issue.count
275 @headers = { :authorization => credentials('jsmith') }
260 @journal_count = Journal.count
261
262 put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
263 end
276 end
264
277
265 should_respond_with :unprocessable_entity
278 should_allow_api_authentication(:put,
266 should_respond_with_content_type 'application/json'
279 '/issues/6.json',
267
280 {:issue => {:subject => ''}}, # Missing subject should fail
281 {:success_code => :unprocessable_entity})
282
268 should "not create a new issue" do
283 should "not create a new issue" do
269 assert_equal Issue.count, @issue_count
284 assert_no_difference('Issue.count') do
285 put '/issues/6.json', @parameters, @headers
286 end
270 end
287 end
271
288
272 should "not create a new journal" do
289 should "not create a new journal" do
273 assert_equal Journal.count, @journal_count
290 assert_no_difference('Journal.count') do
291 put '/issues/6.json', @parameters, @headers
292 end
274 end
293 end
275
294
276 should "have an errors attribute" do
295 should "have an errors attribute" do
296 put '/issues/6.json', @parameters, @headers
297
277 json = ActiveSupport::JSON.decode(response.body)
298 json = ActiveSupport::JSON.decode(response.body)
278 assert_equal "can't be blank", json.first['subject']
299 assert_equal "can't be blank", json.first['subject']
279 end
300 end
@@ -401,8 +401,8 class ActiveSupport::TestCase
401
401
402 # Checks that the response is a valid JSON string
402 # Checks that the response is a valid JSON string
403 def self.should_be_a_valid_json_string
403 def self.should_be_a_valid_json_string
404 should "be a valid JSON string" do
404 should "be a valid JSON string (or empty)" do
405 assert ActiveSupport::JSON.decode(response.body)
405 assert (response.body.blank? || ActiveSupport::JSON.decode(response.body))
406 end
406 end
407 end
407 end
408
408
General Comments 0
You need to be logged in to leave comments. Login now