1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 | const expect = require('chai').expect
|
6 | const ActiveDirectory = require('../index').promiseWrapper
|
7 | const config = require('./config')
|
8 |
|
9 | let server = require('./mockServer')
|
10 |
|
11 | describe('Promised findUser Method', function () {
|
12 | let ad
|
13 | const settings = require('./settings').findUser
|
14 |
|
15 | before(function (done) {
|
16 | server(function (s) {
|
17 | ad = new ActiveDirectory(config)
|
18 | server = s
|
19 | done()
|
20 | })
|
21 | })
|
22 |
|
23 | describe('#findUser()', function () {
|
24 | [ 'userPrincipalName', 'sAMAccountName', 'dn' ].forEach((userAttribute) => {
|
25 | const username = settings.username[userAttribute]
|
26 | it(`should return user for (${userAttribute}) ${username}`, function (done) {
|
27 | ad.findUser(username)
|
28 | .then((user) => {
|
29 | expect(user).to.not.be.null
|
30 | done()
|
31 | })
|
32 | .catch(done)
|
33 | })
|
34 | })
|
35 |
|
36 | it('should return undefined if the username doesn\'t exist', function (done) {
|
37 | ad.findUser('!!!NON-EXISTENT USER!!!')
|
38 | .then((user) => {
|
39 | expect(user).to.be.undefined
|
40 | done()
|
41 | })
|
42 | .catch(done)
|
43 | })
|
44 |
|
45 | it('should return default user attributes when not specified', function (done) {
|
46 | const defaultAttributes = ad.defaultAttributes.user
|
47 | ad.findUser(settings.username.userPrincipalName)
|
48 | .then((user) => {
|
49 | expect(user).to.not.be.null
|
50 |
|
51 | const attributes = Object.keys(user)
|
52 | expect(attributes.length).to.equal(defaultAttributes.length)
|
53 | expect(attributes).to.be.any.members(defaultAttributes)
|
54 |
|
55 | done()
|
56 | })
|
57 | .catch(done)
|
58 | })
|
59 | })
|
60 |
|
61 | describe('#findUser(opts)', function () {
|
62 | it('should use the custom opts.filter if provided', function (done) {
|
63 | const opts = {
|
64 | filter: settings.opts.custom
|
65 | }
|
66 | const username = settings.username.userPrincipalName
|
67 | ad.findUser(opts, username)
|
68 | .then((user) => {
|
69 | expect(user).to.not.be.null
|
70 | expect(user.userPrincipalName).to.not.equal(username)
|
71 | done()
|
72 | })
|
73 | .catch(done)
|
74 | })
|
75 |
|
76 | it('should include groups/membership if opts.includeMembership[] = [ \'all\' ]', function (done) {
|
77 | const opts = {
|
78 | includeMembership: [ 'all' ]
|
79 | }
|
80 | const username = settings.username.userPrincipalName
|
81 | ad.findUser(opts, username)
|
82 | .then((user) => {
|
83 | expect(user).to.not.be.null
|
84 | expect(user.groups.length).to.be.gte(settings.groups.length)
|
85 |
|
86 | const cns = user.groups.map((g) => g.cn)
|
87 | expect(cns).to.deep.include.members(settings.groups)
|
88 |
|
89 | done()
|
90 | })
|
91 | .catch(done)
|
92 | })
|
93 |
|
94 | it('should include groups/membership if opts.includeMembership[] = [ \'user\' ]', function (done) {
|
95 | const opts = {
|
96 | includeMembership: [ 'user' ]
|
97 | }
|
98 | const username = settings.username.userPrincipalName
|
99 | ad.findUser(opts, username)
|
100 | .then((user) => {
|
101 | expect(user).to.not.be.null
|
102 | expect(user.groups.length).to.be.gte(settings.groups.length)
|
103 |
|
104 | const cns = user.groups.map((g) => g.cn)
|
105 | expect(cns).to.deep.include.members(settings.groups)
|
106 |
|
107 | done()
|
108 | })
|
109 | .catch(done)
|
110 | })
|
111 |
|
112 | it('should return expected groups/membership if opts.includeMembership enabled', function (done) {
|
113 | const opts = {
|
114 | includeMembership: [ 'user', 'all' ]
|
115 | }
|
116 | const username = settings.username.userPrincipalName
|
117 | ad.findUser(opts, username)
|
118 | .then((user) => {
|
119 | expect(user).to.not.be.null
|
120 | expect(user.groups.length).to.be.gte(settings.groups.length)
|
121 |
|
122 | const cns = user.groups.map((g) => g.cn)
|
123 | expect(cns).to.deep.include.members(settings.groups)
|
124 |
|
125 | done()
|
126 | })
|
127 | .catch(done)
|
128 | })
|
129 |
|
130 | it('should return only the first user if more than one result returned', function (done) {
|
131 | const opts = {
|
132 | filter: settings.opts.multipleFilter
|
133 | }
|
134 | ad.findUser(opts, '' )
|
135 | .then((user) => {
|
136 | expect(user).to.not.be.null
|
137 | expect(Array.isArray(user)).to.be.false
|
138 |
|
139 | done()
|
140 | })
|
141 | .catch(done)
|
142 | })
|
143 |
|
144 | it('should return only requested attributes', function (done) {
|
145 | const opts = {
|
146 | attributes: [ 'cn' ]
|
147 | }
|
148 | const username = settings.username.userPrincipalName
|
149 | ad.findUser(opts, username)
|
150 | .then((user) => {
|
151 | expect(user).to.not.be.null
|
152 |
|
153 | const keys = Object.keys(user)
|
154 | expect(keys.length).to.be.lte(opts.attributes.length)
|
155 | expect(keys).to.be.any.members(opts.attributes)
|
156 |
|
157 | done()
|
158 | })
|
159 | .catch(done)
|
160 | })
|
161 | })
|
162 | })
|