Skip to content

Commit

Permalink
chore(api): Add jest-cucumber in order to support gherkin natural lan…
Browse files Browse the repository at this point in the history
…guage and migrate the current api e2e tests
  • Loading branch information
alepefe committed Sep 17, 2024
1 parent 4e8947a commit 17103aa
Show file tree
Hide file tree
Showing 12 changed files with 421 additions and 149 deletions.
3 changes: 2 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/api/src/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --config ./test/jest-config.json -i --detectOpenHandles"
"test": "jest --config ./test/jest-config.json -i"
},
"dependencies": {
"@aws-sdk/client-ses": "^3.651.1",
Expand Down Expand Up @@ -56,6 +56,7 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"jest-cucumber": "4.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^7.0.0",
Expand Down
88 changes: 0 additions & 88 deletions api/test/auth/authentication.spec.ts

This file was deleted.

42 changes: 0 additions & 42 deletions api/test/auth/password-recovery.spec.ts

This file was deleted.

12 changes: 12 additions & 0 deletions api/test/e2e/features/password-recovery.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Password Recovery

Scenario: An email should be sent if a user is found
Given a user exists with valid credentials
When the user requests password recovery
Then the user should receive a 201 status code
And an email should be sent

Scenario: No email should be sent if the user is not found
When the user requests password recovery with an invalid email
Then the user should receive a 201 status code
And no email should be sent
18 changes: 18 additions & 0 deletions api/test/e2e/features/sign-in.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Feature: Sign In

Scenario: A user tries to sign in with non-existing credentials
When a user attempts to sign in with non-existing credentials
Then the user should receive a 401 status code
And the response message should be "Invalid credentials"

Scenario: A user tries to sign in with an incorrect password
Given a user exists with valid credentials
When a user attempts to sign in with an incorrect password
Then the user should receive a 401 status code
And the response message should be "Invalid credentials"

Scenario: A user successfully signs in
Given a user exists with valid credentials
When a user attempts to sign in with valid credentials
Then the user should receive a 201 status code
And the access token should be defined
12 changes: 12 additions & 0 deletions api/test/e2e/features/sign-up.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: User Sign Up

Scenario: A user cannot sign up with an already registered email
Given a user exists with valid credentials
When the user attempts to sign up with the same email
Then the user should receive a 409 status code
And the response message should be "Email already exists"

Scenario: A user successfully signs up with a new email
When a user attempts to sign up with valid credentials
Then the user should be registered successfully
And the user should have a valid ID and email
94 changes: 94 additions & 0 deletions api/test/e2e/steps/password-recovery.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { defineFeature, loadFeature } from 'jest-cucumber';
import { Response } from 'supertest';
import { User } from '@shared/entities/users/user.entity';
import { IEmailServiceToken } from '@api/modules/notifications/email/email-service.interface';
import { MockEmailService } from 'api/test/utils/mocks/mock-email.service';
import { TestManager } from 'api/test/utils/test-manager';

const feature = loadFeature('./test/e2e/features/password-recovery.feature');

defineFeature(feature, (test) => {
let testManager: TestManager;
let testUser: User;
let mockEmailService: MockEmailService;

beforeAll(async () => {
testManager = await TestManager.createTestManager();
mockEmailService =
testManager.moduleFixture.get<MockEmailService>(IEmailServiceToken);
});

beforeEach(async () => {
await testManager.clearDatabase();
const { user } = await testManager.setUpTestUser();
testUser = user;
jest.clearAllMocks();
});

afterAll(async () => {
await testManager.close();
});

test('An email should be sent if a user is found', ({
given,
when,
then,
and,
}) => {
let response: Response;

given('a user exists with valid credentials', async () => {
testUser = await testManager.mocks().createUser({
email: '[email protected]',
password: 'password123',
});
});

when('the user requests password recovery', async () => {
response = await testManager
.request()
.post(`/authentication/recover-password`)
.send({ email: testUser.email });
});

then(
/the user should receive a (\d+) status code/,
async (statusCode: string) => {
expect(response.status).toBe(Number.parseInt(statusCode));
},
);

and('an email should be sent', async () => {
expect(mockEmailService.sendMail).toHaveBeenCalledTimes(1);
});
});

test('No email should be sent if the user is not found', ({
when,
then,
and,
}) => {
let response: Response;

when(
'the user requests password recovery with an invalid email',
async () => {
response = await testManager
.request()
.post(`/authentication/recover-password`)
.send({ email: '[email protected]' });
},
);

then(
/the user should receive a (\d+) status code/,
async (statusCode: string) => {
expect(response.status).toBe(Number.parseInt(statusCode));
},
);

and('no email should be sent', async () => {
expect(mockEmailService.sendMail).toHaveBeenCalledTimes(0);
});
});
});
Loading

0 comments on commit 17103aa

Please sign in to comment.