/htsuGrupo4

Primary LanguageJavaScript

htsuGrupo4

Table of Contents

  1. Getting Started
  2. Usage
  3. Testing

Built With

Prerequisites

  • npm
  npm install npm@latest -g
  • a mongoDB instance running

Installation

  1. Clone the repo
   git clone https://github.com/robredomedina/htsuGrupo4
  1. Install NPM packages
    npm i
  1. Create a .env file outside the src folder and set up the following enviroment variables:
    NODE_ENV
    TOKEN_SECRET
    DB_CONN_STR
    GITHUB_CLIENT_ID
    GITHUB_CLIENT_SECRET
    BEARER_TOKEN

where you want NODE_ENV to be set to development, TOKEN_SECRET to be a sha to be used by jwt and DB_CONN_STR the connection string to your mongo database. The github variables are necessary for the github Oauth and BEARER_TOKEN is necessary to authorize jest tests.

  1. Start the server with NPM:
    npm start

Usage

Preview

The API is set to the port 5000. It can be changed in the config.js in the config folder. All the endpoints except those needed for authentification  are protected by custom middleware and will ask for an authentification token wich can be obtained in a successful login with github.<br/>

Client

The client model has the "Name", "Lastname" string attributes. It also has a "Latitude", "Longitude", "Hotspot_Asteroids", "Price" and "Age" as Number attributes. All of them are required. Hotspot_Asteroids and Price are derived from its other attributes.

   get('/api/clients/')

This returns a list of all clients.

    post('/api/clients/addList')

Introduces an array of clients in the database. It expects an object with the array nested inside a "newClients".

    get('/api/clients/:id')
    patch('/api/clients/:id')
    delete('api/clients/:id')

This three endpoints read, update and delete the client with the id passed in the parameter.

Oauth

  post('/api/oauth/github')

This is the github login endpoint. It will redirect to a github login page. On successful login it will redirect to a done message and return the auth token in a cookie.

PHA(Potentially harmful asteroid)

The asteroid model has the "a", "e", "i", "om", "w", "am", "Latitude", "Longitude", fields, all of them numbers, in addition to a "full_name" string. All of its fields are required. The Latitude and Longitude are derived from its other attributes. It has the following exposed endpoints:

  post('/api/pha/add')

This endpoint will add an asteroid to the database. It expects an object with all the asteroids fields except Latitude and Longitude.

  get('/api/pha/findAll')

This returns a list of all asteroids.

  post('/api/pha/addList')

This endpoint adds a list of asteroids to the database. It expects an array of asteroids.

  get('/api/pha/find/:PHAName')
This endpoint returns the asteroid with the name passed in the PHAName parameter.

``` sh
    patch('/api/pha/update/:PHAId')
    delete('/api/pha/remove/:PHAId')
```
These endpoints will update and delete the asteroid with the indicated id respectively.

Testing

Unitary testing is done with jest and supertest. A bearer token is necessary to bypass the route protections, as indicated in the previous section. It is done in a local instance of mongo, in the 27017 port.