Code Monkey home page Code Monkey logo

mdcf-typescript's Introduction

MDCF Typescript ๐Ÿ’ป

express typescript mongo Mongoose Badge Vitest Badge

Getting Started โ€ข API Endpoints โ€ข Unit and E2E Tests

This project is a simple preview of how to use local session cookies for user authentication, using as few external libraries and frameworks as possible.

๐Ÿช SessionToken Cookie rather than JWT (JSON Web Tokens)?

Session-based approaches are more appropriate for applications that prioritise server-side control, robust session management, and sensitive data protection.

๐Ÿš€ Getting Started

Prerequisites

Here is a list of all requisites to run this project.

Cloning

  1. Clone the repository: Clone the project repository to your local machine using the following command in your terminal emulator:
git clone https://github.com/viniciusr-cmd/mdcf-typescript.git
  1. Navigate to the project directory: Change your current directory to the project directory:
cd mdcf-typescript
  1. Install dependencies: Install the project dependencies.

NPM

npm install

YARN

yarn install
  1. Start the project: Finally start the project.

NPM

npm start

YARN

yarn run

๐Ÿ“ Running Unit Tests

This project uses Vitest for unit testing. You also don't need to run the server, vitest can access the express app. Follow these steps to run the tests:

  1. Navigate to the project directory: If you're not already there, change your current directory to the project directory:
cd mdcf-typescript
  1. Install dependencies: If you haven't already done so, install the project dependencies:

NPM

npm install

YARN

yarn install

3. ๐Ÿงช Run the tests: Use the following commands to run the unit tests:

3.1 Run E2E tests: Use the following command to run the E2E tests:

NPM

npm run e2e

YARN

yarn run e2e

3.2 Run User Register Tests: Use the following command to run the User Register tests:

NPM

npm run test-register  

YARN

yarn run test-register

3.3 Run User Login Tests: Use the following command to run the User Login tests:

NPM

npm run test-login  

YARN

yarn run test-login

4. โœ… Results

E2E

โœ“ src/test/user.e2e.spec.ts (8) 584ms
   โœ…  User Registration (2)
     โœ“ POST /auth/register - should create a new user and return 201 status
     โœ“ POST /auth/register - should return 400 status for missing parameters
   โœ…  User Login (3)
     โœ“ POST /auth/login - should login a user and return 200 status
     โœ“ POST /auth/login - should return 400 status for incorrect credentials
     โœ“ POST /auth/login - should return 403 for a distinct password in database that is invalid
   โœ…  User Update (2)
     โœ“ PATCH /users/:id - should update a user and return 200 status
     โœ“ PATCH /users/:id - should return 403 status for forbidden update
   โœ…  User Deletion (1)
     โœ“ DELETE /users/:id - should delete a user and return 204 status
     
     Test Files  1 passed (1)
        Tests  8 passed (8)

TEST-REGISTER

โœ“ src/test/user.register.spec.ts (2) 481ms
   โœ…  User Registration (2)
     โœ“ POST /auth/register - should create a new user and return 201 status
     โœ“ POST /auth/register - should return 400 status for missing parameters

 Test Files  1 passed (1)
      Tests  2 passed (2)

TEST-LOGIN

โœ“ src/test/user.login.spec.ts (3) 543ms
   โœ…  User Login (3)
     โœ“ POST /auth/login - should login a user and return 200 status
     โœ“ POST /auth/login - should return 400 status for incorrect credentials
     โœ“ POST /auth/login - should return 403 for a distinct password in database that is invalid

 Test Files  1 passed (1)
      Tests  3 passed (3)

๐Ÿ“ API Endpoints

Here is listed the main routes of API, and what are their expected request and response bodies. โ€‹

ROUTE DESCRIPTION
POST /auth/register registers user into mongodb database request details
POST /auth/login logs user if registered in database request details
PATCH /users/:id updates the specified user given their ID and if they are the owner request details
DELETE /users/:id deletes the specified user given their ID and if they are the owner response details
GET /users get all users info for debug response details

POST /auth/register

REQUEST

