当我在Rails中发出POST请求但会在测试期间更改会话ID

时间:2016-01-29 12:00:44

标签: ruby-on-rails ruby ruby-on-rails-4 csrf rspec-rails

我在link_tomethod: :post触发的POST请求中遇到了一个相当奇怪的问题。在测试环境中,session_id似乎发生了变化。这导致诸如current_user对象之类的问题也不存在于我发布的操作中。我已经注销了请求和会话信息,我可以看到POST操作的会话已经更改,当我尝试使用current_user时,测试失败。

我通过应用程序周围的表单有其他POST请求。他们工作得很好。它似乎是link_to method: :post周围的一些Rails魔法并且传递了CSRF令牌。

我可以通过将test.rb更改为与development.rb相同来解决此问题。但我确定这不是一个好的解决方案。它可能与某些配置相关,但似乎这是默认行为。

控制器

class RecruitersController < ApplicationController
  before_action -> { STDOUT.puts "Request: #{request.method} #{request.fullpath}" }
  before_action -> { STDOUT.puts "Session: #{session[:session_id]}" }

  ...
end    

触发POST请求的按钮

= link_to "<3", recruiter_request_url(id: recruiter.id), method: :post, remote: true

测试中的输出

Request: GET /recruiters/dashboard
Session: ee8c577fdf6d1714c2a837f0890e0294
Request: GET /recruiters/premium
Session: ee8c577fdf6d1714c2a837f0890e0294
Request: POST /recruiters/request_premium_trial/1
Session: 314c6eef0156aa36a469a4f9ea7513a8

开发中的输出

Request: GET /recruiters/dashboard
Session: cdb333efb5d62e6ddbb5914c8edd7a92
Request: GET /recruiters/premium
Session: cdb333efb5d62e6ddbb5914c8edd7a92
Request: POST /recruiters/request_premium_trial/1
Session: cdb333efb5d62e6ddbb5914c8edd7a92

规范 简单的规范用户登录进入仪表板,进入高级页面,然后单击发出POST请求的链接。

scenario 'Should be able to make request', js:true do
  rsign_in # Function that simulates sign in
  click_on 'Premium'
  click_on '<3'
  assert_text 'Request made' # Fails as we're redirected to sign in page when we try to authenticate the user
end

Test.rb

Rails.application.configure do
  # Set log level
  config.log_level = :debug

  # This means that all URLs need to have 5 parts to them. This is for http://recruiter.127.0.0.1.xip.io:3000
  config.action_dispatch.tld_length = 5
  # Settings specified here will take precedence over those in config/application.rb.

  cache_store = :file_store, "tmp/cache"
  # Use a different job queue
  config.active_job.queue_adapter = Figaro.env.job_queue.to_sym if Figaro.env.job_queue?

  # The test environment is used exclusively to run your application's
  # test suite. You never need to work with it otherwise. Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs. Don't rely on the data there!
  config.cache_classes = true

  # Do not eager load code on boot. This avoids loading your whole application
  # just for the purpose of running a single test. If you are using a tool that
  # preloads Rails for running tests, you may have to set it to true.
  config.eager_load = true

  # Configure static asset server for tests with Cache-Control for performance.
  config.serve_static_files  = true
  config.static_cache_control = 'public, max-age=3600'

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Raise exceptions instead of rendering exception templates.
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment.
  config.action_controller.allow_forgery_protection = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_controller.default_url_options = { host: 'localhost:5000' }
  config.action_mailer.default_url_options = { host: 'localhost:5000' }
  config.action_mailer.delivery_method = :test
  # config.action_mailer.delivery_method = :smtp
  # config.action_mailer.smtp_settings = { address: 'localhost', port: '1025' }
  # Print deprecation notices to the stderr.
  config.active_support.deprecation = :stderr

  config.active_record.raise_in_transactional_callbacks = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
end

Development.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.preview_path = "#{Rails.root}/app/mailers/previews"

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load
  config.action_mailer.default_url_options = { host: 'localhost:3000' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = { address: 'localhost', port: '1025' }

  config.cache_store = :dalli_store
  # Use a different job queue
  config.active_job.queue_adapter = Figaro.env.job_queue.to_sym if Figaro.env.job_queue?

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  config.after_initialize do
    Bullet.enable = false
    Bullet.alert = true
    Bullet.console = true
    Bullet.rails_logger = true
  end
end

1 个答案:

答案 0 :(得分:1)

似乎错误来自我test.rb中的这一行,只是试图将我的链接上的默认主机设置为不是example.com。不知道这是如何导致我遇到的错误。但是在完成配置并试图弄清楚为什么development.rb正在工作之后。这就是我得到的。

config.action_controller.default_url_options = { host: 'localhost:5000' }

感谢所有帮助过我的人。