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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/* 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, 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;
});
};
const badRequestErr = {
status: 'error',
error: 'invalid_request',
error_description: 'Missing parameters'
};
const unsupportedGrantTypeErr = {
status: 'error',
error: 'unsupported_grant_type',
error_description: 'invalid grant type'
};
const authorisationErr = {
status: 'error',
error: 'not_authorised',
error_description: 'user does not have correct authorisation for task'
};
describe('authentication', () => {
before(initDatabase);
context('invalid password authentication', () => {
before(populateDatabase);
it('should reject login to invalid endpoints', () => {
return chai.request(app)
.post('/')
.send({
username: mockUser.username,
password: mockUser.password,
grant_type: 'password'
}).then((res) => {
// File-not-found is a valid response
expect(res).to.have.status(404);
}).catch((reason) => {
// Unauthorised is a valid response
expect(reason).to.have.status(401);
});
});
it('should reject invalid usernames', () => {
return chai.request(app)
.post('/cs3099group-be-4/oauth/token')
.send({
username: 'invalid',
password: mockUser.password,
grant_type: 'password'
}).catch((reason) => {
expect(reason).to.have.status(400);
});
});
it('should reject invalid passwords', () => {
return chai.request(app)
.post('/cs3099group-be-4/oauth/token')
.send({
username: mockUser.username,
password: 'asasasas',
grant_type: 'password'
}).catch((reason) => {
expect(reason).to.have.status(400);
});
});
it('should reject invalid grant_type', () => {
return chai.request(app)
.post('/cs3099group-be-4/oauth/token')
.send({
username: mockUser.username,
password: mockUser.password,
grant_type: 'invalid'
}).catch((reason) => {
expect(reason.response.body).to.eql(unsupportedGrantTypeErr);
});
});
it('should reject empty usernames', () => {
return chai.request(app)
.post('/cs3099group-be-4/oauth/token')
.send({
username: '',
password: mockUser.password,
grant_type: 'password'
}).catch((reason) => {
expect(reason.response.body).to.eql(badRequestErr);
});
});
it('should reject empty passwords', () => {
return chai.request(app)
.post('/cs3099group-be-4/oauth/token')
.send({
username: mockUser.username,
password: '',
grant_type: 'password'
}).catch((reason) => {
expect(reason.response.body).to.eql(badRequestErr);
});
});
it('should reject empty grant_type', () => {
return chai.request(app)
.post('/cs3099group-be-4/oauth/token')
.send({
username: mockUser.username,
password: mockUser.password,
grant_type: ''
}).catch((reason) => {
expect(reason.response.body).to.eql(badRequestErr);
});
});
});
context('valid password authentication', () => {
before(populateDatabase);
it('should accept correct credentials', () => {
return chai.request(app)
.post('/cs3099group-be-4/oauth/token')
.send({
username: mockUser.username,
password: mockUser.password,
grant_type: 'password'
}).then((res) => {
expect(res).to.have.status(200);
});
});
});
context('token authentication', () => {
beforeEach(populateDatabase);
beforeEach(getToken);
it('should reject invalid tokens', () => {
return chai.request(app).get('/')
.set('Authorization', 'Bearer ' + 'invalid')
.then((res) => {
expect(res).to.have.status(401);
}).catch((reason) => {
expect(reason.response.body).to.eql(authorisationErr);
});
});
it('should accept valid tokens', () => {
return chai.request(app).get('/')
.set('Authorization', `Bearer ${token}`)
.then((res) => {
expect(res).to.have.status(200);
});
});
});
});
|