debian-mirror-gitlab/spec/controllers/profiles/two_factor_auths_controller_spec.rb

166 lines
3.8 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Profiles::TwoFactorAuthsController do
2015-09-11 14:41:01 +05:30
before do
# `user` should be defined within the action-specific describe blocks
sign_in(user)
allow(subject).to receive(:current_user).and_return(user)
end
describe 'GET show' do
2015-09-11 14:41:01 +05:30
let(:user) { create(:user) }
it 'generates otp_secret for user' do
2020-09-03 11:15:55 +05:30
expect(User).to receive(:generate_otp_secret).with(32).and_call_original.once
2015-09-11 14:41:01 +05:30
get :show
2015-09-11 14:41:01 +05:30
end
it 'assigns qr_code' do
code = double('qr code')
expect(subject).to receive(:build_qr_code).and_return(code)
get :show
2015-09-11 14:41:01 +05:30
expect(assigns[:qr_code]).to eq code
end
2020-09-03 11:15:55 +05:30
it 'generates a unique otp_secret every time the page is loaded' do
expect(User).to receive(:generate_otp_secret).with(32).and_call_original.twice
2.times do
get :show
end
end
2015-09-11 14:41:01 +05:30
end
describe 'POST create' do
let(:user) { create(:user) }
let(:pin) { 'pin-code' }
def go
2019-02-15 15:39:39 +05:30
post :create, params: { pin_code: pin }
2015-09-11 14:41:01 +05:30
end
context 'with valid pin' do
before do
2015-09-25 12:07:36 +05:30
expect(user).to receive(:validate_and_consume_otp!).with(pin).and_return(true)
2015-09-11 14:41:01 +05:30
end
it 'enables 2fa for the user' do
2015-09-11 14:41:01 +05:30
go
user.reload
expect(user).to be_two_factor_enabled
end
it 'presents plaintext codes for the user to save' do
expect(user).to receive(:generate_otp_backup_codes!).and_return(%w(a b c))
go
expect(assigns[:codes]).to match_array %w(a b c)
end
2020-09-03 11:15:55 +05:30
it 'calls to delete other sessions' do
expect(ActiveSession).to receive(:destroy_all_but_current)
go
end
2015-09-11 14:41:01 +05:30
it 'renders create' do
go
expect(response).to render_template(:create)
end
end
context 'with invalid pin' do
before do
2015-09-25 12:07:36 +05:30
expect(user).to receive(:validate_and_consume_otp!).with(pin).and_return(false)
2015-09-11 14:41:01 +05:30
end
it 'assigns error' do
go
2020-01-01 13:55:28 +05:30
expect(assigns[:error]).to eq _('Invalid pin code')
2015-09-11 14:41:01 +05:30
end
it 'assigns qr_code' do
code = double('qr code')
expect(subject).to receive(:build_qr_code).and_return(code)
go
expect(assigns[:qr_code]).to eq code
end
it 'renders show' do
2015-09-11 14:41:01 +05:30
go
expect(response).to render_template(:show)
2015-09-11 14:41:01 +05:30
end
end
end
describe 'POST codes' do
let(:user) { create(:user, :two_factor) }
it 'presents plaintext codes for the user to save' do
expect(user).to receive(:generate_otp_backup_codes!).and_return(%w(a b c))
post :codes
expect(assigns[:codes]).to match_array %w(a b c)
end
it 'persists the generated codes' do
post :codes
user.reload
expect(user.otp_backup_codes).not_to be_empty
end
end
describe 'DELETE destroy' do
2020-11-24 15:15:51 +05:30
subject { delete :destroy }
context 'for a user that has 2FA enabled' do
let(:user) { create(:user, :two_factor) }
it 'disables two factor' do
subject
expect(user.reload.two_factor_enabled?).to eq(false)
end
it 'redirects to profile_account_path' do
subject
expect(response).to redirect_to(profile_account_path)
end
2015-09-11 14:41:01 +05:30
2020-11-24 15:15:51 +05:30
it 'displays a notice on success' do
subject
2015-09-11 14:41:01 +05:30
2020-11-24 15:15:51 +05:30
expect(flash[:notice])
.to eq _('Two-factor authentication has been disabled successfully!')
end
2015-09-11 14:41:01 +05:30
end
2020-11-24 15:15:51 +05:30
context 'for a user that does not have 2FA enabled' do
let(:user) { create(:user) }
2015-09-11 14:41:01 +05:30
2020-11-24 15:15:51 +05:30
it 'redirects to profile_account_path' do
subject
expect(response).to redirect_to(profile_account_path)
end
it 'displays an alert on failure' do
subject
expect(flash[:alert])
.to eq _('Two-factor authentication is not enabled for this user')
end
2015-09-11 14:41:01 +05:30
end
end
end