omniauth-ldap 1.0.5 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,194 +0,0 @@
1
- require 'spec_helper'
2
- describe "OmniAuth::Strategies::LDAP" do
3
- # :title => "My LDAP",
4
- # :host => '10.101.10.1',
5
- # :port => 389,
6
- # :method => :plain,
7
- # :base => 'dc=intridea, dc=com',
8
- # :uid => 'sAMAccountName',
9
- # :name_proc => Proc.new {|name| name.gsub(/@.*$/,'')}
10
- # :bind_dn => 'default_bind_dn'
11
- # :password => 'password'
12
- class MyLdapProvider < OmniAuth::Strategies::LDAP; end
13
-
14
- let(:app) do
15
- Rack::Builder.new {
16
- use OmniAuth::Test::PhonySession
17
- use MyLdapProvider, :name => 'ldap', :title => 'MyLdap Form', :host => '192.168.1.145', :base => 'dc=score, dc=local', :name_proc => Proc.new {|name| name.gsub(/@.*$/,'')}
18
- run lambda { |env| [404, {'Content-Type' => 'text/plain'}, [env.key?('omniauth.auth').to_s]] }
19
- }.to_app
20
- end
21
-
22
- let(:session) do
23
- last_request.env['rack.session']
24
- end
25
-
26
- it 'should add a camelization for itself' do
27
- OmniAuth::Utils.camelize('ldap').should == 'LDAP'
28
- end
29
-
30
- describe '/auth/ldap' do
31
- before(:each){ get '/auth/ldap' }
32
-
33
- it 'should display a form' do
34
- last_response.status.should == 200
35
- last_response.body.should be_include("<form")
36
- end
37
-
38
- it 'should have the callback as the action for the form' do
39
- last_response.body.should be_include("action='/auth/ldap/callback'")
40
- end
41
-
42
- it 'should have a text field for each of the fields' do
43
- last_response.body.scan('<input').size.should == 2
44
- end
45
- it 'should have a label of the form title' do
46
- last_response.body.scan('MyLdap Form').size.should > 1
47
- end
48
- end
49
-
50
- describe 'post /auth/ldap/callback' do
51
- before(:each) do
52
- @adaptor = double(OmniAuth::LDAP::Adaptor, {:uid => 'ping'})
53
- @adaptor.stub(:filter)
54
- OmniAuth::LDAP::Adaptor.stub(:new).and_return(@adaptor)
55
- end
56
-
57
- context 'failure' do
58
- before(:each) do
59
- @adaptor.stub(:bind_as).and_return(false)
60
- end
61
-
62
- context "when username is not preset" do
63
- it 'should redirect to error page' do
64
- post('/auth/ldap/callback', {})
65
-
66
- last_response.should be_redirect
67
- last_response.headers['Location'].should =~ %r{missing_credentials}
68
- end
69
- end
70
-
71
- context "when username is empty" do
72
- it 'should redirect to error page' do
73
- post('/auth/ldap/callback', {:username => ""})
74
-
75
- last_response.should be_redirect
76
- last_response.headers['Location'].should =~ %r{missing_credentials}
77
- end
78
- end
79
-
80
- context "when username is present" do
81
- context "and password is not preset" do
82
- it 'should redirect to error page' do
83
- post('/auth/ldap/callback', {:username => "ping"})
84
-
85
- last_response.should be_redirect
86
- last_response.headers['Location'].should =~ %r{missing_credentials}
87
- end
88
- end
89
-
90
- context "and password is empty" do
91
- it 'should redirect to error page' do
92
- post('/auth/ldap/callback', {:username => "ping", :password => ""})
93
-
94
- last_response.should be_redirect
95
- last_response.headers['Location'].should =~ %r{missing_credentials}
96
- end
97
- end
98
- end
99
-
100
- context "when username and password are present" do
101
- context "and bind on LDAP server failed" do
102
- it 'should redirect to error page' do
103
- post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
104
-
105
- last_response.should be_redirect
106
- last_response.headers['Location'].should =~ %r{invalid_credentials}
107
- end
108
- context 'and filter is set' do
109
- it 'should bind with filter' do
110
- @adaptor.stub(:filter).and_return('uid=%{username}')
111
- Net::LDAP::Filter.should_receive(:construct).with('uid=ping')
112
- post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
113
-
114
- last_response.should be_redirect
115
- last_response.headers['Location'].should =~ %r{invalid_credentials}
116
- end
117
- end
118
-
119
- end
120
-
121
- context "and communication with LDAP server caused an exception" do
122
- before :each do
123
- @adaptor.stub(:bind_as).and_throw(Exception.new('connection_error'))
124
- end
125
-
126
- it 'should redirect to error page' do
127
- post('/auth/ldap/callback', {:username => "ping", :password => "password"})
128
-
129
- last_response.should be_redirect
130
- last_response.headers['Location'].should =~ %r{ldap_error}
131
- end
132
- end
133
- end
134
- end
135
-
136
- context 'success' do
137
- let(:auth_hash){ last_request.env['omniauth.auth'] }
138
-
139
- before(:each) do
140
- @adaptor.stub(:filter)
141
- @adaptor.stub(:bind_as).and_return(Net::LDAP::Entry.from_single_ldif_string(
142
- %Q{dn: cn=ping, dc=intridea, dc=com
143
- mail: ping@intridea.com
144
- givenname: Ping
145
- sn: Yu
146
- telephonenumber: 555-555-5555
147
- mobile: 444-444-4444
148
- uid: ping
149
- title: dev
150
- address: k street
151
- l: Washington
152
- st: DC
153
- co: U.S.A
154
- postofficebox: 20001
155
- wwwhomepage: www.intridea.com
156
- jpegphoto: http://www.intridea.com/ping.jpg
157
- description: omniauth-ldap
158
- }
159
- ))
160
- end
161
-
162
- it 'should not redirect to error page' do
163
- post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
164
- last_response.should_not be_redirect
165
- end
166
-
167
- context 'and filter is set' do
168
- it 'should bind with filter' do
169
- @adaptor.stub(:filter).and_return('uid=%{username}')
170
- Net::LDAP::Filter.should_receive(:construct).with('uid=ping')
171
- post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
172
-
173
- last_response.should_not be_redirect
174
- end
175
- end
176
-
177
- it 'should map user info to Auth Hash' do
178
- post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
179
- auth_hash.uid.should == 'cn=ping, dc=intridea, dc=com'
180
- auth_hash.info.email.should == 'ping@intridea.com'
181
- auth_hash.info.first_name.should == 'Ping'
182
- auth_hash.info.last_name.should == 'Yu'
183
- auth_hash.info.phone.should == '555-555-5555'
184
- auth_hash.info.mobile.should == '444-444-4444'
185
- auth_hash.info.nickname.should == 'ping'
186
- auth_hash.info.title.should == 'dev'
187
- auth_hash.info.location.should == 'k street, Washington, DC, U.S.A 20001'
188
- auth_hash.info.url.should == 'www.intridea.com'
189
- auth_hash.info.image.should == 'http://www.intridea.com/ping.jpg'
190
- auth_hash.info.description.should == 'omniauth-ldap'
191
- end
192
- end
193
- end
194
- end
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
- describe "OmniAuth::LDAP::Adaptor" do
3
-
4
- describe 'initialize' do
5
- it 'should throw exception when must have field is not set' do
6
- #[:host, :port, :method, :bind_dn]
7
- lambda { OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain'})}.should raise_error(ArgumentError)
8
- end
9
-
10
- it 'should throw exception when method is not supported' do
11
- lambda { OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'myplain', uid: 'uid', port: 389, base: 'dc=com'})}.should raise_error(OmniAuth::LDAP::Adaptor::ConfigurationError)
12
- end
13
-
14
- it 'should setup ldap connection with anonymous' do
15
- adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName'})
16
- adaptor.connection.should_not == nil
17
- adaptor.connection.host.should == '192.168.1.145'
18
- adaptor.connection.port.should == 389
19
- adaptor.connection.base.should == 'dc=intridea, dc=com'
20
- adaptor.connection.instance_variable_get('@auth').should == {:method => :anonymous, :username => nil, :password => nil}
21
- end
22
-
23
- it 'should setup ldap connection with simple' do
24
- adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', bind_dn: 'bind_dn', password: 'password'})
25
- adaptor.connection.should_not == nil
26
- adaptor.connection.host.should == '192.168.1.145'
27
- adaptor.connection.port.should == 389
28
- adaptor.connection.base.should == 'dc=intridea, dc=com'
29
- adaptor.connection.instance_variable_get('@auth').should == {:method => :simple, :username => 'bind_dn', :password => 'password'}
30
- end
31
-
32
- it 'should setup ldap connection with sasl-md5' do
33
- adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', try_sasl: true, sasl_mechanisms: ["DIGEST-MD5"], bind_dn: 'bind_dn', password: 'password'})
34
- adaptor.connection.should_not == nil
35
- adaptor.connection.host.should == '192.168.1.145'
36
- adaptor.connection.port.should == 389
37
- adaptor.connection.base.should == 'dc=intridea, dc=com'
38
- adaptor.connection.instance_variable_get('@auth')[:method].should == :sasl
39
- adaptor.connection.instance_variable_get('@auth')[:mechanism].should == 'DIGEST-MD5'
40
- adaptor.connection.instance_variable_get('@auth')[:initial_credential].should == ''
41
- adaptor.connection.instance_variable_get('@auth')[:challenge_response].should_not be_nil
42
- end
43
-
44
- it 'should setup ldap connection with sasl-gss' do
45
- adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', try_sasl: true, sasl_mechanisms: ["GSS-SPNEGO"], bind_dn: 'bind_dn', password: 'password'})
46
- adaptor.connection.should_not == nil
47
- adaptor.connection.host.should == '192.168.1.145'
48
- adaptor.connection.port.should == 389
49
- adaptor.connection.base.should == 'dc=intridea, dc=com'
50
- adaptor.connection.instance_variable_get('@auth')[:method].should == :sasl
51
- adaptor.connection.instance_variable_get('@auth')[:mechanism].should == 'GSS-SPNEGO'
52
- adaptor.connection.instance_variable_get('@auth')[:initial_credential].should =~ /^NTLMSSP/
53
- adaptor.connection.instance_variable_get('@auth')[:challenge_response].should_not be_nil
54
- end
55
-
56
- it 'should set the encryption method correctly' do
57
- adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'tls', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName'})
58
- adaptor.connection.instance_variable_get('@encryption').should include method: :start_tls
59
- end
60
- end
61
-
62
- describe 'bind_as' do
63
- let(:args) { {:filter => Net::LDAP::Filter.eq('sAMAccountName', 'username'), :password => 'password', :size => 1} }
64
- let(:rs) { Struct.new(:dn).new('new dn') }
65
-
66
- it 'should bind simple' do
67
- adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.126", method: 'plain', base: 'dc=score, dc=local', port: 389, uid: 'sAMAccountName', bind_dn: 'bind_dn', password: 'password'})
68
- adaptor.connection.should_receive(:open).and_yield(adaptor.connection)
69
- adaptor.connection.should_receive(:search).with(args).and_return([rs])
70
- adaptor.connection.should_receive(:bind).with({:username => 'new dn', :password => args[:password], :method => :simple}).and_return(true)
71
- adaptor.bind_as(args).should == rs
72
- end
73
-
74
- it 'should bind sasl' do
75
- adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', try_sasl: true, sasl_mechanisms: ["GSS-SPNEGO"], bind_dn: 'bind_dn', password: 'password'})
76
- adaptor.connection.should_receive(:open).and_yield(adaptor.connection)
77
- adaptor.connection.should_receive(:search).with(args).and_return([rs])
78
- adaptor.connection.should_receive(:bind).and_return(true)
79
- adaptor.bind_as(args).should == rs
80
- end
81
- end
82
- end
data/spec/spec_helper.rb DELETED
@@ -1,14 +0,0 @@
1
- $:.unshift File.expand_path('..', __FILE__)
2
- $:.unshift File.expand_path('../../lib', __FILE__)
3
- require 'simplecov'
4
- SimpleCov.start
5
- require 'rspec'
6
- require 'rack/test'
7
- require 'omniauth'
8
- require 'omniauth-ldap'
9
-
10
- RSpec.configure do |config|
11
- config.include Rack::Test::Methods
12
- config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
13
- end
14
-