backend_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Auth::Backend do
  4. let(:user) { create(:user) }
  5. let(:password) { 'secure' }
  6. let(:auth) { Auth.new(user.login, password) }
  7. let(:instance) { described_class.new(auth) }
  8. describe '#valid?' do
  9. context 'when invalid Setting is present in DB' do
  10. context 'when value is blank' do
  11. before do
  12. create(:setting,
  13. area: 'Security::Authentication',
  14. state: {},)
  15. end
  16. it "doesn't raise an exception" do
  17. expect { instance.valid? }.not_to raise_exception
  18. end
  19. end
  20. context "when adapter can't be constantized" do
  21. before do
  22. create(:setting,
  23. area: 'Security::Authentication',
  24. state: {
  25. adapter: 'This::Will::Never::Work'
  26. },)
  27. end
  28. it "doesn't raise an exception" do
  29. expect { instance.valid? }.not_to raise_exception
  30. end
  31. end
  32. end
  33. context 'when backend prioritization is relevant' do
  34. let(:previous_class_namespace) { 'Auth::Backend::TopPrio' }
  35. let(:later_class_namespace) { 'Auth::Backend::LeastPrio' }
  36. let(:previous_backend_class) { Class.new(Auth::Backend::Base) }
  37. let(:later_backend_class) { Class.new(Auth::Backend::Base) }
  38. let(:previous_backend_instance) { instance_double(previous_class_namespace) }
  39. let(:later_backend_instance) { instance_double(later_class_namespace) }
  40. before do
  41. stub_const previous_class_namespace, previous_backend_class
  42. stub_const later_class_namespace, later_backend_class
  43. Setting.where(area: 'Security::Authentication').destroy_all
  44. create(:setting,
  45. area: 'Security::Authentication',
  46. state: {
  47. adapter: previous_class_namespace,
  48. priority: 1
  49. },)
  50. create(:setting,
  51. area: 'Security::Authentication',
  52. state: {
  53. adapter: later_class_namespace,
  54. priority: 2
  55. },)
  56. allow(previous_class_namespace.constantize).to receive(:new).and_return(previous_backend_instance)
  57. allow(later_class_namespace.constantize).to receive(:new).and_return(later_backend_instance)
  58. allow(previous_backend_instance).to receive(:valid?)
  59. allow(later_backend_instance).to receive(:valid?)
  60. end
  61. context 'when previous backend was valid' do
  62. before do
  63. allow(previous_backend_instance).to receive(:valid?).and_return(true)
  64. end
  65. it "doesn't call valid on later backend" do
  66. instance.valid?
  67. expect(later_backend_instance).not_to have_received(:valid?)
  68. end
  69. end
  70. context 'when previous backend was not valid' do
  71. before do
  72. allow(previous_backend_instance).to receive(:valid?).and_return(false)
  73. end
  74. it 'calls valid on later backend' do
  75. instance.valid?
  76. expect(later_backend_instance).to have_received(:valid?)
  77. end
  78. end
  79. end
  80. end
  81. end