diff --git a/packages/api/knexfile.ts b/packages/api/knexfile.ts index 4648de3..63d29a6 100644 --- a/packages/api/knexfile.ts +++ b/packages/api/knexfile.ts @@ -5,9 +5,13 @@ config(); const options = { client: process.env.DB_CLIENT, - connection: process.env.CONNECTION || { - filename: "db/db.sqlite3" - }, + connection: process.env.CONNECTION + ? { + database: process.env.CONNECTION + } + : { + filename: "db/db.sqlite3" + }, migrations: { directory: "db/migrations", tableName: "migrations" @@ -25,9 +29,13 @@ const configs: Record = { test: { ...options, - connection: process.env.TEST_CONNECTION || { - filename: "db/testdb.sqlite3" - } + connection: process.env.TEST_CONNECTION + ? { + database: process.env.TEST_CONNECTION + } + : { + filename: "db/db.sqlite3" + } }, production: { diff --git a/packages/api/src/modules/auth/router.test.ts b/packages/api/src/modules/auth/router.test.ts index a2d2909..96133fa 100644 --- a/packages/api/src/modules/auth/router.test.ts +++ b/packages/api/src/modules/auth/router.test.ts @@ -11,6 +11,12 @@ import users from "../../../db/seeds/examples/users"; const agent = request.agent(server); describe("Auth router", () => { + before(async function() { + this.timeout(5000); + await knex.migrate.latest(); + await knex.seed.run(); + }); + it("Logs-in a user", async () => { const { email, name, password } = users[0]; @@ -28,17 +34,11 @@ describe("Auth router", () => { }); it("Logs-out a user", async () => { - const { email, password } = users[0]; - const response = await agent .get(`/api/auth/logout`) - .send({ email, password }) - .set("Accept", "application/json") + .set("Accept", "application/text") .expect(200); - expect(response.body).to.deep.equal({ - status: 200, - message: "Successfully logged out" - }); + expect(response.text).to.equal("Successfully logged out"); }); }); diff --git a/packages/api/src/modules/auth/router.ts b/packages/api/src/modules/auth/router.ts index cacad42..aa8bb79 100644 --- a/packages/api/src/modules/auth/router.ts +++ b/packages/api/src/modules/auth/router.ts @@ -23,7 +23,7 @@ router.post( if (!user) throw new HttpError( 400, - "There seems to be nco user with that email" + "There seems to be no user with that email" ); const valid = await bcrypt.compare(password, user.password); @@ -34,14 +34,14 @@ router.post( ctx.body = { status: 200, message: "Successfully log in", - user: { username: user.username, role: user.role } + user: { name: user.name, role: user.role } }; await next(); } ); -router.post("/logout", requireAuthenticated(), async (ctx, next) => { +router.get("/logout", requireAuthenticated(), async (ctx, next) => { ctx.session = null; ctx.status = 200; diff --git a/packages/api/src/modules/users/actions/createUser.ts b/packages/api/src/modules/users/actions/createUser.ts index 6164247..4dbe274 100644 --- a/packages/api/src/modules/users/actions/createUser.ts +++ b/packages/api/src/modules/users/actions/createUser.ts @@ -4,12 +4,12 @@ import bcrypt from "bcrypt"; import User from "../types/User"; export const createUser = async ( - user: Pick + user: Pick ) => { const isTaken = Boolean( await knex("users") .select("id") - .where({ username: user.username }) + .where({ name: user.name }) .orWhere({ email: user.email }) .first() ); diff --git a/packages/api/src/modules/users/router.test.ts b/packages/api/src/modules/users/router.test.ts index e01ffbc..f743b1b 100644 --- a/packages/api/src/modules/users/router.test.ts +++ b/packages/api/src/modules/users/router.test.ts @@ -7,7 +7,7 @@ const agent = request.agent(server); const newUser = { email: "FakeyMcFakerson@fakemail.com", - username: "SomeGuy", + name: "SomeGuy", password: "WhatShouldITypeHere88@" }; diff --git a/packages/api/src/modules/users/router.ts b/packages/api/src/modules/users/router.ts index 5d70622..1b1a5bc 100644 --- a/packages/api/src/modules/users/router.ts +++ b/packages/api/src/modules/users/router.ts @@ -12,9 +12,9 @@ router.post( "/createUser", validateSchema(registerBody, "body"), async (ctx, next) => { - const { username, password, email } = ctx.request.body as RegisterBody; + const { name, password, email } = ctx.request.body as RegisterBody; - const user = await createUser({ email, username, password }); + const user = await createUser({ email, name, password }); if (!user) { throw new HttpError(400, "That username seems to be already taken"); diff --git a/packages/api/src/modules/users/types/RegisterBody.ts b/packages/api/src/modules/users/types/RegisterBody.ts index 5006243..9f43e00 100644 --- a/packages/api/src/modules/users/types/RegisterBody.ts +++ b/packages/api/src/modules/users/types/RegisterBody.ts @@ -1,5 +1,5 @@ export interface RegisterBody { email: string; - username: string; + name: string; password: string; } diff --git a/packages/api/src/modules/users/types/User.ts b/packages/api/src/modules/users/types/User.ts index c9bff20..74741c4 100644 --- a/packages/api/src/modules/users/types/User.ts +++ b/packages/api/src/modules/users/types/User.ts @@ -1,7 +1,7 @@ export default interface User { id: number; email: string; - username: string; + name: string; password: string; role?: string | null; description?: string | null;