|
| 1 | +const chai = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const proxyquire = require('proxyquire'); |
| 4 | +const expect = chai.expect; |
| 5 | + |
| 6 | +describe('ActiveDirectory auth method', () => { |
| 7 | + let ldapStub; |
| 8 | + let dbStub; |
| 9 | + let passportStub; |
| 10 | + let strategyCallback; |
| 11 | + |
| 12 | + const newConfig = JSON.stringify({ |
| 13 | + authentication: [ |
| 14 | + { |
| 15 | + type: 'ActiveDirectory', |
| 16 | + enabled: true, |
| 17 | + adminGroup: 'test-admin-group', |
| 18 | + userGroup: 'test-user-group', |
| 19 | + domain: 'test.com', |
| 20 | + adConfig: { |
| 21 | + url: 'ldap://test-url', |
| 22 | + baseDN: 'dc=test,dc=com', |
| 23 | + searchBase: 'ou=users,dc=test,dc=com', |
| 24 | + }, |
| 25 | + }, |
| 26 | + ], |
| 27 | + }); |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + ldapStub = { |
| 31 | + isUserInAdGroup: sinon.stub(), |
| 32 | + }; |
| 33 | + |
| 34 | + dbStub = { |
| 35 | + updateUser: sinon.stub(), |
| 36 | + }; |
| 37 | + |
| 38 | + passportStub = { |
| 39 | + use: sinon.stub(), |
| 40 | + serializeUser: sinon.stub(), |
| 41 | + deserializeUser: sinon.stub(), |
| 42 | + }; |
| 43 | + |
| 44 | + const fsStub = { |
| 45 | + existsSync: sinon.stub().returns(true), |
| 46 | + readFileSync: sinon.stub().returns(newConfig), |
| 47 | + }; |
| 48 | + |
| 49 | + const config = proxyquire('../src/config', { |
| 50 | + fs: fsStub, |
| 51 | + }); |
| 52 | + |
| 53 | + const { configure } = proxyquire('../src/service/passport/activeDirectory', { |
| 54 | + './ldaphelper': ldapStub, |
| 55 | + '../../db': dbStub, |
| 56 | + '../../config': config, |
| 57 | + 'passport-activedirectory': function (options, callback) { |
| 58 | + strategyCallback = callback; |
| 59 | + return { |
| 60 | + name: 'ActiveDirectory', |
| 61 | + authenticate: () => {}, |
| 62 | + }; |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + configure(passportStub); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should authenticate a valid user and mark them as admin', async () => { |
| 70 | + const mockReq = {}; |
| 71 | + const mockProfile = { |
| 72 | + _json: { |
| 73 | + sAMAccountName: 'test-user', |
| 74 | + mail: 'test@test.com', |
| 75 | + userPrincipalName: 'test@test.com', |
| 76 | + title: 'Test User', |
| 77 | + }, |
| 78 | + displayName: 'Test User', |
| 79 | + }; |
| 80 | + |
| 81 | + ldapStub.isUserInAdGroup |
| 82 | + .onCall(0).resolves(true) |
| 83 | + .onCall(1).resolves(true); |
| 84 | + |
| 85 | + const done = sinon.spy(); |
| 86 | + |
| 87 | + await strategyCallback(mockReq, mockProfile, {}, done); |
| 88 | + |
| 89 | + expect(done.calledOnce).to.be.true; |
| 90 | + const [err, user] = done.firstCall.args; |
| 91 | + expect(err).to.be.null; |
| 92 | + expect(user).to.have.property('username', 'test-user'); |
| 93 | + expect(user).to.have.property('email', 'test@test.com'); |
| 94 | + expect(user).to.have.property('displayName', 'Test User'); |
| 95 | + expect(user).to.have.property('admin', true); |
| 96 | + expect(user).to.have.property('title', 'Test User'); |
| 97 | + |
| 98 | + expect(dbStub.updateUser.calledOnce).to.be.true; |
| 99 | + }); |
| 100 | + |
| 101 | + it('should fail if user is not in user group', async () => { |
| 102 | + const mockReq = {}; |
| 103 | + const mockProfile = { |
| 104 | + _json: { |
| 105 | + sAMAccountName: 'bad-user', |
| 106 | + mail: 'bad@test.com', |
| 107 | + userPrincipalName: 'bad@test.com', |
| 108 | + title: 'Bad User' |
| 109 | + }, |
| 110 | + displayName: 'Bad User' |
| 111 | + }; |
| 112 | + |
| 113 | + ldapStub.isUserInAdGroup.onCall(0).resolves(false); |
| 114 | + |
| 115 | + const done = sinon.spy(); |
| 116 | + |
| 117 | + await strategyCallback(mockReq, mockProfile, {}, done); |
| 118 | + |
| 119 | + expect(done.calledOnce).to.be.true; |
| 120 | + const [err, user] = done.firstCall.args; |
| 121 | + expect(err).to.include('not a member'); |
| 122 | + expect(user).to.be.null; |
| 123 | + |
| 124 | + expect(dbStub.updateUser.notCalled).to.be.true; |
| 125 | + }); |
| 126 | + |
| 127 | + it('should handle LDAP errors gracefully', async () => { |
| 128 | + const mockReq = {}; |
| 129 | + const mockProfile = { |
| 130 | + _json: { |
| 131 | + sAMAccountName: 'error-user', |
| 132 | + mail: 'err@test.com', |
| 133 | + userPrincipalName: 'err@test.com', |
| 134 | + title: 'Whoops' |
| 135 | + }, |
| 136 | + displayName: 'Error User' |
| 137 | + }; |
| 138 | + |
| 139 | + ldapStub.isUserInAdGroup.rejects(new Error('LDAP error')); |
| 140 | + |
| 141 | + const done = sinon.spy(); |
| 142 | + |
| 143 | + await strategyCallback(mockReq, mockProfile, {}, done); |
| 144 | + |
| 145 | + expect(done.calledOnce).to.be.true; |
| 146 | + const [err, user] = done.firstCall.args; |
| 147 | + expect(err).to.contain('LDAP error'); |
| 148 | + expect(user).to.be.null; |
| 149 | + }); |
| 150 | +}); |
0 commit comments