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