main
 1# frozen_string_literal: true
 2
 3require 'rails_helper'
 4
 5RSpec.describe "/mfa" do
 6  context "when username/password entry has been completed" do
 7    let(:current_user) { create(:user, :mfa_configured) }
 8
 9    before { http_login(current_user) }
10
11    describe "GET /mfa/new" do
12      before { get '/mfa/new' }
13
14      specify { expect(response).to have_http_status(:ok) }
15    end
16
17    describe "POST /mfa" do
18      context "when the code is correct" do
19        let(:correct_code) { current_user.mfa.current_totp }
20
21        before { post '/mfa', params: { mfa: { code: correct_code } } }
22
23        specify { expect(response).to redirect_to(response_path) }
24        specify { expect(session[:mfa]).to be_present }
25      end
26
27      context "when the code is incorrect" do
28        let(:incorrect_code) { rand(1_000) }
29
30        before { post '/mfa', params: { mfa: { code: incorrect_code } } }
31
32        specify { expect(response).to redirect_to(new_mfa_path) }
33        specify { expect(flash[:error]).to be_present }
34      end
35    end
36  end
37
38  context "when username/password entry has not been completed" do
39    describe "GET /mfa/new" do
40      before { get '/mfa/new' }
41
42      specify { expect(response).to redirect_to(new_session_path) }
43    end
44  end
45end