Repository navigation
fix(web): preserve domain verification when status checks fail #3553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yashranaway
wants to merge
2
commits into
CapSoftware:main
Choose a base branch
from
yashranaway:fix/preserve-domain-verification-on-check-failure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
apps/web/__tests__/unit/check-organization-domain.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import { Organisation } from "@cap/web-domain"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { checkOrganizationDomain } from "@/actions/organization/check-domain"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| verifiedAt: new Date("2026-01-01T00:00:00Z") as Date | null, | ||
| update: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@cap/database/auth/session", () => ({ | ||
| getCurrentUser: async () => ({ id: "owner" }), | ||
| })); | ||
|
|
||
| vi.mock("@cap/database", () => ({ | ||
| db: () => ({ | ||
| select: () => ({ | ||
| from: () => ({ | ||
| where: async () => [ | ||
| { | ||
| id: "org", | ||
| customDomain: "videos.example.com", | ||
| domainVerified: mocks.verifiedAt, | ||
| }, | ||
| ], | ||
| }), | ||
| }), | ||
| update: () => ({ | ||
| set: (values: unknown) => ({ where: () => mocks.update(values) }), | ||
| }), | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock("@/actions/organization/authorization", () => ({ | ||
| requireOrganizationSettingsManager: vi.fn(), | ||
| })); | ||
|
|
||
| const organizationId = Organisation.OrganisationId.make("org"); | ||
|
|
||
| function providerResponses(misconfigured: boolean) { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn(async (url: string) => { | ||
| if (url.includes("/config?")) { | ||
| return Response.json({ misconfigured }); | ||
| } | ||
| if (url.includes("/records?")) return Response.json({ records: [] }); | ||
| return Response.json({ verified: true }); | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| describe("checkOrganizationDomain", () => { | ||
| beforeEach(() => { | ||
| mocks.verifiedAt = new Date("2026-01-01T00:00:00Z"); | ||
| mocks.update.mockReset(); | ||
| vi.spyOn(console, "error").mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it.each(["network", "invalid JSON"])( | ||
| "keeps verification on a %s failure and reports the lookup error", | ||
| async (failure) => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| failure === "network" | ||
| ? vi.fn().mockRejectedValue(new Error("Network unavailable")) | ||
| : vi | ||
| .fn() | ||
| .mockResolvedValue( | ||
| new Response("Service unavailable", { status: 503 }), | ||
| ), | ||
| ); | ||
|
|
||
| await expect(checkOrganizationDomain(organizationId)).rejects.toThrow( | ||
| "Failed to check domain status", | ||
| ); | ||
| expect(mocks.update).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
|
|
||
| it("keeps verification when the provider returns a JSON error", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn(async () => | ||
| Response.json( | ||
| { error: { code: "internal_server_error", message: "Unavailable" } }, | ||
| { status: 500 }, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| await expect(checkOrganizationDomain(organizationId)).rejects.toThrow( | ||
| "Failed to check domain status", | ||
| ); | ||
| expect(mocks.update).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("revokes verification when the provider no longer has the domain", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn(async (url: string) => { | ||
| if (url.includes("/config?")) { | ||
| return Response.json({ misconfigured: false }); | ||
| } | ||
| if (url.includes("/records?")) return Response.json({ records: [] }); | ||
| return Response.json( | ||
| { error: { code: "not_found", message: "Domain not found" } }, | ||
| { status: 404 }, | ||
| ); | ||
| }), | ||
| ); | ||
|
|
||
| await expect( | ||
| checkOrganizationDomain(organizationId), | ||
| ).resolves.toMatchObject({ verified: false }); | ||
| expect(mocks.update).toHaveBeenCalledExactlyOnceWith({ | ||
| domainVerified: null, | ||
| }); | ||
| }); | ||
|
|
||
| it("revokes verification when the provider confirms misconfiguration", async () => { | ||
| providerResponses(true); | ||
|
|
||
| await expect( | ||
| checkOrganizationDomain(organizationId), | ||
| ).resolves.toMatchObject({ verified: false }); | ||
| expect(mocks.update).toHaveBeenCalledExactlyOnceWith({ | ||
| domainVerified: null, | ||
| }); | ||
| }); | ||
|
|
||
| it("records verification when the provider confirms a new domain", async () => { | ||
| mocks.verifiedAt = null; | ||
| providerResponses(false); | ||
|
|
||
| await expect( | ||
| checkOrganizationDomain(organizationId), | ||
| ).resolves.toMatchObject({ verified: true }); | ||
| expect(mocks.update).toHaveBeenCalledExactlyOnceWith({ | ||
| domainVerified: expect.any(Date), | ||
| }); | ||
| }); | ||
|
|
||
| it("keeps the existing timestamp for a domain that is still verified", async () => { | ||
| providerResponses(false); | ||
|
|
||
| await expect( | ||
| checkOrganizationDomain(organizationId), | ||
| ).resolves.toMatchObject({ verified: true }); | ||
| expect(mocks.update).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.