From c2d5bd5b88e42a5c1f95657ab8e321d83ae0b656 Mon Sep 17 00:00:00 2001 From: Diego Cruz <18170307@itculiacan.edu.mx> Date: Tue, 21 May 2024 09:23:19 -0700 Subject: [PATCH] CI implementation --- .github/workflows/node.js.yml | 9 +++++---- dbscripts/mysql.sql | 4 ++-- src/middlewares/errorMiddleware.ts | 6 +++++- tests/unit/controllers/authController.spec.ts | 11 +++++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index ace22cc..ba3b883 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -36,7 +36,7 @@ jobs: image: mysql:8.0 env: MYSQL_ROOT_PASSWORD: rootpassword - MYSQL_DATABASE: testdb + MYSQL_DATABASE: test ports: - 3307:3306 options: >- @@ -63,14 +63,15 @@ jobs: - name: Initialize database run: | - docker exec -i $(docker ps -q --filter "ancestor=mysql:8.0") mysql -uroot -prootpassword testdb < ./dbscripts/mysql.sql + docker exec -i $(docker ps -q --filter "ancestor=mysql:8.0") mysql -uroot -prootpassword test < ./dbscripts/mysql.sql - name: Run tests env: - MYSQL_DATABASE: testdb + MYSQL_DATABASE: test MYSQL_USER: root MYSQL_HOST: 127.0.0.1 MYSQL_PASSWORD: rootpassword - MYSQL_URL: mysql://root:rootpassword@127.0.0.1:3307/testdb + MYSQL_URL: mysql://root:rootpassword@127.0.0.1:3307/test MYSQL_PORT: 3307 + NODE_ENV: test run: npm test diff --git a/dbscripts/mysql.sql b/dbscripts/mysql.sql index ca39e16..b77f4dc 100644 --- a/dbscripts/mysql.sql +++ b/dbscripts/mysql.sql @@ -1,5 +1,5 @@ -CREATE DATABASE `template` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */; +CREATE DATABASE IF NOT EXISTS `template` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */; USE template; @@ -16,7 +16,7 @@ CREATE TABLE `usuarios` ( -CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */; +CREATE DATABASE IF NOT EXISTS `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */; USE test; diff --git a/src/middlewares/errorMiddleware.ts b/src/middlewares/errorMiddleware.ts index 04a8122..d075e9d 100644 --- a/src/middlewares/errorMiddleware.ts +++ b/src/middlewares/errorMiddleware.ts @@ -22,7 +22,11 @@ export function errorMiddleware( break; default: - new InternalError("Error interno.").send(res); + if (process.env.NODE_ENV !== "production") { + new InternalError(error.message).send(res); + } else { + new InternalError("Error interno.").send(res); + } break; } diff --git a/tests/unit/controllers/authController.spec.ts b/tests/unit/controllers/authController.spec.ts index caf6ab0..1e225a8 100644 --- a/tests/unit/controllers/authController.spec.ts +++ b/tests/unit/controllers/authController.spec.ts @@ -17,6 +17,8 @@ describe('Auth Controller', () => { .post('/api/auth/login') .send({}); + console.log(response.body); + expect(response.status).toBe(400); expect(response.body).toHaveProperty('message', "email is required, contrasena is required"); @@ -26,6 +28,7 @@ describe('Auth Controller', () => { const email = faker.internet.email(); const contrasena = faker.internet.password(); + user = { nombre: 'testuser', email: email, @@ -36,6 +39,8 @@ describe('Auth Controller', () => { .post('/api/auth/register') .send(user); + console.log(response.body); + expect(response.status).toBe(200); expect(response.body).toHaveProperty('message', 'Registro exitoso'); }); @@ -46,6 +51,8 @@ describe('Auth Controller', () => { .post('/api/auth/login') .send({ email: user.email, contrasena: user.contrasena }); + console.log(response.body); + expect(response.status).toBe(200); expect(response.body).toHaveProperty('message', 'Inicio de sesion exitoso'); expect(response.body).toHaveProperty('data'); @@ -61,6 +68,8 @@ describe('Auth Controller', () => { contrasena: '22222222222275f6tuyibinj', }); + console.log(response.body); + expect(response.status).toBe(400); expect(response).toHaveProperty('error'); expect(response.body).toHaveProperty('message', 'Credenciales inválidas'); @@ -90,6 +99,8 @@ describe('Auth Controller', () => { contrasena: 'testpassword', }); + console.log(response.body); + expect(response.status).toBe(400); expect(response).toHaveProperty('error'); expect(response.body).toHaveProperty('message', 'email must be a valid email');