-
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.
Merge pull request #2 from fumeapp/pen-tests
💚 created a pen
- Loading branch information
Showing
10 changed files
with
112 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export interface UserInfo { | ||
email: string | ||
name: string | ||
avatar: string | ||
email: string | ||
payload?: UserPayload | ||
} | ||
|
||
|
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { $fetch } from '@nuxt/test-utils/e2e' | ||
import type { User } from '~/types/models' | ||
import type { User, UserSession } from '~/types/models' | ||
import { createUser } from '~~/server/utils/user' | ||
|
||
const users = [ | ||
{ | ||
session: {} as User, | ||
cookie: '', | ||
cookie: undefined, | ||
email: '[email protected]', | ||
name: 'Test User', | ||
avatar: 'https://avatars.githubusercontent.com/u/31337?v=4', | ||
|
@@ -27,25 +27,29 @@ const users = [ | |
}, | ||
}, | ||
}, | ||
] | ||
] as UserSession[] | ||
|
||
async function setupUsers() { | ||
await Promise.all(users.map(async (userData) => { | ||
userData.session = await createUser(userData, 'github', {}) | ||
})) | ||
async function userFromEmail(email: string): Promise<UserSession> { | ||
const index = users.findIndex(user => user.email === email) | ||
if (index === undefined) throw new Error(`User not found: ${email} - ${index}`) | ||
if (!users[index]) throw new Error('User not found') | ||
if (!users[index].session?.id) | ||
users[index].session = await createUser(users[index], 'github', {}) | ||
return users[index] | ||
} | ||
|
||
async function actingAs(email: string) { | ||
const user = users.find(user => user.email === email) | ||
if (!user) throw new Error('User not found') | ||
const user = await userFromEmail(email) | ||
const { data } = await $fetch('/api/test/session', { method: 'POST', body: { id: user?.session?.id.toString(), hash: user?.session?.hash } }) | ||
user.cookie = data.cookie[1].split(';')[0] as string | ||
const get = (url: string) => $fetch(url, { headers: { cookie: user.cookie as string } }) | ||
return { get } | ||
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 } }) | ||
return { get, post, put } | ||
} | ||
|
||
export { | ||
users, | ||
setupUsers, | ||
actingAs, | ||
userFromEmail, | ||
} |
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,10 @@ | ||
function setupConfig() { | ||
if (process.env.DEVRUN === 'true' && !process.env.CI) | ||
return { host: 'http://localhost:3000' } | ||
else | ||
return {} | ||
} | ||
|
||
export { | ||
setupConfig, | ||
} |
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,39 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { setup } from '@nuxt/test-utils' | ||
import { actingAs, userFromEmail } from './auth' | ||
import { setupConfig } from './config' | ||
import { penColors } from '~/utils/shared' | ||
import type { MetapiResponse } from '~/types/metapi' | ||
import type { Pen } from '~/types/models' | ||
|
||
describe('/api/pen', async () => { | ||
await setup(setupConfig()) | ||
|
||
const pens: Pen[] = [] | ||
|
||
it('post /api/pen - create a pen', async () => { | ||
const { post } = await actingAs('[email protected]') | ||
const { data: pen } = await post('/api/pen', { color: penColors[0] }) as MetapiResponse<Pen> | ||
expect(pen.color).toBe(penColors[0]) | ||
pens.push(pen) | ||
expect(pen.userId).toBe((await userFromEmail('[email protected]')).session.id.toString()) | ||
}) | ||
|
||
it(' get /api/pen - list all pens', async () => { | ||
const { get } = await actingAs('[email protected]') | ||
const response = await get('/api/pen') as MetapiResponse<Pen[]> | ||
expect(response.data[0]).toStrictEqual(pens[0]) | ||
}) | ||
|
||
it ('get /api/pen/:id - get a pen', async () => { | ||
const { get } = await actingAs('[email protected]') | ||
const response = await get(`/api/pen/${pens[0]?.id}`) as MetapiResponse<Pen> | ||
expect(response.data).toStrictEqual(pens[0]) | ||
}) | ||
|
||
it ('put /api/pen/:id - update a pen', async () => { | ||
const { put } = await actingAs('[email protected]') | ||
const { data: pen } = await put(`/api/pen/${pens[0]?.id}`, { color: penColors[1] }) as MetapiResponse<Pen> | ||
expect(pen.color).toBe(penColors[1]) | ||
}) | ||
}) |
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,19 +1,11 @@ | ||
import { beforeAll, describe, expect, it } from 'vitest' | ||
import { describe, expect, it } from 'vitest' | ||
import { $fetch, setup } from '@nuxt/test-utils/e2e' | ||
import { actingAs, setupUsers, users } from './auth' | ||
import { actingAs, users } from './auth' | ||
import { setupConfig } from './config' | ||
import type { MetapiResponse } from '~/types/metapi' | ||
import type { User } from '~/types/models' | ||
|
||
beforeAll(setupUsers) | ||
|
||
function setupConfig() { | ||
if (process.env.DEVRUN === 'true' && !process.env.CI) | ||
return { host: 'http://localhost:3000' } | ||
else | ||
return {} | ||
} | ||
|
||
describe('/api/me', async () => { | ||
describe('/api/me and /api/user', async () => { | ||
await setup(setupConfig()) | ||
it('/api/me should 401', async () => { | ||
try { await $fetch('/api/me') } | ||
|
@@ -22,13 +14,13 @@ describe('/api/me', async () => { | |
} | ||
}) | ||
|
||
it('/api/me returns the currently logged in user', async () => { | ||
it('get /api/me - current user session', async () => { | ||
const { get } = await actingAs('[email protected]') | ||
const response = await get('/api/me') as MetapiResponse<User> | ||
expect(response.data.email).toEqual(users[0]?.session.email) | ||
}) | ||
|
||
it ('/api/user should 404 if a non-admin accesses it', async () => { | ||
it ('get /api/user isAdmin: false - 404', async () => { | ||
try { | ||
await (await actingAs('[email protected]')).get('/api/user') | ||
} | ||
|
@@ -37,8 +29,13 @@ describe('/api/me', async () => { | |
} | ||
}) | ||
|
||
it ('/api/use should return a list of all users if an admin accesses it', async () => { | ||
it ('get /api/user GET', async () => { | ||
const response = await (await actingAs('[email protected]')).get('/api/user') as MetapiResponse<User[]> | ||
expect(response.data.length).toBe(2) | ||
}) | ||
|
||
it ('get /api/user/:id', async () => { | ||
const response = await (await actingAs('[email protected]')).get(`/api/user/${users[0]?.session.id}`) as MetapiResponse<User> | ||
expect(response.data.id).toBe(users[0]?.session.id.toString()) | ||
}) | ||
}) |
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