我有一个多租户应用程序,它使用Apartment用于postgreSQL模式,使用Devise用于用户身份验证。在我尝试编写一些集成测试之前,一切都运行顺利。
以下是我到目前为止的精简版本(随时询问更多信息):
# test/support/sign_in_helper.rb
module SignInHelper
def sign_in_as(name)
sign_in users(name)
end
end
# test/test_helper.rb
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
#...
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
include SignInHelper
end
# test/system/article/post_test.rb
require "application_system_test_case"
class Article::PostTest < ApplicationSystemTestCase
test 'post a new document' do
sign_in_as :will
visit articles_path
click_on 'Add New Article' # redirected to login page after clicking this button
fill_in 'Name', with: 'Hello world!'
fill_in 'Content', with: 'Yes yes'
click_on 'Create Article'
assert_select 'h1', /Hello world!/
end
end
articles_path
需要经过身份验证的用户,因此我知道登录帮助程序有效。但是,每当我尝试转到另一个链接时,突然用户就无法通过身份验证。
我像这样修补了Devise的authenticate_user!
方法:
def authenticate_user!(*args)
byebug
super
end
并确认warden.authenticated?
返回了true
的{{1}},但返回了articles_path
,而随后又尝试导航到false
。
我已经在集成测试类型(控制器和系统)中注意到了此行为。但是,在开发环境中使用此应用程序时这不是问题。
最令人沮丧的部分是,我有一个似乎与此应用程序设置相同的其他应用程序,但是在测试时不会出现此身份验证问题。
我该如何调试此问题?
系统
更新1(2019年2月4日)
这是@BKSpurgeon要求的Articles控制器
new_article_path
更新2(2019年2月4日)
我编写了简单的中间件,并将其放置在Warden之前进行调试:
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :archive]
def index
@articles = Article.where(archived: false)
end
def show
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article, notice: 'Your article was successfully created.'
else
flash[:error] = @article.errors.full_messages.to_sentence
render :new
end
end
def edit
end
def update
if @article.update(article_params)
redirect_to articles_path, notice: 'Your article was successfully updated.'
else
flash[:error] = @article.errors.full_messages.to_sentence
render :edit
end
end
def archive
if @article.archive!
redirect_to articles_path, notice: 'Your article was successfully archived.'
else
render :edit
end
end
private
def set_article
@article = Article.find(params[:id])
end
def article_params
params.require(:article).permit(:name, :content, :archived)
end
end
而且我注意到,监护人似乎在每次请求(包括资产请求)后都会清除其用户:
# lib/debug_warden_middleware.rb
class DebugWardenMiddleware
def initialize(app)
@app = app
end
def call(env)
@status, @headers, @response = @app.call(env)
if env['warden'].present?
puts "User (#{env['warden'].user.present?}), Class: #{@response.class.name}"
end
return [@status, @headers, @response]
end
end
# config/application.rb
#...
module AppName
class Application < Rails::Application
# ...
config.middleware.insert_before Warden::Manager, DebugWardenMiddleware
end
end
请注意,我同时使用了Sprockets和Webpacker。
答案 0 :(得分:1)
好吧,在@BKSpurgeon(在聊天消息中)和@Old Pro都建议我尝试在单独的应用程序中重现问题以与大家分享之后,我迷失了答案。
因为此应用是多租户的,并且使用了Apartment gem,所以我的测试设置有些复杂。不幸的是,我认为其中的设置不相关(big mistake, huge!),因此未包含其中的某些设置。直到我尝试重新创建应用程序时,我才注意到该缺陷。这是我正在使用的测试设置:
scan()
...
# test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
# support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
# Apartment setup
Apartment::Tenant.drop('sample') rescue nil
Apartment::Tenant.create('sample') rescue nil
Apartment::Tenant.switch! 'sample'
# Capybara setup
Capybara.configure do |config|
config.default_host = 'http://lvh.me'
config.always_include_port = true
end
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
include SubdomainHelper
include SignInHelper # dependent on SubomainHelper
setup do
default_url_options[:host] = 'lvh.me'
Apartment::Tenant.switch! 'public'
end
end
...
# test/support/sign_in_helper.rb
module SignInHelper
def sign_in_as(name)
# This right here was the problem code
user = users(name)
use_account user.account
sign_in user
end
end
...
# test/support/subdomain_helper.rb
module SubdomainHelper
def use_account(account)
account.save unless account.persisted?
default_url_options[:host] = "#{account.subdomain}.lvh.me"
Capybara.app_host = "http://#{account.subdomain}.lvh.me"
Apartment::Tenant.switch! account.subdomain
end
end
最终的问题是,在我实际切换到包含该用户的租户之前,我试图在# test/system/article/post_test.rb
require "application_system_test_case"
class Article::PostTest < ApplicationSystemTestCase
test 'post a new document' do
sign_in_as :will
visit articles_path
click_on 'Add New Article' # redirected to login page after clicking this button
fill_in 'Name', with: 'Hello world!'
fill_in 'Content', with: 'Yes yes'
click_on 'Create Article'
assert_select 'h1', /Hello world!/
end
end
中抓住该用户。有了这些知识,我将代码修改为如下所示:
SignInHelper
...
# test/support/sign_in_helper.rb
module SignInHelper
def sign_in_as(name, account)
use_account accounts(account)
sign_in users(name)
end
end
现在会话仍在继续。仍然令我困惑的一件事是,为什么我根本无法登录?我已经将其归纳为挥之不去的测试数据,但这比其他任何事情都更重要。