webhooks_controller_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::WebhooksController do
  4. render_views
  5. let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  6. before do
  7. sign_in user, scope: :user
  8. end
  9. describe 'GET #index' do
  10. it 'returns http success' do
  11. get :index
  12. expect(response).to have_http_status(:success)
  13. end
  14. end
  15. describe 'GET #new' do
  16. it 'returns http success and renders view' do
  17. get :new
  18. expect(response).to have_http_status(:success)
  19. expect(response).to render_template(:new)
  20. end
  21. end
  22. describe 'POST #create' do
  23. it 'creates a new webhook record with valid data' do
  24. expect do
  25. post :create, params: { webhook: { url: 'https://example.com/hook', events: ['account.approved'] } }
  26. end.to change(Webhook, :count).by(1)
  27. expect(response).to be_redirect
  28. end
  29. it 'does not create a new webhook record with invalid data' do
  30. expect do
  31. post :create, params: { webhook: { url: 'https://example.com/hook', events: [] } }
  32. end.to_not change(Webhook, :count)
  33. expect(response).to have_http_status(:success)
  34. expect(response).to render_template(:new)
  35. end
  36. end
  37. context 'with an existing record' do
  38. let!(:webhook) { Fabricate(:webhook, events: ['account.created', 'report.created']) }
  39. describe 'GET #show' do
  40. it 'returns http success and renders view' do
  41. get :show, params: { id: webhook.id }
  42. expect(response).to have_http_status(:success)
  43. expect(response).to render_template(:show)
  44. end
  45. end
  46. describe 'GET #edit' do
  47. it 'returns http success and renders view' do
  48. get :edit, params: { id: webhook.id }
  49. expect(response).to have_http_status(:success)
  50. expect(response).to render_template(:edit)
  51. end
  52. end
  53. describe 'PUT #update' do
  54. it 'updates the record with valid data' do
  55. put :update, params: { id: webhook.id, webhook: { url: 'https://example.com/new/location' } }
  56. expect(webhook.reload.url).to match(%r{new/location})
  57. expect(response).to redirect_to(admin_webhook_path(webhook))
  58. end
  59. it 'does not update the record with invalid data' do
  60. expect do
  61. put :update, params: { id: webhook.id, webhook: { url: '' } }
  62. end.to_not change(webhook, :url)
  63. expect(response).to have_http_status(:success)
  64. expect(response).to render_template(:edit)
  65. end
  66. end
  67. describe 'POST #enable' do
  68. it 'enables the webhook' do
  69. post :enable, params: { id: webhook.id }
  70. expect(webhook.reload).to be_enabled
  71. expect(response).to redirect_to(admin_webhook_path(webhook))
  72. end
  73. end
  74. describe 'POST #disable' do
  75. it 'disables the webhook' do
  76. post :disable, params: { id: webhook.id }
  77. expect(webhook.reload).to_not be_enabled
  78. expect(response).to redirect_to(admin_webhook_path(webhook))
  79. end
  80. end
  81. describe 'DELETE #destroy' do
  82. it 'destroys the record' do
  83. expect do
  84. delete :destroy, params: { id: webhook.id }
  85. end.to change(Webhook, :count).by(-1)
  86. expect(response).to redirect_to(admin_webhooks_path)
  87. end
  88. end
  89. end
  90. end