admin_two_factor_spec.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'User', authenticated_as: :admin, current_user_id: 1, type: :request do
  4. let(:agent) { create(:agent) }
  5. let(:admin) { create(:admin) }
  6. let(:two_factor_pref) { create(:user_two_factor_preference, :authenticator_app, user: agent) }
  7. let(:two_factor_enabled) { true }
  8. before do
  9. Setting.set('two_factor_authentication_method_authenticator_app', two_factor_enabled)
  10. end
  11. describe 'DELETE /users/:id/admin_two_factor/remove_authentication_method' do
  12. it 'deletes record' do
  13. two_factor_pref
  14. expect { delete "/api/v1/users/#{agent.id}/admin_two_factor/remove_authentication_method", params: { method: 'authenticator_app' }, as: :json }
  15. .to change { agent.two_factor_preferences.count }
  16. .to(0)
  17. end
  18. end
  19. describe 'DELETE /users/:id/admin_two_factor/remove_all_authentication_methods' do
  20. it 'deletes records' do
  21. two_factor_pref
  22. # add disabled two factor method
  23. create(:user_two_factor_preference, :security_keys, user: agent)
  24. expect { delete "/api/v1/users/#{agent.id}/admin_two_factor/remove_all_authentication_methods", as: :json }
  25. .to change { agent.two_factor_preferences.count }
  26. .to(0)
  27. end
  28. end
  29. describe 'GET /users/:id/admin_two_factor/enabled_authentication_methods' do
  30. context 'with disabled authenticator app method' do
  31. let(:two_factor_enabled) { false }
  32. it 'response is blank' do
  33. two_factor_pref
  34. get "/api/v1/users/#{agent.id}/admin_two_factor/enabled_authentication_methods", as: :json
  35. expect(json_response).to be_blank
  36. end
  37. end
  38. it 'lists enabled method' do
  39. get "/api/v1/users/#{agent.id}/admin_two_factor/enabled_authentication_methods", as: :json
  40. expect(json_response.first).to eq({
  41. 'method' => 'authenticator_app',
  42. 'configured' => false,
  43. 'default' => false,
  44. })
  45. end
  46. it 'lists in-use method as configured' do
  47. two_factor_pref
  48. get "/api/v1/users/#{agent.id}/admin_two_factor/enabled_authentication_methods", as: :json
  49. expect(json_response.first).to eq({
  50. 'method' => 'authenticator_app',
  51. 'configured' => true,
  52. 'default' => true,
  53. })
  54. end
  55. end
  56. end