Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): keep custom domain when provider removal fails

Aditya Garud0bbcda68d8bd
diff --git a/apps/web/__tests__/unit/remove-organization-domain.test.ts b/apps/web/__tests__/unit/remove-organization-domain.test.ts
new file mode 100644
index 000000000..d0d04ebe6
--- /dev/null
+++ b/apps/web/__tests__/unit/remove-organization-domain.test.ts
@@ -0,0 +1,80 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ updates: [] as unknown[],
+}));
+
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: async () => ({ id: "owner" }),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({
+ from: () => ({
+ where: async () => [{ id: "org", customDomain: "video.example.com" }],
+ }),
+ }),
+ update: () => ({
+ set: (values: unknown) => ({
+ where: async () => {
+ mocks.updates.push(values);
+ },
+ }),
+ }),
+ }),
+}));
+
+vi.mock("@/actions/organization/authorization", () => ({
+ requireOrganizationSettingsManager: async () => ({}),
+}));
+
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+
+import type { Organisation } from "@cap/web-domain";
+import { removeOrganizationDomain } from "@/actions/organization/remove-domain";
+
+const organizationId = "org" as Organisation.OrganisationId;
+
+describe("removeOrganizationDomain", () => {
+ beforeEach(() => {
+ mocks.updates = [];
+ vi.unstubAllGlobals();
+ });
+
+ it("keeps the domain when the provider rejects removal", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(new Response(null, { status: 403 })),
+ );
+
+ await expect(removeOrganizationDomain(organizationId)).rejects.toThrow();
+ expect(mocks.updates).toEqual([]);
+ });
+
+ it("clears the domain after the provider removes it", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(new Response(null, { status: 204 })),
+ );
+
+ await expect(removeOrganizationDomain(organizationId)).resolves.toEqual({
+ success: true,
+ });
+ expect(mocks.updates).toEqual([
+ { customDomain: null, domainVerified: null },
+ ]);
+ });
+
+ it("clears a domain that is already absent at the provider", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(new Response(null, { status: 404 })),
+ );
+
+ await expect(removeOrganizationDomain(organizationId)).resolves.toEqual({
+ success: true,
+ });
+ expect(mocks.updates).toHaveLength(1);
+ });
+});
diff --git a/apps/web/actions/organization/remove-domain.ts b/apps/web/actions/organization/remove-domain.ts
index 5d1c051bc..a38f560e3 100644
--- a/apps/web/actions/organization/remove-domain.ts
+++ b/apps/web/actions/organization/remove-domain.ts
@@ -28,7 +28,7 @@ export async function removeOrganizationDomain(
try {
if (organization.customDomain) {
- await fetch(
+ const response = await fetch(
`https://api.vercel.com/v9/projects/${
process.env.VERCEL_PROJECT_ID
}/domains/${organization.customDomain.toLowerCase()}?teamId=${
@@ -41,6 +41,9 @@ export async function removeOrganizationDomain(
},
},
);
+ if (!response.ok && response.status !== 404) {
+ throw new Error("Failed to remove domain from hosting provider");
+ }
}
await db()