Commit 982d4d51e8110bec280eb00db0fb756b062103d9
1 parent
f4bca105
Exists in
spb-stable
and in
2 other branches
Backport Adapter#dn_matches_filter? from EE
Showing
2 changed files
with
35 additions
and
0 deletions
Show diff stats
lib/gitlab/ldap/adapter.rb
@@ -77,6 +77,10 @@ module Gitlab | @@ -77,6 +77,10 @@ module Gitlab | ||
77 | users(*args).first | 77 | users(*args).first |
78 | end | 78 | end |
79 | 79 | ||
80 | + def dn_matches_filter?(dn, filter) | ||
81 | + ldap_search(base: dn, filter: filter, scope: Net::LDAP::SearchScope_BaseObject, attributes: %w{dn}).any? | ||
82 | + end | ||
83 | + | ||
80 | def ldap_search(*args) | 84 | def ldap_search(*args) |
81 | results = ldap.search(*args) | 85 | results = ldap.search(*args) |
82 | 86 |
@@ -0,0 +1,31 @@ | @@ -0,0 +1,31 @@ | ||
1 | +require 'spec_helper' | ||
2 | + | ||
3 | +describe Gitlab::LDAP::Adapter do | ||
4 | + let(:adapter) { Gitlab::LDAP::Adapter.new } | ||
5 | + | ||
6 | + describe :dn_matches_filter? do | ||
7 | + let(:ldap) { double(:ldap) } | ||
8 | + subject { adapter.dn_matches_filter?(:dn, :filter) } | ||
9 | + before { adapter.stub(ldap: ldap) } | ||
10 | + | ||
11 | + context "when the search is successful" do | ||
12 | + context "and the result is non-empty" do | ||
13 | + before { ldap.stub(search: [:foo]) } | ||
14 | + | ||
15 | + it { should be_true } | ||
16 | + end | ||
17 | + | ||
18 | + context "and the result is empty" do | ||
19 | + before { ldap.stub(search: []) } | ||
20 | + | ||
21 | + it { should be_false } | ||
22 | + end | ||
23 | + end | ||
24 | + | ||
25 | + context "when the search encounters an error" do | ||
26 | + before { ldap.stub(search: nil, get_operation_result: double(code: 1, message: 'some error')) } | ||
27 | + | ||
28 | + it { should be_false } | ||
29 | + end | ||
30 | + end | ||
31 | +end |