aboutsummaryrefslogtreecommitdiff
path: root/test/integration/index.routes.test.ts
blob: acd462a609554fc54420ff8337c8e076384dbccc (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* tslint:disable:newline-per-chained-call */
/* Chai assertions look silly when formatted multiline. */
/* tslint:disable:no-unused-expression */
/* Chai assertions do not require assignation. */
/* tslint:disable:typedef */
/* Mocha tests do not require strong typing. */

import * as chai from 'chai';
const expect: Chai.ExpectStatic = chai.expect;
import { Credentials, Database, addUser, initDB, stopDB, resetDB
} from '../test-db';
import { default as User } from '../../src/db/model/User';

// Chai-http must be imported this way:
// tslint:disable-next-line:no-var-requires no-require-imports
chai.use(require('chai-http'));

// Mocha must be imported as a side-effect library:
// tslint:disable-next-line:no-import-side-effect
import 'mocha';

import * as App from '../../src/app';
const app = App.TestApp();

let database: Database;
let mockUser: Credentials;
let token: string;

const initDatabase = async() => {
	database = initDB();
	await database.authenticate();
};

const populateDatabase = async() => {
	await resetDB(database);
	mockUser = await addUser(database);
};

const getToken = async() => {
	return chai.request(app)
	.post('/cs3099group-be-4/oauth/token')
	.send({
		username: mockUser.username,
		password: mockUser.password,
		grant_type: 'password'
	})
	.then((res) => {
		token = res.body.access_token;
	});
};

describe('routes : index', () => {
	before(initDatabase);
	after(stopDB);

	describe('GET /', () => {
		before(populateDatabase);
		it('should return 401 without authentication', () => {
			chai.request(app).get('/')
			.catch((err) => {
				return expect(err).to.have.status(401);
			});
		});

		context('with authentication', () => {
			beforeEach(getToken);
			it('should return 200', () => {
				return chai.request(app).get('/')
				.set('Authorization', `Bearer ${token}`)
				.then((res: ChaiHttp.Response) => {
					expect(res).to.have.status(200);
				});
			});
			it('should return json', () => {
				return chai.request(app).get('/')
				.set('Authorization', `Bearer ${token}`)
				.then((res: ChaiHttp.Response) => {
					expect(res.type).to.eql('application/json');
				});
			});

			it('should return a message prop', () => {
				return chai.request(app).get('/')
				.set('Authorization', `Bearer ${token}`)
				.then((res: ChaiHttp.Response) => {
					expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!');
				});
			});

			it('should return an important prop', () => {
				return chai.request(app).get('/')
				.set('Authorization', `Bearer ${token}`)
				.then((res: ChaiHttp.Response) => {
					expect(res.body.important)
						.to.eql('Endpoints start from /cs3099group-be-4/');
				});
			});
		});

	});
});