File: strategy_test_case.rb

package info (click to toggle)
ruby-omniauth 1.3.1-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 324 kB
  • sloc: ruby: 1,896; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,156 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'rack'
require 'omniauth/test'

module OmniAuth
  module Test
    # Support for testing OmniAuth strategies.
    #
    # @example Usage
    #   class MyStrategyTest < Test::Unit::TestCase
    #     include OmniAuth::Test::StrategyTestCase
    #     def strategy
    #       # return the parameters to a Rack::Builder map call:
    #       [MyStrategy.new, :some, :configuration, :options => 'here']
    #     end
    #     setup do
    #       post '/auth/my_strategy/callback', :user => { 'name' => 'Dylan', 'id' => '445' }
    #     end
    #   end
    module StrategyTestCase
      def app
        strat = strategy
        resp = app_response
        Rack::Builder.new do
          use(OmniAuth::Test::PhonySession)
          use(*strat)
          run lambda { |env| [404, {'Content-Type' => 'text/plain'}, [resp || env.key?('omniauth.auth').to_s]] }
        end.to_app
      end

      def app_response
        nil
      end

      def session
        last_request.env['rack.session']
      end

      def strategy
        error = NotImplementedError.new('Including specs must define #strategy')
        fail(error)
      end
    end
  end
end