Skip to content

Commit

Permalink
Added tests to the gateway service
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Mario committed Apr 27, 2024
1 parent f7d2c5f commit 44a5448
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
9 changes: 5 additions & 4 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: "[email protected]",
pass: "akskfqgakjvcswyg ",
pass: "akskfqgakjvcswyg",
},
});

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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',
Expand Down
51 changes: 49 additions & 2 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -12,8 +13,6 @@ jest.mock('jsonwebtoken');

jest.mock('axios');



describe('Gateway Service with mocked micro services', () => {

// Mock responses from external services
Expand All @@ -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:"[email protected]"}})
} else if(url.endsWith('/changePassword')){
return Promise.resolve({data : {token: 'mockedToken', username : 'testuser', email:"[email protected]"}})
}
});

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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: '[email protected]', 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: '[email protected]', 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){
Expand Down

0 comments on commit 44a5448

Please sign in to comment.