Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3453 from bgeuken/ldap_con_tests
Ldap con tests
- Loading branch information
Showing
4 changed files
with
119 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
RSpec.shared_context 'setup ldap mock' do |opts| | ||
let(:ldap_mock) { double(:ldap) } | ||
|
||
before do | ||
opts ||= {} | ||
expected_port = (opts[:for_ssl] ? 636 : 389) | ||
connection = (opts[:for_ssl] ? LDAP::SSLConn : LDAP::Conn) | ||
|
||
stub_const('CONFIG', CONFIG.merge({ 'ldap_servers' => 'my_ldap_server.com' })) | ||
|
||
allow(ldap_mock).to receive(:set_option).with(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_OFF) | ||
allow(ldap_mock).to receive(:set_option).with(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) | ||
|
||
if opts[:for_ssl] | ||
allow(connection).to receive(:new).with( | ||
'my_ldap_server.com', expected_port, opts[:start_tls] == true | ||
).and_return(ldap_mock) | ||
else | ||
allow(connection).to receive(:new).with( | ||
'my_ldap_server.com', expected_port | ||
).and_return(ldap_mock) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
RSpec.shared_examples 'a ldap connection' do | ||
context 'when a connection can be established' do | ||
before do | ||
allow(ldap_mock).to receive(:bind).with('tux', 'tux_password') | ||
allow(ldap_mock).to receive(:bound?).and_return(true) | ||
end | ||
|
||
it 'returns the connection object' do | ||
expect(UserLdapStrategy.initialize_ldap_con('tux', 'tux_password')).to be ldap_mock | ||
end | ||
end | ||
|
||
context 'when no connection can be established' do | ||
before do | ||
allow(ldap_mock).to receive(:bind).with('tux', 'tux_password') | ||
allow(ldap_mock).to receive(:bound?).and_return(false) | ||
end | ||
|
||
it { expect(UserLdapStrategy.initialize_ldap_con('tux', 'tux_password')).to be nil } | ||
end | ||
|
||
context 'when establishing a connection fails with an error' do | ||
let(:err_object) { double(error: 'something happened') } | ||
|
||
before do | ||
allow(ldap_mock).to receive(:bound?) | ||
allow(ldap_mock).to receive(:bind).with('tux', 'tux_password').and_raise(LDAP::ResultError) | ||
allow(ldap_mock).to receive(:err).and_return(err_object) | ||
allow(ldap_mock).to receive(:err2string).with(err_object).and_return('something happened') | ||
end | ||
|
||
it { expect(UserLdapStrategy.initialize_ldap_con('tux', 'tux_password')).to be nil } | ||
end | ||
end |