webhooks_spec.rb 7.1 KB

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