Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from ldapjs/fix-search-req-scope
Browse files Browse the repository at this point in the history
Fix SearchRequest scope property
  • Loading branch information
jsumners authored Mar 8, 2023
2 parents 6652801 + c19fa65 commit 80b6df8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
34 changes: 23 additions & 11 deletions lib/messages/search-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,15 @@ class SearchRequest extends LdapMessage {
}

/**
* The current search scope value.
* The current search scope value. Can be matched against the exported
* scope statics.
*
* @returns {string}
* @returns {number}
*
* @throws When the scope is set to an unrecognized scope constant.
*/
get scope () {
switch (this.#scope) {
case search.SCOPE_BASE_OBJECT:
return 'base'
case search.SCOPE_ONE_LEVEL:
return 'single'
case search.SCOPE_SUBTREE:
return 'subtree'
}
return this.#scope
}

/**
Expand All @@ -318,6 +312,24 @@ class SearchRequest extends LdapMessage {
this.#scope = resolvedScope
}

/**
* The current search scope value as a string name.
*
* @returns {string} One of 'base', 'single', or 'subtree'.
*
* @throws When the scope is set to an unrecognized scope constant.
*/
get scopeName () {
switch (this.#scope) {
case search.SCOPE_BASE_OBJECT:
return 'base'
case search.SCOPE_ONE_LEVEL:
return 'single'
case search.SCOPE_SUBTREE:
return 'subtree'
}
}

/**
* The number of entries to limit search results to.
*
Expand Down Expand Up @@ -424,7 +436,7 @@ class SearchRequest extends LdapMessage {
*/
_pojo (obj = {}) {
obj.baseObject = this.baseObject.toString()
obj.scope = this.scope
obj.scope = this.scopeName
obj.derefAliases = this.derefAliases
obj.sizeLimit = this.sizeLimit
obj.timeLimit = this.timeLimit
Expand Down
15 changes: 10 additions & 5 deletions lib/messages/search-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,24 @@ tap.test('.filter', t => {
tap.test('.scope', t => {
t.test('sets/gets', async t => {
const req = new SearchRequest()
t.equal(req.scope, 'base')
t.equal(req.scopeName, 'base')
t.equal(req.scope, 0)

req.scope = SearchRequest.SCOPE_SINGLE
t.equal(req.scope, 'single')
t.equal(req.scopeName, 'single')
t.equal(req.scope, 1)

req.scope = SearchRequest.SCOPE_SUBTREE
t.equal(req.scope, 'subtree')
t.equal(req.scopeName, 'subtree')
t.equal(req.scope, 2)

req.scope = 'SUB'
t.equal(req.scope, 'subtree')
t.equal(req.scopeName, 'subtree')
t.equal(req.scope, 2)

req.scope = 'base'
t.equal(req.scope, 'base')
t.equal(req.scopeName, 'base')
t.equal(req.scope, 0)
})

t.test('throws for invalid value', async t => {
Expand Down

0 comments on commit 80b6df8

Please sign in to comment.