Added, improved and fixed tests

This commit is contained in:
Florian Sylvain
2023-02-01 09:08:52 +01:00
parent e7078a419d
commit 58ee9050c5
+100 -2
View File
@@ -1,6 +1,8 @@
import { initServer } from '../app.js' import { initServer } from '../app.js'
import request from 'supertest' import request from 'supertest'
import { PrismaClient } from '@prisma/client' import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcrypt'
import { equals } from '@jest/expect-utils'
const prisma = new PrismaClient() const prisma = new PrismaClient()
const app = initServer() const app = initServer()
@@ -11,9 +13,23 @@ const testUserCredentials = {
password: "aaaaaaaaaa" password: "aaaaaaaaaa"
} }
beforeAll(async () => {
await prisma.user.create({
data: {
email: testUserCredentials.email,
password: await bcrypt.hash(testUserCredentials.password, 10),
name: '',
picture_path: ''
}
})
})
afterAll(async () => { afterAll(async () => {
await prisma.user.delete({ await prisma.user.delete({
where: { email: "a@a.com" } where: { email: testUserCredentials.email }
})
await prisma.user.delete({
where: { email: 'b@b.com' }
}) })
}) })
@@ -32,11 +48,24 @@ describe('POST /v1/session/register', () => {
const res = await request(app) const res = await request(app)
.post('/v1/session/register') .post('/v1/session/register')
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.send(JSON.stringify(testUserCredentials)) .send(JSON.stringify({
email: 'b@b.com',
password: 'bbbbbbbbbbb'
}))
expect(res.statusCode).toEqual(200) expect(res.statusCode).toEqual(200)
expect(res.body).toHaveProperty('message') expect(res.body).toHaveProperty('message')
}) })
it('returns status code 400 and an error message', async () => {
const res = await request(app)
.post('/v1/session/register')
.set('Content-Type', 'application/json')
.send(JSON.stringify(testUserCredentials))
expect(res.statusCode).toEqual(400)
expect(res.body).toHaveProperty('message')
})
}) })
describe('POST /v1/session/login', () => { describe('POST /v1/session/login', () => {
@@ -53,6 +82,75 @@ describe('POST /v1/session/login', () => {
expect(res.statusCode).toEqual(200) expect(res.statusCode).toEqual(200)
expect(jwtCookie).not.toBe(undefined) expect(jwtCookie).not.toBe(undefined)
}) })
it('returns status code 400 and credentials error message', async () => {
const res = await request(app)
.post('/v1/session/login')
.set('Content-Type', 'application/json')
.send(JSON.stringify({
email: 'rip bozo',
password: 'rip bozo'
}))
expect(res.statusCode).toEqual(400)
expect(res.body).toHaveProperty('message')
})
})
describe('POST /v1/category', () => {
it('returns status code 200 and success message', async () => {
const res = await request(app)
.post('/v1/category')
.set('Content-Type', 'application/json')
.set('Cookie', jwtCookie as string)
.send(JSON.stringify({
name: 'VueJS Composition API'
}))
expect(res.status).toEqual(200)
expect(res.body).toHaveProperty('message')
})
it('returns status code 400 and error message', async () => {
const res = await request(app)
.post('/v1/category')
.set('Content-Type', 'application/json')
.set('Cookie', jwtCookie as string)
.send(JSON.stringify({
pouet: 'VueJS Composition API'
}))
expect(res.status).toEqual(400)
expect(res.body).toHaveProperty('message')
})
})
describe('GET /v1/category', () => {
it('returns status code 200 and the category created', async () => {
const res = await request(app)
.get('/v1/category')
.set('Content-Type', 'application/json')
.set('Cookie', jwtCookie as string)
.send(JSON.stringify({
name: 'VueJS Composition API'
}))
expect(res.status).toEqual(200)
expect(res.body).toHaveProperty('category')
})
it('returns status code 400 and error message', async () => {
const res = await request(app)
.post('/v1/category')
.set('Content-Type', 'application/json')
.set('Cookie', jwtCookie as string)
.send(JSON.stringify({
pouet: 'VueJS Composition API'
}))
expect(res.status).toEqual(400)
expect(res.body).toHaveProperty('message')
})
}) })
describe('PUT /v1/user', () => { describe('PUT /v1/user', () => {