Skip to content

Commit 0558c35

Browse files
Merge pull request #3 from pieroguerrero/unit-testing
Unit Testing and API security best practices were added
2 parents 7cc4813 + 33785ed commit 0558c35

25 files changed

+8721
-1593
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"rules": {
1616
"no-console": "off",
1717
"prefer-template": "off",
18-
"quotes": ["error", "double"],
18+
"quotes": ["warn", "double"],
1919
"react/prop-types": "off"
2020
}
2121
}

README.md

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
# Auth Backend
2-
> Typescript RESTful backend application for User Authentication and Authorization.
2+
> Typescript RESTful backend application template for User Authentication and Authorization.
33
44
## Table of Contents
55
- Auth Backend
66
- [Table of Contents](#table-of-contents)
77
- [General Information](#general-information)
88
- [Features](#features)
9-
- [Technologies and Techniques used](#technologies-and-techniques-used)
9+
- [Technologies and Techniques](#technologies-and-techniques)
10+
- [Project configuration](#project-configuration)
11+
- [Main application](#main-application)
12+
- [Security](#security)
13+
- [Testing](#testing)
1014
- [Setup](#setup)
1115
- [Project Status](#project-status)
1216
- [Room for Improvement](#room-for-improvement)
1317

1418

1519
## General Information
1620
- This project was created to fullfil the need several Software Engineers have when trying to create a Fullstack Web project from scratch.
17-
- It provides the source code backend template to authenticate and authorize users by exposing RESTful APIs to do so.
21+
- It provides the source code backend template to authenticate and authorize users by exposing RESTful APIs. It also secures the APIs with techniques such as Limiting the number of calls pero IP and delaying the calls reponse.
1822

1923
## Features
2024
- Username and password signup ✔
@@ -31,19 +35,11 @@
3135
- Account verification via SMS 🔜
3236
- Authentication with Google 🔜
3337

34-
## Technologies and Techniques used
38+
## Technologies and Techniques
3539

36-
- This project was implenmented 100% with Typescript, Nodejs and Express.
37-
- Authentication is implemented with the [bcryptjs](https://github.com/kelektiv/node.bcrypt.js) library for password encryption and the Local stategy from the [Passport](https://www.passportjs.org/) library.
38-
- Authorization is done with JWT strategy from the Passport library.
39-
- Tokenization is done with [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken).
40-
- ```MongoDB``` and the ```mongoose``` ORM were used for the databse.
41-
- Email service is handled with [nodemailer](https://github.com/nodemailer/nodemailer/) and using gmail as Email server for testing purposes.
42-
- [Cors](https://github.com/expressjs/cors) and [Helmet](https://github.com/helmetjs/helmet) were used to avoid well-known web vulnerabilities.
43-
- [Compression](https://github.com/expressjs/compression) is being used for performance.
44-
- The ```.env``` file is handeled with [dotenv](https://github.com/motdotla/dotenv) and managed by a configurator module to facilitate its usage across the other application modules.
40+
### Project configuration
41+
- The configuration variables are stored in a ```.env``` file. This file is managed by a configurator module to facilitate its usage across the other application modules by using the [dotenv](https://github.com/motdotla/dotenv) library.
4542
- The project architecture was implemented by enhancing the concepts of [MVC](https://developer.mozilla.org/en-US/docs/Glossary/MVC) to get a more robust architecture with clear separation of concerns:
46-
4743
<div style="margin-left: 3rem;" >
4844

4945
```
@@ -52,16 +48,42 @@
5248
┃ ┣ 📂controllers => Orchestrators that use Services and Middlewares to provide a response.
5349
┃ ┣ 📂interfaces => Typescript Interface and Type definitions to be used in the project.
5450
┃ ┣ 📂middlewares => Functions to be executed before the Router's main controllers.
55-
┃ ┣ 📂models => Entity definitions that encapsulate Database and ORM services.
51+
┃ ┣ 📂models => Entity definitions that encapsulate Database and ORM apis.
5652
┃ ┣ 📂routers => Routers of the application.
53+
┃ ┃ ┗📂__tests__ => Jest test files.
5754
┃ ┣ 📂services => Functions containing the all the Business Logic of the application.
58-
┃ ┗ 📂util => Functions used in across the folders in multiple times.
55+
┃ ┗ 📂util => Functions used multiple times across the folders in the project.
5956
┣ 📂config => Configuration for the different components of the application.
57+
┃ ┗ 📂tests => Test configuration files.
6058
┣ 📂public => Publicly available resources.
6159
┗ 📜index.ts => Main file that starts the database and the main application.
6260
```
6361
</div>
6462

63+
### Main application
64+
- This project was implenmented 100% with Typescript, Nodejs and Express.
65+
- Authentication is implemented with the [bcryptjs](https://github.com/kelektiv/node.bcrypt.js) library for password encryption and the Local stategy from the [Passport](https://www.passportjs.org/) library.
66+
- Authorization is done with JWT strategy from the Passport library.
67+
- Tokenization is done with [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken).
68+
- ```MongoDB``` and the ```mongoose``` ORM were used for the databse.
69+
- Email service is handled with [nodemailer](https://github.com/nodemailer/nodemailer/) and using gmail as Email server for testing purposes.
70+
- [Compression](https://github.com/expressjs/compression) is being used for performance.
71+
72+
### Security
73+
- [Cors](https://github.com/expressjs/cors) and [Helmet](https://github.com/helmetjs/helmet) were used to avoid well-known web vulnerabilities.
74+
- The number of calls to the SignUp route is limited to avoid infinite calls that may block the internal resources, this was implemented with [express-rate-limit](https://github.com/express-rate-limit/express-rate-limit)
75+
- Every SignUp route call from the same IP will have a delayed response to avoid bruteforce attacks. This is implemented with [express-slow-down](https://github.com/express-rate-limit/express-slow-down)
76+
77+
78+
### Testing
79+
- Unite Testing code coverage is 100%
80+
- The unit testing is done with [Jest](https://github.com/facebook/jest).
81+
- [SuperTest](https://github.com/visionmedia/supertest) library is used to test the HTTP server.
82+
- A MongoDB in memory server was used to perform a complete test with database interaction, this was possible with the [mongodb-memory-server](https://github.com/nodkz/mongodb-memory-server) library.
83+
- [MailSlurp](https://github.com/mailslurp/mailslurp-client) with a free account configuration was used to perform the Account email verification:
84+
85+
![tests-results](https://user-images.githubusercontent.com/26049605/203177022-96eae3bd-beea-41b4-b1a2-feed75e6f33e.PNG)
86+
6587
## Setup
6688
1. Clone this project by doing:
6789
```
@@ -109,6 +131,20 @@ EMAIL_GMAIL_HOST="smtp.gmail.com"
109131
EMAIL_GMAIL_ADDRESS="<your-own-value-here>"
110132
#Gmail email sender password:
111133
EMAIL_GMAIL_PASS="<your-own-value-here>"
134+
#Secret key to perform Email Verification testing with Jest and Supertest. You can get one free at: https://mailslurp.com/
135+
EMAIL_MAILSLURP_KEY="YOUR KEY HERE"
136+
137+
#API SECURE SETTINGS
138+
#Establishes the time in milliseconds in wich an IP can make a certain number of calls
139+
RATE_LIMIT_TIME_IN_MS="60000"
140+
#Establishes the number of calls that can be made in the time set by RATE_LIMIT_TIME_IN_MS
141+
RATE_LIMIT_MAX_CALLS="2"
142+
#How long to keep records of requests in memory.
143+
SPEED_LIMIT_TIME_WINDOW_IN_MS="30000"
144+
#Max number of connections during SPEED_LIMIT_TIME_WINDOW_IN_MS before starting to delay responses. Defaults to 1. Set to 0 to disable delaying.
145+
SPEED_LIMIT_DELAY_AFTER="1"
146+
#How long to delay the response, multiplied by (number recent hits - SPEED_LIMIT_DELAY_AFTER). Defaults to 1000 (1 second). Set to 0 to disable delaying.
147+
SPEE_LIMIT_DELAYING_TIME_IN_MS="500"
112148
113149
```
114150

@@ -117,5 +153,4 @@ Project is: _in progress_
117153

118154
## Room for Improvement
119155
There are always room for improvement, in this project so far the thinkgs that can be improved are:
120-
- Unit testing coverage.
121156
- A separate web page for the Docs containing the API catalogs.

jest.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Config } from "@jest/types";
2+
// Sync object
3+
const config: Config.InitialOptions = {
4+
verbose: true,
5+
transform: {
6+
"^.+\\.tsx?$": "ts-jest",
7+
},
8+
};
9+
export default config;

0 commit comments

Comments
 (0)