compare-request.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict'
  2. const tap = require('tap')
  3. const { operations } = require('@ldapjs/protocol')
  4. const { BerReader, BerWriter } = require('@ldapjs/asn1')
  5. const { DN } = require('@ldapjs/dn')
  6. const CompareRequest = require('./compare-request')
  7. const { compareRequestBytes } = require('./_fixtures/message-byte-arrays')
  8. tap.test('basic', t => {
  9. t.test('constructor no args', async t => {
  10. const req = new CompareRequest()
  11. t.strictSame(req.pojo, {
  12. messageId: 1,
  13. protocolOp: operations.LDAP_REQ_COMPARE,
  14. type: 'CompareRequest',
  15. entry: null,
  16. attribute: '',
  17. value: '',
  18. controls: []
  19. })
  20. })
  21. t.test('constructor with args', async t => {
  22. const req = new CompareRequest({
  23. entry: 'dn=foo,dc=example,dc=com',
  24. attribute: 'foo',
  25. value: 'bar'
  26. })
  27. t.strictSame(req.pojo, {
  28. messageId: 1,
  29. protocolOp: operations.LDAP_REQ_COMPARE,
  30. type: 'CompareRequest',
  31. entry: 'dn=foo,dc=example,dc=com',
  32. attribute: 'foo',
  33. value: 'bar',
  34. controls: []
  35. })
  36. })
  37. t.test('.type', async t => {
  38. const req = new CompareRequest()
  39. t.equal(req.type, 'CompareRequest')
  40. })
  41. t.test('.dn', async t => {
  42. const req = new CompareRequest({ entry: 'dn=foo,dc=example,dc=com' })
  43. t.equal(req.dn.toString(), 'dn=foo,dc=example,dc=com')
  44. })
  45. t.end()
  46. })
  47. tap.test('.attribute', t => {
  48. t.test('gets and sets', async t => {
  49. const req = new CompareRequest()
  50. t.equal(req.attribute, '')
  51. req.attribute = 'foo'
  52. t.equal(req.attribute, 'foo')
  53. })
  54. t.end()
  55. })
  56. tap.test('.entry', t => {
  57. t.test('gets and sets', async t => {
  58. const req = new CompareRequest()
  59. t.equal(req.entry, null)
  60. req.entry = 'cn=foo'
  61. t.equal(req.entry.toString(), 'cn=foo')
  62. req.entry = DN.fromString('sn=bar')
  63. t.equal(req.entry.toString(), 'sn=bar')
  64. })
  65. t.test('throws for bad value', async t => {
  66. const req = new CompareRequest()
  67. t.throws(
  68. () => {
  69. req.entry = { cn: 'foo' }
  70. },
  71. 'entry must be a valid DN string or instance of LdapDn'
  72. )
  73. })
  74. t.end()
  75. })
  76. tap.test('.value', t => {
  77. t.test('gets and sets', async t => {
  78. const req = new CompareRequest()
  79. t.equal(req.value, '')
  80. req.value = 'foo'
  81. t.equal(req.value, 'foo')
  82. })
  83. t.end()
  84. })
  85. tap.test('_toBer', t => {
  86. t.test('converts instance to BER', async t => {
  87. const req = new CompareRequest({
  88. messageId: 2,
  89. entry: 'uid=jdoe,ou=People,dc=example,dc=com',
  90. attribute: 'employeeType',
  91. value: 'salaried'
  92. })
  93. const writer = new BerWriter()
  94. req._toBer(writer)
  95. t.equal(
  96. Buffer.from(compareRequestBytes.slice(5)).compare(writer.buffer),
  97. 0
  98. )
  99. })
  100. t.end()
  101. })
  102. tap.test('_pojo', t => {
  103. t.test('returns a pojo representation', async t => {
  104. const req = new CompareRequest({
  105. entry: 'cn=foo',
  106. attribute: 'bar',
  107. value: 'baz'
  108. })
  109. t.strictSame(req._pojo(), {
  110. entry: 'cn=foo',
  111. attribute: 'bar',
  112. value: 'baz'
  113. })
  114. })
  115. t.end()
  116. })
  117. tap.test('#parseToPojo', t => {
  118. t.test('throws if operation incorrect', async t => {
  119. const reqBuffer = Buffer.from(compareRequestBytes)
  120. reqBuffer[5] = 0x61
  121. const reader = new BerReader(reqBuffer)
  122. reader.readSequence()
  123. reader.readInt()
  124. t.throws(
  125. () => CompareRequest.parseToPojo(reader),
  126. Error('found wrong protocol operation: 0x61')
  127. )
  128. })
  129. t.test('returns a pojo representation', async t => {
  130. const reqBuffer = Buffer.from(compareRequestBytes)
  131. const reader = new BerReader(reqBuffer)
  132. reader.readSequence()
  133. reader.readInt()
  134. const pojo = CompareRequest.parseToPojo(reader)
  135. t.equal(pojo.protocolOp, operations.LDAP_REQ_COMPARE)
  136. t.equal(pojo.entry, 'uid=jdoe,ou=People,dc=example,dc=com')
  137. t.equal(pojo.attribute, 'employeeType')
  138. t.equal(pojo.value, 'salaried')
  139. })
  140. t.end()
  141. })