about summary refs log tree commit diff
path: root/spec/controllers/settings/two_factor_authentication/otp_authentication_controller_spec.rb
blob: 17e8fa9b8212e9b581c0c112a529ca53ab3ccabb (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true

require 'rails_helper'

describe Settings::TwoFactorAuthentication::OtpAuthenticationController do
  render_views

  let(:user) { Fabricate(:user) }

  describe 'GET #show' do
    context 'when signed in' do
      before do
        sign_in user, scope: :user
      end

      describe 'when user has OTP enabled' do
        before do
          user.update(otp_required_for_login: true)
        end

        it 'redirects to two factor authentciation methods list page' do
          get :show

          expect(response).to redirect_to settings_two_factor_authentication_methods_path
        end
      end

      describe 'when user does not have OTP enabled' do
        before do
          user.update(otp_required_for_login: false)
        end

        it 'returns http success' do
          get :show

          expect(response).to have_http_status(200)
        end
      end
    end

    context 'when not signed in' do
      it 'redirects' do
        get :show

        expect(response).to redirect_to new_user_session_path
      end
    end
  end

  describe 'POST #create' do
    context 'when signed in' do
      before do
        sign_in user, scope: :user
      end

      describe 'when user has OTP enabled' do
        before do
          user.update(otp_required_for_login: true)
        end

        describe 'when creation succeeds' do
          it 'redirects to code confirmation page without updating user secret and setting otp secret in the session' do
            expect do
              post :create, session: { challenge_passed_at: Time.now.utc }
            end.to not_change { user.reload.otp_secret }
               .and change { session[:new_otp_secret] }

            expect(response).to redirect_to(new_settings_two_factor_authentication_confirmation_path)
          end
        end
      end

      describe 'when user does not have OTP enabled' do
        before do
          user.update(otp_required_for_login: false)
        end

        describe 'when creation succeeds' do
          it 'redirects to code confirmation page without updating user secret and setting otp secret in the session' do
            expect do
              post :create, session: { challenge_passed_at: Time.now.utc }
            end.to not_change { user.reload.otp_secret }
               .and change { session[:new_otp_secret] }

            expect(response).to redirect_to(new_settings_two_factor_authentication_confirmation_path)
          end
        end
      end
    end

    context 'when not signed in' do
      it 'redirects to login' do
        get :show

        expect(response).to redirect_to new_user_session_path
      end
    end
  end
end