Commit 4734ba1b2ee255d77f5c6110981f05ae87974450

Authored by Daniel
1 parent b2d7c543
Exists in colab and in 2 other branches master, stable

Update header and request handling in tests

The repository/notify_push feature was making post request directly
using Rack::Test methods, which is not optimal when we have Capybara to
handle all the driver setup and flexibility for us.

Change it to use Capybara methods, and fix all the steps accordingly. To
make things easier, add some helper methods to deal with headers in
different drivers.

Also fix a small clarity issue in the notify_push unity tests that also
have to set headers.
features/step_definitions/repository_steps.rb
@@ -168,18 +168,14 @@ end @@ -168,18 +168,14 @@ end
168 168
169 When(/^I push some commits to the repository$/) do 169 When(/^I push some commits to the repository$/) do
170 request = FactoryGirl.build(:gitlab_webhook_request) 170 request = FactoryGirl.build(:gitlab_webhook_request)
171 - request.headers.each do |k, v|  
172 - header k, v  
173 - end  
174 - @response = post repository_notify_push_path(id: @repository.id), request.params 171 + set_headers(request.headers)
  172 + page.driver.post(repository_notify_push_path(id: @repository.id), request.params)
175 end 173 end
176 174
177 When(/^I push some commits to an invalid repository$/) do 175 When(/^I push some commits to an invalid repository$/) do
178 request = FactoryGirl.build(:gitlab_webhook_request) 176 request = FactoryGirl.build(:gitlab_webhook_request)
179 - request.headers.each do |k, v|  
180 - header k, v  
181 - end  
182 - @response = post repository_notify_push_path(id: 0), request.params 177 + set_headers(request.headers)
  178 + page.driver.post(repository_notify_push_path(id: 0), request.params)
183 end 179 end
184 180
185 Then(/^I should see the sample metric's name$/) do 181 Then(/^I should see the sample metric's name$/) do
@@ -283,5 +279,5 @@ Then(/^Mezuro should process the repository again$/) do @@ -283,5 +279,5 @@ Then(/^Mezuro should process the repository again$/) do
283 end 279 end
284 280
285 Then(/^I should get a not found error$/) do 281 Then(/^I should get a not found error$/) do
286 - expect(@response.status).to eq(404) 282 + expect(page.driver.status_code).to eq(404)
287 end 283 end
features/support/env.rb
@@ -20,8 +20,11 @@ end @@ -20,8 +20,11 @@ end
20 # instead of editing this one. Cucumber will automatically load all features/**/*.rb 20 # instead of editing this one. Cucumber will automatically load all features/**/*.rb
21 # files. 21 # files.
22 22
  23 +require 'warden'
23 require 'cucumber/rails' 24 require 'cucumber/rails'
24 require 'capybara/poltergeist' 25 require 'capybara/poltergeist'
  26 +require_relative 'header'
  27 +
25 #Capybara.default_driver = :poltergeist 28 #Capybara.default_driver = :poltergeist
26 Capybara.javascript_driver = :poltergeist 29 Capybara.javascript_driver = :poltergeist
27 Capybara.default_max_wait_time = 20 # default is 2 seconds 30 Capybara.default_max_wait_time = 20 # default is 2 seconds
@@ -81,5 +84,6 @@ Cucumber::Rails::Database.javascript_strategy = :truncation @@ -81,5 +84,6 @@ Cucumber::Rails::Database.javascript_strategy = :truncation
81 # KalibroClient hooks 84 # KalibroClient hooks
82 require 'kalibro_client/kalibro_cucumber_helpers/hooks.rb' 85 require 'kalibro_client/kalibro_cucumber_helpers/hooks.rb'
83 86
84 -# Warden test helpers so the user authentication can be as fast as possible  
85 -include Warden::Test::Helpers 87 +Warden.test_mode!
  88 +World(Warden::Test::Helpers, HeaderUtils)
  89 +
features/support/header.rb 0 → 100644
@@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
  1 +module HeaderUtils
  2 + def set_header(key, value)
  3 + header_method = nil
  4 + if defined?(page) && ! page.driver.nil?
  5 + header_method = [:add_header, :header].find(&page.driver.method(:respond_to?))
  6 + end
  7 +
  8 + raise StandardError.new("No header setting method available in current driver: #{page.driver}") unless header_method
  9 + page.driver.send(header_method, key, value)
  10 + end
  11 +
  12 + def set_headers(headers)
  13 + headers.each(&method(:set_header))
  14 + end
  15 +end
spec/controllers/repositories_controller_spec.rb
@@ -475,10 +475,7 @@ describe RepositoriesController, :type => :controller do @@ -475,10 +475,7 @@ describe RepositoriesController, :type => :controller do
475 let(:webhook_request) { FactoryGirl.build(:gitlab_webhook_request) } 475 let(:webhook_request) { FactoryGirl.build(:gitlab_webhook_request) }
476 476
477 def post_push 477 def post_push
478 - webhook_request.headers.each do |k, v|  
479 - @request.headers[k] = v  
480 - end  
481 - 478 + @request.headers.merge!(webhook_request.headers)
482 post :notify_push, {id: repository.id, format: :json}.merge(webhook_request.params) 479 post :notify_push, {id: repository.id, format: :json}.merge(webhook_request.params)
483 end 480 end
484 481