webhooks_controller_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. 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 'DELETE #destroy' do
  68. it 'destroys the record' do
  69. expect do
  70. delete :destroy, params: { id: webhook.id }
  71. end.to change(Webhook, :count).by(-1)
  72. expect(response).to redirect_to(admin_webhooks_path)
  73. end
  74. end
  75. end
  76. end