debian-mirror-gitlab/spec/services/users/validate_manual_otp_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.7 KiB
Ruby
Raw Permalink Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe Users::ValidateManualOtpService, feature_category: :user_profile do
2021-01-03 14:25:43 +05:30
let_it_be(:user) { create(:user) }
2021-09-30 23:02:18 +05:30
2021-01-03 14:25:43 +05:30
let(:otp_code) { 42 }
subject(:validate) { described_class.new(user).execute(otp_code) }
context 'Devise' do
it 'calls Devise strategy' do
expect_next_instance_of(::Gitlab::Auth::Otp::Strategies::Devise) do |strategy|
expect(strategy).to receive(:validate).with(otp_code).once
end
validate
end
end
context 'FortiAuthenticator' do
before do
2021-02-22 17:27:13 +05:30
stub_feature_flags(forti_authenticator: user)
allow(::Gitlab.config.forti_authenticator).to receive(:enabled).and_return(true)
2021-01-03 14:25:43 +05:30
end
2022-07-16 23:28:13 +05:30
it 'calls ManualOtp strategy' do
expect_next_instance_of(::Gitlab::Auth::Otp::Strategies::FortiAuthenticator::ManualOtp) do |strategy|
2021-01-03 14:25:43 +05:30
expect(strategy).to receive(:validate).with(otp_code).once
end
validate
end
2023-05-27 22:25:52 +05:30
it 'handles unexpected error' do
error_message = "boom!"
expect_next_instance_of(::Gitlab::Auth::Otp::Strategies::FortiAuthenticator::ManualOtp) do |strategy|
expect(strategy).to receive(:validate).with(otp_code).once.and_raise(StandardError, error_message)
end
expect(Gitlab::ErrorTracking).to receive(:log_exception)
result = validate
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq(error_message)
end
2021-01-03 14:25:43 +05:30
end
2021-02-22 17:27:13 +05:30
context 'FortiTokenCloud' do
before do
stub_feature_flags(forti_token_cloud: user)
allow(::Gitlab.config.forti_token_cloud).to receive(:enabled).and_return(true)
end
it 'calls FortiTokenCloud strategy' do
expect_next_instance_of(::Gitlab::Auth::Otp::Strategies::FortiTokenCloud) do |strategy|
expect(strategy).to receive(:validate).with(otp_code).once
end
validate
end
end
2022-07-16 23:28:13 +05:30
2023-05-27 22:25:52 +05:30
context 'DuoAuth' do
2022-07-16 23:28:13 +05:30
before do
2023-05-27 22:25:52 +05:30
allow(::Gitlab.config.duo_auth).to receive(:enabled).and_return(true)
2022-07-16 23:28:13 +05:30
end
2023-05-27 22:25:52 +05:30
it 'calls DuoAuth strategy' do
expect_next_instance_of(::Gitlab::Auth::Otp::Strategies::DuoAuth::ManualOtp) do |strategy|
expect(strategy).to receive(:validate).with(otp_code).once
end
validate
end
it "handles unexpected error" do
2022-07-16 23:28:13 +05:30
error_message = "boom!"
2023-05-27 22:25:52 +05:30
expect_next_instance_of(::Gitlab::Auth::Otp::Strategies::DuoAuth::ManualOtp) do |strategy|
2022-07-16 23:28:13 +05:30
expect(strategy).to receive(:validate).with(otp_code).once.and_raise(StandardError, error_message)
end
expect(Gitlab::ErrorTracking).to receive(:log_exception)
result = validate
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq(error_message)
end
end
2021-01-03 14:25:43 +05:30
end