UNPKG

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