From f156baebc0e78e65b17a4a4eb7ef51cff0a7255f Mon Sep 17 00:00:00 2001 From: kevin olson Date: Mon, 19 Aug 2024 12:19:03 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20successful=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nuxt.config.ts | 5 +++-- server/api/[...slug].ts | 7 +++++-- server/controllers/me.ts | 1 - server/controllers/test.ts | 8 ++++++++ test/user.test.ts | 30 ++++++++++++++++++++---------- 5 files changed, 36 insertions(+), 15 deletions(-) delete mode 100644 server/controllers/me.ts create mode 100644 server/controllers/test.ts diff --git a/nuxt.config.ts b/nuxt.config.ts index 1e9cc02..8d20753 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -9,7 +9,7 @@ export default defineNuxtConfig({ '@nuxt/content', '@nuxt/image', 'nuxt-auth-utils', - "@nuxt/test-utils" + '@nuxt/test-utils', ], eslint: { config: { @@ -25,6 +25,7 @@ export default defineNuxtConfig({ }, compatibilityDate: '2024-08-01', runtimeConfig: { + appEnv: '', session: { maxAge: 60 * 60 * 24 * 365, // 1 year name: 'fumebio-session', @@ -63,4 +64,4 @@ export default defineNuxtConfig({ }, }, }, -}) \ No newline at end of file +}) diff --git a/server/api/[...slug].ts b/server/api/[...slug].ts index b6fc803..61e6950 100644 --- a/server/api/[...slug].ts +++ b/server/api/[...slug].ts @@ -1,7 +1,6 @@ import { createRouter, useBase } from 'h3' import { githubHandler, googleHandler } from '../utils/oauth' import logout from '../controllers/logout' -import me from '../controllers/me' import token from '../controllers/token' import pen from '../controllers/pen' import pens from '../controllers/pens' @@ -10,12 +9,16 @@ import cartridges from '../controllers/cartridges' import shots from '../controllers/shots' import shot from '../controllers/shot' import user from '../controllers/user' +import test from '../controllers/test' const router = createRouter() -router.get('/me', me) +router.get('/me', authedHandler(async ({ user }) => metapi().render(user))) router.get('/**', defineEventHandler(event => metapi().notFound(event))) +if (useRuntimeConfig().appEnv === 'test') + router.post('/test/session', test.create) + router.get('/oauth/google', googleHandler) router.get('/oauth/github', githubHandler) router.get('/oauth/microsoft', microsoftHandler) diff --git a/server/controllers/me.ts b/server/controllers/me.ts deleted file mode 100644 index b9c0ac1..0000000 --- a/server/controllers/me.ts +++ /dev/null @@ -1 +0,0 @@ -export default authedHandler(async ({ user }) => metapi().render(user)) diff --git a/server/controllers/test.ts b/server/controllers/test.ts new file mode 100644 index 0000000..225741c --- /dev/null +++ b/server/controllers/test.ts @@ -0,0 +1,8 @@ +const create = defineEventHandler(async (event) => { + await setUserSession(event, { user: await readBody(event) }) + return metapi().success('test session created', { headers: event.node.res.getHeader('set-cookie') }) +}) + +export default { + create, +} diff --git a/test/user.test.ts b/test/user.test.ts index cc31b9c..5f35c7c 100644 --- a/test/user.test.ts +++ b/test/user.test.ts @@ -1,7 +1,9 @@ // @vitest-environment nuxt -import { describe, expect, it } from 'vitest' +import { beforeAll, describe, expect, it } from 'vitest' import { $fetch, setup } from '@nuxt/test-utils/e2e' import { createUser } from '~~/server/utils/user' +import type { MetapiResponse } from '~/types/metapi' +import type { User } from '~/types/models' const users = [ { @@ -27,21 +29,29 @@ const users = [ }, ] -describe('my test', async () => { +beforeAll(async () => { await setup({ host: 'http://localhost:3000' }) - users.map(async (userData) => { userData.session = await createUser(userData, 'github', {}) }) +}) - it('/api/me with no session should 401', async () => { - try { - await $fetch('/api/me') - } +async function actingAs(email: string) { + const user = users.find(user => user.email === email) + const { data } = await $fetch('/api/test/session', { method: 'POST', body: user?.session }) + const cookie = data.headers[1].split(';')[0] + const get = (url: string) => $fetch(url, { headers: { cookie } }) + return { get } +} + +describe('/api/me', async () => { + it('no session should 401', async () => { + try { await $fetch('/api/me') } catch (error: any) { expect(error.response.status).toBe(401) } }) - it('/api/me returns the currently loggeed in user', async () => { - const { data: user } = await $fetch('/api/me') - console.log(user) + + it('returns the currently loggeed in user', async () => { + const response = await (await actingAs('test@test.com')).get('/api/me') as MetapiResponse + expect(JSON.stringify(response.data)).toEqual(JSON.stringify(users[0]?.session)) }) })