rerunArguments.test.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import {
  4. buildArgumentFormQuery,
  5. buildRerunStartActionArgs,
  6. hasMissingRerunArguments,
  7. logEntryArgumentsToStartActionArgs,
  8. rerunNeedsArgumentForm
  9. } from '../utils/rerunArguments.js'
  10. test('logEntryArgumentsToStartActionArgs maps proto arguments for StartAction', () => {
  11. assert.deepEqual(
  12. logEntryArgumentsToStartActionArgs({
  13. arguments: [
  14. { name: 'host', value: 'example.com' },
  15. { name: 'port', value: '443' }
  16. ]
  17. }),
  18. [
  19. { name: 'host', value: 'example.com' },
  20. { name: 'port', value: '443' }
  21. ]
  22. )
  23. })
  24. test('hasMissingRerunArguments requires password fields to be re-entered', () => {
  25. const action = {
  26. arguments: [
  27. { name: 'user', type: 'ascii_identifier', required: true },
  28. { name: 'pass', type: 'password', required: true }
  29. ]
  30. }
  31. assert.equal(
  32. hasMissingRerunArguments(action, [{ name: 'user', value: 'alice' }]),
  33. true
  34. )
  35. })
  36. test('hasMissingRerunArguments requires very_dangerous_raw_string fields to be re-entered', () => {
  37. const action = {
  38. arguments: [
  39. { name: 'host', type: 'ascii_identifier', required: true },
  40. { name: 'payload', type: 'very_dangerous_raw_string', required: false }
  41. ]
  42. }
  43. assert.equal(
  44. hasMissingRerunArguments(action, [{ name: 'host', value: 'db-1' }]),
  45. true
  46. )
  47. })
  48. test('hasMissingRerunArguments detects missing required stored arguments', () => {
  49. const action = {
  50. arguments: [{ name: 'host', type: 'ascii_identifier', required: true }]
  51. }
  52. assert.equal(hasMissingRerunArguments(action, []), true)
  53. assert.equal(
  54. hasMissingRerunArguments(action, [{ name: 'host', value: 'db-1' }]),
  55. false
  56. )
  57. })
  58. test('rerunNeedsArgumentForm can start directly when stored args are complete', () => {
  59. const action = {
  60. arguments: [{ name: 'host', type: 'ascii_identifier', required: true }]
  61. }
  62. const logEntry = {
  63. arguments: [{ name: 'host', value: 'db-1' }]
  64. }
  65. assert.equal(rerunNeedsArgumentForm(action, logEntry), false)
  66. })
  67. test('rerunNeedsArgumentForm opens the form when justification is missing', () => {
  68. const action = { justification: true, arguments: [] }
  69. assert.equal(rerunNeedsArgumentForm(action, {}), true)
  70. assert.equal(
  71. rerunNeedsArgumentForm(action, { justification: 'approved change' }),
  72. false
  73. )
  74. })
  75. test('buildRerunStartActionArgs includes stored justification', () => {
  76. assert.deepEqual(
  77. buildRerunStartActionArgs('binding-1', {
  78. arguments: [{ name: 'host', value: 'db-1' }],
  79. justification: 'maintenance window'
  80. }, {
  81. justification: true,
  82. arguments: [{ name: 'host', type: 'ascii_identifier' }]
  83. }),
  84. {
  85. bindingId: 'binding-1',
  86. arguments: [{ name: 'host', value: 'db-1' }],
  87. justification: 'maintenance window'
  88. }
  89. )
  90. })
  91. test('buildArgumentFormQuery prefills non-password stored arguments', () => {
  92. assert.deepEqual(
  93. buildArgumentFormQuery({
  94. arguments: [
  95. { name: 'host', value: 'db-1' },
  96. { name: 'port', value: '5432' }
  97. ]
  98. }),
  99. {
  100. host: 'db-1',
  101. port: '5432'
  102. }
  103. )
  104. })