diff --git a/app/types/auth.d.ts b/app/types/auth.d.ts index 52af32b..7946b83 100644 --- a/app/types/auth.d.ts +++ b/app/types/auth.d.ts @@ -1,3 +1,3 @@ declare module '#auth-utils' { - interface User extends import('~/types/models').User -} \ No newline at end of file + interface User extends import('~/types/models').User {} +} diff --git a/app/utils/shared.ts b/app/utils/shared.ts index 77ea635..a42878b 100644 --- a/app/utils/shared.ts +++ b/app/utils/shared.ts @@ -1,7 +1,9 @@ import type { HeaderLink } from '@nuxt/ui-pro/types' import type { CookieOptions } from '#app' -(BigInt.prototype as any).toJSON = function () { return this.toString() } +(BigInt.prototype as any).toJSON = function () { + return this.toString() +} export const weekDays = [...Array(7)].map((_, i) => new Intl.DateTimeFormat('en-US', { weekday: 'short' }).format(new Date(1970, 0, 4 + i)), diff --git a/lib/prisma.ts b/lib/prisma.ts index 7578cab..95de492 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -6,6 +6,7 @@ const prismaClientSingleton = () => { declare const globalThis: { prismaGlobal: ReturnType +// eslint-disable-next-line no-restricted-globals } & typeof global const prisma = globalThis.prismaGlobal ?? prismaClientSingleton() diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml index e5a788a..9bee74d 100644 --- a/prisma/migrations/migration_lock.toml +++ b/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually # It should be added in your version-control system (i.e. Git) -provider = "mysql" \ No newline at end of file +provider = "mysql" diff --git a/server/controllers/cartridge.ts b/server/controllers/cartridge.ts index c2e168d..45cac28 100644 --- a/server/controllers/cartridge.ts +++ b/server/controllers/cartridge.ts @@ -2,7 +2,7 @@ import { z } from 'zod' import type { Cartridge } from '~/types/models' import { cartridgeContents, cartridgeMgs, cartridgeMls } from '~/utils/shared' -const index = authedHandler(async ({ user, event }) => { +const index = authedHandler(async ({ user }) => { return metapi().render( await prisma.cartridge.findMany({ where: { diff --git a/server/controllers/pen.ts b/server/controllers/pen.ts index 761b798..4b5af5f 100644 --- a/server/controllers/pen.ts +++ b/server/controllers/pen.ts @@ -1,17 +1,7 @@ import { z } from 'zod' import { penColors } from '~/utils/shared' -const inc = { - cartridge: { - include: { - shots: { - orderBy: { - date: 'asc', - }, - }, - }, - }, -} +const inc = { cartridge: { include: { shots: { orderBy: { date: 'asc' } } } } } const index = defineEventHandler(async (event) => { const { user } = await requireUserSession(event) @@ -61,8 +51,7 @@ const update = authedHandler(async ({ user, event }) => { shotDay: body?.shotDay || undefined, }) if (!parsed.success) return metapi().error(event, parsed.error.issues, 400) - console.log(parsed.data) - const update = { + return metapi().success('pen updated', await prisma.pen.update({ where: { id: parsed.data.id, userId: user.id, @@ -73,11 +62,7 @@ const update = authedHandler(async ({ user, event }) => { shotDay: parsed.data.shotDay || null, }, include: inc, - } - console.log(update) - const pen = await prisma.pen.update(update) - - return metapi().success('pen updated', pen) + })) }) const get = defineEventHandler(async (event) => { diff --git a/server/controllers/shot.ts b/server/controllers/shot.ts index ca519ad..fcf02f1 100644 --- a/server/controllers/shot.ts +++ b/server/controllers/shot.ts @@ -1,5 +1,4 @@ import { z } from 'zod' -import { format } from 'date-fns' const index = defineEventHandler(async (event) => { const { user } = await requireUserSession(event) diff --git a/test/auth.ts b/test/auth.ts index 1c2fda3..dc283a3 100644 --- a/test/auth.ts +++ b/test/auth.ts @@ -48,19 +48,22 @@ async function actingAs(email: string) { const post = (url: string, params: object) => $fetch>(url, { method: 'POST', body: params, headers: { cookie: user.cookie as string } }) const put = (url: string, params: object) => $fetch>(url, { method: 'PUT', body: params, headers: { cookie: user.cookie as string } }) const remove = (url: string, params?: object) => $fetch>(url, { method: 'DELETE', body: params, headers: { cookie: user.cookie as string } }) - const notFound = async (method: string, url: string, params?: object): Promise => { + const expectStatus = async (expectedStatus: number, method: string, url: string, params?: object): Promise => { try { await $fetch(url, { method, body: params, headers: { cookie: user.cookie as string } }) - throw new Error(`Expected 404 status for ${method}: ${url}, but request succeeded`) + throw new Error(`Expected ${expectedStatus} status for ${method}: ${url}, but request succeeded`) } catch (error) { - if (error.response && error.response.status === 404) - return 404 + if (error.response && error.response.status === expectedStatus) + return expectedStatus throw error } } - return { get, post, put, remove, notFound, user } + const notFound = (method: string, url: string, params?: object) => expectStatus(404, method, url, params) + const unAuth = (method: string, url: string, params?: object) => expectStatus(401, method, url, params) + + return { get, post, put, remove, notFound, unAuth, user } } export { diff --git a/test/cartridge.test.ts b/test/cartridge.test.ts index 076759f..ddca333 100644 --- a/test/cartridge.test.ts +++ b/test/cartridge.test.ts @@ -37,9 +37,8 @@ describe('/api/cartridge', async () => { it('delete /api/cartridge/:id - delete a cartridge', async () => { if (!cartridges[0]) throw new Error('Cartridge not found') - const { remove, get } = await actingAs('test@test.com') + const { remove, notFound } = await actingAs('test@test.com') await remove(`/api/cartridge/${cartridges[0]?.id}`) - try { await get(`/api/cartridge/${cartridges[0]?.id}`) } - catch (error: any) { expect(error.response.status).toBe(404) } + expect(await notFound('GET', `/api/cartridge/${cartridges[0]?.id}`)).toBe(404) }) }) diff --git a/test/cartridges.test.ts b/test/cartridges.test.ts index df57833..353b5c9 100644 --- a/test/cartridges.test.ts +++ b/test/cartridges.test.ts @@ -43,10 +43,8 @@ describe ('/api/user/:user/cartridge admin-only apiResource', async () => { it ('delete /api/user/:user/cartridge/:id - delete a cartridge', async () => { if (!cartridges[0]) throw new Error('Cartridge not found') - const { remove, get } = await actingAs('admin@test.com') - + const { remove, notFound } = await actingAs('admin@test.com') await remove(`/api/user/1/cartridge/${cartridges[0]?.id}`) - try { await get(`/api/user/1/cartridge/${cartridges[0]?.id}`) } - catch (error: any) { expect(error.response.status).toBe(404) } + expect(await notFound('GET', `/api/user/1/cartridge/${cartridges[0]?.id}`)).toBe(404) }) }) diff --git a/test/pen.test.ts b/test/pen.test.ts index 73d651f..f6fde6f 100644 --- a/test/pen.test.ts +++ b/test/pen.test.ts @@ -37,9 +37,8 @@ describe('/api/pen', async () => { it ('delete /api/pen/:id - delete a pen', async () => { if (!pens[0]) throw new Error('Pen not found') - const { remove, get } = await actingAs('test@test.com') + const { remove, notFound } = await actingAs('test@test.com') await remove(`/api/pen/${pens[0]?.id}`) - try { await get(`/api/pen/${pens[0]?.id}`) } - catch (error: any) { expect(error.response.status).toBe(404) } + expect(await notFound('GET', `/api/pen/${pens[0]?.id}`)).toBe(404) }) }) diff --git a/test/pens.test.ts b/test/pens.test.ts index e3b1ac5..c37e411 100644 --- a/test/pens.test.ts +++ b/test/pens.test.ts @@ -38,10 +38,8 @@ describe ('/api/user/:user/pen admin-only apiResource', async () => { it ('remove /api/user/:user/pen/:id - delete a pen', async () => { if (!pens[0]) throw new Error('Pen not found') - const { remove, get } = await actingAs('admin@test.com') - + const { remove, notFound } = await actingAs('admin@test.com') await remove(`/api/user/1/pen/${pens[0]?.id}`) - try { await get(`/api/user/1/pen/${pens[0]?.id}`) } - catch (error: any) { expect(error.response.status).toBe(404) } + expect(await notFound('GET', `/api/user/1/pen/${pens[0]?.id}`)).toBe(404) }) }) diff --git a/test/shot.test.ts b/test/shot.test.ts index 48caba2..bfb565d 100644 --- a/test/shot.test.ts +++ b/test/shot.test.ts @@ -42,9 +42,8 @@ describe('/api/shot', async () => { it ('delete /api/shot/:id - delete a shot', async () => { if (!shots[0]) throw new Error('Shot not found') - const { remove, get } = await actingAs('test@test.com') + const { remove, notFound } = await actingAs('test@test.com') await remove(`/api/shot/${shots[0]?.id}`) - try { await get(`/api/shot/${shots[0]?.id}`) } - catch (error: any) { expect(error.response.status).toBe(404) } + expect(await notFound('GET', `/api/shot/${shots[0]?.id}`)).toBe(404) }) }) diff --git a/test/user.test.ts b/test/user.test.ts index 3bc0ce8..00d6fb4 100644 --- a/test/user.test.ts +++ b/test/user.test.ts @@ -7,7 +7,9 @@ import type { User } from '~/types/models' describe('/api/me and /api/user', async () => { await setup(setupConfig()) it('/api/me should 401', async () => { - try { await $fetch('/api/me') } + try { + await $fetch('/api/me') + } catch (error: any) { expect(error.response.status).toBe(401) } }) @@ -17,9 +19,9 @@ describe('/api/me and /api/user', async () => { expect(response.data.email).toEqual(user.session.email) }) - it ('get /api/user isAdmin: false - 404', async () => { - try { await (await actingAs('test@test.com')).get('/api/all/user') } - catch (error: any) { expect(error.response.status).toBe(404) } + it ('get /api/all/user isAdmin: false - 404', async () => { + const { notFound } = await actingAs('test@test.com') + expect(await notFound('GET', '/api/all/user')).toBe(404) }) it ('get /api/user GET', async () => {