Sha256: 8d4a5db949c8a909cde6c61d1d87a8b453e2caa674c7b1cfb6c37f1d0ad0020b

Contents?: true

Size: 1.9 KB

Versions: 10

Compression:

Stored size: 1.9 KB

Contents

module Clearance
  module Testing
    module Matchers
      def deny_access(opts = {})
        DenyAccessMatcher.new(self, opts)
      end

      class DenyAccessMatcher
        attr_reader :failure_message, :negative_failure_message

        def initialize(context, opts)
          @context = context
          @flash = opts[:flash]
          @url = opts[:redirect]

          @failure_message = ''
          @negative_failure_message = ''
        end

        def description
          'deny access'
        end

        def matches?(controller)
          @controller = controller
          sets_the_flash? && redirects_to_url?
        end

        private

        def denied_access_url
          if @controller.signed_in?
            '/'
          else
            @controller.sign_in_url
          end
        end

        def flash_notice
          @controller.flash[:notice]
        end

        def flash_notice_value
          if flash_notice.respond_to?(:values)
            flash_notice.values.first
          else
            flash_notice
          end
        end

        def redirects_to_url?
          @url ||= denied_access_url

          begin
            @context.send(:assert_redirected_to, @url)
            @negative_failure_message << "Didn't expect to redirect to #{@url}."
            true
          rescue Clearance::Testing::AssertionError
            @failure_message << "Expected to redirect to #{@url} but did not."
            false
          end
        end

        def sets_the_flash?
          if @flash.blank?
            true
          else
            if flash_notice_value == @flash
              @negative_failure_message << "Didn't expect to set the flash to #{@flash}"
              true
            else
              @failure_message << "Expected the flash to be set to #{@flash} but was #{flash_notice_value}"
              false
            end
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
clearance-1.1.0 lib/clearance/testing/deny_access_matcher.rb
clearance-1.0.1 lib/clearance/testing/deny_access_matcher.rb
clearance-1.0.0 lib/clearance/testing/deny_access_matcher.rb
clearance-1.0.0.rc8 lib/clearance/testing/deny_access_matcher.rb
clearance-1.0.0.rc7 lib/clearance/testing/deny_access_matcher.rb
clearance-1.0.0.rc6 lib/clearance/testing/deny_access_matcher.rb
clearance-1.0.0.rc4 lib/clearance/testing/deny_access_matcher.rb
clearance-1.0.0.rc3 lib/clearance/testing/deny_access_matcher.rb
clearance-1.0.0.rc2 lib/clearance/testing/deny_access_matcher.rb
clearance-1.0.0.rc1 lib/clearance/testing/deny_access_matcher.rb