|
| 1 | +const ldap = require('../lib/Adapters/Auth/ldap'); |
| 2 | +const mockLdapServer = require('./MockLdapServer'); |
| 3 | + |
| 4 | +it('Should fail with missing options', done => { |
| 5 | + try { |
| 6 | + ldap.validateAuthData({ id: 'testuser', password: 'testpw' }); |
| 7 | + } catch (error) { |
| 8 | + jequal(error.message, 'LDAP auth configuration missing'); |
| 9 | + done(); |
| 10 | + } |
| 11 | +}); |
| 12 | + |
| 13 | +it('Should succeed with right credentials', done => { |
| 14 | + mockLdapServer(1010, 'uid=testuser, o=example').then(server => { |
| 15 | + const options = { |
| 16 | + suffix: 'o=example', |
| 17 | + url: 'ldap://localhost:1010', |
| 18 | + dn: 'uid={{id}}, o=example', |
| 19 | + }; |
| 20 | + ldap |
| 21 | + .validateAuthData({ id: 'testuser', password: 'secret' }, options) |
| 22 | + .then(done) |
| 23 | + .catch(done.fail) |
| 24 | + .finally(() => server.close()); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +it('Should fail with wrong credentials', done => { |
| 29 | + mockLdapServer(1010, 'uid=testuser, o=example').then(server => { |
| 30 | + const options = { |
| 31 | + suffix: 'o=example', |
| 32 | + url: 'ldap://localhost:1010', |
| 33 | + dn: 'uid={{id}}, o=example', |
| 34 | + }; |
| 35 | + ldap |
| 36 | + .validateAuthData({ id: 'testuser', password: 'wrong!' }, options) |
| 37 | + .then(done.fail) |
| 38 | + .catch(err => { |
| 39 | + jequal(err.message, 'LDAP: Wrong username or password'); |
| 40 | + done(); |
| 41 | + }) |
| 42 | + .finally(() => server.close()); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +it('Should succeed if user is in given group', done => { |
| 47 | + mockLdapServer(1010, 'uid=testuser, o=example').then(server => { |
| 48 | + const options = { |
| 49 | + suffix: 'o=example', |
| 50 | + url: 'ldap://localhost:1010', |
| 51 | + dn: 'uid={{id}}, o=example', |
| 52 | + groupCn: 'powerusers', |
| 53 | + groupFilter: |
| 54 | + '(&(uniqueMember=uid={{id}}, o=example)(objectClass=groupOfUniqueNames))', |
| 55 | + }; |
| 56 | + |
| 57 | + ldap |
| 58 | + .validateAuthData({ id: 'testuser', password: 'secret' }, options) |
| 59 | + .then(done) |
| 60 | + .catch(done.fail) |
| 61 | + .finally(() => server.close()); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +it('Should fail if user is not in given group', done => { |
| 66 | + mockLdapServer(1010, 'uid=testuser, o=example').then(server => { |
| 67 | + const options = { |
| 68 | + suffix: 'o=example', |
| 69 | + url: 'ldap://localhost:1010', |
| 70 | + dn: 'uid={{id}}, o=example', |
| 71 | + groupDn: 'ou=somegroup, o=example', |
| 72 | + groupFilter: |
| 73 | + '(&(uniqueMember=uid={{id}}, o=example)(objectClass=groupOfUniqueNames))', |
| 74 | + }; |
| 75 | + |
| 76 | + ldap |
| 77 | + .validateAuthData({ id: 'testuser', password: 'secret' }, options) |
| 78 | + .then(done.fail) |
| 79 | + .catch(err => { |
| 80 | + jequal(err.message, 'LDAP: User not in group'); |
| 81 | + done(); |
| 82 | + }) |
| 83 | + .finally(() => server.close()); |
| 84 | + }); |
| 85 | +}); |
0 commit comments