File: ldap_spec.rb

package info (click to toggle)
ruby-omniauth-ldap 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 180 kB
  • sloc: ruby: 720; makefile: 3
file content (284 lines) | stat: -rw-r--r-- 10,196 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
require 'spec_helper'

describe "OmniAuth::Strategies::LDAP" do
  # :title => "My LDAP",
  # :host => '10.101.10.1',
  # :port => 389,
  # :method => :plain,
  # :verify_certificates => true,
  # :base => 'dc=intridea, dc=com',
  # :uid => 'sAMAccountName',
  # :name_proc => Proc.new {|name| name.gsub(/@.*$/,'')}
  # :bind_dn => 'default_bind_dn'
  # :password => 'password'
  class MyLdapProvider < OmniAuth::Strategies::LDAP; end

  let(:app) do
    Rack::Builder.new {
      use OmniAuth::Test::PhonySession
      use MyLdapProvider, :name => 'ldap', :title => 'MyLdap Form', :host => '192.168.1.145', :base => 'dc=score, dc=local', :name_proc => Proc.new {|name| name.gsub(/@.*$/,'')}
      run lambda { |env| [404, {'Content-Type' => 'text/plain'}, [env.key?('omniauth.auth').to_s]] }
    }.to_app
  end

  let(:session) do
    last_request.env['rack.session']
  end

  it 'should add a camelization for itself' do
    OmniAuth::Utils.camelize('ldap').should == 'LDAP'
  end

  describe '/auth/ldap' do
    let!(:csrf_token) { SecureRandom.base64(32) }
    let(:post_env) { make_env('/auth/ldap', 'rack.session' => { csrf: csrf_token }, 'rack.input' => StringIO.new("authenticity_token=#{escaped_token}")) }
    let(:escaped_token) { URI.encode_www_form_component(csrf_token, Encoding::UTF_8) }

    before(:each) { post '/auth/ldap', nil, post_env }

    def make_env(path = '/auth/ldap', props = {})
      {
        'REQUEST_METHOD' => 'POST',
        'PATH_INFO' => path,
        'rack.session' => {},
        'rack.input' => StringIO.new('test=true')
      }.merge(props)
    end

    it 'should display a form' do
      expect(last_response.status).to eq(200)
      expect(last_response.body).to be_include("<form")
    end

    it 'should have the callback as the action for the form' do
      expect(last_response.body).to be_include("action='/auth/ldap/callback'")
    end

    it 'should have a text field for each of the fields' do
      expect(last_response.body.scan('<input').size).to eq(2)
    end
    it 'should have a label of the form title' do
      expect(last_response.body.scan('MyLdap Form').size).to be > 1
    end
  end

  describe 'post /auth/ldap/callback' do
    before(:each) do
      @adaptor = double(OmniAuth::LDAP::Adaptor, {:uid => 'ping'})
      @adaptor.stub(:filter)
      allow(OmniAuth::LDAP::Adaptor).to receive(:new).and_return(@adaptor)
    end

    context 'failure' do
      before(:each) do
        allow(@adaptor).to receive(:bind_as).and_return(false)
      end

      it 'should fail with missing_credentials' do
        post('/auth/ldap/callback', {})
        last_response.should be_redirect
        last_response.headers['Location'].should =~ %r{missing_credentials}
      end

      it 'should redirect to error page' do
        post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})

        expect(last_response).to be_redirect
        expect(last_response.headers['Location']).to match('invalid_credentials')
        expect(last_request.env['omniauth.error'].message).to eq('Invalid credentials for ping')
      end

      it 'should redirect to error page when there is exception' do
        @adaptor.stub(:bind_as).and_throw(Exception.new('connection_error'))
        post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
        last_response.should be_redirect
        last_response.headers['Location'].should =~ %r{ldap_error}
      end

      context 'wrong request method' do
        it 'redirects to error page' do
          get('/auth/ldap/callback', { username: 'ping', password: 'password' })

          expect(last_response).to be_redirect
          expect(last_response.headers['Location']).to match('invalid_request_method')
        end
      end

      context "when username is not preset" do
        it 'should redirect to error page' do
          post('/auth/ldap/callback', {})

          expect(last_response).to be_redirect
          expect(last_response.headers['Location']).to match(%r{missing_credentials})
        end
      end

      context "when username is empty" do
        it 'should redirect to error page' do
          post('/auth/ldap/callback', {:username => ""})

          expect(last_response).to be_redirect
          expect(last_response.headers['Location']).to match(%r{missing_credentials})
        end
      end

      context "when username is present" do
        context "and password is not preset" do
          it 'should redirect to error page' do
            post('/auth/ldap/callback', {:username => "ping"})

            expect(last_response).to be_redirect
            expect(last_response.headers['Location']).to match(%r{missing_credentials})
          end
        end

        context "and password is empty" do
          it 'should redirect to error page' do
            post('/auth/ldap/callback', {:username => "ping", :password => ""})

            expect(last_response).to be_redirect
            expect(last_response.headers['Location']).to match(%r{missing_credentials})
          end
        end
      end

      context "when username and password are present" do
        context "and bind on LDAP server failed" do
          it 'should redirect to error page' do
            post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})

            expect(last_response).to be_redirect
            expect(last_response.headers['Location']).to match('invalid_credentials')
            expect(last_request.env['omniauth.error'].message).to eq('Invalid credentials for ping')
          end
          context 'and filter is set' do
            it 'should bind with filter' do
              @adaptor.stub(:filter).and_return('uid=%{username}')
              Net::LDAP::Filter.should_receive(:construct).with('uid=ping')
              post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})

              expect(last_response).to be_redirect
              expect(last_response.headers['Location']).to match('invalid_credentials')
              expect(last_request.env['omniauth.error'].message).to eq('Invalid credentials for ping')
            end
          end

        end

        context "and communication with LDAP server caused an exception" do
          before :each do
            allow(@adaptor).to receive(:bind_as).and_throw(Exception.new('connection_error'))
          end

          it 'should redirect to error page' do
            post('/auth/ldap/callback', {:username => "ping", :password => "password"})

            expect(last_response).to be_redirect
            expect(last_response.headers['Location']).to match(%r{ldap_error})
          end
        end
      end
    end

    context 'success' do
      let(:auth_hash){ last_request.env['omniauth.auth'] }

      before(:each) do
        @adaptor.stub(:filter)
        allow(@adaptor).to receive(:bind_as).and_return(Net::LDAP::Entry.from_single_ldif_string(
%Q{dn: cn=ping, dc=intridea, dc=com
mail: ping@intridea.com
givenname: Ping
sn: Yu
telephonenumber: 555-555-5555
mobile: 444-444-4444
uid: ping
title: dev
address: k street
l: Washington
st: DC
co: U.S.A
postofficebox: 20001
wwwhomepage: www.intridea.com
jpegphoto: http://www.intridea.com/ping.jpg
description: omniauth-ldap
}
    ))
      end

      it 'should not redirect to error page' do
        post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
        last_response.should_not be_redirect
      end

      context 'and filter is set' do
        it 'should bind with filter' do
          @adaptor.stub(:filter).and_return('uid=%{username}')
          Net::LDAP::Filter.should_receive(:construct).with('uid=ping')
          post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})

        expect(last_response).not_to be_redirect
        end
      end

      it 'should map user info to Auth Hash' do
        post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
        expect(auth_hash.uid).to eq('cn=ping, dc=intridea, dc=com')
        expect(auth_hash.info.email).to eq('ping@intridea.com')
        expect(auth_hash.info.first_name).to eq('Ping')
        expect(auth_hash.info.last_name).to eq('Yu')
        expect(auth_hash.info.phone).to eq('555-555-5555')
        expect(auth_hash.info.mobile).to eq('444-444-4444')
        expect(auth_hash.info.nickname).to eq('ping')
        expect(auth_hash.info.title).to eq('dev')
        expect(auth_hash.info.location).to eq('k street, Washington, DC, U.S.A 20001')
        expect(auth_hash.info.url).to eq('www.intridea.com')
        expect(auth_hash.info.image).to eq('http://www.intridea.com/ping.jpg')
        expect(auth_hash.info.description).to eq('omniauth-ldap')
      end
    end

    context 'alternate fields' do
      let(:auth_hash){ last_request.env['omniauth.auth'] }

      before(:each) do
        @adaptor.stub(:filter)
        @adaptor.stub(:bind_as).and_return(Net::LDAP::Entry.from_single_ldif_string(
%Q{dn: cn=ping, dc=intridea, dc=com
userprincipalname: ping@intridea.com
givenname: Ping
sn: Yu
telephonenumber: 555-555-5555
mobile: 444-444-4444
uid: ping
title: dev
address: k street
l: Washington
st: DC
co: U.S.A
postofficebox: 20001
wwwhomepage: www.intridea.com
jpegphoto: http://www.intridea.com/ping.jpg
description: omniauth-ldap
}
    ))
      end

      it 'should map user info to Auth Hash' do
        post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
        auth_hash.uid.should == 'cn=ping, dc=intridea, dc=com'
        auth_hash.info.email.should == 'ping@intridea.com'
        auth_hash.info.first_name.should == 'Ping'
        auth_hash.info.last_name.should == 'Yu'
        auth_hash.info.phone.should == '555-555-5555'
        auth_hash.info.mobile.should == '444-444-4444'
        auth_hash.info.nickname.should == 'ping'
        auth_hash.info.title.should == 'dev'
        auth_hash.info.location.should == 'k street, Washington, DC, U.S.A 20001'
        auth_hash.info.url.should == 'www.intridea.com'
        auth_hash.info.image.should == 'http://www.intridea.com/ping.jpg'
        auth_hash.info.description.should == 'omniauth-ldap'
      end
    end
  end
end