generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7d2c5f
commit 44a5448
Showing
2 changed files
with
54 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ const transporter = nodemailer.createTransport({ | |
service: 'Gmail', | ||
auth: { | ||
user: "[email protected]", | ||
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', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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:"[email protected]"}}) | ||
} else if(url.endsWith('/changePassword')){ | ||
return Promise.resolve({data : {token: 'mockedToken', username : 'testuser', email:"[email protected]"}}) | ||
} | ||
}); | ||
|
||
|
@@ -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: '[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){ | ||
|