|
7 | 7 |
|
8 | 8 | const Assert = require('assert');
|
9 | 9 | const Authentication = require('../src/authentication');
|
10 |
| -const LDAP = require('ldapjs'); |
11 |
| -const Server = LDAP.createServer(); |
12 |
| - |
13 |
| -const USERNAME = 'username'; |
14 |
| -const PASSWORD = 'password'; |
15 |
| -const PASSWORD_WRONG = 'pass-wrong'; |
16 |
| - |
| 10 | +const Mock = require('./mock/LDAP'); |
17 | 11 |
|
18 | 12 | describe('LDAP Authentication', function () {
|
19 | 13 |
|
20 |
| - before(function () { |
21 |
| - Server.listen(10388, '0.0.0.0'); |
22 |
| - |
23 |
| - Server.bind('uid=admin,ou=system', function (req, res, next) { |
24 |
| - if (req.credentials === PASSWORD_WRONG) { |
25 |
| - return next(new LDAP.InvalidCredentialsError()); |
26 |
| - } else { |
27 |
| - res.end(); |
28 |
| - return next(); |
29 |
| - } |
30 |
| - }); |
31 |
| - |
32 |
| - Server.search('ou=users,ou=system', function (req, res, next) { |
33 |
| - var entry = { |
34 |
| - dn: 'uid=admin, ou=system', |
35 |
| - attributes: { |
36 |
| - objectclass: ['top', 'organization'], |
37 |
| - o: ['system'], |
38 |
| - uid: USERNAME |
39 |
| - } |
40 |
| - }; |
41 |
| - |
42 |
| - // Returns an entry for all other searches, simulating a missing entry. |
43 |
| - if (req.filter.json.value === 'missing') { |
44 |
| - res.end(); |
45 |
| - return next(); |
46 |
| - } else { |
47 |
| - res.send(entry); |
48 |
| - res.end(); |
49 |
| - return next(); |
50 |
| - } |
51 |
| - }); |
52 |
| - }); |
53 |
| - |
54 |
| - |
55 |
| - Server.search('ou=groups,ou=system', function (req, res, next) { |
56 |
| - var entry = { |
57 |
| - dn: 'ou=groups,ou=system', |
58 |
| - attributes: { |
59 |
| - objectClass: 'groupOfNames', |
60 |
| - member: 'uid=' + USERNAME, |
61 |
| - cn: 'group-name' |
62 |
| - } |
63 |
| - }; |
64 |
| - |
65 |
| - res.send(entry); |
66 |
| - res.end(); |
67 |
| - return next(); |
68 |
| - }); |
| 14 | + before((function () { |
| 15 | + Mock.init(); |
| 16 | + })); |
69 | 17 |
|
70 | 18 | it('Should bind successfully with a client.', function (done) {
|
71 |
| - Authentication.ldap(USERNAME, PASSWORD, function (err, user) { |
72 |
| - Assert.equal(user.uid, USERNAME); |
| 19 | + Authentication.ldap(Mock.USERNAME, Mock.PASSWORD, function (err, user) { |
| 20 | + Assert.equal(user.uid, Mock.USERNAME); |
73 | 21 | Assert.equal(err, null);
|
74 | 22 | done();
|
75 | 23 | });
|
76 | 24 | });
|
77 | 25 |
|
78 | 26 | it('Should fail to bind with an user using wrong password.', function (done) {
|
79 |
| - Authentication.ldap(USERNAME, PASSWORD_WRONG, function (err, user) { |
| 27 | + Authentication.ldap(Mock.USERNAME, Mock.PASSWORD_WRONG, function (err, user) { |
80 | 28 | Assert.notEqual(err, null);
|
81 | 29 | done();
|
82 | 30 | });
|
83 | 31 | });
|
84 | 32 |
|
85 | 33 | it('Should fail to bind with a user that do not exist.', function (done) {
|
86 |
| - Authentication.ldap('missing', PASSWORD, function (err, user) { |
| 34 | + Authentication.ldap('missing', Mock.PASSWORD, function (err, user) { |
87 | 35 | Assert.equal(user, null);
|
88 | 36 | Assert.notEqual(err, null);
|
89 | 37 | done();
|
90 | 38 | });
|
91 | 39 | });
|
92 | 40 |
|
93 | 41 | it('Should retrieve all the groups an user is member of.', function (done) {
|
94 |
| - Authentication.ldap(USERNAME, PASSWORD, function (err, user) { |
| 42 | + Authentication.ldap(Mock.USERNAME, Mock.PASSWORD, function (err, user) { |
95 | 43 | Assert.equal(user.groups.length, 1);
|
96 | 44 | Assert.equal(err, null);
|
97 | 45 | done();
|
|
0 commit comments