Skip to content
This repository has been archived by the owner on Dec 28, 2018. It is now read-only.

Commit

Permalink
Merge pull request #26 from SkYNewZ/develop
Browse files Browse the repository at this point in the history
* Change docker image for fixing this issue sass/node-sass#2031
* `clinet.connect()` only once
* Use one variable `DATABSE_CONNECTION_STING` instead of 5 other variables
* Use 3 different account to prevent [this type](qlaffont/fortnite-api#42) of error...
  • Loading branch information
SkYNewZ committed Apr 3, 2018
2 parents db381ca + dbc8ff8 commit 027603b
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 149 deletions.
6 changes: 1 addition & 5 deletions .env.example
Expand Up @@ -2,8 +2,4 @@ LOGIN_EMAIL=
LOGIN_PASSWORD=
OAUTH_EPIC_LAUNCHER=
OAUTH_FORTNITE=
PGHOST=
PGPORT=
PGDATABASE=
PGUSER=
PGPASSWORD=
DATABSE_CONNECTION_STING=
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -12,7 +12,7 @@ COPY . .
RUN npm run build


FROM node:9-alpine
FROM node:9
ARG BUILD_DATE
ARG VCS_REF
ARG VERSION
Expand Down
6 changes: 1 addition & 5 deletions README.md
Expand Up @@ -50,11 +50,7 @@ LOGIN_EMAIL=Your epic games account
LOGIN_PASSWORD=Your epic games password
OAUTH_EPIC_LAUNCHER=See init part
OAUTH_FORTNITE=See init part
PGHOST=Postgres host
PGPORT=Postgres port
PGDATABASE=Postgres database
PGUSER=Postgres user
PGPASSWORD=Postgres password
DATABSE_CONNECTION_STING=postgres://user:password@host:port/database
```

### Start in developement mode
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
3.1.0
3.1.1
6 changes: 1 addition & 5 deletions docker-compose.yml
Expand Up @@ -15,11 +15,7 @@ services:
- LOGIN_PASSWORD=XXXXX
- OAUTH_EPIC_LAUNCHER=XXXXX
- OAUTH_FORTNITE=XXXXX
- PGHOST=database
- PGPORT=5432
- PGDATABASE=fortnite-api
- PGUSER=postgres
- PGPASSWORD=secretpassword
- DATABSE_CONNECTION_STING=postgres://postgres:secretpassword@database:5432/fortnite-api

database:
image: postgres
Expand Down
6 changes: 1 addition & 5 deletions docs/README.md
Expand Up @@ -46,11 +46,7 @@ LOGIN_EMAIL=Your epic games account
LOGIN_PASSWORD=Your epic games password
OAUTH_EPIC_LAUNCHER=See init part
OAUTH_FORTNITE=See init part
PGHOST=Postgres host
PGPORT=Postgres port
PGDATABASE=Postgres database
PGUSER=Postgres user
PGPASSWORD=Postgres password
DATABSE_CONNECTION_STING=postgres://user:password@host:port/database
```

### Start in developement mode
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "rest-fortnite-api",
"version": "3.1.0",
"version": "3.1.1",
"description": "REST Fortnite API. Get stats, score, kills...",
"main": "index.js",
"scripts": {
Expand Down
14 changes: 14 additions & 0 deletions src/config/config.ts
@@ -1,3 +1,17 @@
import { Client } from "pg";

const client = new Client({
connectionString: process.env.DATABSE_CONNECTION_STING,
});
client.connect((err) => {
/* istanbul ignore if */
if (err) {
throw err;
} else {
console.log("Database connected");
}
});

export const AppConfig = {
debug: process.env.DEBUG || false,
secret: process.env.SECRET || "g6ePcR'G36]l63n",
Expand Down
2 changes: 1 addition & 1 deletion src/public/swagger.json
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "FORTNITE REST API",
"description": "REST API about Fortnite game",
"version": "3.1.0"
"version": "3.1.1"
},
"host": "skynewz-api-fortnite.herokuapp.com",
"basePath": "/api",
Expand Down
2 changes: 1 addition & 1 deletion src/public/swagger.yaml
Expand Up @@ -2,7 +2,7 @@ swagger: '2.0'
info:
title: FORTNITE REST API
description: REST API about Fortnite game
version: 3.1.0
version: 3.1.1
host: skynewz-api-fortnite.herokuapp.com
basePath: /api
schemes:
Expand Down
13 changes: 10 additions & 3 deletions src/routes/security.ts
Expand Up @@ -2,14 +2,21 @@ import * as bcrypt from "bcrypt";
import { Request, Response } from "express";
import { NextFunction } from "express-serve-static-core";
import { JsonWebTokenError, NotBeforeError, sign, TokenExpiredError, verify } from "jsonwebtoken";
import { Client, Pool, QueryResult } from "pg";
import { Client, QueryResult } from "pg";
import { AppConfig } from "../config/config";

const pool = new Pool();
const client = new Client({
connectionString: process.env.DATABSE_CONNECTION_STING,
});
/* istanbul ignore next */
client.connect()
.catch((e) => {
throw e.message;
});

export function getToken(req: Request, res: Response) {

pool.query("SELECT * FROM users where email=$1::text", [req.body.email], (err: Error, result: QueryResult) => {
client.query("SELECT * FROM users where email=$1::text", [req.body.email], (err: Error, result: QueryResult) => {
/* istanbul ignore if */
if (err) { throw err; }

Expand Down
7 changes: 4 additions & 3 deletions test/check.ts
Expand Up @@ -13,13 +13,14 @@ chai.use(chaiHttp);
describe("Check", () => {
let token: string = "";

before((done) => {
chai.request(AppServer)
before(done => {
chai
.request(AppServer)
.post("/api/oauth/token")
.type("application/x-www-form-urlencoded")
.send({
email: process.env.OAUTH_TEST_MAIL,
password: process.env.OAUTH_TEST_PASSWORD,
password: process.env.OAUTH_TEST_PASSWORD
})
.end((err, res) => {
token = res.body.access_token;
Expand Down
7 changes: 4 additions & 3 deletions test/news.ts
Expand Up @@ -13,13 +13,14 @@ chai.use(chaiHttp);
describe("News", () => {
let token: string = "";

before((done) => {
chai.request(AppServer)
before(done => {
chai
.request(AppServer)
.post("/api/oauth/token")
.type("application/x-www-form-urlencoded")
.send({
email: process.env.OAUTH_TEST_MAIL,
password: process.env.OAUTH_TEST_PASSWORD,
password: process.env.OAUTH_TEST_PASSWORD
})
.end((err, res) => {
token = res.body.access_token;
Expand Down
192 changes: 95 additions & 97 deletions test/oauth.ts
Expand Up @@ -8,107 +8,105 @@ import { AppServer } from "../src/index";
chai.use(chaiHttp);

describe("Oauth", () => {
let token: string = "";
let token: string = "";

it("it should return 404 because user not found in database", (done: MochaDone) => {
chai.request(AppServer)
.post("/api/oauth/token")
.type("application/x-www-form-urlencoded")
.send({
email: "foo",
password: "bar",
})
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body).to.be.a("object");
expect(res.body)
.to.have.property("message")
.to.equal(
"Authentication failed. User not found"
);
done();
});
});
it("it should return 404 because user not found in database", (done: MochaDone) => {
chai
.request(AppServer)
.post("/api/oauth/token")
.type("application/x-www-form-urlencoded")
.send({
email: "foo",
password: "bar"
})
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body).to.be.a("object");
expect(res.body)
.to.have.property("message")
.to.equal("Authentication failed. User not found");
done();
});
});

it("it should return 404 because wrong password", (done: MochaDone) => {
chai.request(AppServer)
.post("/api/oauth/token")
.type("application/x-www-form-urlencoded")
.send({
email: process.env.OAUTH_TEST_MAIL,
password: process.env.OAUTH_TEST_PASSWORD + "bar",
})
.end((err, res) => {
expect(res).to.have.status(401);
expect(res.body).to.be.a("object");
expect(res.body)
.to.have.property("message")
.to.equal(
"Authentication failed."
);
done();
});
});
it("it should return 404 because wrong password", (done: MochaDone) => {
chai
.request(AppServer)
.post("/api/oauth/token")
.type("application/x-www-form-urlencoded")
.send({
email: process.env.OAUTH_TEST_MAIL,
password: process.env.OAUTH_TEST_PASSWORD + "bar"
})
.end((err, res) => {
expect(res).to.have.status(401);
expect(res.body).to.be.a("object");
expect(res.body)
.to.have.property("message")
.to.equal("Authentication failed.");
done();
});
});

it("it should return 200 and a wonderful token", (done: MochaDone) => {
chai.request(AppServer)
.post("/api/oauth/token")
.type("application/x-www-form-urlencoded")
.send({
email: process.env.OAUTH_TEST_MAIL,
password: process.env.OAUTH_TEST_PASSWORD,
})
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.a("object");
expect(res.body).to.have.property("access_token");
expect(res.body).to.have.property("expiresIn");
token = res.body.access_token;
done();
});
});
it("it should return 200 and a wonderful token", (done: MochaDone) => {
chai
.request(AppServer)
.post("/api/oauth/token")
.type("application/x-www-form-urlencoded")
.send({
email: process.env.OAUTH_TEST_MAIL,
password: process.env.OAUTH_TEST_PASSWORD
})
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.a("object");
expect(res.body).to.have.property("access_token");
expect(res.body).to.have.property("expiresIn");
token = res.body.access_token;
done();
});
});

it("it shoult return 403 because not token provided", (done: MochaDone) => {
chai
.request(AppServer)
.get("/api/check")
.end((err, res) => {
expect(res).to.have.status(403);
expect(res.body)
.to.have.property("message")
.to.equal(
"No Bearer token provided."
);
done();
});
});
it("it shoult return 403 because not token provided", (done: MochaDone) => {
chai
.request(AppServer)
.get("/api/check")
.end((err, res) => {
expect(res).to.have.status(403);
expect(res.body)
.to.have.property("message")
.to.equal("No Bearer token provided.");
done();
});
});

it("it shoult return 403 because token expired", (done: MochaDone) => {
chai
.request(AppServer)
.get("/api/check")
.set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InF1ZW50aW5AbGVtYWlyZXByby5mciIsImlhdCI6MTUyMjcwMTIzNiwiZXhwIjoxNTIyNzAxNTM2fQ.oi2-XvpCjOynTF7TSVxKA53bY5N_FNVMFyFiCBtDcgY")
.end((err, res) => {
expect(res).to.have.status(403);
expect(res.body)
.to.have.property("message")
.to.equal(
"Failed to authenticate token."
);
done();
});
});
it("it shoult return 403 because token expired", (done: MochaDone) => {
chai
.request(AppServer)
.get("/api/check")
.set(
"Authorization",
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InF1ZW50aW5AbGVtYWlyZXByby5mciIsImlhdCI6MTUyMjcwMTIzNiwiZXhwIjoxNTIyNzAxNTM2fQ.oi2-XvpCjOynTF7TSVxKA53bY5N_FNVMFyFiCBtDcgY"
)
.end((err, res) => {
expect(res).to.have.status(403);
expect(res.body)
.to.have.property("message")
.to.equal("Failed to authenticate token.");
done();
});
});

it("it shoult return 200 on /api/check with the previous token", (done: MochaDone) => {
chai
.request(AppServer)
.get("/api/check")
.set("Authorization", "Bearer " + token)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.a("object");
expect(res.body).to.have.property("status");
done();
});
});
it("it shoult return 200 on /api/check with the previous token", (done: MochaDone) => {
chai
.request(AppServer)
.get("/api/check")
.set("Authorization", "Bearer " + token)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.a("object");
expect(res.body).to.have.property("status");
done();
});
});
});
7 changes: 4 additions & 3 deletions test/pve.ts
Expand Up @@ -19,13 +19,14 @@ describe("PVE", () => {
this.skip();
});

before((done) => {
chai.request(AppServer)
before(done => {
chai
.request(AppServer)
.post("/api/oauth/token")
.type("application/x-www-form-urlencoded")
.send({
email: process.env.OAUTH_TEST_MAIL,
password: process.env.OAUTH_TEST_PASSWORD,
password: process.env.OAUTH_TEST_PASSWORD
})
.end((err, res) => {
token = res.body.access_token;
Expand Down

0 comments on commit 027603b

Please sign in to comment.