2026-01-31 22:41:15 +00:00
|
|
|
defmodule SimpleshopTheme.WebhooksTest do
|
feat: add Printify order submission and fulfilment tracking
Submit paid orders to Printify via provider API with idempotent
guards, Stripe address mapping, and error handling. Track fulfilment
status through submitted → processing → shipped → delivered via
webhook-driven updates (primary) and Oban Cron polling fallback.
- 9 fulfilment fields on orders (status, provider IDs, tracking, timestamps)
- OrderSubmissionWorker with retry logic, auto-enqueued after Stripe payment
- FulfilmentStatusWorker polls every 30 mins for missed webhook events
- Printify order webhook handlers (sent-to-production, shipment, delivered)
- Admin UI: fulfilment column in table, fulfilment card with tracking info,
submit/retry and refresh buttons on order detail
- Mox provider mocking for test isolation (Provider.for_type configurable)
- 33 new tests (555 total), verified against real Printify API
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 09:51:51 +00:00
|
|
|
use SimpleshopTheme.DataCase, async: false
|
2026-01-31 22:41:15 +00:00
|
|
|
|
feat: add Printify order submission and fulfilment tracking
Submit paid orders to Printify via provider API with idempotent
guards, Stripe address mapping, and error handling. Track fulfilment
status through submitted → processing → shipped → delivered via
webhook-driven updates (primary) and Oban Cron polling fallback.
- 9 fulfilment fields on orders (status, provider IDs, tracking, timestamps)
- OrderSubmissionWorker with retry logic, auto-enqueued after Stripe payment
- FulfilmentStatusWorker polls every 30 mins for missed webhook events
- Printify order webhook handlers (sent-to-production, shipment, delivered)
- Admin UI: fulfilment column in table, fulfilment card with tracking info,
submit/retry and refresh buttons on order detail
- Mox provider mocking for test isolation (Provider.for_type configurable)
- 33 new tests (555 total), verified against real Printify API
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 09:51:51 +00:00
|
|
|
alias SimpleshopTheme.Orders
|
2026-01-31 22:41:15 +00:00
|
|
|
alias SimpleshopTheme.Webhooks
|
|
|
|
|
|
|
|
|
|
import SimpleshopTheme.ProductsFixtures
|
feat: add Printify order submission and fulfilment tracking
Submit paid orders to Printify via provider API with idempotent
guards, Stripe address mapping, and error handling. Track fulfilment
status through submitted → processing → shipped → delivered via
webhook-driven updates (primary) and Oban Cron polling fallback.
- 9 fulfilment fields on orders (status, provider IDs, tracking, timestamps)
- OrderSubmissionWorker with retry logic, auto-enqueued after Stripe payment
- FulfilmentStatusWorker polls every 30 mins for missed webhook events
- Printify order webhook handlers (sent-to-production, shipment, delivered)
- Admin UI: fulfilment column in table, fulfilment card with tracking info,
submit/retry and refresh buttons on order detail
- Mox provider mocking for test isolation (Provider.for_type configurable)
- 33 new tests (555 total), verified against real Printify API
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 09:51:51 +00:00
|
|
|
import SimpleshopTheme.OrdersFixtures
|
2026-01-31 22:41:15 +00:00
|
|
|
|
|
|
|
|
setup do
|
|
|
|
|
conn = provider_connection_fixture(%{provider_type: "printify"})
|
|
|
|
|
{:ok, provider_connection: conn}
|
|
|
|
|
end
|
|
|
|
|
|
feat: add Printify order submission and fulfilment tracking
Submit paid orders to Printify via provider API with idempotent
guards, Stripe address mapping, and error handling. Track fulfilment
status through submitted → processing → shipped → delivered via
webhook-driven updates (primary) and Oban Cron polling fallback.
- 9 fulfilment fields on orders (status, provider IDs, tracking, timestamps)
- OrderSubmissionWorker with retry logic, auto-enqueued after Stripe payment
- FulfilmentStatusWorker polls every 30 mins for missed webhook events
- Printify order webhook handlers (sent-to-production, shipment, delivered)
- Admin UI: fulfilment column in table, fulfilment card with tracking info,
submit/retry and refresh buttons on order detail
- Mox provider mocking for test isolation (Provider.for_type configurable)
- 33 new tests (555 total), verified against real Printify API
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 09:51:51 +00:00
|
|
|
describe "handle_printify_event/2 — product events" do
|
2026-01-31 22:41:15 +00:00
|
|
|
test "product:updated triggers sync", %{provider_connection: _conn} do
|
|
|
|
|
result =
|
|
|
|
|
Webhooks.handle_printify_event(
|
|
|
|
|
"product:updated",
|
|
|
|
|
%{"id" => "123", "shop_id" => "456"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert {:ok, %Oban.Job{}} = result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "product:publish:started triggers sync", %{provider_connection: _conn} do
|
|
|
|
|
result =
|
|
|
|
|
Webhooks.handle_printify_event(
|
|
|
|
|
"product:publish:started",
|
|
|
|
|
%{"id" => "123"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert {:ok, %Oban.Job{}} = result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "product:deleted triggers delete", %{provider_connection: _conn} do
|
|
|
|
|
result =
|
|
|
|
|
Webhooks.handle_printify_event(
|
|
|
|
|
"product:deleted",
|
|
|
|
|
%{"id" => "123"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert {:ok, %Oban.Job{}} = result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "shop:disconnected returns ok" do
|
|
|
|
|
assert :ok = Webhooks.handle_printify_event("shop:disconnected", %{})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "unknown event returns ok" do
|
|
|
|
|
assert :ok = Webhooks.handle_printify_event("unknown:event", %{})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "returns error when no provider connection" do
|
|
|
|
|
SimpleshopTheme.Repo.delete_all(SimpleshopTheme.Products.ProviderConnection)
|
|
|
|
|
|
|
|
|
|
assert {:error, :no_connection} =
|
|
|
|
|
Webhooks.handle_printify_event(
|
|
|
|
|
"product:updated",
|
|
|
|
|
%{"id" => "123"}
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
feat: add Printify order submission and fulfilment tracking
Submit paid orders to Printify via provider API with idempotent
guards, Stripe address mapping, and error handling. Track fulfilment
status through submitted → processing → shipped → delivered via
webhook-driven updates (primary) and Oban Cron polling fallback.
- 9 fulfilment fields on orders (status, provider IDs, tracking, timestamps)
- OrderSubmissionWorker with retry logic, auto-enqueued after Stripe payment
- FulfilmentStatusWorker polls every 30 mins for missed webhook events
- Printify order webhook handlers (sent-to-production, shipment, delivered)
- Admin UI: fulfilment column in table, fulfilment card with tracking info,
submit/retry and refresh buttons on order detail
- Mox provider mocking for test isolation (Provider.for_type configurable)
- 33 new tests (555 total), verified against real Printify API
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 09:51:51 +00:00
|
|
|
|
|
|
|
|
describe "handle_printify_event/2 — order events" do
|
|
|
|
|
test "order:sent-to-production updates fulfilment status" do
|
|
|
|
|
{order, _v, _p, _c} = submitted_order_fixture()
|
|
|
|
|
|
|
|
|
|
assert {:ok, updated} =
|
|
|
|
|
Webhooks.handle_printify_event("order:sent-to-production", %{
|
|
|
|
|
"id" => "printify_abc",
|
|
|
|
|
"external_id" => order.order_number
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert updated.fulfilment_status == "processing"
|
|
|
|
|
assert updated.provider_status == "in-production"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "order:shipment:created sets tracking info and shipped_at" do
|
|
|
|
|
{order, _v, _p, _c} = submitted_order_fixture()
|
|
|
|
|
|
|
|
|
|
assert {:ok, updated} =
|
|
|
|
|
Webhooks.handle_printify_event("order:shipment:created", %{
|
|
|
|
|
"id" => "printify_abc",
|
|
|
|
|
"external_id" => order.order_number,
|
|
|
|
|
"shipments" => [
|
|
|
|
|
%{
|
|
|
|
|
"tracking_number" => "1Z999AA1",
|
|
|
|
|
"tracking_url" => "https://ups.com/track/1Z999AA1"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert updated.fulfilment_status == "shipped"
|
|
|
|
|
assert updated.tracking_number == "1Z999AA1"
|
|
|
|
|
assert updated.tracking_url == "https://ups.com/track/1Z999AA1"
|
|
|
|
|
assert updated.shipped_at != nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "order:shipment:delivered sets delivered_at" do
|
|
|
|
|
{order, _v, _p, _c} = submitted_order_fixture()
|
|
|
|
|
|
|
|
|
|
# First mark as shipped
|
|
|
|
|
{:ok, order} =
|
|
|
|
|
Orders.update_fulfilment(order, %{
|
|
|
|
|
fulfilment_status: "shipped",
|
|
|
|
|
shipped_at: DateTime.utc_now() |> DateTime.truncate(:second)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert {:ok, updated} =
|
|
|
|
|
Webhooks.handle_printify_event("order:shipment:delivered", %{
|
|
|
|
|
"id" => "printify_abc",
|
|
|
|
|
"external_id" => order.order_number
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert updated.fulfilment_status == "delivered"
|
|
|
|
|
assert updated.delivered_at != nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "order event with unknown external_id returns error" do
|
|
|
|
|
assert {:error, :order_not_found} =
|
|
|
|
|
Webhooks.handle_printify_event("order:sent-to-production", %{
|
|
|
|
|
"id" => "printify_abc",
|
|
|
|
|
"external_id" => "SS-000000-NOPE"
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "order event with missing external_id returns error" do
|
|
|
|
|
assert {:error, :missing_external_id} =
|
|
|
|
|
Webhooks.handle_printify_event("order:sent-to-production", %{
|
|
|
|
|
"id" => "printify_abc"
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
end
|
2026-01-31 22:41:15 +00:00
|
|
|
end
|