Sha256: 5b111a755e33bcd15f92b998bffa9809ea6c85fb4973acc490eef3f0592b9c79

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

require 'clearance/testing/assertion_error'

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

5 entries across 5 versions & 1 rubygems

Version Path
clearance-1.4.1 lib/clearance/testing/deny_access_matcher.rb
clearance-1.4.0 lib/clearance/testing/deny_access_matcher.rb
clearance-1.3.0 lib/clearance/testing/deny_access_matcher.rb
clearance-1.2.1 lib/clearance/testing/deny_access_matcher.rb
clearance-1.2.0 lib/clearance/testing/deny_access_matcher.rb