-
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.
Merge pull request #5 from evantahler/seesion
Seesion
- Loading branch information
Showing
20 changed files
with
389 additions
and
65 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 |
---|---|---|
|
@@ -30,6 +30,8 @@ jobs: | |
services: | ||
redis: | ||
image: redis | ||
ports: | ||
- 6379:6379 | ||
postgres: | ||
image: postgres | ||
env: | ||
|
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { test, expect, beforeAll, afterAll } from "bun:test"; | ||
import { describe, test, expect, beforeAll, afterAll } from "bun:test"; | ||
import { api, type ActionResponse } from "../../api"; | ||
import type { UserCreate } from "../../actions/user"; | ||
import type { UserCreate, UserEdit } from "../../actions/user"; | ||
import { config } from "../../config"; | ||
import type { SessionCreate } from "../../actions/session"; | ||
|
||
const url = `http://${config.server.web.host}:${config.server.web.port}`; | ||
|
||
|
@@ -14,34 +15,82 @@ afterAll(async () => { | |
await api.stop(); | ||
}); | ||
|
||
test("user can be created", async () => { | ||
const res = await fetch(url + "/api/user", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
name: "person 1", | ||
email: "[email protected]", | ||
password: "password", | ||
}), | ||
describe("userCreate", () => { | ||
test("user can be created", async () => { | ||
const res = await fetch(url + "/api/user", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
name: "Mario Mario", | ||
email: "[email protected]", | ||
password: "mushroom1", | ||
}), | ||
}); | ||
const response = (await res.json()) as ActionResponse<UserCreate>; | ||
expect(res.status).toBe(200); | ||
|
||
expect(response.user.id).toEqual(1); | ||
expect(response.user.email).toEqual("[email protected]"); | ||
}); | ||
const response = (await res.json()) as ActionResponse<UserCreate>; | ||
expect(res.status).toBe(200); | ||
|
||
expect(response.id).toEqual(1); | ||
expect(response.email).toEqual("[email protected]"); | ||
test("email must be unique", async () => { | ||
const res = await fetch(url + "/api/user", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
name: "Mario Mario", | ||
email: "[email protected]", | ||
password: "mushroom1", | ||
}), | ||
}); | ||
const response = (await res.json()) as ActionResponse<UserCreate>; | ||
expect(res.status).toBe(500); | ||
expect(response.error?.message).toMatch(/violates unique constraint/); | ||
}); | ||
}); | ||
|
||
test("email must be unique", async () => { | ||
const res = await fetch(url + "/api/user", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
name: "person 1", | ||
email: "[email protected]", | ||
password: "password", | ||
}), | ||
describe("userEdit", () => { | ||
test("it fails without a session", async () => { | ||
const res = await fetch(url + "/api/user", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ name: "new name" }), | ||
}); | ||
const response = (await res.json()) as ActionResponse<UserEdit>; | ||
expect(res.status).toBe(500); | ||
expect(response.error?.message).toMatch(/User not found/); | ||
}); | ||
|
||
test("the user can be updated", async () => { | ||
const sessionRes = await fetch(url + "/api/session", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
email: "[email protected]", | ||
password: "mushroom1", | ||
}), | ||
}); | ||
const sessionResponse = | ||
(await sessionRes.json()) as ActionResponse<SessionCreate>; | ||
expect(sessionRes.status).toBe(200); | ||
const sessionId = sessionResponse.session.id; | ||
|
||
await Bun.sleep(1001); | ||
|
||
const res = await fetch(url + "/api/user", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Set-Cookie": `${config.session.cookieName}=${sessionId}`, | ||
}, | ||
body: JSON.stringify({ name: "new name" }), | ||
}); | ||
const response = (await res.json()) as ActionResponse<UserEdit>; | ||
expect(res.status).toBe(200); | ||
expect(response.user.name).toEqual("new name"); | ||
expect(response.user.email).toEqual("[email protected]"); | ||
expect(sessionResponse.user.updatedAt).toBeLessThan( | ||
response.user.updatedAt, | ||
); | ||
}); | ||
const response = (await res.json()) as ActionResponse<UserCreate>; | ||
expect(res.status).toBe(500); | ||
expect(response.error?.message).toMatch(/violates unique constraint/); | ||
}); |
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,6 @@ | ||
import { loadFromEnvIfSet } from "../util/config"; | ||
|
||
export const configSession = { | ||
ttl: await loadFromEnvIfSet("session.ttl", 1000 * 60 * 60 * 24), // one day, in seconds | ||
cookieName: await loadFromEnvIfSet("session.cookieName", "__session"), | ||
}; |
Oops, something went wrong.