group_spec.rb 2.8 KB

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