summaryrefslogtreecommitdiff
path: root/users/test/support/auth_test_helper.rb
blob: 99dc141a06b4afc77c3be8ca3f801a2c409d3e0f (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
module AuthTestHelper
  include StubRecordHelper
  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 = nil)
    @current_user = stub_user(user_or_method_hash)
    unless @current_user.respond_to? :is_admin?
      @current_user.stubs(:is_admin?).returns(false)
    end
    request.env['warden'] = stub :user => @current_user
    return @current_user
  end

  def assert_access_denied(denied = true, logged_in = true)
    if denied
      assert_equal({:alert => "Not authorized"}, flash.to_hash)
      # todo: eventually probably eliminate separate conditions
      assert_redirected_to login_path if !logged_in
      assert_redirected_to root_path if logged_in 
    else
      assert flash[:alert].blank?
    end
  end

  protected

  # Will create a stub user for logging in from either
  # * a hash of methods to stub
  # * a user record
  # * nil -> create a user record stub
  def stub_user(user_or_method_hash)
    if user_or_method_hash.is_a?(Hash)
      stub_record User, user_or_method_hash
    else
      user_or_method_hash || stub_record(User)
    end
  end
end

class ActionController::TestCase
  include AuthTestHelper
end