issue-923.test.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict'
  2. const tap = require('tap')
  3. const ldapjs = require('../../lib')
  4. const { DN } = require('@ldapjs/dn')
  5. const Change = require('@ldapjs/change')
  6. const SCHEME = process.env.SCHEME || 'ldap'
  7. const HOST = process.env.HOST || '127.0.0.1'
  8. const PORT = process.env.PORT || 389
  9. const baseURL = `${SCHEME}://${HOST}:${PORT}`
  10. const client = ldapjs.createClient({ url: baseURL })
  11. tap.teardown(() => {
  12. client.unbind()
  13. })
  14. tap.test('modifies entry specified by dn string', t => {
  15. t.plan(4)
  16. client.bind('cn=admin,dc=planetexpress,dc=com', 'GoodNewsEveryone', (err) => {
  17. t.error(err, 'bind error')
  18. })
  19. const dn = 'cn=large10,ou=large_ou,dc=planetexpress,dc=com'
  20. const change = new Change({
  21. operation: 'replace',
  22. modification: {
  23. type: 'givenName',
  24. values: ['test']
  25. }
  26. })
  27. client.modify(dn, change, (err) => {
  28. t.error(err, 'modify error')
  29. validateChange({ t, expected: 'test', client })
  30. })
  31. })
  32. tap.test('modifies entry specified by dn object', t => {
  33. t.plan(4)
  34. client.bind('cn=admin,dc=planetexpress,dc=com', 'GoodNewsEveryone', (err) => {
  35. t.error(err, 'bind error')
  36. })
  37. const dn = DN.fromString('cn=large10,ou=large_ou,dc=planetexpress,dc=com')
  38. const change = new Change({
  39. operation: 'replace',
  40. modification: {
  41. type: 'givenName',
  42. values: ['test2']
  43. }
  44. })
  45. client.modify(dn, change, (err) => {
  46. t.error(err, 'modify error')
  47. validateChange({ t, expected: 'test2', client })
  48. })
  49. })
  50. function validateChange ({ t, expected, client }) {
  51. const searchBase = 'ou=large_ou,dc=planetexpress,dc=com'
  52. const searchOpts = {
  53. filter: '(cn=large10)',
  54. scope: 'subtree',
  55. attributes: ['givenName'],
  56. sizeLimit: 10,
  57. timeLimit: 0
  58. }
  59. client.search(searchBase, searchOpts, (err, res) => {
  60. t.error(err, 'search error')
  61. res.on('searchEntry', entry => {
  62. t.equal(
  63. entry.attributes.filter(a => a.type === 'givenName').pop().values.pop(),
  64. expected
  65. )
  66. })
  67. res.on('error', err => {
  68. t.error(err, 'search entry error')
  69. })
  70. res.on('end', () => {
  71. t.end()
  72. })
  73. })
  74. }