##// END OF EJS Templates
Allow key authentication when creating issues (with tests) #6447...
Eric Davis -
r4251:4b1dd334a596
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
30 accept_key_auth :index, :show, :create
31 31
32 32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 33
@@ -91,69 +91,70 class ApiTest::IssuesTest < ActionController::IntegrationTest
91 91 end
92 92
93 93 context "POST /issues.xml" do
94 setup do
95 @issue_count = Issue.count
96 @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
97 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
98 end
99
100 should_respond_with :created
101 should_respond_with_content_type 'application/xml'
94 should_allow_api_authentication(:post,
95 '/issues.xml',
96 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
97 {:success_code => :created})
102 98
103 99 should "create an issue with the attributes" do
104 assert_equal Issue.count, @issue_count + 1
105
106 issue = Issue.first(:order => 'id DESC')
107 @attributes.each do |attribute, value|
108 assert_equal value, issue.send(attribute)
100 assert_difference('Issue.count') do
101 post '/issues.xml', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith')
109 102 end
103
104 issue = Issue.first(:order => 'id DESC')
105 assert_equal 1, issue.project_id
106 assert_equal 2, issue.tracker_id
107 assert_equal 3, issue.status_id
108 assert_equal 'API test', issue.subject
110 109 end
111 110 end
112 111
113 112 context "POST /issues.xml with failure" do
114 setup do
115 @attributes = {:project_id => 1}
116 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
117 end
118
119 should_respond_with :unprocessable_entity
120 should_respond_with_content_type 'application/xml'
113 should_allow_api_authentication(:post,
114 '/issues.xml',
115 {:issue => {:project_id => 1}},
116 {:success_code => :unprocessable_entity})
121 117
122 118 should "have an errors tag" do
119 assert_no_difference('Issue.count') do
120 post '/issues.xml', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith')
121 end
122
123 123 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
124 124 end
125 125 end
126 126
127 127 context "POST /issues.json" do
128 setup do
129 @issue_count = Issue.count
130 @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
131 post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
132 end
133
134 should_respond_with :created
135 should_respond_with_content_type 'application/json'
128 should_allow_api_authentication(:post,
129 '/issues.json',
130 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
131 {:success_code => :created})
136 132
137 133 should "create an issue with the attributes" do
138 assert_equal Issue.count, @issue_count + 1
139
140 issue = Issue.first(:order => 'id DESC')
141 @attributes.each do |attribute, value|
142 assert_equal value, issue.send(attribute)
134 assert_difference('Issue.count') do
135 post '/issues.json', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith')
143 136 end
137
138 issue = Issue.first(:order => 'id DESC')
139 assert_equal 1, issue.project_id
140 assert_equal 2, issue.tracker_id
141 assert_equal 3, issue.status_id
142 assert_equal 'API test', issue.subject
144 143 end
144
145 145 end
146 146
147 147 context "POST /issues.json with failure" do
148 setup do
149 @attributes = {:project_id => 1}
150 post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
151 end
152
153 should_respond_with :unprocessable_entity
154 should_respond_with_content_type 'application/json'
148 should_allow_api_authentication(:post,
149 '/issues.json',
150 {:issue => {:project_id => 1}},
151 {:success_code => :unprocessable_entity})
155 152
156 153 should "have an errors element" do
154 assert_no_difference('Issue.count') do
155 post '/issues.json', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith')
156 end
157
157 158 json = ActiveSupport::JSON.decode(response.body)
158 159 assert_equal "can't be blank", json.first['subject']
159 160 end
@@ -195,10 +195,13 class ActiveSupport::TestCase
195 195 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
196 196 # @param [String] url the request url
197 197 # @param [optional, Hash] parameters additional request parameters
198 def self.should_allow_api_authentication(http_method, url, parameters={})
199 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters)
200 should_allow_http_basic_auth_with_key(http_method, url, parameters)
201 should_allow_key_based_auth(http_method, url, parameters)
198 # @param [optional, Hash] options additional options
199 # @option options [Symbol] :success_code Successful response code (:success)
200 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
201 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
202 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
203 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
204 should_allow_key_based_auth(http_method, url, parameters, options)
202 205 end
203 206
204 207 # Test that a request allows the username and password for HTTP BASIC
@@ -206,7 +209,13 class ActiveSupport::TestCase
206 209 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
207 210 # @param [String] url the request url
208 211 # @param [optional, Hash] parameters additional request parameters
209 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={})
212 # @param [optional, Hash] options additional options
213 # @option options [Symbol] :success_code Successful response code (:success)
214 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
215 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
216 success_code = options[:success_code] || :success
217 failure_code = options[:failure_code] || :unauthorized
218
210 219 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
211 220 context "with a valid HTTP authentication" do
212 221 setup do
@@ -215,7 +224,7 class ActiveSupport::TestCase
215 224 send(http_method, url, parameters, {:authorization => @authorization})
216 225 end
217 226
218 should_respond_with :success
227 should_respond_with success_code
219 228 should_respond_with_content_type_based_on_url(url)
220 229 should "login as the user" do
221 230 assert_equal @user, User.current
@@ -229,7 +238,7 class ActiveSupport::TestCase
229 238 send(http_method, url, parameters, {:authorization => @authorization})
230 239 end
231 240
232 should_respond_with :unauthorized
241 should_respond_with failure_code
233 242 should_respond_with_content_type_based_on_url(url)
234 243 should "not login as the user" do
235 244 assert_equal User.anonymous, User.current
@@ -241,7 +250,7 class ActiveSupport::TestCase
241 250 send(http_method, url, parameters, {:authorization => ''})
242 251 end
243 252
244 should_respond_with :unauthorized
253 should_respond_with failure_code
245 254 should_respond_with_content_type_based_on_url(url)
246 255 should "include_www_authenticate_header" do
247 256 assert @controller.response.headers.has_key?('WWW-Authenticate')
@@ -256,7 +265,13 class ActiveSupport::TestCase
256 265 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
257 266 # @param [String] url the request url
258 267 # @param [optional, Hash] parameters additional request parameters
259 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={})
268 # @param [optional, Hash] options additional options
269 # @option options [Symbol] :success_code Successful response code (:success)
270 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
271 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
272 success_code = options[:success_code] || :success
273 failure_code = options[:failure_code] || :unauthorized
274
260 275 context "should allow http basic auth with a key for #{http_method} #{url}" do
261 276 context "with a valid HTTP authentication using the API token" do
262 277 setup do
@@ -266,7 +281,7 class ActiveSupport::TestCase
266 281 send(http_method, url, parameters, {:authorization => @authorization})
267 282 end
268 283
269 should_respond_with :success
284 should_respond_with success_code
270 285 should_respond_with_content_type_based_on_url(url)
271 286 should_be_a_valid_response_string_based_on_url(url)
272 287 should "login as the user" do
@@ -282,7 +297,7 class ActiveSupport::TestCase
282 297 send(http_method, url, parameters, {:authorization => @authorization})
283 298 end
284 299
285 should_respond_with :unauthorized
300 should_respond_with failure_code
286 301 should_respond_with_content_type_based_on_url(url)
287 302 should "not login as the user" do
288 303 assert_equal User.anonymous, User.current
@@ -296,7 +311,13 class ActiveSupport::TestCase
296 311 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
297 312 # @param [String] url the request url, without the key=ZXY parameter
298 313 # @param [optional, Hash] parameters additional request parameters
299 def self.should_allow_key_based_auth(http_method, url, parameters={})
314 # @param [optional, Hash] options additional options
315 # @option options [Symbol] :success_code Successful response code (:success)
316 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
317 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
318 success_code = options[:success_code] || :success
319 failure_code = options[:failure_code] || :unauthorized
320
300 321 context "should allow key based auth using key=X for #{http_method} #{url}" do
301 322 context "with a valid api token" do
302 323 setup do
@@ -311,7 +332,7 class ActiveSupport::TestCase
311 332 send(http_method, request_url, parameters)
312 333 end
313 334
314 should_respond_with :success
335 should_respond_with success_code
315 336 should_respond_with_content_type_based_on_url(url)
316 337 should_be_a_valid_response_string_based_on_url(url)
317 338 should "login as the user" do
@@ -323,10 +344,16 class ActiveSupport::TestCase
323 344 setup do
324 345 @user = User.generate_with_protected!
325 346 @token = Token.generate!(:user => @user, :action => 'feeds')
326 send(http_method, url + "?key=#{@token.value}")
347 # Simple url parse to add on ?key= or &key=
348 request_url = if url.match(/\?/)
349 url + "&key=#{@token.value}"
350 else
351 url + "?key=#{@token.value}"
352 end
353 send(http_method, request_url, parameters)
327 354 end
328 355
329 should_respond_with :unauthorized
356 should_respond_with failure_code
330 357 should_respond_with_content_type_based_on_url(url)
331 358 should "not login as the user" do
332 359 assert_equal User.anonymous, User.current
General Comments 0
You need to be logged in to leave comments. Login now