saleor-apps-redis_apl/apps/taxes/scripts/migrations/app-webhook-migrator.test.ts

190 lines
5.4 KiB
TypeScript
Raw Normal View History

feat: change the flow from OrderCreated to OrderConfirmed (#826) * refactor: :recycle: extract order-metadata-manager * feat: :construction: add basic boilerplate * feat: :sparkles: add readExternalIdFromOrderMetadata * Revert "feat: :sparkles: add readExternalIdFromOrderMetadata" This reverts commit a78d9d4597672f8605cf998a9f784aebaab27de1. * feat: :sparkles: add order-cancelled avatax adapter * test: :white_check_mark: add tests for AvataxOrderCancelledPayloadTransformer * refactor: avataxId instead of externalId * refactor: :recycle: split up webhook response * build: :arrow_up: upgrade avatax * refactor: :recycle: extend logging in webhook response errors * fix: :bug: split privateMetadata and publicMetadata * fix: :bug: use "DEFAULT" value of companyCode for commit to work * fix: :alembic: fix voidTransaction type * refactor: :truck: order_created -> order_confirmed * fix: :bug: change voidReason * build: :construction_worker: add changeset * refactor: :fire: order_fulfilled webhook * feat: Avatax metadata tax calculation date (#843) * feat: :sparkles: add metadata tax calculation date * build: :construction_worker: add changeset * feat: Avatax metadata document code (#844) * feat: :sparkles: provide document code through metadata field * build: :construction_worker: add changeset * refactor: :recycle: fallback to default company code for migration * refactor: :recycle: patch order-created files and add deprecation note * Revert "refactor: :fire: order_fulfilled webhook" This reverts commit fd098642735ae9d62e3a876088226bd0f108afd6. * refactor: :recycle: patch order-fulfilled files and add deprecation note * fix: :bug: bring back deprecated webhooks to manifest * feat: :alembic: add AppWebhookMigrator (#850) * refactor: :truck: order_created -> order_confirmed * refactor: :fire: order_fulfilled webhook * feat: :alembic: add AppWebhookMigrator * feat: :sparkles: add mode to migrator * feat: :sparkles: add draft of run-report and migrateWebhook method * refactor: :recycle: address feedback * feat: :sparkles: add tests and new structure * refactor: :fire: util * feat: :sparkles: add enable/disable webhook rollback flow * refactor: :recycle: modify the taxes-migration flow * refactor: :recycle: generalize document code & date resolver * chore: :card_file_box: add run-migration * chore: :bulb: update comments about migration flow * fix: :bug: slice document code * refactor: :recycle: try/catch at the top level * chore: :bulb: add comments * Update shiny-meals-wait.md * Update soft-steaks-know.md * Update soft-steaks-know.md * fix: :white_check_mark: fix test * feat: :sparkles: change createTransaction to createOrAdjustTransaction this feature grants idempotency of the transaction flow * feat: :sparkles: add number field to OrderConfirmed payload * chore: :bulb: add deprecation comment to metadata method * docs: :memo: add todo comment to refactor sumPayloadLines * feat: :sparkles: add resolveStringOrThrow and use it for email * fix: :bug: add missing number to mock
2023-08-10 11:08:20 +00:00
import { SaleorAsyncWebhook, SaleorSyncWebhook } from "@saleor/app-sdk/handlers/next";
import { describe, it, vitest, expect } from "vitest";
import { AppWebhookMigrator } from "./app-webhook-migrator";
import { AppWebhookRepository } from "./app-webhook-repository";
describe("AppWebhookMigrator", () => {
describe("registerWebhookIfItDoesntExist", () => {
describe("in report mode", () => {
it("does not call create", async () => {
const createMock = vitest.fn();
const appWebhookRepository = {
create: createMock,
getAll: () => [],
} as unknown as AppWebhookRepository;
const appWebhookMigrator = new AppWebhookMigrator(
{
appWebhookRepository,
apiUrl: "apiUrl",
appId: "appId",
},
{ mode: "report" }
);
await appWebhookMigrator.registerWebhookIfItDoesntExist({} as SaleorAsyncWebhook);
expect(createMock).not.toHaveBeenCalled();
});
});
describe("in migrate mode", () => {
it("calls create", async () => {
const createMock = vitest.fn();
const appWebhookRepository = {
create: createMock,
getAll: () => [],
} as unknown as AppWebhookRepository;
const appWebhookMigrator = new AppWebhookMigrator(
{
appWebhookRepository,
apiUrl: "apiUrl",
appId: "appId",
},
{ mode: "migrate" }
);
const webhookHandler = {
getWebhookManifest: () => {
return {
name: "OrderCreated",
targetUrl: `targetUrl`,
query: "query",
asyncEvents: ["OrderCreated"],
};
},
} as unknown as SaleorAsyncWebhook;
await appWebhookMigrator.registerWebhookIfItDoesntExist(webhookHandler);
expect(createMock).toHaveBeenCalled();
});
});
});
describe("rollbackWebhookMigrations", () => {
describe("in report mode", () => {
it("does not call delete", async () => {
const deleteMock = vitest.fn();
const appWebhookRepository = {
delete: deleteMock,
getAll: () => [],
} as unknown as AppWebhookRepository;
const appWebhookMigrator = new AppWebhookMigrator(
{
appWebhookRepository,
apiUrl: "apiUrl",
appId: "appId",
},
{ mode: "report" }
);
await appWebhookMigrator.rollbackWebhookMigrations(
"OrderCreated",
{} as unknown as SaleorSyncWebhook
);
expect(deleteMock).not.toHaveBeenCalled();
});
});
describe("in migrate mode", async () => {
const deleteMock = vitest.fn();
const enableMock = vitest.fn();
const appWebhookRepository = {
delete: deleteMock,
enable: enableMock,
getAll: () => [
{
id: "id-1234",
name: "OrderConfirmed",
},
{
id: "id-5678",
name: "OrderCreated",
},
{
id: "id",
name: "OrderUpdated",
},
],
} as unknown as AppWebhookRepository;
const appWebhookMigrator = new AppWebhookMigrator(
{
appWebhookRepository,
apiUrl: "apiUrl",
appId: "appId",
},
{ mode: "migrate" }
);
await appWebhookMigrator.rollbackWebhookMigrations("OrderCreated", {
name: "OrderConfirmed",
targetUrl: `targetUrl`,
query: "query",
events: ["OrderConfirmed"],
} as unknown as SaleorSyncWebhook);
it("calls delete with the id that matches the handler", async () => {
expect(deleteMock).toHaveBeenCalledWith("id-1234");
});
it("calls enable with the id that matches the name of the previous webhook", async () => {
expect(enableMock).toHaveBeenCalledWith("id-5678");
});
});
});
describe("DANGEROUS_DELETE_APP_WEBHOOK_BY_NAME", () => {
describe("in report mode", () => {
it("does not call delete", async () => {
const deleteMock = vitest.fn();
const appWebhookRepository = {
delete: deleteMock,
getAll: () => [],
} as unknown as AppWebhookRepository;
const appWebhookMigrator = new AppWebhookMigrator(
{
appWebhookRepository,
apiUrl: "apiUrl",
appId: "appId",
},
{ mode: "report" }
);
await appWebhookMigrator.DANGEROUS_DELETE_APP_WEBHOOK_BY_NAME("OrderCreated");
expect(deleteMock).not.toHaveBeenCalled();
});
});
describe("in migrate mode", () => {
it("calls delete", async () => {
const deleteMock = vitest.fn();
const appWebhookRepository = {
delete: deleteMock,
getAll: () => [
{
name: "OrderCreated",
id: "id-1234",
},
],
} as unknown as AppWebhookRepository;
const appWebhookMigrator = new AppWebhookMigrator(
{
appWebhookRepository,
apiUrl: "apiUrl",
appId: "appId",
},
{ mode: "migrate" }
);
await appWebhookMigrator.DANGEROUS_DELETE_APP_WEBHOOK_BY_NAME("OrderCreated");
expect(deleteMock).toHaveBeenCalledWith("id-1234");
});
});
});
});