summaryrefslogtreecommitdiff
path: root/spec/controllers/registrations/welcome_controller_spec.rb
blob: 008259a8bfa02c3e6c01b88a5af2facd93741f3e (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
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 'spec_helper'

RSpec.describe Registrations::WelcomeController do
  let(:user) { create(:user) }

  describe '#welcome' do
    subject(:show) { get :show }

    context 'without a signed in user' do
      it { is_expected.to redirect_to new_user_registration_path }
    end

    context 'when role or setup_for_company is not set' do
      before do
        sign_in(user)
      end

      it { is_expected.to render_template(:show) }
    end

    context 'when role is required and setup_for_company is not set' do
      before do
        user.set_role_required!
        sign_in(user)
      end

      it { is_expected.to render_template(:show) }
    end

    context 'when role and setup_for_company is set' do
      before do
        user.update!(setup_for_company: false)
        sign_in(user)
      end

      it { is_expected.to redirect_to(dashboard_projects_path)}
    end

    context 'when role is set and setup_for_company is not set' do
      before do
        user.update!(role: :software_developer)
        sign_in(user)
      end

      it { is_expected.to render_template(:show) }
    end

    context '2FA is required from group' do
      before do
        user = create(:user, require_two_factor_authentication_from_group: true)
        sign_in(user)
      end

      it 'does not perform a redirect' do
        expect(subject).not_to redirect_to(profile_two_factor_auth_path)
      end
    end
  end

  describe '#update' do
    let(:email_opted_in) { '0' }

    subject(:update) do
      patch :update, params: { user: { role: 'software_developer', setup_for_company: 'false', email_opted_in: email_opted_in } }
    end

    context 'without a signed in user' do
      it { is_expected.to redirect_to new_user_registration_path }
    end

    context 'with a signed in user' do
      before do
        sign_in(user)
      end

      it { is_expected.to redirect_to(dashboard_projects_path)}

      context 'when the user opted in' do
        let(:email_opted_in) { '1' }

        it 'sets the email_opted_in field' do
          subject

          expect(controller.current_user.email_opted_in).to eq(true)
        end
      end

      context 'when the user opted out' do
        it 'sets the email_opted_in field' do
          subject

          expect(controller.current_user.email_opted_in).to eq(false)
        end
      end
    end
  end
end