Browse Source

Fixes - Initial fetching of existing security keys configuration is raising an exception.

Florian Liebe 1 year ago
parent
commit
9396adf8b9

+ 10 - 3
app/controllers/user/two_factors_controller.rb

@@ -68,7 +68,9 @@ class User::TwoFactorsController < ApplicationController
   def two_factor_authentication_method_configuration
     check_method!
     check_two_factor_method!
-    fetch_user_two_factor_preference!
+    fetch_user_two_factor_preference!(raise_exception: false)
+
+    return render json: { configuration: {} }, status: :ok if @user_two_factor_preference.nil?
 
     render json: { configuration: @user_two_factor_preference.configuration }, status: :ok
   end
@@ -106,9 +108,14 @@ class User::TwoFactorsController < ApplicationController
     true
   end
 
-  def fetch_user_two_factor_preference!
+  def fetch_user_two_factor_preference!(raise_exception: true)
     pref = @two_factor_method.user_two_factor_preference
-    raise Exceptions::UnprocessableEntity, __('There is no stored configuration for this two-factor authentication method.') if pref.blank? || pref.configuration.blank?
+
+    if pref.blank? || pref.configuration.blank?
+      raise Exceptions::UnprocessableEntity, __('There is no stored configuration for this two-factor authentication method.') if raise_exception
+
+      return
+    end
 
     @user_two_factor_preference ||= pref
 

+ 9 - 0
spec/requests/user/two_factor_spec.rb

@@ -234,6 +234,15 @@ RSpec.describe 'User', current_user_id: 1, performs_jobs: true, type: :request d
     end
 
     context 'with valid params' do
+      context 'with no stored two-factor preference' do
+        let(:two_factor_pref) { nil }
+
+        it 'returns nothing', :aggregate_failures do
+          expect(response).to have_http_status(:ok)
+          expect(json_response['configuration']).to be_empty
+        end
+      end
+
       it 'returns configuration', :aggregate_failures do
         expect(response).to have_http_status(:ok)
         expect(json_response['configuration']).to include('secret').and include('code').and include('provisioning_uri')