使用 rspec 测试自定义设计和/或 Warden 策略

发布于 2024-12-14 12:59:59 字数 248 浏览 8 评论 0原文

我看到很多导致 devise 和 Warden 定制授权策略的内容,但我具体要使用 rspec 测试这些解决方案。与此问题类似: 筛选能够签名的用户在 Devise

我可以做什么来测试这种实现。 Rails 3.1.1、Devise(最新)等

I see plenty that lead to custom authorization strategies for devise and warden, but what I'm sepcifically after is testing these solutions with rspec. Similar to this question: Filtering users who are able to sign in with Devise

What can I do to test this sort of implementation. Rails 3.1.1, Devise (most current), etc.

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

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

发布评论

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

评论(1

叹梦 2024-12-21 12:59:59

对于那些将来可能这样做的人,这是我的解决方案:

这个类通过 Devise 设置新的身份验证策略(并且它也可以与 Warden 一起使用,只需进行一些小的更改)。

require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class AndroidAuthenticatable < Authenticatable
      def valid? 
        # return true/false
                return valid_params? && valid_headers?
      end 

      def authenticate! 
        failure_message = "Authentication failed for device/user"

        klass = mapping.to # if we're going to come here, we can mock this
        begin
          # code to determine whether or not to authenticate this request
          # if yes, success!(instance of klass)
          # if no, fail!(messsage)
        rescue
          fail!(failure_message) # always fail if not success
        end
      end 

      protected
      def valid_params?
        # params that show whether request should be here
      end

      def valid_headers?
        # headers that determine if request should be here
      end
    end
  end
end

上一课位于我的 lib/.../strategies 目录中。我还配置了 lib,以便通过 Rails 配置自动加载。

从 rspec 方面来看,在创建上述类之后,我写了一些存根/模拟。这是一个可以帮助您入门的基本 rspec 文件。

# I do this in before block or right before test executes
@request = mock(:request)
@strategy = Devise::Strategies::AndroidAuthenticatable.new(nil)
@request.should_receive(:headers).and_return({#hash of the headers you are testing})
@strategy.should_receive(:params).at_least(:once).and_return({#hash of the params})
@strategy.should_receive(:request).and_return(@request)

# Break these up as needed to test failing and successful 
# strategies for your application
lambda {
   @strategy.should be_valid
   @strategy.authenticate!.should eql :success 
}.should_not raise_error

这并不包含所有内容,但我认为它应该让我们在使用 Warden 或 Devise 添加策略时拥有良好的开端。实际上,我必须实施我认为可行的方法,然后进行正确的测试以在事后证明它。现在我们也许可以反过来做。

For those that may do this in the future, here is my solution:

This is the class that sets a new strategy for authentication through Devise (and it could also be used with Warden with a few small changes).

require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class AndroidAuthenticatable < Authenticatable
      def valid? 
        # return true/false
                return valid_params? && valid_headers?
      end 

      def authenticate! 
        failure_message = "Authentication failed for device/user"

        klass = mapping.to # if we're going to come here, we can mock this
        begin
          # code to determine whether or not to authenticate this request
          # if yes, success!(instance of klass)
          # if no, fail!(messsage)
        rescue
          fail!(failure_message) # always fail if not success
        end
      end 

      protected
      def valid_params?
        # params that show whether request should be here
      end

      def valid_headers?
        # headers that determine if request should be here
      end
    end
  end
end

The previous class is in my lib/.../strategies directory. I also have lib configured for auto-loading through the rails configuration.

From the rspec side, after I created the above class I write out a few stubs/mocks. Here is a basic rspec file to get you started.

# I do this in before block or right before test executes
@request = mock(:request)
@strategy = Devise::Strategies::AndroidAuthenticatable.new(nil)
@request.should_receive(:headers).and_return({#hash of the headers you are testing})
@strategy.should_receive(:params).at_least(:once).and_return({#hash of the params})
@strategy.should_receive(:request).and_return(@request)

# Break these up as needed to test failing and successful 
# strategies for your application
lambda {
   @strategy.should be_valid
   @strategy.authenticate!.should eql :success 
}.should_not raise_error

This isn't all inclusive, but I feel it should get us a good head start when adding strategies with Warden or Devise. I actually had to implement what I thought would work and then right tests to prove it after the fact. Now we can do it the other way around perhaps.

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