Skip to content

Commit

Permalink
[GH-38] routes
Browse files Browse the repository at this point in the history
  • Loading branch information
pablojvritx committed Dec 14, 2023
1 parent 3a28efa commit 4bb8b93
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 21 deletions.
9 changes: 2 additions & 7 deletions src/apps/apiApp/controllers/Auth/RegisterController.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import httpStatus from 'http-status';
import { NextFunction, Request, Response } from 'express';
import { Controller } from '../../shared/interfaces/Controller';
import { InvalidArgumentError } from '../../../../Contexts/shared/domain/errors/InvalidArgumentError';
import { RegisterUser } from '../../../../Contexts/apiApp/Auth/application';

export class RegisterController implements Controller {
constructor(protected register: RegisterUser) {}

async run(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const { email, username, password, repeatPassword } = req.body;
if (password !== repeatPassword) {
throw new InvalidArgumentError('Passwords do not match');
}

await this.register.run({ email, password, username });
const { email, username, password } = req.body;
await this.register.run({ email, username, password });

res.status(this.status()).send();
} catch (error) {
Expand Down
11 changes: 6 additions & 5 deletions src/apps/apiApp/routes/Auth/auth.routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextFunction, Request, Response, Router } from 'express';
import { body, checkExact, param } from 'express-validator';
import { body, check, checkExact, param } from 'express-validator';

import container from '../../dependency-injection';

Expand All @@ -22,10 +22,11 @@ export const register = (router: Router) => {
const registerReqSchema = [
body('email').exists().isEmail(),
body('username').exists().isString(),
// body('password').exists().isStrongPassword(),
// body('repeatPassword').exists().isStrongPassword(),
body('password').exists().isString(),
body('repeatPassword').exists().isString(),
body('password').exists().isStrongPassword(),
body('repeatPassword').exists().isStrongPassword(),
check('repeatPassword', 'Passwords do not match').custom(
(value: string, { req }) => value === req.body.password
),
checkExact()
];

Expand Down
10 changes: 1 addition & 9 deletions tests/apps/apiApp/controllers/Auth/RegisterController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,7 @@ describe('RegisterController', () => {
expect(res.send).toHaveBeenCalledWith();
});

it("should fail if passwords don't match", async () => {
req = { body: { ...request, repeatPassword: 'differentPassword' } };

await controller.run(req as Request, res as Response, next);

expect(next).toHaveBeenCalledWith(expect.any(InvalidArgumentError));
});

it('should fail if user exists', async () => {
it('should fail if user email exists', async () => {
repository = new UserRepositoryMock({ exists: true });
service = new RegisterUser(repository, encrypter);
controller = new RegisterController(service);
Expand Down
46 changes: 46 additions & 0 deletions tests/apps/apiApp/features/Auth/login-user.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Feature: Login
In order to use the application
As a user
I want to be able to login

Background:
Scenario: Register with valid credentials
Given a POST request to "/api/v1/Auth/register" with body
"""
{
"username": "login1",
"email": "[email protected]",
"password": "#aD3fe2.0%",
"repeatPassword": "#aD3fe2.0%"
}
"""
Then the response status code should be 201
Then the response body should be empty


Scenario: Login with valid credentials
Given a POST request to "/api/v1/Auth/login" with body
"""
{
"email": "[email protected]",
"password": "#aD3fe2.0%"
}
"""
Then the response status code should be 200
Then the response body should include an auth token

Scenario: Fail with invalid credentials
Given a POST request to "/api/v1/Auth/login" with body
"""
{
"email": "[email protected]",
"password": "#aDXXXXXXX3fe2.0%"
}
"""
Then the response status code should be 401
Then the response body should be
"""
{
"message": "Invalid credentials"
}
"""
86 changes: 86 additions & 0 deletions tests/apps/apiApp/features/Auth/register-user.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Feature: Register a new user
In order to use the application
I want to register a new user

Scenario: Register a valid user
Given a POST request to "/api/v1/Auth/register" with body
"""
{
"username": "register1",
"email": "[email protected]",
"password": "#aD3fe2.0%",
"repeatPassword": "#aD3fe2.0%"
}
"""
Then the response status code should be 201
Then the response body should be empty

Scenario: Existing email
Given a POST request to "/api/v1/Auth/register" with body
"""
{
"username": "register",
"email": "[email protected]",
"password": "#aD3fe2.0%",
"repeatPassword": "#aD3fe2.0%"
}
"""
Then the response status code should be 400
Then the response body should be
"""
{
"message": "User <[email protected]> already exists"
}
"""

Scenario: Password and repeat password are different
Given a POST request to "/api/v1/Auth/register" with body
"""
{
"username": "register",
"email": "[email protected]",
"password": "#aD3fe2.0%",
"repeatPassword": "#aD3fe2.0%1"
}
"""
Then the response status code should be 400
Then the response body should be
"""
{
"errors": [
{
"repeatPassword": "Passwords do not match at body."
}
]
}
"""

Scenario: Invalid arguments
Given a POST request to "/api/v1/Auth/register" with body
"""
{
"email": "aaJaa",
"password": "1234",
"repeatPassword": "1234"
}
"""
Then the response status code should be 400
Then the response body should be
"""
{
"errors": [
{
"email": "Invalid value at body. Value: aaJaa"
},
{
"username": "Invalid value at body. Value: undefined"
},
{
"password": "Invalid value at body."
},
{
"repeatPassword": "Invalid value at body."
}
]
}
"""
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Then(
}
);

Then('the response body should include an auth token', async () => {
assert.isNotEmpty(_response.body.token);
});

Then('the response body should be empty', async () => {
assert.isEmpty(_response.body);
});

0 comments on commit 4bb8b93

Please sign in to comment.