【发布时间】:2016-05-07 03:52:55
【问题描述】:
在link_to 和method: :post 触发的POST 请求中,我遇到了一个相当奇怪的问题。在测试环境中,session_id 似乎发生了变化。这会导致诸如 current_user 对象在我发布的操作中也不存在等问题。我已经注销了请求和会话信息,我可以看到 POST 操作的会话已更改,当我尝试使用 current_user 时测试失败。
我通过应用程序周围的表单收到其他 POST 请求。他们工作正常。 link_to 和 method: :post 并传递 CSRF 令牌似乎是一些 Rails 魔法。
我可以通过将我的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
【问题讨论】:
-
能不能把相关的
spec加进去 -
@Marwen 我已经添加了它。这很简单。只需转到页面并单击链接。我也添加了
test.rb,以防是配置问题。 -
您的规范中有错字。将
rsign_in更改为sign_in。 -
@NeilAtkinson
rsign_in是我正在使用的一个模拟招聘人员登录过程的函数。我的主要问题是单击链接后发出的 POST 请求。会话由于某种原因被重置,从而导致身份验证失败。在那里发表评论以表明相同。 -
当您单击“
标签: ruby-on-rails ruby ruby-on-rails-4 csrf rspec-rails