From 44a5448f7787a5e915eb62fb25d7e93f15c169b6 Mon Sep 17 00:00:00 2001 From: Mister-Mario Date: Sat, 27 Apr 2024 11:42:05 +0200 Subject: [PATCH] Added tests to the gateway service --- gatewayservice/gateway-service.js | 9 +++-- gatewayservice/gateway-service.test.js | 51 +++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index cf72ba0..232c866 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -16,7 +16,7 @@ const transporter = nodemailer.createTransport({ service: 'Gmail', auth: { user: "wiqen1b@gmail.com", - pass: "akskfqgakjvcswyg ", + pass: "akskfqgakjvcswyg", }, }); @@ -80,6 +80,7 @@ app.post('/forgetPassword', async (req, res) => { app.get('/tokenFromCode/:code', async (req, res) => { try { + console.log(forgetPasswords) var code = parseInt(req.params.code); if(forgetPasswords.has(code)){ var token = forgetPasswords.get(code) @@ -301,9 +302,9 @@ function getRandomSixDigitNumber() { async function sendEmail(res, email, username, numbers) { // ConfiguraciĆ³n del correo const mailOptions = { - from: process.env.EMAIL_USER, // Remitente - to: email, // Destinatario - subject: 'Hello ' + username + ' this is the wiqen1b team', // Asunto + from: process.env.EMAIL_USER, + to: email, + subject: 'Hello ' + username + ' this is the wiqen1b team', text: 'We see that you have requested a password change.\n' + 'Please introduce the code: ' + numbers + '. You have around 10 minutes to change your password \n' + 'In case you have not requested a password change forget this email existance', diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index a51f5f1..0517d12 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -2,6 +2,7 @@ const request = require('supertest'); const axios = require('axios'); const jwt = require('jsonwebtoken'); const app = require('./gateway-service'); +const nodemailer = require('nodemailer'); afterAll(async () => { app.close(); @@ -12,8 +13,6 @@ jest.mock('jsonwebtoken'); jest.mock('axios'); - - describe('Gateway Service with mocked micro services', () => { // Mock responses from external services @@ -24,6 +23,10 @@ describe('Gateway Service with mocked micro services', () => { return Promise.resolve({ data: { username: 'newuser' } }); } else if(url.endsWith('/record')){ return Promise.resolve({data : {user:'testuser'}}) + } else if(url.endsWith('/forgetPassword')){ + return Promise.resolve({data : { token: 'mockedToken', username : 'testuser', email:"example@example.com"}}) + } else if(url.endsWith('/changePassword')){ + return Promise.resolve({data : {token: 'mockedToken', username : 'testuser', email:"example@example.com"}}) } }); @@ -60,6 +63,14 @@ describe('Gateway Service with mocked micro services', () => { callback(null, "decoded"); }); + + //Mock nodemailer + jest.mock('nodemailer', () => ({ + createTransport: jest.fn().mockReturnValue({ + sendMail: jest.fn(), + }), + })); + // Test /login endpoint it('should forward login request to auth service', async () => { const response = await request(app) @@ -146,6 +157,42 @@ describe('Gateway Service with mocked micro services', () => { }); + //Test /forgetPassword + it('should forward the request and send an email', async () => { + const response = await request(app) + .post('/forgetPassword') + .send({ email: 'example@example.com', username: 'testuser'}); + expect(response.statusCode).toBe(200); + expect(response.text).toBe('Email sent successfully'); + }) + + //Test tokenFromCode/:code + it('should find a token', async () => { + //First generate the code:token + + const fixedTimestamp = 1683078000000; + jest.spyOn(Date, 'now').mockReturnValue(fixedTimestamp); + + await request(app) + .post('/forgetPassword') + .send({ email: 'example@example.com', username: 'testuser'}); + const response = await request(app).get('/tokenFromCode/000000'); + + expect(response.statusCode).toBe(200); + expect(response.body).toHaveProperty('token', "mockedToken"); + }) + + //Test /changePassword + it('should forward the request', async () => { + const response = await request(app) + .post('/changePassword') + .send({ username: 'testuser', password: 'newpassword' }) + .set('token', 'valorDelToken'); + + expect(response.statusCode).toBe(200); + expect(response.body.username).toBe('testuser'); + }) + }); function checkRecord(response){