-
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
1 parent
7bc1218
commit 63ce72e
Showing
10 changed files
with
180 additions
and
52 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
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,66 @@ | ||
import { test, expect, beforeAll, afterAll } from "bun:test"; | ||
import { api, type ActionResponse } from "../../api"; | ||
import { config } from "../../config"; | ||
import { users } from "../../schema/users"; | ||
import { hashPassword } from "../../ops/UserOps"; | ||
import type { SessionCreate } from "../../actions/session"; | ||
|
||
const url = `http://${config.server.web.host}:${config.server.web.port}`; | ||
|
||
beforeAll(async () => { | ||
await api.start(); | ||
await api.db.clearDatabase(); | ||
await api.db.db.insert(users).values({ | ||
name: "Mario Mario", | ||
email: "[email protected]", | ||
password_hash: await hashPassword("mushroom1"), | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await api.stop(); | ||
}); | ||
|
||
test("returns user when matched", async () => { | ||
const res = await fetch(url + "/api/session", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
email: "[email protected]", | ||
password: "mushroom1", | ||
}), | ||
}); | ||
const response = (await res.json()) as ActionResponse<SessionCreate>; | ||
expect(res.status).toBe(200); | ||
|
||
expect(response.id).toEqual(1); | ||
expect(response.name).toEqual("Mario Mario"); | ||
}); | ||
|
||
test("fails when users is not found", async () => { | ||
const res = await fetch(url + "/api/session", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
email: "[email protected]", | ||
password: "xxx", | ||
}), | ||
}); | ||
const response = (await res.json()) as ActionResponse<SessionCreate>; | ||
expect(res.status).toBe(500); | ||
expect(response.error?.message).toEqual("User not found"); | ||
}); | ||
|
||
test("fails when passwords do not match", async () => { | ||
const res = await fetch(url + "/api/session", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
email: "[email protected]", | ||
password: "yoshi", | ||
}), | ||
}); | ||
const response = (await res.json()) as ActionResponse<SessionCreate>; | ||
expect(res.status).toBe(500); | ||
expect(response.error?.message).toEqual("Password does not match"); | ||
}); |
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,46 @@ | ||
import { eq } from "drizzle-orm"; | ||
import { api, type Action, type ActionParams } from "../api"; | ||
import { users } from "../schema/users"; | ||
import { ensureString } from "../util/formatters"; | ||
import { emailValidator, passwordValidator } from "../util/validators"; | ||
import { serializeUser, checkPassword } from "../ops/UserOps"; | ||
import { ErrorType, TypedError } from "../classes/TypedError"; | ||
import { HTTP_METHOD } from "../classes/Action"; | ||
|
||
export class SessionCreate implements Action { | ||
name = "sessionCreate"; | ||
web = { route: "/session", method: HTTP_METHOD.PUT }; | ||
inputs = { | ||
email: { | ||
required: true, | ||
validator: emailValidator, | ||
formatter: ensureString, | ||
}, | ||
password: { | ||
required: true, | ||
validator: passwordValidator, | ||
formatter: ensureString, | ||
}, | ||
}; | ||
|
||
run = async (params: ActionParams<SessionCreate>) => { | ||
const [user] = await api.db.db | ||
.select() | ||
.from(users) | ||
.where(eq(users.email, params.email.toLowerCase())); | ||
|
||
if (!user) { | ||
throw new TypedError("User not found", ErrorType.CONNECTION_ACTION_RUN); | ||
} | ||
|
||
const passwordMatch = await checkPassword(user, params.password); | ||
if (!passwordMatch) { | ||
throw new TypedError( | ||
"Password does not match", | ||
ErrorType.CONNECTION_ACTION_RUN, | ||
); | ||
} | ||
|
||
return serializeUser(user); | ||
}; | ||
} |
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
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
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,27 @@ | ||
const stringLengthValidator = (s: string, minLength: number) => { | ||
if (s.length < minLength) { | ||
throw new Error( | ||
`This field is required and must be at least ${minLength} characters long`, | ||
); | ||
} | ||
}; | ||
|
||
export const nameValidator = (p: string) => { | ||
stringLengthValidator(p, 3); | ||
|
||
return true as const; | ||
}; | ||
|
||
export const emailValidator = (p: string): true => { | ||
stringLengthValidator(p, 3); | ||
if (!p.includes("@")) throw new Error(`This is not a valid email`); | ||
if (!p.includes(".")) throw new Error(`This is not a valid email`); | ||
|
||
return true as const; | ||
}; | ||
|
||
export const passwordValidator = (p: string): true => { | ||
stringLengthValidator(p, 3); | ||
|
||
return true as const; | ||
}; |