Skip to content

Commit 43b5c0d

Browse files
committed
tests: Added e2e testing for auth endpoints and fixed error in api-ci file
1 parent 4c0ca4c commit 43b5c0d

File tree

9 files changed

+520
-31
lines changed

9 files changed

+520
-31
lines changed

.github/workflows/api-ci.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,17 @@ jobs:
7373
postgres-e2e:
7474
image: postgres:13
7575
ports:
76-
- 5434:5432
76+
- 5432:5432
7777
env:
7878
POSTGRES_DB: db_e2e
7979
POSTGRES_USER: e2e
8080
POSTGRES_PASSWORD: e2e123
8181

82+
redis-e2e:
83+
image: redis:8
84+
ports:
85+
- 6380:6379
86+
8287
steps:
8388
- name: Checkout
8489
uses: actions/checkout@v2
@@ -88,7 +93,10 @@ jobs:
8893
run: npm run e2e:ci
8994
env:
9095
PORT: 3000
91-
DATABASE_URL: postgres://e2e:e2e123@localhost:5434/db_e2e
96+
DATABASE_URL: postgres://e2e:e2e123@postgres-e2e:5432/db_e2e
9297
JWT_SECRET_VERIFY_EMAIL: verify-email-test
9398
JWT_ACCESS_SECRET: access-secret-test
9499
JWT_REFRESH_SECRET: refresh-secret-test
100+
REDIS_HOST: redis-e2e
101+
REDIS_PORT: 6380
102+
REDIS_PASSWORD:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { config } = require('../../../../config/config');
2+
const { sendEmail } = require('../../adapters/email/nodemailerAdapter');
3+
const { sendVerificationEmailName } = require('../../../../utils/constants');
4+
5+
async function processEmailJob(job) {
6+
const { email, name, token } = job.data;
7+
8+
switch (job.name) {
9+
case sendVerificationEmailName:
10+
return sendEmail({
11+
from: config.smtpEmail,
12+
to: email,
13+
subject: 'Email Verification',
14+
html: `<p>Hello ${name},</p><p>Please verify your email by clicking the link below(This link doesn't work, it's just an example.):</p><a href="${config.frontUrl}/auth/verify-email/email-confirmed?token=${token}">Verify Email. Please, Please copy this token and paste it into the authorize section of the swagger documentation. Token: ${token}</a>`,
15+
});
16+
default:
17+
throw new Error(`Unknown job name: ${job.name}`);
18+
}
19+
}
20+
21+
module.exports = { processEmailJob };

api/src/infrastructure/queues/workers/email.worker.js

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,12 @@ const { Worker } = require('bullmq');
33
const { config } = require('../../../../config/config');
44
const logger = require('../../../../utils/logger/logger');
55
const redis = require('../../store/cache/index');
6-
const { sendEmail } = require('../../adapters/email/nodemailerAdapter');
7-
const {
8-
emailQueueName,
9-
sendVerificationEmailName,
10-
} = require('../../../../utils/constants');
6+
const { emailQueueName } = require('../../../../utils/constants');
7+
const { processEmailJob } = require('./email.process');
118

12-
const emailWorker = new Worker(
13-
emailQueueName,
14-
async (job) => {
15-
const { email, name, token } = job.data;
16-
17-
switch (job.name) {
18-
case sendVerificationEmailName:
19-
await sendEmail({
20-
from: config.smtpEmail,
21-
to: email,
22-
subject: 'Email Verification',
23-
html: `<p>Hello ${name},</p><p>Please verify your email by clicking the link below(This link doesn't work, it's just an example.):</p><a href="${config.frontUrl}/auth/verify-email/email-confirmed?token=${token}">Verify Email. Please, Please copy this token and paste it into the authorize section of the swagger documentation. Token: ${token}</a>`,
24-
});
25-
break;
26-
default:
27-
throw new Error(`Unknown job name: ${job.name}`);
28-
}
29-
},
30-
{ connection: redis },
31-
);
9+
const emailWorker = new Worker(emailQueueName, processEmailJob, {
10+
connection: redis,
11+
});
3212

3313
const { isProd } = config;
3414

@@ -49,3 +29,5 @@ emailWorker.on('failed', (job, err) => {
4929
console.error(message);
5030
}
5131
});
32+
33+
module.exports = { processEmailJob, emailWorker };

api/src/interfaces/controllers/auth.contoller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const verifyEmailToActivateAccount = async (req, res, next) => {
7070
const tokenInCookies = req.cookies.verifyEmail;
7171

7272
if (!token || !tokenInCookies)
73-
throw boom.unauthorized('Tokens not provided');
73+
throw boom.unauthorized('Tokens was not provided');
7474
if (tokenInCookies !== token)
7575
throw boom.unauthorized('Tokens do not match');
7676

@@ -109,7 +109,7 @@ const changePassword = async (req, res, next) => {
109109
try {
110110
const { newPassword } = req.body;
111111
const token = req.cookies.verifyEmail;
112-
if (!token) throw boom.unauthorized('Token not provided');
112+
if (!token) throw boom.unauthorized('Token was not provided');
113113

114114
const { message } = await authService.changePassword(token, newPassword);
115115
if (!message)

api/src/interfaces/middlewares/authentication.handler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ async function refreshTokens(req, res, next) {
4444

4545
try {
4646
const decodedAccessToken = authService.validateAccessToken(accessToken);
47+
4748
if (decodedAccessToken) {
4849
req.user = decodedAccessToken;
4950
req.tokens = { accessToken };

api/src/interfaces/routes/auth.router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const {
5858
* 429:
5959
* description: Too many login attempts, please try again later.
6060
*/
61-
router.post('/login', limiter(5, 15 * 60 * 100, LOGIN_LIMITER_MESSAGE), login);
61+
router.post('/login', limiter(6, 15 * 60 * 100, LOGIN_LIMITER_MESSAGE), login);
6262

6363
/**
6464
* @swagger
@@ -95,7 +95,7 @@ router.post('/login', limiter(5, 15 * 60 * 100, LOGIN_LIMITER_MESSAGE), login);
9595
*/
9696
router.post(
9797
'/send-verification-email',
98-
limiter(3, 15 * 60 * 100, EMAIL_LIMITER_MESSAGE),
98+
limiter(4, 15 * 60 * 100, EMAIL_LIMITER_MESSAGE),
9999
sendVerificationEmail,
100100
);
101101

0 commit comments

Comments
 (0)