group_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. # rails autoloading issue
  4. require 'ldap'
  5. require 'ldap/group'
  6. RSpec.describe Ldap::Group do
  7. # required as 'let' to perform test based
  8. # expectations and reuse it in 'let' instance
  9. # as additional parameter
  10. let(:mocked_ldap) { double }
  11. describe '.uid_attribute' do
  12. it 'responds to .uid_attribute' do
  13. expect(described_class).to respond_to(:uid_attribute)
  14. end
  15. it 'returns uid attribute' do
  16. expect(described_class.uid_attribute).to be_a(String)
  17. end
  18. end
  19. describe 'initialization config parameters' do
  20. it 'reuses given Ldap instance if given' do
  21. config = {}
  22. expect(Ldap).not_to receive(:new).with(config)
  23. described_class.new(config, ldap: mocked_ldap)
  24. end
  25. it 'takes optional filter' do
  26. filter = '(objectClass=custom)'
  27. config = {
  28. filter: filter
  29. }
  30. instance = described_class.new(config, ldap: mocked_ldap)
  31. expect(instance.filter).to eq(filter)
  32. end
  33. it 'takes optional uid_attribute' do
  34. uid_attribute = 'dn'
  35. config = {
  36. uid_attribute: uid_attribute
  37. }
  38. instance = described_class.new(config, ldap: mocked_ldap)
  39. expect(instance.uid_attribute).to eq(uid_attribute)
  40. end
  41. it 'creates own Ldap instance if none given' do
  42. expect(Ldap).to receive(:new)
  43. described_class.new
  44. end
  45. end
  46. describe 'instance methods' do
  47. let(:initialization_config) do
  48. {
  49. uid_attribute: 'dn',
  50. filter: '(objectClass=group)',
  51. }
  52. end
  53. let(:instance) do
  54. described_class.new(initialization_config, ldap: mocked_ldap)
  55. end
  56. describe '#list' do
  57. it 'responds to #list' do
  58. expect(instance).to respond_to(:list)
  59. end
  60. it 'returns a Hash of groups' do
  61. ldap_entry = build(:ldap_entry)
  62. allow(mocked_ldap).to receive(:search).and_return(ldap_entry)
  63. expect(instance.list).to be_a(Hash)
  64. end
  65. end
  66. describe '#filter' do
  67. let(:initialization_config) do
  68. {
  69. uid_attribute: 'dn',
  70. }
  71. end
  72. it 'responds to #filter' do
  73. expect(instance).to respond_to(:filter)
  74. end
  75. it 'tries filters and returns first one with entries' do
  76. allow(mocked_ldap).to receive(:entries?).and_return(true)
  77. expect(instance.filter).to be_a(String)
  78. end
  79. it 'fails if no filter found entries' do
  80. allow(mocked_ldap).to receive(:entries?).and_return(false)
  81. expect(instance.filter).to be_nil
  82. end
  83. end
  84. describe '#uid_attribute' do
  85. it 'responds to #uid_attribute' do
  86. expect(instance).to respond_to(:uid_attribute)
  87. end
  88. it 'returns the uid attribute' do
  89. expect(instance.uid_attribute).to be_a(String)
  90. end
  91. end
  92. end
  93. end