Skip to content

Commit

Permalink
💚 successful tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Aug 19, 2024
1 parent a97a50f commit f156bae
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
5 changes: 3 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default defineNuxtConfig({
'@nuxt/content',
'@nuxt/image',
'nuxt-auth-utils',
"@nuxt/test-utils"
'@nuxt/test-utils',
],
eslint: {
config: {
Expand All @@ -25,6 +25,7 @@ export default defineNuxtConfig({
},
compatibilityDate: '2024-08-01',
runtimeConfig: {
appEnv: '',
session: {
maxAge: 60 * 60 * 24 * 365, // 1 year
name: 'fumebio-session',
Expand Down Expand Up @@ -63,4 +64,4 @@ export default defineNuxtConfig({
},
},
},
})
})
7 changes: 5 additions & 2 deletions server/api/[...slug].ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion server/controllers/me.ts

This file was deleted.

8 changes: 8 additions & 0 deletions server/controllers/test.ts
Original file line number Diff line number Diff line change
@@ -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,
}
30 changes: 20 additions & 10 deletions test/user.test.ts
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand All @@ -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('[email protected]')).get('/api/me') as MetapiResponse<User>
expect(JSON.stringify(response.data)).toEqual(JSON.stringify(users[0]?.session))
})
})

0 comments on commit f156bae

Please sign in to comment.