-
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.
- Loading branch information
Showing
5 changed files
with
36 additions
and
15 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
This file was deleted.
Oops, something went wrong.
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,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, | ||
} |
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,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('[email protected]')).get('/api/me') as MetapiResponse<User> | ||
expect(JSON.stringify(response.data)).toEqual(JSON.stringify(users[0]?.session)) | ||
}) | ||
}) |