summaryrefslogtreecommitdiff
path: root/test/support/auth_test_helper.rb
blob: df008e5209ae6b44a200e5b669fca34ddde9bd5b (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module AuthTestHelper
  extend ActiveSupport::Concern

  # Controller will fetch current user from warden.
  # Make it pick up our current_user
  included do
    setup do
      request.env['warden'] ||= stub :user => nil
    end
  end

  def login(user_or_method_hash = {})
    if user_or_method_hash.respond_to?(:reverse_merge)
      user_or_method_hash.reverse_merge! :is_admin? => false
    end
    @current_user = stub_record(:user, user_or_method_hash)
    request.env['warden'] = stub :user => @current_user
    request.env['HTTP_AUTHORIZATION'] = header_for_token_auth
    return @current_user
  end

  def assert_access_granted
    assert flash[:alert].blank?,
      "expected to have access but there was a flash alert"
  end

  def expect_logout
    expect_warden_logout
    @token.expects(:destroy) if @token
  end

  # authenticate as the api monitor
  def monitor_auth(&block)
    token_auth(APP_CONFIG['api_tokens']['monitor'], &block)
  end

  # authenticate with a token
  def token_auth(token_str)
    original = request.env['HTTP_AUTHORIZATION']
    request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(token_str)
    if block_given?
      yield
      request.env['HTTP_AUTHORIZATION'] = original
    end
  end

  protected

  def header_for_token_auth
    @token = stub_record(:token, :authenticate => @current_user)
    Token.stubs(:find_by_token).with(@token.token).returns(@token)
    ActionController::HttpAuthentication::Token.encode_credentials @token.token
  end

  def expect_warden_logout
    raw = mock('raw session')
    raw.expects(:inspect)
    request.env['warden'].expects(:raw_session).returns(raw)
    request.env['warden'].expects(:logout)
  end

end

class ActionController::TestCase
  include AuthTestHelper
end