chore: initial commit

This commit is contained in:
Divlo
2021-10-24 04:06:16 +02:00
commit 714cc643ba
260 changed files with 40783 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import { formatErrors } from '../formatErrors'
test('__test__/utils/formatErrors', () => {
expect(formatErrors('randomSring')).toEqual([])
const errors = [
{ message: 'some error message' },
{ message: 'another error' }
]
expect(formatErrors(errors)).toEqual(['some error message', 'another error'])
})

View File

@@ -0,0 +1,57 @@
import request from 'supertest'
import application from '../../application'
import User from '../../models/User'
interface AuthenticateUserOptions {
name?: string
email?: string
password?: string
shouldBeConfirmed?: boolean
alreadySignedUp?: boolean
}
export async function authenticateUserTest (
options: AuthenticateUserOptions = {}
): Promise<{
accessToken: string
refreshToken: string
expiresIn: string
type: 'Bearer'
userId: number
}> {
const {
name = 'John',
email = 'contact@test.com',
shouldBeConfirmed = true,
password = 'test',
alreadySignedUp = false
} = options
if (!alreadySignedUp) {
const { body: signupBody } = await request(application)
.post('/users/signup')
.send({ name, email, password })
.expect(201)
let signinResponse: any = { body: {} }
if (shouldBeConfirmed) {
const user = await User.findOne({ where: { id: signupBody.user.id } })
await request(application)
.get(`/users/confirmEmail?tempToken=${user?.tempToken as string}`)
.send()
.expect(200)
signinResponse = await request(application)
.post('/users/signin')
.send({ email, password })
.expect(200)
}
return { ...signinResponse.body, userId: signupBody.user.id }
}
const signinResponse = await request(application)
.post('/users/signin')
.send({ email, password })
.expect(200)
const user = await User.findOne({ where: { email } })
return { ...signinResponse.body, userId: user?.id }
}

View File

@@ -0,0 +1,8 @@
/** formatErrors for testing purpose (no types safety) */
export const formatErrors = (errors: any): string[] => {
try {
return errors.map((e: any) => e.message)
} catch {
return []
}
}

View File

@@ -0,0 +1,5 @@
export const wait = async (ms: number): Promise<void> => {
return await new Promise((resolve) => {
setTimeout(resolve, ms)
})
}