webhooks_controller_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. it 'returns all' do
  10. get '/api/v1/webhooks.json'
  11. expect(json_response.length).to eq(webhooks.length)
  12. end
  13. context 'with agent permissions', authenticated_as: :agent do
  14. it 'request is forbidden' do
  15. get '/api/v1/webhooks.json'
  16. expect(response).to have_http_status(:forbidden)
  17. end
  18. end
  19. end
  20. context 'when showing webhook' do
  21. let!(:webhook) { create(:webhook) }
  22. it 'returns ok' do
  23. get "/api/v1/webhooks/#{webhook.id}.json"
  24. expect(response).to have_http_status(:ok)
  25. end
  26. context 'with inactive template' do
  27. let!(:inactive_webhook) { create(:webhook, active: false) }
  28. it 'returns ok' do
  29. get "/api/v1/webhooks/#{inactive_webhook.id}.json"
  30. expect(response).to have_http_status(:ok)
  31. end
  32. end
  33. context 'with agent permissions', authenticated_as: :agent do
  34. it 'request is forbidden' do
  35. get "/api/v1/webhooks/#{webhook.id}.json"
  36. expect(response).to have_http_status(:forbidden)
  37. end
  38. end
  39. end
  40. context 'when creating webhook' do
  41. it 'returns created' do
  42. post '/api/v1/webhooks.json', params: { name: 'Foo', endpoint: 'http://example.com/endpoint', ssl_verify: true, active: true }
  43. expect(response).to have_http_status(:created)
  44. end
  45. context 'with agent permissions', authenticated_as: :agent do
  46. it 'request is forbidden' do
  47. post '/api/v1/webhooks.json', params: { name: 'Foo', endpoint: 'http://example.com/endpoint', ssl_verify: true, active: true }
  48. expect(response).to have_http_status(:forbidden)
  49. end
  50. end
  51. end
  52. context 'when updating webhook' do
  53. let!(:webhook) { create(:webhook) }
  54. it 'returns ok' do
  55. put "/api/v1/webhooks/#{webhook.id}.json", params: { name: 'Foo' }
  56. expect(response).to have_http_status(:ok)
  57. end
  58. context 'with agent permissions', authenticated_as: :agent do
  59. it 'request is forbidden' do
  60. put "/api/v1/webhooks/#{webhook.id}.json", params: { name: 'Foo' }
  61. expect(response).to have_http_status(:forbidden)
  62. end
  63. end
  64. end
  65. context 'when destroying webhook' do
  66. let!(:webhook) { create(:webhook) }
  67. it 'returns ok' do
  68. delete "/api/v1/webhooks/#{webhook.id}.json"
  69. expect(response).to have_http_status(:ok)
  70. end
  71. context 'with agent permissions', authenticated_as: :agent do
  72. it 'request is forbidden' do
  73. delete "/api/v1/webhooks/#{webhook.id}.json"
  74. expect(response).to have_http_status(:forbidden)
  75. end
  76. end
  77. end
  78. context 'when fetching custom payload replacements' do
  79. it 'returns ok' do
  80. get '/api/v1/webhooks/payload/replacements.json'
  81. expect(response).to have_http_status(:ok)
  82. end
  83. it 'returns a hash' do
  84. get '/api/v1/webhooks/payload/replacements.json'
  85. expect(json_response).to be_an_instance_of(Hash)
  86. end
  87. context 'with agent permissions', authenticated_as: :agent do
  88. it 'request is forbidden' do
  89. get '/api/v1/webhooks/payload/replacements.json'
  90. expect(response).to have_http_status(:forbidden)
  91. end
  92. end
  93. end
  94. end
  95. end