From d0bcb85368925867c4f0d83d9d67d098ec00bfe9 Mon Sep 17 00:00:00 2001 From: pedrodecf Date: Tue, 30 Jan 2024 12:25:57 -0300 Subject: [PATCH] E2E tests for authentication controller --- src/controller/session/authUser.spec.ts | 81 +++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/controller/session/authUser.spec.ts diff --git a/src/controller/session/authUser.spec.ts b/src/controller/session/authUser.spec.ts new file mode 100644 index 0000000..ea67497 --- /dev/null +++ b/src/controller/session/authUser.spec.ts @@ -0,0 +1,81 @@ +import { afterAll, beforeAll, describe, expect, test } from 'vitest' +import { app } from '../../app' +import request from 'supertest' + +describe('User Login E2E', () => { + beforeAll(async () => { + await app.ready() + }) + + afterAll(async () => { + await app.close() + }) + + test('should be able to login', async () => { + const email = 'john_doe@email.com.br' + const name = 'John' + const surname = 'Doe' + const password = 'password' + + await request(app.server).post('/user').send({ + email, + name, + surname, + password, + }) + + const userData = await request(app.server) + .post('/login') + .send({ email, password }) + + expect(userData.statusCode).toEqual(200) + expect(userData.body).toEqual({ + user: expect.any(Object), + token: expect.any(String), + }) + }) + + test('should not be able to login because the password is incorrect', async () => { + const email = 'john_doe@email.com.br' + const name = 'John' + const surname = 'Doe' + const password = 'password' + const wrongPassword = 'wrongPassword' + + await request(app.server).post('/user').send({ + email, + name, + surname, + password, + }) + + const userData = await request(app.server) + .post('/login') + .send({ email, password: wrongPassword }) + + expect(userData.statusCode).toEqual(401) + expect(userData.body.user).toEqual(expect.objectContaining({})) + }) + + test('should not be able to login because the email is incorrect', async () => { + const email = 'john_doe@email.com.br' + const wrongEmail = 'wrong@email.com.br' + const name = 'John' + const surname = 'Doe' + const password = 'password' + + await request(app.server).post('/user').send({ + email, + name, + surname, + password, + }) + + const userData = await request(app.server) + .post('/login') + .send({ email: wrongEmail, password }) + + expect(userData.statusCode).toEqual(401) + expect(userData.body.user).toEqual(expect.objectContaining({})) + }) +})