{
  "username": "testuser",
  "password": "testpassword"
}

RESPONSE

{
  "authentication": {
    "password": "e0dc347803777c3bf601ebbc2e1c1223c8b4ec1ff6567507823babb6b15bcaae",
    "salt": "trHLYZNZ8L5v6YQHPduclIeEjAX6XfCJDtTmGr7LtYTGdsDtVw5fLPk5NfebChAHy2vrfIUvzRre48PvTDl8LHZid0ahLacF5NQ5HswAHD8QYURMmXXTtP3JlMGbhe2eR3mw45RtdQQwR0hPntpIjSH3sE1cdf3IUvkhtk8As2I=",
    "sessionToken": "651b7dcd0fd8cb5ae3687e8518470aca12e4d0d989ad66de1207eaf473f973fe"
  },
  "_id": "65da248452b8fd995ae08b92",
  "username": "testuser",
  "__v": 0
}

POST /auth/login

REQUEST

{
  "username": "testuser",
  "password": "testpassword"
}

RESPONSE

{
  "authentication": {
    "password": "e0dc347803777c3bf601ebbc2e1c1223c8b4ec1ff6567507823babb6b15bcaae",
    "salt": "trHLYZNZ8L5v6YQHPduclIeEjAX6XfCJDtTmGr7LtYTGdsDtVw5fLPk5NfebChAHy2vrfIUvzRre48PvTDl8LHZid0ahLacF5NQ5HswAHD8QYURMmXXTtP3JlMGbhe2eR3mw45RtdQQwR0hPntpIjSH3sE1cdf3IUvkhtk8As2I=",
    "sessionToken": "651b7dcd0fd8cb5ae3687e8518470aca12e4d0d989ad66de1207eaf473f973fe"
  },
  "_id": "65da248452b8fd995ae08b92",
  "username": "testuser",
  "__v": 0
}

PATCH /users/:id

REQUEST

{
  "username": "TESTUSERUPDATED"
}

RESPONSE

{
	"authentication": {
		"password": "f3a8f30ee08e90b348a4a7b9a81008ab363176f8dab32b7c3bbc74c6f4f1acf7",
		"salt": "1prFs01jEV5TGz5z8DneFJRiAzYg/AbE+jG92W/NZjrXYBXA2cn8saOtWcFUePbVYJhSzG7fx7tBrS2xouayp4R+KRdpPoif+T2uRgrcNTdvVcq+KXxyyt1G+rpkDZWmZwWLdC3hB8xvwTTYYAonM15rSJszQM7/q3a3bXDX03U=",
		"sessionToken": "7dd4efbff4ca66e4fa380294a66787a6ef6fac79b08f9745069f37e00ad1a947"
	},
	"_id": "65da2a0e3aa903838235d7e4",
	"username": "TESTUSERUPDATED",
	"__v": 0
}

DELETE /users/:id

CURL REQUEST

curl --request DELETE \
  --url http://localhost:8080/users/65da2a0e3aa903838235d7e4

RESPONSE

204 No Content

GET /users

RESPONSE

{
	"authentication": {
		"password": "f3a8f30ee08e90b348a4a7b9a81008ab363176f8dab32b7c3bbc74c6f4f1acf7",
		"salt": "1prFs01jEV5TGz5z8DneFJRiAzYg/AbE+jG92W/NZjrXYBXA2cn8saOtWcFUePbVYJhSzG7fx7tBrS2xouayp4R+KRdpPoif+T2uRgrcNTdvVcq+KXxyyt1G+rpkDZWmZwWLdC3hB8xvwTTYYAonM15rSJszQM7/q3a3bXDX03U=",
		"sessionToken": "7dd4efbff4ca66e4fa380294a66787a6ef6fac79b08f9745069f37e00ad1a947"
	},
	"_id": "65da2a0e3aa903838235d7e4",
	"username": "TESTUSERUPDATED",
	"__v": 0
}

Documentations that might help

๐Ÿ“ How to create a Pull Request

๐Ÿ’พ Commit pattern

mdcf-typescript's People

Contributors

viniciusr-cmd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.