-
Notifications
You must be signed in to change notification settings - Fork 0
DC-4635 Add Tests #58
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c146fa7
fix: create-db tests added
aidankmcalister 11e8668
feat: claim page render and auth call tests working
aidankmcalister d724a94
feat: claim flow/page tests
aidankmcalister 03c0761
feat: github workflow added
aidankmcalister 41eecb3
fix: update workflow
aidankmcalister 3f5310a
fix: test updates
aidankmcalister 90b0f22
fix: coderabbit update
aidankmcalister 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
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,48 @@ | ||
name: Tests | ||
|
||
on: | ||
pull_request: | ||
branches: [main] | ||
push: | ||
branches: [main] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
pull-requests: read | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup pnpm | ||
uses: pnpm/action-setup@v4 | ||
with: | ||
version: 9 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
cache: "pnpm" | ||
|
||
- name: Install Dependencies | ||
run: pnpm install --frozen-lockfile | ||
working-directory: ./claim-db-worker | ||
|
||
- name: Run claim-db-worker tests | ||
run: pnpm test | ||
working-directory: ./claim-db-worker | ||
env: | ||
NODE_ENV: test | ||
|
||
- name: Install create-db dependencies | ||
run: pnpm install --frozen-lockfile | ||
working-directory: ./create-db | ||
|
||
- name: Run create-db tests | ||
run: pnpm test | ||
working-directory: ./create-db | ||
env: | ||
NODE_ENV: test |
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,196 @@ | ||
// __tests__/callback-api.test.ts | ||
import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
import { GET } from "../app/api/auth/callback/route"; | ||
import { NextRequest } from "next/server"; | ||
|
||
vi.mock("@/lib/env", () => ({ | ||
getEnv: vi.fn(() => ({ | ||
CLAIM_DB_RATE_LIMITER: { | ||
limit: vi.fn(() => Promise.resolve({ success: true })), | ||
}, | ||
POSTHOG_API_KEY: "test-key", | ||
POSTHOG_API_HOST: "https://app.posthog.com", | ||
})), | ||
})); | ||
|
||
vi.mock("@/lib/auth-utils", () => ({ | ||
exchangeCodeForToken: vi.fn(), | ||
validateProject: vi.fn(), | ||
})); | ||
|
||
vi.mock("@/lib/response-utils", () => ({ | ||
redirectToError: vi.fn(), | ||
redirectToSuccess: vi.fn(), | ||
getBaseUrl: vi.fn(() => "http://localhost:3000"), | ||
})); | ||
|
||
vi.mock("@/lib/project-transfer", () => ({ | ||
transferProject: vi.fn(), | ||
})); | ||
|
||
const mockFetch = vi.fn(); | ||
global.fetch = mockFetch; | ||
|
||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
describe("auth callback API", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("successful claim flow", () => { | ||
it("completes full OAuth callback and project transfer", async () => { | ||
const { exchangeCodeForToken, validateProject } = await import( | ||
"@/lib/auth-utils" | ||
); | ||
const { redirectToError, redirectToSuccess } = await import( | ||
"@/lib/response-utils" | ||
); | ||
const { transferProject } = await import("@/lib/project-transfer"); | ||
|
||
vi.mocked(exchangeCodeForToken).mockResolvedValue({ | ||
access_token: "test-token", | ||
}); | ||
|
||
vi.mocked(validateProject).mockResolvedValue(undefined); | ||
|
||
vi.mocked(transferProject).mockResolvedValue({ | ||
success: true, | ||
status: 200, | ||
}); | ||
|
||
vi.mocked(redirectToSuccess).mockReturnValue( | ||
new Response(null, { | ||
status: 302, | ||
headers: { Location: "/success?projectID=test-project-123" }, | ||
}) | ||
); | ||
|
||
mockFetch.mockResolvedValue({ | ||
ok: true, | ||
json: async () => ({}), | ||
}); | ||
|
||
const request = new NextRequest( | ||
"http://localhost:3000/api/auth/callback?code=test-code&state=test-state&projectID=test-project-123" | ||
); | ||
|
||
const response = await GET(request); | ||
|
||
expect(exchangeCodeForToken).toHaveBeenCalledWith( | ||
"test-code", | ||
expect.stringContaining("test-project-123") | ||
); | ||
expect(validateProject).toHaveBeenCalledWith("test-project-123"); | ||
expect(transferProject).toHaveBeenCalledWith( | ||
"test-project-123", | ||
"test-token" | ||
); | ||
expect(redirectToSuccess).toHaveBeenCalledWith( | ||
request, | ||
"test-project-123" | ||
); | ||
|
||
expect(mockFetch).toHaveBeenCalledWith( | ||
expect.stringContaining("posthog.com"), | ||
expect.objectContaining({ | ||
method: "POST", | ||
body: expect.stringContaining("create_db:claim_successful"), | ||
}) | ||
); | ||
}); | ||
}); | ||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
describe("error handling", () => { | ||
it("handles missing parameters", async () => { | ||
const { redirectToError } = await import("@/lib/response-utils"); | ||
|
||
vi.mocked(redirectToError).mockReturnValue( | ||
new Response(null, { | ||
status: 302, | ||
headers: { Location: "/error" }, | ||
}) | ||
); | ||
|
||
const request = new NextRequest( | ||
"http://localhost:3000/api/auth/callback?code=test-code" | ||
); | ||
|
||
await GET(request); | ||
|
||
expect(redirectToError).toHaveBeenCalledWith( | ||
request, | ||
"Missing State Parameter", | ||
"Please try again.", | ||
"The state parameter is required for security purposes." | ||
); | ||
}); | ||
|
||
it("handles auth token exchange failure", async () => { | ||
const { exchangeCodeForToken } = await import("@/lib/auth-utils"); | ||
const { redirectToError } = await import("@/lib/response-utils"); | ||
|
||
vi.mocked(exchangeCodeForToken).mockRejectedValue( | ||
new Error("Invalid authorization code") | ||
); | ||
|
||
vi.mocked(redirectToError).mockReturnValue( | ||
new Response(null, { | ||
status: 302, | ||
headers: { Location: "/error" }, | ||
}) | ||
); | ||
|
||
const request = new NextRequest( | ||
"http://localhost:3000/api/auth/callback?code=invalid-code&state=test-state&projectID=test-project-123" | ||
); | ||
|
||
await GET(request); | ||
|
||
expect(redirectToError).toHaveBeenCalledWith( | ||
request, | ||
"Authentication Failed", | ||
"Failed to authenticate with Prisma. Please try again.", | ||
"Invalid authorization code" | ||
); | ||
}); | ||
|
||
it("handles project transfer failure", async () => { | ||
const { exchangeCodeForToken, validateProject } = await import( | ||
"@/lib/auth-utils" | ||
); | ||
const { redirectToError } = await import("@/lib/response-utils"); | ||
const { transferProject } = await import("@/lib/project-transfer"); | ||
|
||
vi.mocked(exchangeCodeForToken).mockResolvedValue({ | ||
access_token: "test-token", | ||
}); | ||
|
||
vi.mocked(validateProject).mockResolvedValue(undefined); | ||
|
||
vi.mocked(transferProject).mockResolvedValue({ | ||
success: false, | ||
status: 403, | ||
error: "Insufficient permissions", | ||
}); | ||
|
||
vi.mocked(redirectToError).mockReturnValue( | ||
new Response(null, { | ||
status: 302, | ||
headers: { Location: "/error" }, | ||
}) | ||
); | ||
|
||
const request = new NextRequest( | ||
"http://localhost:3000/api/auth/callback?code=test-code&state=test-state&projectID=test-project-123" | ||
); | ||
|
||
await GET(request); | ||
|
||
expect(redirectToError).toHaveBeenCalledWith( | ||
request, | ||
"Transfer Failed", | ||
"Failed to transfer the project. Please try again.", | ||
expect.stringContaining("Insufficient permissions") | ||
); | ||
}); | ||
}); | ||
}); |
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.