webhooks_controller_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Webhook, type: :request do
  4. let(:agent) { create(:agent) }
  5. let(:admin) { create(:admin) }
  6. describe 'request handling', authenticated_as: :admin do
  7. context 'when listing webhooks' do
  8. let!(:webhooks) { create_list(:webhook, 10) }
  9. before do
  10. get '/api/v1/webhooks.json'
  11. end
  12. it 'returns all' do
  13. expect(json_response.length).to eq(webhooks.length)
  14. end
  15. context 'with agent permissions', authenticated_as: :agent do
  16. it 'request is forbidden' do
  17. expect(response).to have_http_status(:forbidden)
  18. end
  19. end
  20. end
  21. context 'when showing webhook' do
  22. let!(:webhook) { create(:webhook) }
  23. before do
  24. get "/api/v1/webhooks/#{webhook.id}.json"
  25. end
  26. it 'returns ok' do
  27. expect(response).to have_http_status(:ok)
  28. end
  29. context 'with inactive template' do
  30. let!(:webhook) { create(:webhook, active: false) } # rubocop:disable RSpec/LetSetup
  31. it 'returns ok' do
  32. expect(response).to have_http_status(:ok)
  33. end
  34. end
  35. context 'with agent permissions', authenticated_as: :agent do
  36. it 'request is forbidden' do
  37. expect(response).to have_http_status(:forbidden)
  38. end
  39. end
  40. end
  41. context 'when creating webhook' do
  42. before do
  43. post '/api/v1/webhooks.json', params: { name: 'Foo', endpoint: 'http://example.com/endpoint', ssl_verify: true, active: true }
  44. end
  45. it 'returns created' do
  46. expect(response).to have_http_status(:created)
  47. end
  48. context 'with agent permissions', authenticated_as: :agent do
  49. it 'request is forbidden' do
  50. expect(response).to have_http_status(:forbidden)
  51. end
  52. end
  53. end
  54. context 'when updating webhook' do
  55. let!(:webhook) { create(:webhook) }
  56. before do
  57. put "/api/v1/webhooks/#{webhook.id}.json", params: { name: 'Foo' }
  58. end
  59. it 'returns ok' do
  60. expect(response).to have_http_status(:ok)
  61. end
  62. context 'with agent permissions', authenticated_as: :agent do
  63. it 'request is forbidden' do
  64. expect(response).to have_http_status(:forbidden)
  65. end
  66. end
  67. end
  68. context 'when destroying webhook' do
  69. let!(:webhook) { create(:webhook) }
  70. before do
  71. delete "/api/v1/webhooks/#{webhook.id}.json"
  72. end
  73. it 'returns ok' do
  74. expect(response).to have_http_status(:ok)
  75. end
  76. context 'with agent permissions', authenticated_as: :agent do
  77. it 'request is forbidden' do
  78. expect(response).to have_http_status(:forbidden)
  79. end
  80. end
  81. end
  82. context 'when fetching pre-defined webhooks' do
  83. before do
  84. get '/api/v1/webhooks/pre_defined.json'
  85. end
  86. it 'returns ok' do
  87. expect(response).to have_http_status(:ok)
  88. end
  89. it 'returns an array' do
  90. expect(json_response).to be_an_instance_of(Array)
  91. end
  92. context 'with agent permissions', authenticated_as: :agent do
  93. it 'request is forbidden' do
  94. expect(response).to have_http_status(:forbidden)
  95. end
  96. end
  97. end
  98. context 'when fetching custom payload replacements' do
  99. before do
  100. get '/api/v1/webhooks/payload/replacements.json'
  101. end
  102. it 'returns ok' do
  103. expect(response).to have_http_status(:ok)
  104. end
  105. it 'returns a hash' do
  106. expect(json_response).to be_an_instance_of(Hash)
  107. end
  108. it 'returns no webhook variables by default' do
  109. expect(json_response).not_to include('webhook')
  110. end
  111. context 'with agent permissions', authenticated_as: :agent do
  112. it 'request is forbidden' do
  113. expect(response).to have_http_status(:forbidden)
  114. end
  115. end
  116. context "when the pre-defined webhook type 'Mattermost' is used" do
  117. before do
  118. get '/api/v1/webhooks/payload/replacements?pre_defined_webhook_type=Mattermost'
  119. end
  120. it 'returns webhook variables' do
  121. expect(json_response).to include('webhook' => %w[messaging_username messaging_channel messaging_icon_url])
  122. end
  123. end
  124. context "when the pre-defined webhook type 'Slack' is used" do
  125. before do
  126. get '/api/v1/webhooks/payload/replacements?pre_defined_webhook_type=Slack'
  127. end
  128. it 'returns no webhook variables' do
  129. expect(json_response).not_to include('webhook')
  130. end
  131. end
  132. end
  133. end
  134. end