2015-05-09 21:04:32 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Profiles::TwoFactorAuthsController do
|
|
|
|
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
|
|
|
|
|
2016-06-06 04:44:51 +00:00
|
|
|
describe 'GET show' do
|
2015-05-09 21:04:32 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2015-05-22 22:33:44 +00:00
|
|
|
it 'generates otp_secret for user' do
|
2015-05-23 22:47:53 +00:00
|
|
|
expect(User).to receive(:generate_otp_secret).with(32).and_return('secret').once
|
2015-05-22 22:33:44 +00:00
|
|
|
|
2016-06-06 04:44:51 +00:00
|
|
|
get :show
|
|
|
|
get :show # Second hit shouldn't re-generate it
|
2015-05-09 21:04:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns qr_code' do
|
|
|
|
code = double('qr code')
|
|
|
|
expect(subject).to receive(:build_qr_code).and_return(code)
|
|
|
|
|
2016-06-06 04:44:51 +00:00
|
|
|
get :show
|
2015-05-09 21:04:32 +00:00
|
|
|
expect(assigns[:qr_code]).to eq code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST create' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:pin) { 'pin-code' }
|
|
|
|
|
|
|
|
def go
|
|
|
|
post :create, pin_code: pin
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with valid pin' do
|
|
|
|
before do
|
2015-09-20 01:16:18 +00:00
|
|
|
expect(user).to receive(:validate_and_consume_otp!).with(pin).and_return(true)
|
2015-05-09 21:04:32 +00:00
|
|
|
end
|
|
|
|
|
2016-06-06 04:44:51 +00:00
|
|
|
it 'enables 2fa for the user' do
|
2015-05-09 21:04:32 +00:00
|
|
|
go
|
|
|
|
|
|
|
|
user.reload
|
2015-06-19 19:14:37 +00:00
|
|
|
expect(user).to be_two_factor_enabled
|
2015-05-09 21:04:32 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
it 'renders create' do
|
|
|
|
go
|
|
|
|
expect(response).to render_template(:create)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid pin' do
|
|
|
|
before do
|
2015-09-20 01:16:18 +00:00
|
|
|
expect(user).to receive(:validate_and_consume_otp!).with(pin).and_return(false)
|
2015-05-09 21:04:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns error' do
|
|
|
|
go
|
|
|
|
expect(assigns[:error]).to eq 'Invalid pin code'
|
|
|
|
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
|
|
|
|
|
2016-06-06 04:44:51 +00:00
|
|
|
it 'renders show' do
|
2015-05-09 21:04:32 +00:00
|
|
|
go
|
2016-06-06 04:44:51 +00:00
|
|
|
expect(response).to render_template(:show)
|
2015-05-09 21:04:32 +00:00
|
|
|
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
|
2015-07-10 20:08:39 +00:00
|
|
|
let(:user) { create(:user, :two_factor) }
|
2015-05-09 21:04:32 +00:00
|
|
|
|
2015-07-10 20:08:39 +00:00
|
|
|
it 'disables two factor' do
|
|
|
|
expect(user).to receive(:disable_two_factor!)
|
2015-05-09 21:04:32 +00:00
|
|
|
|
|
|
|
delete :destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to profile_account_path' do
|
|
|
|
delete :destroy
|
|
|
|
|
|
|
|
expect(response).to redirect_to(profile_account_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|