diff --git a/src/__tests__/app.test.ts b/src/__tests__/app.test.ts index 5aa7fa5..15cecb9 100644 --- a/src/__tests__/app.test.ts +++ b/src/__tests__/app.test.ts @@ -1,6 +1,8 @@ import { initServer } from '../app.js' import request from 'supertest' import { PrismaClient } from '@prisma/client' +import bcrypt from 'bcrypt' +import { equals } from '@jest/expect-utils' const prisma = new PrismaClient() const app = initServer() @@ -11,9 +13,23 @@ const testUserCredentials = { password: "aaaaaaaaaa" } +beforeAll(async () => { + await prisma.user.create({ + data: { + email: testUserCredentials.email, + password: await bcrypt.hash(testUserCredentials.password, 10), + name: '', + picture_path: '' + } + }) +}) + afterAll(async () => { 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) .post('/v1/session/register') .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.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', () => { @@ -53,6 +82,75 @@ describe('POST /v1/session/login', () => { expect(res.statusCode).toEqual(200) 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', () => {