Add button to send test data to endpoint

It will allow a user to easily test the endpoint
This commit is contained in:
Gaetan Craig-Riou
2025-12-02 16:05:44 +11:00
parent 72085be896
commit 5e4df41ec8
6 changed files with 86 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
-# Existing endpoints
- if webhooks.empty? # Only one allowed for now.
%tr
%td= t("components.webhook_endpoint_form.event_types.#{webhook_type}")
%td= t("components.webhook_endpoint_form.event_types.#{webhook_type}")
%td
= form_with(url: helpers.account_webhook_endpoints_path, id: "#{webhook_type}_webhook_endpoint") do |f|
= f.url_field :'webhook_endpoint[url]', id: "#{webhook_type}_webhook_endpoint_url", placeholder: t('components.webhook_endpoint_form.url.create_placeholder'), required: true, size: 64
@@ -13,10 +13,13 @@
%tr
%td= t("components.webhook_endpoint_form.event_types.#{webhook_type}")
%td= webhook_endpoint.url
%td.actions
%td.actions.endpoints-actions
- if webhook_endpoint.persisted?
= button_to helpers.account_webhook_endpoint_path(webhook_endpoint), method: :delete,
class: "tiny alert no-margin",
data: { confirm: I18n.t(:are_you_sure) } do
= I18n.t(:delete)
= form_tag helpers.webhook_endpoint_test_account_path(webhook_endpoint), class: "button_to", 'data-turbo': true do
= button_tag type: "submit", class: "tiny alert no-margin", data: { confirm: I18n.t(:are_you_sure) } do
= I18n.t("components.webhook_endpoint_form.test_endpoint")

View File

@@ -1,7 +1,7 @@
# frozen_string_literal: true
class WebhookEndpointsController < BaseController
before_action :load_resource, only: :destroy
before_action :load_resource, only: [:destroy, :test]
def create
webhook_endpoint = spree_current_user.webhook_endpoints.new(webhook_endpoint_params)
@@ -25,6 +25,55 @@ class WebhookEndpointsController < BaseController
redirect_to redirect_path
end
def test # rubocop:disable Metrics/MethodLength
at = Time.zone.now
test_payload = {
payment: {
updated_at: at,
amount: 0.00,
state: "completed"
},
enterprise: {
abn: "65797115831",
acn: "",
name: "TEST Enterprise",
address: {
address1: "1 testing street",
address2: "",
city: "TestCity",
zipcode: "1234"
}
},
order: {
total: 0.00,
currency: "AUD",
line_items: [
{
quantity: 1,
price: 20.00,
tax_category_name: "VAT",
product_name: "Test product",
name_to_display: "",
unit_to_display: "1kg"
}
]
}
}
WebhookDeliveryJob.perform_later(@webhook_endpoint.url, "payment.completed", test_payload, at:)
flash[:success] = t(".success")
respond_with do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.update(
:flashes, partial: "shared/flashes", locals: { flashes: flash }
)
end
end
end
private
def load_resource
@webhook_endpoint = spree_current_user.webhook_endpoints.find(params[:id])
end

View File

@@ -169,3 +169,12 @@ table {
right: calc(12px + 2 * 2px + 2 * 1px);
}
}
// Webhook Endpoints
td.endpoints-actions {
display: flex;
form {
padding-right: $padding-small;
}
}

View File

@@ -4088,6 +4088,8 @@ en:
destroy:
success: Webhook endpoint successfully deleted
error: Webhook endpoint failed to delete
test:
success: Some test data has been sent to the webhook url
spree:
order_updated: "Order Updated"
@@ -5096,7 +5098,7 @@ en:
event_types:
order_cycle_opened: Order Cycle Opened
payment_status_changed: Post webhook on Payment status change
test_endpoint: Test webhook endpoint
# Gem to prevent bot form submissions
invisible_captcha:

View File

@@ -34,6 +34,7 @@ Spree::Core::Engine.routes.draw do
resource :account, :controller => 'users' do
resources :webhook_endpoints, only: [:create, :destroy], controller: '/webhook_endpoints'
post '/webhook_endpoints/:id/test', to: "/webhook_endpoints#test", as: "webhook_endpoint_test"
end
match '/admin/orders/bulk_management' => 'admin/orders#bulk_management', :as => "admin_bulk_order_management", via: :get

View File

@@ -69,4 +69,22 @@ RSpec.describe WebhookEndpointsController do
expect(response).to redirect_to "/account#/developer_settings"
end
end
describe "#test" do
let(:webhook_endpoint) {
user.webhook_endpoints.create(url: "https://url", webhook_type: "payment_status_changed" )
}
subject { spree_post :test, id: webhook_endpoint.id, format: :turbo_stream }
it "enqueus a webhook job" do
expect { subject }.to enqueue_job(WebhookDeliveryJob).exactly(1).times
end
it "shows a success mesage" do
subject
expect(flash[:success]).to eq "Some test data has been sent to the webhook url"
end
end
end