Skip to content

Commit

Permalink
💚 create users when we need em - why complicate
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Aug 21, 2024
1 parent 1a0c33b commit a0700db
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 21 deletions.
3 changes: 2 additions & 1 deletion app/types/models.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Token as PrismaToken, User as PrismaUser } from '@prisma/client'
import type { Cartridge as PrismaCartridge, Pen as PrismaPen, Shot as PrismaShot } from '@prisma/client'
import type { UserInfo } from '~/types/oauth'

export interface Token extends PrismaToken {
client: import('ua-parser-js').IResult
Expand All @@ -25,7 +26,7 @@ export interface User extends PrismaUser {
hash: string
}

export interface UserSession extends User {
export interface UserSession extends UserInfo {
session: User
cookie?: string
}
Expand Down
2 changes: 1 addition & 1 deletion app/types/oauth.d.ts
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
}

Expand Down
22 changes: 8 additions & 14 deletions test/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,17 @@ const users = [
},
] as UserSession[]

async function setupUsers() {
await Promise.all(users.map(async (userData, index) => {
if (users[index].session?.id) return
users[index].session = await createUser(userData, 'github', {})
}))
}

function userFromEmail(email: string) {
const user = users.find(user => user.email === email)
if (!user) throw new Error('User not found')
return user
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 = userFromEmail(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 } })
Expand All @@ -55,7 +50,6 @@ async function actingAs(email: string) {

export {
users,
setupUsers,
actingAs,
userFromEmail,
}
5 changes: 2 additions & 3 deletions test/pen.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { describe, expect, it } from 'vitest'
import { setup } from '@nuxt/test-utils'
import { actingAs, setupUsers, userFromEmail } from './auth'
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 setupUsers()
await setup(setupConfig())

const pens: Pen[] = []
Expand All @@ -17,7 +16,7 @@ describe('/api/pen', async () => {
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(userFromEmail('[email protected]').session.id.toString())
expect(pen.userId).toBe((await userFromEmail('[email protected]')).session.id.toString())
})

it(' get /api/pen - list all pens', async () => {
Expand Down
3 changes: 1 addition & 2 deletions test/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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'

describe('/api/me and /api/user', async () => {
await setupUsers()
await setup(setupConfig())
it('/api/me should 401', async () => {
try { await $fetch('/api/me') }
Expand Down

0 comments on commit a0700db

Please sign in to comment.