-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
517 additions
and
97 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
TURSO_URL = "libsql://auth-db-username.turso.io" | ||
TURSO_AUTH_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | ||
COOKIE_SIGNING = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | ||
TURSO_URL="libsql://your-db.turso.io" | ||
TURSO_AUTH_TOKEN="your-auth-token" | ||
JWT_ACCESS_SECRET="your-access-secret" | ||
JWT_REFRESH_SECRET="your-refresh-secret" |
This file contains 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 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,159 @@ | ||
import { describe, expect, mock, test } from "bun:test"; | ||
import type { Context, TypedResponse } from "hono"; | ||
import type { RedirectStatusCode } from "hono/utils/http-status"; | ||
import { handleLogin, handleRegistration } from "./handler"; | ||
|
||
type ContextEnv = { | ||
TURSO_URL: string; | ||
TURSO_AUTH_TOKEN: string; | ||
}; | ||
|
||
const createMockTypedResponse = ( | ||
location: string, | ||
status: RedirectStatusCode = 302, | ||
): Response & TypedResponse<undefined, typeof status, "redirect"> => { | ||
return { | ||
...new Response(null, { | ||
status, | ||
headers: { Location: location }, | ||
}), | ||
status, | ||
redirect: location, | ||
_data: undefined, | ||
_status: status, | ||
_format: "redirect", | ||
} as Response & TypedResponse<undefined, typeof status, "redirect">; | ||
}; | ||
|
||
const mockContext = { | ||
env: { | ||
TURSO_URL: "libsql://test", | ||
TURSO_AUTH_TOKEN: "test", | ||
}, | ||
req: { | ||
parseBody: async () => ({ | ||
email: "[email protected]", | ||
password: "password123", | ||
}), | ||
} as unknown as Request, | ||
redirect: (location: string, status: RedirectStatusCode = 302) => | ||
createMockTypedResponse(location, status), | ||
// Required Context properties | ||
finalized: false, | ||
error: null, | ||
event: null, | ||
executionCtx: null, | ||
get: () => undefined, | ||
header: () => undefined, | ||
match: () => false, | ||
newResponse: () => new Response(), | ||
set: () => {}, | ||
update: () => new Response(), | ||
// Handle other required Context properties | ||
param: () => "", | ||
data: {}, | ||
json: () => new Response(), | ||
text: () => new Response(), | ||
html: () => new Response(), | ||
status: () => mockContext, | ||
res: undefined, | ||
// Add runtime type information | ||
runtime: "bun", | ||
} as unknown as Context<{ Bindings: ContextEnv }>; | ||
|
||
describe("Handler", () => { | ||
describe("handleLogin", () => { | ||
test("redirects with error for invalid credentials", async () => { | ||
mock.module("./services", () => ({ | ||
accountService: { | ||
authenticate: async () => ({ authenticated: false }), | ||
}, | ||
})); | ||
|
||
const response = await handleLogin(mockContext); | ||
expect(response.headers.get("Location")).toBe( | ||
"/?error=Invalid email or password", | ||
); | ||
}); | ||
|
||
test("creates session and redirects on successful login", async () => { | ||
mock.module("./services", () => ({ | ||
accountService: { | ||
authenticate: async () => ({ authenticated: true, userId: 1 }), | ||
}, | ||
})); | ||
|
||
mock.module("./session", () => ({ | ||
createSession: async () => "test-session-id", | ||
})); | ||
|
||
const response = await handleLogin(mockContext); | ||
expect(response.headers.get("Location")).toBe("/?authenticated=true"); | ||
}); | ||
|
||
test("handles authentication errors gracefully", async () => { | ||
mock.module("./services", () => ({ | ||
accountService: { | ||
authenticate: async () => { | ||
throw new Error("Auth error"); | ||
}, | ||
}, | ||
})); | ||
|
||
const response = await handleLogin(mockContext); | ||
expect(response.headers.get("Location")).toBe( | ||
"/?error=Authentication failed. Please try again.", | ||
); | ||
}); | ||
}); | ||
|
||
describe("handleRegistration", () => { | ||
test("redirects on successful registration", async () => { | ||
mock.module("./services", () => ({ | ||
accountService: { | ||
createAccount: async () => ({ rowsAffected: 1 }), | ||
}, | ||
})); | ||
|
||
const response = await handleRegistration(mockContext); | ||
expect(response.headers.get("Location")).toBe("/?registered=true"); | ||
}); | ||
|
||
test("handles validation errors", async () => { | ||
const validationError = new Error("Password too short") as Error & { | ||
code: string; | ||
message: string; | ||
}; | ||
validationError.code = "VALIDATION_ERROR"; | ||
validationError.message = "Password must be at least 8 characters"; | ||
|
||
mock.module("./services", () => ({ | ||
accountService: { | ||
createAccount: async () => { | ||
throw validationError; | ||
}, | ||
}, | ||
})); | ||
|
||
const response = await handleRegistration(mockContext); | ||
expect(response.headers.get("Location")).toBe( | ||
"/?error=Password must be at least 8 characters", | ||
); | ||
}); | ||
|
||
test("handles unexpected registration errors", async () => { | ||
mock.module("./services", () => ({ | ||
accountService: { | ||
createAccount: async () => { | ||
throw new Error("Unexpected error"); | ||
}, | ||
}, | ||
})); | ||
|
||
const response = await handleRegistration(mockContext); | ||
expect(response.headers.get("Location")).toBe( | ||
"/?error=Registration failed. Please try again.", | ||
); | ||
}); | ||
}); | ||
}); |
This file contains 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 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.