-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.spec.ts
65 lines (53 loc) · 1.79 KB
/
App.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import * as chai from "chai";
import chaiHttp = require("chai-http");
import App from "./App";
import { Globals } from "./common/Globals";
import JwtHelper from "./common/JwtHelper";
import SimpleLogger from "./loggers/SimpleLogger";
const createContainer = require("./ioc/createContainer");
const logger = new SimpleLogger();
const app = new App(createContainer(), logger).express;
chai.use(chaiHttp);
const expect = chai.expect;
let authToken = "";
let request = chai.request(app);
describe("App", () => {
before(() => {
return request
.post("/v1/auth/login")
.send({ email: "[email protected] ", password: "admin" })
.then(res => {
authToken = res.body.token;
});
});
beforeEach(function() {
request = chai.request(app);
return;
});
it("should fail (not authorized)", () => {
const planetID = 167546850;
return request.get(`/v1/buildings/${planetID}`).then(res => {
expect(res.status).to.equals(Globals.Statuscode.NOT_AUTHORIZED);
expect(res.body.error).to.be.equals("Authentication failed");
});
});
it("should fail (invalid userID)", () => {
const planetID = 167546850;
return request
.get(`/v1/buildings/${planetID}`)
.set("Authorization", JwtHelper.generateToken(parseInt("iAmNotAValidUserId", 10)))
.then(res => {
expect(res.status).to.equals(Globals.Statuscode.NOT_AUTHORIZED);
expect(res.body.error).to.be.equals("Authentication failed");
});
});
it("should fail (route does not exist)", () => {
return request
.get("/v1/idontexist")
.set("Authorization", authToken)
.then(res => {
expect(res.status).to.equals(Globals.Statuscode.NOT_FOUND);
expect(res.body.error).to.be.equals("The route does not exist");
});
});
});