Module: Softwear::Auth::Spec

Defined in:
lib/softwear/auth/spec.rb

Instance Method Summary collapse

Instance Method Details

#sign_in_as(user) ⇒ Object Also known as: sign_in, login_as



48
49
50
51
52
53
54
55
56
57
# File 'lib/softwear/auth/spec.rb', line 48

def (user)
  @_signed_in_user = user

  allow_any_instance_of(Softwear::Library::ControllerAuthentication)
    .to receive(:user_token).and_return 'abc123'

  if respond_to?(:session) && session.respond_to?(:[]=)
    session[:user_token] = 'abc123'
  end
end

#spec_usersObject



4
5
6
# File 'lib/softwear/auth/spec.rb', line 4

def spec_users
  User.instance_variable_get(:@_spec_users)
end

#stub_authentication!(config, *a) ⇒ Object



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
# File 'lib/softwear/auth/spec.rb', line 8

def stub_authentication!(config, *a)
  config.before(:each, *a) do
    User.instance_variable_set(:@_spec_users, [])

    allow(User).to receive(:all)   { spec_users }
    allow(User).to receive(:find)  { |n| spec_users.find { |u| u.id.to_s == n.to_s } }
    allow(User).to receive(:auth)  { @_signed_in_user or false }
    allow(User).to receive(:raw_query) { |q| raise "Unstubbed authentication query \"#{q}\"" }

    allow(Figaro.env).to receive(:softwear_hub_url).and_return 'http://hub.example.com'

    allow_any_instance_of(Softwear::Library::ControllerAuthentication)
      .to receive(:user_token)
      .and_return('')

    if (controller rescue false)
      controller.class_eval { helper Softwear::Auth::Helper }

      allow(controller).to receive(:current_user) { @_signed_in_user }
      controller.class_eval { helper_method :current_user }

      allow(controller).to receive(:user_signed_in?) { !!@_signed_in_user }
      controller.class_eval { helper_method :user_signed_in? }

      allow(controller).to receive(:destroy_user_session_path) { '#' }
      controller.class_eval { helper_method :destroy_user_session_path }

      allow(controller).to receive(:users_path) { '#' }
      controller.class_eval { helper_method :users_path }

      allow(controller).to receive(:edit_user_path) { '#' }
      controller.class_eval { protected; helper_method :edit_user_path }
    end
  end

  config.after(:each, *a) do
    User.instance_variable_set(:@_spec_users, nil)
  end
end