ldap_spec.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. require 'rails_helper'
  2. RSpec.describe Ldap do
  3. context 'initialization config parameters' do
  4. # required as 'let' to perform test based
  5. # expectations and reuse it in mock_initialization
  6. # as return param of Net::LDAP.new
  7. let(:mocked_ldap) { double(bind: true) }
  8. def mock_initialization(given:, expected:)
  9. allow(Net::LDAP).to receive(:new).with(expected).and_return(mocked_ldap)
  10. described_class.new(given)
  11. end
  12. it 'uses explicit host and port' do
  13. config = {
  14. host: 'localhost',
  15. port: 1337,
  16. }
  17. mock_initialization(
  18. given: config,
  19. expected: config,
  20. )
  21. end
  22. context 'bind credentials' do
  23. it 'uses given credentials' do
  24. config = {
  25. host: 'localhost',
  26. port: 1337,
  27. bind_user: 'JohnDoe',
  28. bind_pw: 'zammad',
  29. }
  30. params = {
  31. host: 'localhost',
  32. port: 1337,
  33. }
  34. allow(mocked_ldap).to receive(:auth).with(config[:bind_user], config[:bind_pw])
  35. mock_initialization(
  36. given: config,
  37. expected: params,
  38. )
  39. end
  40. it 'requires bind_user' do
  41. config = {
  42. host: 'localhost',
  43. port: 1337,
  44. bind_pw: 'zammad',
  45. }
  46. params = {
  47. host: 'localhost',
  48. port: 1337,
  49. }
  50. allow(mocked_ldap).to receive(:auth)
  51. mock_initialization(
  52. given: config,
  53. expected: params,
  54. )
  55. expect(mocked_ldap).not_to have_received(:auth).with(config[:bind_user], config[:bind_pw])
  56. end
  57. it 'requires bind_pw' do
  58. config = {
  59. host: 'localhost',
  60. port: 1337,
  61. bind_user: 'JohnDoe',
  62. }
  63. params = {
  64. host: 'localhost',
  65. port: 1337,
  66. }
  67. allow(mocked_ldap).to receive(:auth)
  68. mock_initialization(
  69. given: config,
  70. expected: params,
  71. )
  72. expect(mocked_ldap).not_to have_received(:auth).with(config[:bind_user], config[:bind_pw])
  73. end
  74. end
  75. it 'extracts port from host' do
  76. config = {
  77. host: 'localhost:1337'
  78. }
  79. params = {
  80. host: 'localhost',
  81. port: 1337,
  82. }
  83. mock_initialization(
  84. given: config,
  85. expected: params,
  86. )
  87. end
  88. context 'host_url' do
  89. it 'parses protocol and host' do
  90. config = {
  91. host_url: 'ldaps://localhost'
  92. }
  93. params = {
  94. host: 'localhost',
  95. port: 636,
  96. encryption: Hash
  97. }
  98. mock_initialization(
  99. given: config,
  100. expected: params,
  101. )
  102. end
  103. it 'prefers parsing over explicit parameters' do
  104. config = {
  105. host: 'anotherhost',
  106. port: 7777,
  107. host_url: 'ldap://localhost:389'
  108. }
  109. params = {
  110. host: 'localhost',
  111. port: 389,
  112. }
  113. mock_initialization(
  114. given: config,
  115. expected: params,
  116. )
  117. end
  118. end
  119. it 'falls back to default ldap port' do
  120. config = {
  121. host: 'localhost',
  122. }
  123. params = {
  124. host: 'localhost',
  125. port: 389,
  126. }
  127. mock_initialization(
  128. given: config,
  129. expected: params,
  130. )
  131. end
  132. it 'uses explicit ssl' do
  133. config = {
  134. host: 'localhost',
  135. port: 1337,
  136. ssl: true,
  137. }
  138. expected = {
  139. host: 'localhost',
  140. port: 1337,
  141. encryption: Hash,
  142. }
  143. mock_initialization(
  144. given: config,
  145. expected: expected,
  146. )
  147. end
  148. it "uses 'ldap_config' Setting as fallback" do
  149. config = {
  150. host: 'localhost',
  151. port: 1337,
  152. }
  153. allow(Setting).to receive(:get)
  154. allow(Setting).to receive(:get).with('ldap_config').and_return(config)
  155. mock_initialization(
  156. given: nil,
  157. expected: config,
  158. )
  159. end
  160. end
  161. context 'instance methods' do
  162. # required as 'let' to perform test based
  163. # expectations and reuse it in 'let' instance
  164. # as return param of Net::LDAP.new
  165. let(:mocked_ldap) { double(bind: true) }
  166. let(:instance) do
  167. allow(Net::LDAP).to receive(:new).and_return(mocked_ldap)
  168. described_class.new(
  169. host: 'localhost',
  170. port: 1337,
  171. )
  172. end
  173. describe '#preferences' do
  174. it 'responds to #preferences' do
  175. expect(instance).to respond_to(:preferences)
  176. end
  177. it 'returns preferences' do
  178. attributes = {
  179. namingcontexts: ['ou=dep1,ou=org', 'ou=dep2,ou=org']
  180. }
  181. allow(mocked_ldap).to receive(:search_root_dse).and_return(attributes)
  182. expect(instance.preferences).to eq(attributes)
  183. end
  184. end
  185. describe '#search' do
  186. let(:base) { 'DC=domain,DC=tld' }
  187. let(:filter) { '(objectClass=user)' }
  188. it 'responds to #search' do
  189. expect(instance).to respond_to(:search)
  190. end
  191. it 'performs search for a filter, base and scope and yields of returned entries' do
  192. scope = Net::LDAP::SearchScope_BaseObject
  193. additional = {
  194. base: base,
  195. scope: scope,
  196. }
  197. expected = {
  198. filter: filter,
  199. base: base,
  200. scope: scope,
  201. }
  202. yield_entry = build(:ldap_entry)
  203. allow(mocked_ldap).to receive(:search).with(include(expected)).and_yield(yield_entry).and_return(true)
  204. check_entry = nil
  205. instance.search(filter, additional) { |entry| check_entry = entry }
  206. expect(check_entry).to eq(yield_entry)
  207. end
  208. it 'falls back to whole subtree scope search' do
  209. additional = {
  210. base: base,
  211. }
  212. expected = {
  213. filter: filter,
  214. base: base,
  215. scope: Net::LDAP::SearchScope_WholeSubtree,
  216. }
  217. yield_entry = build(:ldap_entry)
  218. allow(mocked_ldap).to receive(:search).with(include(expected)).and_yield(yield_entry).and_return(true)
  219. check_entry = nil
  220. instance.search(filter, additional) { |entry| check_entry = entry }
  221. expect(check_entry).to eq(yield_entry)
  222. end
  223. it 'falls back to base_dn configuration parameter' do
  224. expected = {
  225. filter: filter,
  226. base: base,
  227. scope: Net::LDAP::SearchScope_WholeSubtree,
  228. }
  229. allow(Net::LDAP).to receive(:new).and_return(mocked_ldap)
  230. instance = described_class.new(
  231. host: 'localhost',
  232. port: 1337,
  233. base_dn: base,
  234. )
  235. yield_entry = build(:ldap_entry)
  236. allow(mocked_ldap).to receive(:search).with(include(expected)).and_yield(yield_entry).and_return(true)
  237. check_entry = nil
  238. instance.search(filter) { |entry| check_entry = entry }
  239. expect(check_entry).to eq(yield_entry)
  240. end
  241. end
  242. describe '#entries?' do
  243. let(:filter) { '(objectClass=user)' }
  244. it 'responds to #entries?' do
  245. expect(instance).to respond_to(:entries?)
  246. end
  247. it 'returns true if entries are present' do
  248. params = {
  249. filter: filter
  250. }
  251. allow(mocked_ldap).to receive(:search).with(include(params)).and_yield(build(:ldap_entry)).and_return(nil)
  252. expect(instance.entries?(filter)).to be true
  253. end
  254. it 'returns false if no entries are present' do
  255. params = {
  256. filter: filter
  257. }
  258. allow(mocked_ldap).to receive(:search).with(include(params)).and_return(true)
  259. expect(instance.entries?(filter)).to be false
  260. end
  261. end
  262. end
  263. end