summaryrefslogtreecommitdiff
path: root/spec/views/devise/sessions/new.html.haml_spec.rb
blob: e8232a2c06771db965c9b4e87e9ca8fc0b97e891 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'devise/sessions/new' do
  describe 'marketing text' do
    subject { render(template: 'devise/sessions/new', layout: 'layouts/devise') }

    before do
      stub_devise
      disable_captcha
      allow(Gitlab).to receive(:com?).and_return(true)
    end

    it 'when flash is anything it renders marketing text' do
      flash[:notice] = "You can't do that"

      subject

      expect(rendered).to have_content('A complete DevOps platform')
    end

    it 'when flash notice is devise confirmed message it hides marketing text' do
      flash[:notice] = t(:confirmed, scope: [:devise, :confirmations])

      subject

      expect(rendered).not_to have_content('A complete DevOps platform')
    end
  end

  describe 'ldap' do
    include LdapHelpers

    let(:server) { { provider_name: 'ldapmain', label: 'LDAP' }.with_indifferent_access }

    before do
      enable_ldap
      stub_devise
      disable_captcha
      disable_sign_up
      disable_other_signin_methods

      allow(view).to receive(:experiment_enabled?).and_return(false)
    end

    it 'is shown when enabled' do
      render

      expect(rendered).to have_selector('.new-session-tabs')
      expect(rendered).to have_selector('[data-qa-selector="ldap_tab"]') # rubocop:disable QA/SelectorUsage
      expect(rendered).to have_field('LDAP Username')
    end

    it 'is not shown when LDAP sign in is disabled' do
      disable_ldap_sign_in

      render

      expect(rendered).to have_content('No authentication methods configured')
      expect(rendered).not_to have_selector('[data-qa-selector="ldap_tab"]') # rubocop:disable QA/SelectorUsage
      expect(rendered).not_to have_field('LDAP Username')
    end
  end

  def disable_other_signin_methods
    allow(view).to receive(:password_authentication_enabled_for_web?).and_return(false)
    allow(view).to receive(:omniauth_enabled?).and_return(false)
  end

  def disable_sign_up
    allow(view).to receive(:allow_signup?).and_return(false)
  end

  def stub_devise
    allow(view).to receive(:devise_mapping).and_return(Devise.mappings[:user])
    allow(view).to receive(:resource).and_return(spy)
    allow(view).to receive(:resource_name).and_return(:user)
  end

  def enable_ldap
    stub_ldap_setting(enabled: true)
    allow(view).to receive(:ldap_servers).and_return([server])
    allow(view).to receive(:form_based_providers).and_return([:ldapmain])
    allow(view).to receive(:omniauth_callback_path).with(:user, 'ldapmain').and_return('/ldapmain')
  end

  def disable_ldap_sign_in
    allow(view).to receive(:ldap_sign_in_enabled?).and_return(false)
    allow(view).to receive(:ldap_servers).and_return([])
  end

  def disable_captcha
    allow(view).to receive(:captcha_enabled?).and_return(false)
    allow(view).to receive(:captcha_on_login_required?).and_return(false)
  end
end