如何在停产的私有资源页面上测试预期的404响应?

时间:2018-11-10 20:04:39

标签: ruby-on-rails

我有一个应用程序,用户是名为“活页夹”的对象的作者。这是一个私有资源,除非共享它,否则一个用户不能查看另一用户的活页夹。每当用户尝试执行此类操作时,我都希望将我的应用程序添加到404。到目前为止,这是我尝试过的。

class BindersController < ApplicationController
  before_action :authenticate_user!
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_access, only: [:show, :edit, :update, :destroy]

  # ...

  private
    def authenticate_access
      if current_user != @binder.user
        respond_to do |format|
          format.html { head :missing }
          format.json { head :missing }
        end
      end
    end
end

我的问题是,Rails在开发和测试中更喜欢500。这使得无法通过以下测试来检查。

class BindersControllerTest < ActionDispatch::IntegrationTest
  include Warden::Test::Helpers

  setup do
    @alices_binder = binders(:alices_binder)
    @alice = users(:alice)
    @eve = users(:eve)
  end

  teardown do
    Warden.test_reset!
  end

  test 'binders#show should be missing if accessed by wrong user'
    login_as @eve, scope: :user
    get binder_url(@alices_binder)
    assert_response :missing
  end
end

我该如何正确地停止生产这种行为?

0 个答案:

没有答案