测试After_authentication以实施监狱

发布于 2025-02-07 21:04:27 字数 342 浏览 1 评论 0 原文

身份验证后我有一个自定义的工作流程,我想对其进行测试。

在初始评估器上,我发现了一些

Warden::Manager.after_authentication do |user, auth, _opts|
  next unless user.ban?

  auth.logout

  throw(:warden, :message => "You're currently ban. Impossible to connect")
end

我认为它已读取了整个文档,而没有找到正确的测试方法。有什么想法吗?

I've a custom workflow after authentication and I would like to test it.

On initialiser I got something like

Warden::Manager.after_authentication do |user, auth, _opts|
  next unless user.ban?

  auth.logout

  throw(:warden, :message => "You're currently ban. Impossible to connect")
end

I think it's read the whole documentation without finding a correct way to test it. Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福丶如此 2025-02-14 21:04:27

我终于找到了一种方法。并不是那么明显。让我与您分享。

根据源

after_authentication只是after_set_user的包装器,只有在通过身份验证路径设置用户时才调用。选项和产生的参数与after_set_user中的参数相同。

所以这是我的解决方法。

describe Warden::Manager do
  describe '#after_authentication' do
    # add here the name of your config file, it's very important
    let(:intializer_file_location) { 'config/initializers/warden.rb' }

    subject do
      after_authentication_proc.call(user, double_auth, opts)
    end

    let(:after_authentication_proc) do
      p, _options = described_class._after_set_user.find do |p, opts|
        opts[:event] == :authentication &&
          p.source_location.first.include?(intializer_file_location)
      end
      p
    end

    let(:user) { create(:user) }
    let(:opts) { {} }
    let(:double_auth) { double(:double_auth) }

    it 'does nothing' do
      expect(subject).to be_nil
    end
  end
end

享受测试!

I finally found a way. Not that obvious. Let me share it with you.

According to the source warden/hooks.rb#L71-L78

after_authentication is just a wrapper to after_set_user, which is only invoked when the user is set through the authentication path. The options and yielded arguments are the same as in after_set_user.

So here is my workaround.

describe Warden::Manager do
  describe '#after_authentication' do
    # add here the name of your config file, it's very important
    let(:intializer_file_location) { 'config/initializers/warden.rb' }

    subject do
      after_authentication_proc.call(user, double_auth, opts)
    end

    let(:after_authentication_proc) do
      p, _options = described_class._after_set_user.find do |p, opts|
        opts[:event] == :authentication &&
          p.source_location.first.include?(intializer_file_location)
      end
      p
    end

    let(:user) { create(:user) }
    let(:opts) { {} }
    let(:double_auth) { double(:double_auth) }

    it 'does nothing' do
      expect(subject).to be_nil
    end
  end
end

Enjoy testing! ????

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文