useSSLVerificationWarningHandler.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { type Ref } from 'vue'
  3. import {
  4. FormHandlerExecution,
  5. type ChangedField,
  6. type FormHandlerFunctionData,
  7. type FormHandlerFunctionReactivity,
  8. type FormValues,
  9. type FormSchemaField,
  10. } from '#shared/components/Form/types.ts'
  11. import { useSSLVerificationWarningHandler } from '../composables/useSSLVerificationWarningHandler.ts'
  12. import type { FormKitNode } from '@formkit/core'
  13. import type { Except, SetOptional } from 'type-fest'
  14. const getReactivity = (
  15. changeFields?: Ref<Record<string, Partial<FormSchemaField>>>,
  16. fields?: Record<
  17. string,
  18. {
  19. show: boolean
  20. updateFields: boolean
  21. props: Except<
  22. SetOptional<FormSchemaField, 'type'>,
  23. 'show' | 'props' | 'updateFields' | 'relation'
  24. >
  25. }
  26. >,
  27. values?: Record<string, unknown>,
  28. ) =>
  29. ({
  30. changeFields,
  31. schemaData: {
  32. fields: {
  33. ...fields,
  34. },
  35. ...values,
  36. },
  37. updateSchemaDataField: () => {},
  38. }) as unknown as FormHandlerFunctionReactivity
  39. const getData = (changedField?: ChangedField, values?: FormValues) => {
  40. const mockedSet = vi.fn()
  41. const mockedRemove = vi.fn()
  42. return {
  43. data: {
  44. formNode: {
  45. store: {
  46. set: mockedSet,
  47. remove: mockedRemove,
  48. },
  49. on: vi.fn(),
  50. } as unknown as FormKitNode,
  51. changedField,
  52. values,
  53. getNodeByName: vi.fn(),
  54. findNodeByName: vi.fn(),
  55. } as FormHandlerFunctionData,
  56. mockedSet,
  57. mockedRemove,
  58. }
  59. }
  60. describe('useSSLVerificationWarningHandler callback', () => {
  61. it('sets warning when sslVerify is turned off (initial)', async () => {
  62. const { callback } = useSSLVerificationWarningHandler()
  63. const reactivity = getReactivity(undefined, {
  64. sslVerify: {
  65. show: true,
  66. updateFields: false,
  67. props: { name: 'sslVerify', value: false },
  68. },
  69. })
  70. const { data, mockedRemove, mockedSet } = getData(undefined, {
  71. sslVerify: false,
  72. })
  73. callback(FormHandlerExecution.InitialSettled, reactivity, data)
  74. expect(mockedSet).toHaveBeenCalledWith({
  75. blocking: false,
  76. key: 'sslVerificationWarning',
  77. meta: {},
  78. type: 'warning',
  79. value:
  80. 'Turning off SSL verification is a security risk and should be used only temporary. Use this option at your own risk!',
  81. visible: true,
  82. })
  83. expect(mockedRemove).not.toBeCalled()
  84. })
  85. it('sets warning when sslVerify is turned off (change)', async () => {
  86. const { callback } = useSSLVerificationWarningHandler()
  87. const reactivity = getReactivity(undefined, {
  88. sslVerify: {
  89. show: true,
  90. updateFields: false,
  91. props: { name: 'sslVerify', value: true },
  92. },
  93. })
  94. const { data, mockedRemove, mockedSet } = getData(
  95. {
  96. name: 'sslVerify',
  97. newValue: false,
  98. oldValue: true,
  99. },
  100. { sslVerify: true },
  101. )
  102. callback(FormHandlerExecution.FieldChange, reactivity, data)
  103. expect(mockedSet).toHaveBeenCalledWith({
  104. blocking: false,
  105. key: 'sslVerificationWarning',
  106. meta: {},
  107. type: 'warning',
  108. value:
  109. 'Turning off SSL verification is a security risk and should be used only temporary. Use this option at your own risk!',
  110. visible: true,
  111. })
  112. expect(mockedRemove).not.toBeCalled()
  113. })
  114. it('clears warning when sslVerify is turned on (change)', async () => {
  115. const { callback } = useSSLVerificationWarningHandler()
  116. const reactivity = getReactivity(undefined, {
  117. sslVerify: {
  118. show: true,
  119. updateFields: false,
  120. props: { name: 'sslVerify', value: false },
  121. },
  122. })
  123. const { data, mockedRemove, mockedSet } = getData(
  124. {
  125. name: 'sslVerify',
  126. newValue: true,
  127. oldValue: false,
  128. },
  129. { sslVerify: false },
  130. )
  131. callback(FormHandlerExecution.FieldChange, reactivity, data)
  132. expect(mockedSet).not.toBeCalled()
  133. expect(mockedRemove).toBeCalledWith('sslVerificationWarning')
  134. })
  135. it('clears warning when sslVerify is disabled', async () => {
  136. const { callback } = useSSLVerificationWarningHandler()
  137. const reactivity = getReactivity(undefined, {
  138. sslVerify: {
  139. show: true,
  140. updateFields: false,
  141. props: { disabled: true, name: 'sslVerify', value: false },
  142. },
  143. })
  144. const { data, mockedRemove, mockedSet } = getData(
  145. {
  146. name: 'sslVerify',
  147. newValue: true,
  148. oldValue: false,
  149. },
  150. { sslVerify: false },
  151. )
  152. callback(FormHandlerExecution.FieldChange, reactivity, data)
  153. expect(mockedSet).not.toBeCalled()
  154. expect(mockedRemove).toBeCalledWith('sslVerificationWarning')
  155. })
  156. it('sets warning when sslVerify is enabled', async () => {
  157. const { callback } = useSSLVerificationWarningHandler()
  158. const reactivity = getReactivity(undefined, {
  159. sslVerify: {
  160. show: true,
  161. updateFields: false,
  162. props: { disabled: false, name: 'sslVerify', value: true },
  163. },
  164. })
  165. const { data, mockedRemove, mockedSet } = getData(
  166. {
  167. name: 'sslVerify',
  168. newValue: false,
  169. oldValue: true,
  170. },
  171. { sslVerify: true },
  172. )
  173. callback(FormHandlerExecution.FieldChange, reactivity, data)
  174. expect(mockedSet).toHaveBeenCalledWith({
  175. blocking: false,
  176. key: 'sslVerificationWarning',
  177. meta: {},
  178. type: 'warning',
  179. value:
  180. 'Turning off SSL verification is a security risk and should be used only temporary. Use this option at your own risk!',
  181. visible: true,
  182. })
  183. expect(mockedRemove).not.toBeCalled()
  184. })
  185. it('does not execute when sslVerify is not present', async () => {
  186. const { callback } = useSSLVerificationWarningHandler()
  187. const reactivity = getReactivity()
  188. const { data, mockedRemove, mockedSet } = getData()
  189. callback(FormHandlerExecution.InitialSettled, reactivity, data)
  190. expect(mockedSet).not.toBeCalled()
  191. expect(mockedRemove).not.toBeCalled()
  192. })
  193. it('does not execute when another field is changed', async () => {
  194. const { callback } = useSSLVerificationWarningHandler()
  195. const reactivity = getReactivity(undefined, {
  196. sslVerify: {
  197. show: true,
  198. updateFields: false,
  199. props: { name: 'sslVerify', value: false },
  200. },
  201. })
  202. const { data, mockedRemove, mockedSet } = getData(
  203. {
  204. name: 'foobar',
  205. newValue: false,
  206. oldValue: true,
  207. },
  208. { sslVerify: false },
  209. )
  210. callback(FormHandlerExecution.FieldChange, reactivity, data)
  211. expect(mockedSet).not.toBeCalled()
  212. expect(mockedRemove).not.toBeCalled()
  213. })
  214. })