1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 | const expect = require('chai').expect
|
6 | const ldapjs = require('ldapjs')
|
7 | const ActiveDirectory = require('../index')
|
8 | const config = require('./config')
|
9 |
|
10 | let server = require('./mockServer')
|
11 |
|
12 | describe('find Method', function () {
|
13 | let ad
|
14 | const settings = require('./settings').find
|
15 | const timeout = 6000
|
16 |
|
17 | before(function (done) {
|
18 | server(function (s) {
|
19 | ad = new ActiveDirectory(config)
|
20 | server = s
|
21 | done()
|
22 | })
|
23 | })
|
24 |
|
25 | describe('#find()', function () {
|
26 | settings.queries.forEach(function (query) {
|
27 | const userCount = query.results.users.length
|
28 | const groupCount = query.results.groups.length
|
29 | const otherCount = query.results.other.length
|
30 | const _query = (query.query.filter) ? query.query.filter : query.query
|
31 | it(`should return ${userCount} users, ${groupCount} groups, ${otherCount} other for query '${_query}'`, function (done) {
|
32 | this.timeout(timeout)
|
33 |
|
34 | ad.find(_query, function (err, results) {
|
35 | expect(err).to.be.null
|
36 | expect(results).to.not.be.null;
|
37 |
|
38 | ['users', 'groups', 'other'].forEach((key) => {
|
39 | const expectedResults = query.results[key]
|
40 | const actualResults = results[key]
|
41 |
|
42 | expect(actualResults.length).to.equal(expectedResults.length)
|
43 |
|
44 | const cns = actualResults.map((result) => {
|
45 | return result.cn
|
46 | })
|
47 | expectedResults.forEach((expectedResult) => {
|
48 | expect(cns.filter((cn) => {
|
49 | return cn
|
50 | .toLowerCase()
|
51 | .indexOf(expectedResult.toLowerCase()) !== -1
|
52 | }).length)
|
53 | .to.equal(1)
|
54 | })
|
55 | })
|
56 |
|
57 | done()
|
58 | })
|
59 | })
|
60 | })
|
61 | it('should return default query attributes when not specified', function (done) {
|
62 | const defaultAttributes = {
|
63 | groups: ActiveDirectory.defaultAttributes.group,
|
64 | users: ActiveDirectory.defaultAttributes.user
|
65 | }
|
66 | defaultAttributes.other = Array.from(new Set(
|
67 | [].concat(defaultAttributes.groups, defaultAttributes.users)
|
68 | ))
|
69 |
|
70 | const query = settings.queries[0]
|
71 | ad.find(query.query, function (err, results) {
|
72 | expect(err).to.be.null
|
73 | expect(results).to.not.be.null;
|
74 |
|
75 | ['users', 'groups', 'other'].forEach((key) => {
|
76 | const keyAttributes = defaultAttributes[key]
|
77 | results[key].forEach((result) => {
|
78 | const attributes = Object.keys(result)
|
79 | expect(attributes.length).to.be.lte(keyAttributes.length)
|
80 | attributes.forEach((attribute) => {
|
81 | expect(keyAttributes).to.contain(attribute)
|
82 | })
|
83 | })
|
84 | })
|
85 |
|
86 | done()
|
87 | })
|
88 | })
|
89 | })
|
90 |
|
91 | describe('#find(opts)', function () {
|
92 | it('should include groups/membership groups and users if opts.includeMembership[] = [ \'all\' ]', function (done) {
|
93 | this.timeout(timeout)
|
94 |
|
95 | const query = settings.queries[0]
|
96 | const opts = {
|
97 | includeMembership: [ 'all' ],
|
98 | filter: query.query
|
99 | }
|
100 | ad.find(opts, function (err, results) {
|
101 | expect(err).to.be.null
|
102 | expect(results).to.not.be.null
|
103 |
|
104 | results['users'].forEach((user) => {
|
105 | expect(user.groups).to.exist
|
106 | })
|
107 |
|
108 | results['groups'].forEach((group) => {
|
109 | expect(group.groups).to.exist
|
110 | })
|
111 |
|
112 | results['other'].forEach((other) => {
|
113 | expect(other.groups).to.not.exist
|
114 | })
|
115 |
|
116 | done()
|
117 | })
|
118 | })
|
119 |
|
120 | it('should include groups/membership for groups if opts.includeMembership[] = [ \'group\' ]', function (done) {
|
121 | this.timeout(timeout)
|
122 |
|
123 | const query = settings.queries[0]
|
124 | const opts = {
|
125 | includeMembership: [ 'group' ],
|
126 | filter: query.query
|
127 | }
|
128 | ad.find(opts, function (err, results) {
|
129 | expect(err).to.be.null
|
130 | expect(results).to.not.be.null
|
131 |
|
132 | results['groups'].forEach((group) => {
|
133 | expect(group.groups).to.exist
|
134 | });
|
135 |
|
136 | ['users', 'other'].forEach((key) => {
|
137 | const items = results[key]
|
138 | items.forEach((item) => {
|
139 | expect(item.groups).to.not.exist
|
140 | })
|
141 | })
|
142 |
|
143 | done()
|
144 | })
|
145 | })
|
146 |
|
147 | it('should include groups/membership for users if opts.includeMembership[] = [ \'user\' ]', function (done) {
|
148 | this.timeout(timeout)
|
149 |
|
150 | const query = settings.queries[0]
|
151 | const opts = {
|
152 | includeMembership: [ 'user' ],
|
153 | filter: query.query
|
154 | }
|
155 | ad.find(opts, function (err, results) {
|
156 | expect(err).to.be.null
|
157 | expect(results).to.not.be.null
|
158 |
|
159 | results['users'].forEach((user) => {
|
160 | expect(user.groups).to.exist
|
161 | });
|
162 |
|
163 | ['groups', 'other'].forEach((key) => {
|
164 | const items = results[key]
|
165 | items.forEach((item) => {
|
166 | expect(item.groups).to.not.exist
|
167 | })
|
168 | })
|
169 |
|
170 | done()
|
171 | })
|
172 | })
|
173 |
|
174 | it('should not include groups/membership if opts.includeMembership disabled', function (done) {
|
175 | const query = settings.queries[0]
|
176 | const opts = {
|
177 | includeMembership: false,
|
178 | filter: query.query
|
179 | }
|
180 | ad.find(opts, function (err, results) {
|
181 | expect(err).to.be.null
|
182 | expect(results).to.not.be.null;
|
183 |
|
184 | ['users', 'groups', 'other'].forEach((key) => {
|
185 | const items = results[key]
|
186 | items.forEach((item) => {
|
187 | expect(item.groups).to.not.exist
|
188 | })
|
189 | })
|
190 |
|
191 | done()
|
192 | })
|
193 | })
|
194 |
|
195 | it('should return only requested attributes', function (done) {
|
196 | this.timeout(timeout)
|
197 | const query = settings.queries[0]
|
198 | const opts = {
|
199 | attributes: [ 'cn' ],
|
200 | filter: query.query
|
201 | }
|
202 | ad.find(opts, function (err, results) {
|
203 | expect(err).to.be.null
|
204 | expect(results).to.not.be.null;
|
205 |
|
206 | ['users', 'groups', 'other'].forEach((key) => {
|
207 | results[key].forEach((result) => {
|
208 | const keys = Object.keys(result)
|
209 | expect(keys.length).to.be.lte(opts.attributes.length)
|
210 | if (keys.length === opts.attributes.length) {
|
211 | expect(keys).to.deep.equal(opts.attributes)
|
212 | }
|
213 | })
|
214 | })
|
215 |
|
216 | done()
|
217 | })
|
218 | })
|
219 |
|
220 | it('should return err (ConnectionError) when connection timeouts', function (done) {
|
221 | new ActiveDirectory({
|
222 | url: 'ldap://example.com',
|
223 | connectTimeout: 100
|
224 | }).find({}, function (err, result) {
|
225 | expect(err).to.be.an.instanceOf(ldapjs.ConnectionError)
|
226 | expect(result).to.be.undefined
|
227 | done()
|
228 | })
|
229 | })
|
230 | })
|
231 | })
|