ldap_spec.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'rails_helper'
  2. require 'lib/auth/backend_examples'
  3. require 'auth/ldap'
  4. RSpec.describe ::Auth::Ldap do
  5. let(:user) { create(:user) }
  6. let(:password) { 'somepassword' }
  7. let(:instance) { described_class.new({ adapter: described_class.name }) }
  8. context '#valid?' do
  9. it_behaves_like 'Auth backend'
  10. it 'authenticates users' do
  11. allow(Setting).to receive(:get)
  12. expect(Setting).to receive(:get).with('ldap_integration').and_return(true)
  13. ldap_user = double(valid?: true)
  14. expect(::Ldap::User).to receive(:new).and_return(ldap_user)
  15. result = instance.valid?(user, password)
  16. expect(result).to be true
  17. end
  18. it 'authenticates via configurable user attributes' do
  19. allow(Setting).to receive(:get)
  20. expect(Setting).to receive(:get).with('ldap_integration').and_return(true)
  21. instance = described_class.new(
  22. adapter: described_class.name,
  23. login_attributes: %w[firstname],
  24. )
  25. ldap_user = double
  26. expect(ldap_user).to receive(:valid?).with(user.firstname, password).and_return(true)
  27. expect(::Ldap::User).to receive(:new).and_return(ldap_user)
  28. result = instance.valid?(user, password)
  29. expect(result).to be true
  30. end
  31. context 'invalid' do
  32. it "doesn't authenticate if 'ldap_integration' Setting is disabled" do
  33. allow(Setting).to receive(:get)
  34. expect(Setting).to receive(:get).with('ldap_integration').and_return(false)
  35. result = instance.valid?(user, password)
  36. expect(result).to be false
  37. end
  38. it "doesn't authenticate if ldap says 'nope'" do
  39. allow(Setting).to receive(:get)
  40. expect(Setting).to receive(:get).with('ldap_integration').and_return(true)
  41. ldap_user = double(valid?: false)
  42. expect(::Ldap::User).to receive(:new).and_return(ldap_user)
  43. result = instance.valid?(user, password)
  44. expect(result).to be false
  45. end
  46. end
  47. end
  48. end