Welcome to mirror list, hosted at ThFree Co, Russian Federation.

forti_authenticator_spec.rb « strategies « otp « auth « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc20df98185fab0757f5062eaa765017a7ae474d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Auth::Otp::Strategies::FortiAuthenticator do
  let_it_be(:user) { create(:user) }

  let(:otp_code) { 42 }

  let(:host) { 'forti_authenticator.example.com' }
  let(:port) { '444' }
  let(:api_username) { 'janedoe' }
  let(:api_token) { 's3cr3t' }

  let(:forti_authenticator_auth_url) { "https://#{host}:#{port}/api/v1/auth/" }
  let(:response_status) { 200 }

  subject(:validate) { described_class.new(user).validate(otp_code) }

  before do
    stub_feature_flags(forti_authenticator: user)

    stub_forti_authenticator_config(
      enabled: true,
      host: host,
      port: port,
      username: api_username,
      access_token: api_token
    )

    request_body = { username: user.username,
                     token_code: otp_code }

    stub_request(:post, forti_authenticator_auth_url)
      .with(body: JSON(request_body),
            headers: { 'Content-Type': 'application/json' },
            basic_auth: [api_username, api_token])
      .to_return(status: response_status, body: '')
  end

  context 'successful validation' do
    it 'returns success' do
      expect(validate[:status]).to eq(:success)
    end
  end

  context 'unsuccessful validation' do
    let(:response_status) { 401 }

    it 'returns error' do
      expect(validate[:status]).to eq(:error)
    end
  end

  context 'unexpected error' do
    it 'returns error' do
      error_message = 'boom!'
      stub_request(:post, forti_authenticator_auth_url).to_raise(StandardError.new(error_message))

      expect(validate[:status]).to eq(:error)
      expect(validate[:message]).to eq(error_message)
    end
  end

  def stub_forti_authenticator_config(forti_authenticator_settings)
    allow(::Gitlab.config.forti_authenticator).to(receive_messages(forti_authenticator_settings))
  end
end