Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Improve admin webhooks UI + tests
Damian Legawiec29c951dada6b
diff --git a/admin/app/assets/tailwind/spree/admin/components/_dialogs.css b/admin/app/assets/tailwind/spree/admin/components/_dialogs.cssindex 6fd6c8e0aa..9dfebf2cd7 100644--- a/admin/app/assets/tailwind/spree/admin/components/_dialogs.css+++ b/admin/app/assets/tailwind/spree/admin/components/_dialogs.css@@ -70,12 +70,12 @@.drawer {@apply fixed top-0 right-0 left-auto flex-col justify-between p-0 overflow-hidden;- height: calc(100dvh - 2rem);- max-height: calc(100dvh - 2rem);- width: calc(100vw - 2rem);+ height: calc(100dvh - 1rem);+ max-height: calc(100dvh - 1rem);+ width: calc(100vw - 1rem);max-width: 600px;min-width: 320px;- margin: 1rem;+ margin: 0.5rem;}.drawer[open] {diff --git a/admin/app/controllers/spree/admin/webhook_deliveries_controller.rb b/admin/app/controllers/spree/admin/webhook_deliveries_controller.rbindex 1963c24d75..4b45a3671c 100644--- a/admin/app/controllers/spree/admin/webhook_deliveries_controller.rb+++ b/admin/app/controllers/spree/admin/webhook_deliveries_controller.rb@@ -9,16 +9,6 @@ module Spreebelongs_to 'spree/webhook_endpoint'- def retry- if @webhook_delivery.webhook_endpoint.active?- Spree::WebhookDeliveryJob.perform_later(@webhook_delivery.id, @webhook_delivery.webhook_endpoint.secret_key)- flash[:success] = Spree.t('admin.webhook_deliveries.retry_queued')- else- flash[:error] = Spree.t('admin.webhook_deliveries.endpoint_inactive')- end- redirect_to spree.admin_webhook_endpoint_webhook_deliveries_path(@webhook_delivery.webhook_endpoint)- end-privatedef collection_default_sortdiff --git a/admin/app/controllers/spree/admin/webhook_endpoints_controller.rb b/admin/app/controllers/spree/admin/webhook_endpoints_controller.rbindex ce82bc7d45..c96d56bc9b 100644--- a/admin/app/controllers/spree/admin/webhook_endpoints_controller.rb+++ b/admin/app/controllers/spree/admin/webhook_endpoints_controller.rb@@ -16,6 +16,10 @@ module Spreedef location_after_savespree.admin_webhook_endpoint_path(@object)end++ def update_turbo_stream_enabled?+ true+ endendendenddiff --git a/admin/app/views/spree/admin/oauth_applications/index.html.erb b/admin/app/views/spree/admin/oauth_applications/index.html.erbindex d0caf7a9ac..bfc8ef96db 100644--- a/admin/app/views/spree/admin/oauth_applications/index.html.erb+++ b/admin/app/views/spree/admin/oauth_applications/index.html.erb@@ -10,8 +10,8 @@<%= content_for :page_alerts do %><div class="alert alert-info"><p>- Here you can manage API keys to access the <%= link_to 'Platform API', 'https://spreecommerce.org/docs/api-reference/platform/authentication', target: '_blank' %>.- For headless storefronts you don't need an API key, please use the <%= link_to 'Storefront API', 'https://spreecommerce.org/docs/api-reference/storefront/authentication', target: '_blank' %>.+ Here you can manage API keys to access the <%= external_link_to 'Platform API', 'https://spreecommerce.org/docs/api-reference/platform/authentication' %>.+ For headless storefronts you don't need an API key, please use the <%= external_link_to 'Storefront API', 'https://spreecommerce.org/docs/api-reference/storefront/authentication' %>.</p></div><% end %>diff --git a/admin/app/views/spree/admin/webhook_deliveries/_table_row.html.erb b/admin/app/views/spree/admin/webhook_deliveries/_table_row.html.erbindex 5cad47bcd3..4f8faeaa7b 100644--- a/admin/app/views/spree/admin/webhook_deliveries/_table_row.html.erb+++ b/admin/app/views/spree/admin/webhook_deliveries/_table_row.html.erb@@ -6,7 +6,7 @@<%= webhook_delivery_status_badge(webhook_delivery) %></td><td class="w-20">- <%= spree_time(webhook_delivery.delivered_at) %>+ <%= webhook_delivery.delivered_at ? spree_time(webhook_delivery.delivered_at) : '-' %></td><td class="w-20"><%= webhook_delivery.execution_time ? "#{webhook_delivery.execution_time}ms" : '-' %>diff --git a/admin/app/views/spree/admin/webhook_deliveries/show.html.erb b/admin/app/views/spree/admin/webhook_deliveries/show.html.erbindex 32386aa699..1fd72d78a4 100644--- a/admin/app/views/spree/admin/webhook_deliveries/show.html.erb+++ b/admin/app/views/spree/admin/webhook_deliveries/show.html.erb@@ -7,6 +7,11 @@<span class="text-gray-600"><%= Spree.t(:event) %></span><span><code><%= @webhook_delivery.event_name %></code></span></li>+ <li class="list-group-item flex justify-between items-center">+ <span class="text-gray-600"><%= Spree.t(:status) %></span>+ <span><%= webhook_delivery_status_badge(@webhook_delivery) %></span>+ </li>+<li class="list-group-item flex justify-between items-center"><span class="text-gray-600"><%= Spree.t(:url) %></span><span><%= external_link_to(@webhook_delivery.url, @webhook_delivery.url) %></span>@@ -18,6 +23,30 @@<span><%= l(@webhook_delivery.delivered_at, format: :long) %></span></li><% end %>+ <% if @webhook_delivery.response_code.present? %>+ <li class="list-group-item flex justify-between items-center">+ <span class="text-gray-600"><%= Spree.t(:response_code) %></span>+ <code><%= @webhook_delivery.response_code %></code>+ </li>+ <% end %>+ <% if @webhook_delivery.execution_time.present? %>+ <li class="list-group-item flex justify-between items-center">+ <span class="text-gray-600"><%= Spree.t('admin.webhook_deliveries.execution_time') %></span>+ <code><%= @webhook_delivery.execution_time %>ms</code>+ </li>+ <% end %>+ <% if @webhook_delivery.error_type.present? %>+ <li class="list-group-item flex justify-between items-center">+ <span class="text-gray-600"><%= Spree.t('admin.webhook_deliveries.error_type') %></span>+ <code><%= @webhook_delivery.error_type %></code>+ </li>+ <% end %>+ <% if @webhook_delivery.request_errors.present? %>+ <li class="list-group-item flex justify-between items-center">+ <span class="text-gray-600"><%= Spree.t('admin.webhook_deliveries.request_errors') %></span>+ <code><%= @webhook_delivery.request_errors %></code>+ </li>+ <% end %></ul><h5 class="text-lg font-bold"><%= Spree.t('admin.webhook_deliveries.request_payload') %></h5>diff --git a/admin/app/views/spree/admin/webhook_endpoints/_details.html.erb b/admin/app/views/spree/admin/webhook_endpoints/_details.html.erbindex 936c33464c..f055ff2891 100644--- a/admin/app/views/spree/admin/webhook_endpoints/_details.html.erb+++ b/admin/app/views/spree/admin/webhook_endpoints/_details.html.erb@@ -1,12 +1,22 @@-<div class="card mb-6">+<div class="card mb-6" id="<%= spree_dom_id(@webhook_endpoint) %>_details"><div class="card-header flex justify-between"><h5 class="card-title"><%= Spree.t(:details) %></h5>- <%= link_to_with_icon 'edit', Spree.t(:edit), spree.edit_admin_webhook_endpoint_path(@webhook_endpoint), icon: 'pencil', class: 'btn btn-sm btn-light' %>+ <%= link_to_with_icon 'edit', Spree.t(:edit), spree.edit_admin_webhook_endpoint_path(@webhook_endpoint),+ class: 'btn btn-sm btn-light', data: { turbo_frame: :drawer, action: 'drawer#open' } %></div><div class="card-body p-0"><ul class="list-group list-group-flush">+ <li class="list-group-item flex justify-between items-center">+ <span class="text-gray-600">+ <%= Spree.t(:active) %>+ </span>+ <span>+ <%= active_badge(@webhook_endpoint.active?) %>+ </span>+ </li>+<li class="list-group-item flex justify-between items-top gap-10"><span class="text-gray-600"><%= Spree.t('admin.webhook_endpoints.event_subscriptions') %>diff --git a/admin/app/views/spree/admin/webhook_endpoints/edit.html.erb b/admin/app/views/spree/admin/webhook_endpoints/edit.html.erbindex 520a6f47c2..3148099442 100644--- a/admin/app/views/spree/admin/webhook_endpoints/edit.html.erb+++ b/admin/app/views/spree/admin/webhook_endpoints/edit.html.erb@@ -1 +1,13 @@-<%= render 'spree/admin/shared/edit_resource' %>\ No newline at end of file+<%= turbo_frame_tag :drawer do %>+ <%= form_with model: [:admin, @object], url: object_url, scope: @resource.object_name, id: "edit_#{controller_name.singularize}_#{@object.id}", data: local_assigns[:data] || {} do |f| %>+ <%= drawer_header(Spree.t(:edit)) %>+ <div class="drawer-body">+ <%= render 'spree/admin/shared/error_messages', target: @object %>+ <%= render 'form', f: f %>+ </div>+ <div class="drawer-footer">+ <%= turbo_save_button_tag %>+ <%= drawer_discard_button %>+ </div>+ <% end %>+<% end %>\ No newline at end of filediff --git a/admin/app/views/spree/admin/webhook_endpoints/show.html.erb b/admin/app/views/spree/admin/webhook_endpoints/show.html.erbindex 8e781b9c24..628a7ced5f 100644--- a/admin/app/views/spree/admin/webhook_endpoints/show.html.erb+++ b/admin/app/views/spree/admin/webhook_endpoints/show.html.erb@@ -7,7 +7,7 @@<div class="col-span-12 lg:col-span-8"><%= render 'summary' %>- <%= turbo_frame_tag :webhook_deliveries, src: spree.admin_webhook_endpoint_webhook_deliveries_path(@webhook_endpoint), loading: :lazy, autoscroll: true, data: { autoscroll_block: :nearest, autoscroll_behavior: :smooth } do %>+ <%= turbo_frame_tag :webhook_deliveries, src: spree.admin_webhook_endpoint_webhook_deliveries_path(@webhook_endpoint), loading: :lazy do %><%= render 'spree/admin/shared/spinner' %><% end %></div>diff --git a/admin/app/views/spree/admin/webhook_endpoints/update.turbo_stream.erb b/admin/app/views/spree/admin/webhook_endpoints/update.turbo_stream.erbnew file mode 100644index 0000000000..f7d61cc42b--- /dev/null+++ b/admin/app/views/spree/admin/webhook_endpoints/update.turbo_stream.erb@@ -0,0 +1,5 @@+<%= turbo_render_alerts :drawer_alerts %>++<%= turbo_stream.replace spree_dom_id(@webhook_endpoint) + '_details' do %>+ <%= render 'spree/admin/webhook_endpoints/details', webhook_endpoint: @webhook_endpoint %>+<% end %>\ No newline at end of filediff --git a/admin/config/locales/en.yml b/admin/config/locales/en.ymlindex ad9dd6bbff..b615334c1d 100644--- a/admin/config/locales/en.yml+++ b/admin/config/locales/en.yml@@ -307,15 +307,15 @@ en:delivery_details: Delivery Detailsdelivery_info: Delivery Informationendpoint_inactive: Cannot retry - endpoint is inactive+ error_type: Error Typeevent: Eventexecution_time: Execution Timefailed: Failedpending: Pending+ request_errors: Request Errorsrequest_payload: Request Payloadresponse_body: Response Bodyresponse_code: Response Code- retry: Retry- retry_queued: Delivery has been queued for retrywebhook_endpoints:back_to_endpoint: Back to endpointdeliveries: Deliveriesdiff --git a/admin/config/routes.rb b/admin/config/routes.rbindex 0d9ecd1f2d..9a108333bd 100644--- a/admin/config/routes.rb+++ b/admin/config/routes.rb@@ -220,11 +220,7 @@ Spree::Core::Engine.add_routes do# developer toolsresources :oauth_applicationsresources :webhook_endpoints do- resources :webhook_deliveries, only: [:index, :show] do- member do- post :retry- end- end+ resources :webhook_deliveries, only: [:index, :show]end# errorsdiff --git a/admin/spec/controllers/spree/admin/webhook_deliveries_controller_spec.rb b/admin/spec/controllers/spree/admin/webhook_deliveries_controller_spec.rbnew file mode 100644index 0000000000..65838b207e--- /dev/null+++ b/admin/spec/controllers/spree/admin/webhook_deliveries_controller_spec.rb@@ -0,0 +1,144 @@+# frozen_string_literal: true++require 'spec_helper'++RSpec.describe Spree::Admin::WebhookDeliveriesController, type: :controller do+ stub_authorization!+ render_views++ let(:store) { @default_store }+ let(:webhook_endpoint) { create(:webhook_endpoint, store: store) }++ describe 'GET #index' do+ subject(:index) { get :index, params: { webhook_endpoint_id: webhook_endpoint.id } }++ let!(:deliveries) { create_list(:webhook_delivery, 3, webhook_endpoint: webhook_endpoint) }+ let!(:other_endpoint_deliveries) { create_list(:webhook_delivery, 2) }++ it 'renders the index template' do+ index++ expect(response).to render_template(:index)+ end++ it 'assigns deliveries for the webhook endpoint only' do+ index++ expect(assigns[:collection]).to contain_exactly(*deliveries)+ end++ it 'assigns the parent webhook endpoint' do+ index++ expect(assigns[:webhook_endpoint]).to eq(webhook_endpoint)+ end++ context 'with mixed delivery statuses' do+ let!(:successful_delivery) { create(:webhook_delivery, :successful, webhook_endpoint: webhook_endpoint) }+ let!(:failed_delivery) { create(:webhook_delivery, :failed, webhook_endpoint: webhook_endpoint) }+ let!(:pending_delivery) { create(:webhook_delivery, :pending, webhook_endpoint: webhook_endpoint) }++ it 'includes all delivery statuses' do+ index++ expect(assigns[:collection]).to include(successful_delivery, failed_delivery, pending_delivery)+ end+ end++ context 'ordering' do+ # Use a separate endpoint to avoid conflicts with deliveries from outer context+ let(:ordering_endpoint) { create(:webhook_endpoint, store: store) }+ let!(:old_delivery) { create(:webhook_delivery, webhook_endpoint: ordering_endpoint, created_at: 2.days.ago) }+ let!(:new_delivery) { create(:webhook_delivery, webhook_endpoint: ordering_endpoint, created_at: 1.hour.ago) }++ it 'orders deliveries by created_at descending' do+ get :index, params: { webhook_endpoint_id: ordering_endpoint.id }++ collection = assigns[:collection]+ expect(collection.first).to eq(new_delivery)+ expect(collection.last).to eq(old_delivery)+ end+ end+ end++ describe 'GET #show' do+ subject(:show) { get :show, params: { webhook_endpoint_id: webhook_endpoint.id, id: delivery.id } }++ let(:delivery) { create(:webhook_delivery, :successful, webhook_endpoint: webhook_endpoint) }++ it 'renders the show template' do+ show++ expect(response).to render_template(:show)+ end++ it 'assigns the webhook delivery' do+ show++ expect(assigns[:webhook_delivery]).to eq(delivery)+ end++ it 'assigns the parent webhook endpoint' do+ show++ expect(assigns[:webhook_endpoint]).to eq(webhook_endpoint)+ end++ context 'with successful delivery' do+ let(:delivery) do+ create(:webhook_delivery, :successful,+ webhook_endpoint: webhook_endpoint,+ response_code: 200,+ execution_time: 150,+ response_body: '{"status": "ok"}')+ end++ it 'shows delivery details' do+ show++ expect(response.body).to include('200')+ end+ end++ context 'with failed delivery' do+ let(:delivery) do+ create(:webhook_delivery, :failed,+ webhook_endpoint: webhook_endpoint,+ response_code: 500,+ request_errors: 'Internal Server Error')+ end++ it 'shows error information' do+ show++ expect(response.body).to include('500')+ end+ end++ context 'with timeout error' do+ let(:delivery) do+ create(:webhook_delivery, :timeout,+ webhook_endpoint: webhook_endpoint)+ end++ it 'shows timeout information' do+ show++ expect(assigns[:webhook_delivery].error_type).to eq('timeout')+ end+ end++ context 'with connection error' do+ let(:delivery) do+ create(:webhook_delivery, :connection_error,+ webhook_endpoint: webhook_endpoint)+ end++ it 'shows connection error information' do+ show++ expect(assigns[:webhook_delivery].error_type).to eq('connection_error')+ end+ end+ end+enddiff --git a/admin/spec/controllers/spree/admin/webhook_endpoints_controller_spec.rb b/admin/spec/controllers/spree/admin/webhook_endpoints_controller_spec.rbnew file mode 100644index 0000000000..953c3a4f82--- /dev/null+++ b/admin/spec/controllers/spree/admin/webhook_endpoints_controller_spec.rb@@ -0,0 +1,228 @@+# frozen_string_literal: true++require 'spec_helper'++RSpec.describe Spree::Admin::WebhookEndpointsController, type: :controller do+ stub_authorization!+ render_views++ let(:store) { @default_store }++ describe 'GET #index' do+ subject(:index) { get :index }++ let!(:webhook_endpoints) { create_list(:webhook_endpoint, 3, store: store) }+ let!(:other_store_endpoints) { create_list(:webhook_endpoint, 2, store: create(:store)) }++ it 'renders the index template' do+ index++ expect(response).to render_template(:index)+ end++ it 'assigns webhook endpoints for current store only' do+ index++ expect(assigns[:collection]).to contain_exactly(*webhook_endpoints)+ end+ end++ describe 'GET #show' do+ subject(:show) { get :show, params: { id: webhook_endpoint.id } }++ let(:webhook_endpoint) { create(:webhook_endpoint, store: store) }++ it 'renders the show template' do+ show++ expect(response).to render_template(:show)+ end++ it 'assigns the webhook endpoint' do+ show++ expect(assigns[:webhook_endpoint]).to eq(webhook_endpoint)+ end+ end++ describe 'GET #new' do+ subject(:new_action) { get :new }++ it 'renders the new template' do+ new_action++ expect(response).to render_template(:new)+ end++ it 'assigns a new webhook endpoint' do+ new_action++ expect(assigns[:webhook_endpoint]).to be_a_new(Spree::WebhookEndpoint)+ end+ end++ describe 'POST #create' do+ subject(:create_endpoint) { post :create, params: { webhook_endpoint: endpoint_params } }++ let(:endpoint_params) do+ {+ url: 'https://example.com/webhooks/receiver',+ active: true,+ subscriptions: %w[order.created order.completed]+ }+ end++ it 'creates a new webhook endpoint' do+ expect { create_endpoint }.to change(Spree::WebhookEndpoint, :count).by(1)+ end++ it 'sets the attributes correctly' do+ create_endpoint++ endpoint = Spree::WebhookEndpoint.last+ expect(endpoint.url).to eq('https://example.com/webhooks/receiver')+ expect(endpoint.active).to be true+ expect(endpoint.subscriptions).to eq(%w[order.created order.completed])+ expect(endpoint.store).to eq(store)+ end++ it 'generates a secret key' do+ create_endpoint++ endpoint = Spree::WebhookEndpoint.last+ expect(endpoint.secret_key).to be_present+ end++ it 'redirects to show page' do+ create_endpoint++ expect(response).to redirect_to(spree.admin_webhook_endpoint_path(Spree::WebhookEndpoint.last))+ end++ context 'with invalid params' do+ let(:endpoint_params) { { url: '' } }++ it 'does not create a webhook endpoint' do+ expect { create_endpoint }.not_to change(Spree::WebhookEndpoint, :count)+ end++ it 'renders the new template' do+ create_endpoint++ expect(response).to render_template(:new)+ end+ end++ context 'with all events subscription' do+ let(:endpoint_params) do+ {+ url: 'https://example.com/webhooks/all',+ active: true,+ subscriptions: ['*']+ }+ end++ it 'creates endpoint subscribed to all events' do+ create_endpoint++ endpoint = Spree::WebhookEndpoint.last+ expect(endpoint.subscriptions).to eq(['*'])+ expect(endpoint.subscribed_to?('order.created')).to be true+ expect(endpoint.subscribed_to?('product.updated')).to be true+ end+ end+ end++ describe 'GET #edit' do+ subject(:edit) { get :edit, params: { id: webhook_endpoint.id } }++ let(:webhook_endpoint) { create(:webhook_endpoint, store: store) }++ it 'renders the edit template' do+ edit++ expect(response).to render_template(:edit)+ end++ it 'assigns the webhook endpoint' do+ edit++ expect(assigns[:webhook_endpoint]).to eq(webhook_endpoint)+ end+ end++ describe 'PUT #update' do+ subject(:update_endpoint) { put :update, params: { id: webhook_endpoint.id, webhook_endpoint: endpoint_params } }++ let!(:webhook_endpoint) { create(:webhook_endpoint, store: store, url: 'https://old.example.com/webhook', active: true) }++ let(:endpoint_params) do+ {+ url: 'https://new.example.com/webhook',+ active: false,+ subscriptions: %w[product.created product.updated]+ }+ end++ it 'updates the webhook endpoint' do+ update_endpoint++ webhook_endpoint.reload+ expect(webhook_endpoint.url).to eq('https://new.example.com/webhook')+ expect(webhook_endpoint.active).to be false+ expect(webhook_endpoint.subscriptions).to eq(%w[product.created product.updated])+ end++ it 'redirects to show page' do+ update_endpoint++ expect(response).to redirect_to(spree.admin_webhook_endpoint_path(webhook_endpoint))+ end++ context 'with invalid params' do+ let(:endpoint_params) { { url: 'invalid-url' } }++ it 'does not update the webhook endpoint' do+ update_endpoint++ webhook_endpoint.reload+ expect(webhook_endpoint.url).to eq('https://old.example.com/webhook')+ end++ it 'renders the edit template' do+ update_endpoint++ expect(response).to render_template(:edit)+ end+ end++ context 'when deactivating endpoint' do+ let(:endpoint_params) { { active: false } }++ it 'marks the endpoint as inactive' do+ update_endpoint++ webhook_endpoint.reload+ expect(webhook_endpoint.active).to be false+ end+ end+ end++ describe 'DELETE #destroy' do+ subject(:destroy_endpoint) { delete :destroy, params: { id: webhook_endpoint.id } }++ let!(:webhook_endpoint) { create(:webhook_endpoint, store: store) }++ it 'soft deletes the webhook endpoint' do+ destroy_endpoint++ expect(webhook_endpoint.reload.deleted_at).not_to be_nil+ end++ it 'redirects to index' do+ destroy_endpoint++ expect(response).to redirect_to(spree.admin_webhook_endpoints_path)+ end+ end+end