ldap_spec.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Ldap', type: :request do
  4. let!(:admin) do
  5. create(:admin, groups: Group.all)
  6. end
  7. describe 'discover' do
  8. let(:params) do
  9. {
  10. name: 'Example LDAP',
  11. host: 'example.ldap.okta.com',
  12. ssl: 'ssl',
  13. ssl_verify: true,
  14. active: 'true'
  15. }
  16. end
  17. context 'when disallow bin anon is active' do
  18. it 'returns special exception treatment for not allowed anonymous bind' do
  19. authenticated_as(admin)
  20. post '/api/v1/integration/ldap/discover', params: params, as: :json
  21. expect(json_response).to eq('result' => 'ok', 'error' => 'disallow-bind-anon')
  22. end
  23. context 'with other error code' do
  24. let(:ldap_instance) { instance_double(Net::LDAP) }
  25. let(:params) do
  26. {
  27. name: 'Example LDAP',
  28. host: 'localhost',
  29. ssl: 'off',
  30. active: 'true'
  31. }
  32. end
  33. let(:operation_result_struct) { Struct.new(:code, :message) }
  34. before do
  35. allow(Net::LDAP).to receive(:new).with({ host: params[:host], port: 389 }).and_return(ldap_instance)
  36. allow(ldap_instance).to receive_messages(
  37. bind: false,
  38. get_operation_result: operation_result_struct.new(50, 'Insufficient Access Rights')
  39. )
  40. end
  41. it 'returns special exception treatment for not allowed anonymous bind' do
  42. authenticated_as(admin)
  43. post '/api/v1/integration/ldap/discover', params: params, as: :json
  44. expect(json_response).to eq('result' => 'ok', 'error' => 'disallow-bind-anon')
  45. end
  46. end
  47. end
  48. end
  49. end