webhooks_spec.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Manage > Webhook', type: :system do
  4. context 'when showing the example payload' do
  5. it 'shows correctly' do
  6. visit '/#manage/webhook'
  7. within :active_content do
  8. click 'a[data-type="payload"]'
  9. end
  10. in_modal do
  11. expect(page).to have_text('X-Zammad-Trigger:')
  12. end
  13. end
  14. end
  15. context 'when creating new webhook' do
  16. let(:custom_payload) { JSON.pretty_generate(Webhook::PreDefined::Mattermost.new.custom_payload) }
  17. before do
  18. visit '/#manage/webhook'
  19. within :active_content do
  20. click 'button[data-toggle="dropdown"]'
  21. click '.dropdown-menu [role="menuitem"]', text: 'Pre-defined Webhook'
  22. end
  23. end
  24. it 'provides pre-defined webhooks' do
  25. in_modal do
  26. find('[name="pre_defined_webhook_id"]').select 'Mattermost Notifications'
  27. click_button 'Next'
  28. expect(page).to have_field('Name', with: 'Mattermost Notifications')
  29. expect(page).to have_select('Pre-defined Webhook', text: 'Mattermost Notifications', disabled: :all)
  30. expect(page).to have_field('Messaging Username', with: '')
  31. expect(page).to have_field('Messaging Channel', with: '')
  32. expect(page).to have_field('Messaging Icon URL', with: 'https://zammad.com/assets/images/logo-200x200.png')
  33. expect(page).to have_field('Custom Payload', checked: false, visible: :all)
  34. expect(page).to have_field('custom_payload', with: custom_payload, disabled: :all, visible: :all)
  35. expect(page).to have_field('Note', with: 'Pre-defined webhook for Mattermost Notifications.')
  36. fill_in 'Endpoint', with: 'https://example.com/mattermost_endpoint'
  37. fill_in 'Messaging Username', with: 'username'
  38. fill_in 'Messaging Channel', with: '#channel'
  39. click_button 'Submit'
  40. end
  41. expect(Webhook.last).to have_attributes(
  42. name: 'Mattermost Notifications',
  43. pre_defined_webhook_type: 'Mattermost',
  44. customized_payload: false,
  45. custom_payload: nil,
  46. note: 'Pre-defined webhook for Mattermost Notifications.',
  47. preferences: include(
  48. pre_defined_webhook: include(
  49. messaging_username: 'username',
  50. messaging_channel: '#channel',
  51. messaging_icon_url: 'https://zammad.com/assets/images/logo-200x200.png',
  52. ),
  53. ),
  54. )
  55. end
  56. context 'with customized payload' do
  57. let(:custom_payload) { JSON.pretty_generate(Webhook::PreDefined::RocketChat.new.custom_payload) }
  58. it 'overrides pre-defined payload' do
  59. in_modal do
  60. find('[name="pre_defined_webhook_id"]').select 'Rocket Chat Notifications'
  61. click_button 'Next'
  62. expect(page).to have_field('Custom Payload', checked: false, visible: :all)
  63. expect(page).to have_field('custom_payload', with: custom_payload, disabled: :all, visible: :all)
  64. fill_in 'Endpoint', with: 'https://example.com/rocketchat_endpoint'
  65. click 'label[for="attribute-customized_payload"]'
  66. click_button 'Submit'
  67. end
  68. expect(Webhook.last).to have_attributes(
  69. pre_defined_webhook_type: 'RocketChat',
  70. customized_payload: true,
  71. custom_payload: custom_payload.gsub(%r{\n}, "\r\n"),
  72. )
  73. end
  74. end
  75. end
  76. context 'when editing existing webhook' do
  77. let!(:webhook) { create(:mattermost_webhook) }
  78. let(:custom_payload) { JSON.pretty_generate(Webhook::PreDefined::Mattermost.new.custom_payload) }
  79. before do
  80. visit '/#manage/webhook'
  81. within :active_content do
  82. click "tr[data-id='#{webhook.id}'] td:first-child"
  83. end
  84. end
  85. it 'supports pre-defined webhooks' do
  86. in_modal do
  87. expect(page).to have_select('Pre-defined Webhook', text: 'Mattermost Notifications', disabled: :all)
  88. expect(page).to have_field('Messaging Username', with: webhook.preferences['pre_defined_webhook']['messaging_username'])
  89. expect(page).to have_field('Messaging Channel', with: webhook.preferences['pre_defined_webhook']['messaging_channel'])
  90. expect(page).to have_field('Messaging Icon URL', with: webhook.preferences['pre_defined_webhook']['messaging_icon_url'])
  91. expect(page).to have_field('Custom Payload', checked: false, visible: :all)
  92. expect(page).to have_field('custom_payload', with: custom_payload, disabled: :all, visible: :all)
  93. fill_in 'Messaging Username', with: 'username'
  94. fill_in 'Messaging Channel', with: '#channel'
  95. fill_in 'Messaging Icon URL', with: 'https://example.com/logo.png'
  96. click_button 'Submit'
  97. end
  98. expect(webhook.reload).to have_attributes(
  99. preferences: include(
  100. pre_defined_webhook: include(
  101. messaging_username: 'username',
  102. messaging_channel: '#channel',
  103. messaging_icon_url: 'https://example.com/logo.png',
  104. ),
  105. ),
  106. )
  107. end
  108. context 'with customized payload' do
  109. let!(:webhook) { create(:rocketchat_webhook, customized_payload: true, custom_payload: '{}') }
  110. it 'resets custom payload' do
  111. in_modal do
  112. expect(page).to have_field('Custom Payload', checked: true, visible: :all)
  113. expect(page).to have_field('custom_payload', with: webhook.custom_payload, disabled: :all, visible: :all)
  114. click 'label[for="attribute-customized_payload"]'
  115. click_button 'Submit'
  116. end
  117. expect(webhook.reload).to have_attributes(
  118. customized_payload: false,
  119. custom_payload: nil,
  120. )
  121. end
  122. end
  123. end
  124. context 'when checking custom payload validation' do
  125. it 'shows error message' do
  126. visit '/#manage/webhook'
  127. within :active_content do
  128. click 'button[data-type="new"]'
  129. end
  130. in_modal do
  131. fill_in 'name', with: 'Test'
  132. fill_in 'endpoint', with: 'https://example.com/webhook'
  133. click 'label[for="attribute-customized_payload"]'
  134. find(:code_editor, 'custom_payload').send_keys 'invalid json'
  135. expect(page).to have_css('div.CodeMirror-lint-marker-error')
  136. click '.js-submit'
  137. expect(page).to have_css('div[data-attribute-name="custom_payload"].has-error')
  138. .and have_text('Please enter a valid JSON string.')
  139. end
  140. end
  141. end
  142. context 'when deleting' do
  143. let!(:webhook) { create(:webhook) }
  144. let!(:trigger) { create(:trigger, perform: { 'notification.webhook' => { 'webhook_id' => webhook.id.to_s } }) }
  145. it 'referenced webhook shows error message' do
  146. visit '/#manage/webhook'
  147. within :active_content do
  148. click '.js-action'
  149. click '.js-delete'
  150. end
  151. in_modal do
  152. click '.js-submit'
  153. expect(page).to have_text('Cannot delete').and(have_text("##{trigger.id}"))
  154. end
  155. end
  156. end
  157. end