1
|
|
|
/** global: jest */ |
2
|
|
|
/* global jest describe test expect beforeAll afterAll jasmine */ |
3
|
|
|
|
4
|
|
|
// Load local environments |
5
|
|
|
require('dotenv').config(); |
|
|
|
|
6
|
|
|
process.env.PORT = 9000; |
7
|
|
|
|
8
|
|
|
const Server = require('../../src/server'); |
9
|
|
|
const User = require('../../src/repository/User'); |
10
|
|
|
const config = require('../../src/config/server.config.js'); |
11
|
|
|
|
12
|
|
|
jest.dontMock('console'); |
13
|
|
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; // 10s |
|
|
|
|
14
|
|
|
|
15
|
|
|
const serverPromise = Server.start(); |
16
|
|
|
|
17
|
|
|
const user1 = { |
18
|
|
|
username: 'testuser', |
19
|
|
|
password: 'testuser', |
20
|
|
|
isTemporary: false, |
21
|
|
|
}; |
22
|
|
|
|
23
|
|
|
const validPassword = 'valid-password'; |
24
|
|
|
const shortPassword = 'short'; |
25
|
|
|
|
26
|
|
|
describe('User', () => { |
27
|
|
|
let id; |
28
|
|
|
const loginUrl = `${config.url.apiPrefix}/login`; |
29
|
|
|
const changePasswordUrl = `${config.url.apiPrefix}/passwords`; |
30
|
|
|
|
31
|
|
|
beforeAll(() => User.add(user1).then((result) => { |
32
|
|
|
id = result.data[0]._id; |
33
|
|
|
})); |
34
|
|
|
|
35
|
|
|
test('login success', () => { |
|
|
|
|
36
|
|
|
return serverPromise.then((server) => { |
|
|
|
|
37
|
|
|
return server.inject({ url: loginUrl, method: 'POST', payload: user1 }).then((response) => { |
38
|
|
|
expect(response.statusCode).toBe(200); |
39
|
|
|
const payload = JSON.parse(response.payload); |
40
|
|
|
expect(payload).toBeDefined(); |
41
|
|
|
expect(payload.success).toBe(true); |
42
|
|
|
}); |
43
|
|
|
}); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
test('login fail', () => { |
|
|
|
|
47
|
|
|
return serverPromise.then((server) => { |
|
|
|
|
48
|
|
|
return server.inject({ |
49
|
|
|
url: loginUrl, |
50
|
|
|
method: 'POST', |
51
|
|
|
payload: { username: user1.username, password: `no-${user1.password}` } |
|
|
|
|
52
|
|
|
}).then((response) => { |
53
|
|
|
expect(response.statusCode).toBe(401); |
54
|
|
|
const payload = JSON.parse(response.payload); |
55
|
|
|
expect(payload).toBeDefined(); |
56
|
|
|
expect(payload.success).toBe(false); |
57
|
|
|
}); |
|
|
|
|
58
|
|
|
}); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
test('change password', () => { |
|
|
|
|
62
|
|
|
return serverPromise.then((server) => { |
|
|
|
|
63
|
|
|
return server.inject({ |
64
|
|
|
url: changePasswordUrl, |
65
|
|
|
method: 'PUT', |
66
|
|
|
payload: { password: validPassword }, |
67
|
|
|
credentials: { username: 'testuser' }, |
68
|
|
|
}).then((response) => { |
69
|
|
|
expect(response.statusCode).toBe(200); |
70
|
|
|
}); |
71
|
|
|
}); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
test('change password without authentication', () => { |
|
|
|
|
75
|
|
|
return serverPromise.then((server) => { |
|
|
|
|
76
|
|
|
return server.inject({ url: changePasswordUrl, method: 'PUT', payload: { password: shortPassword } }) |
77
|
|
|
.then((response) => { |
78
|
|
|
expect(response.statusCode).toBe(401); |
79
|
|
|
}); |
80
|
|
|
}); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
test('change password with invalid password', () => { |
|
|
|
|
84
|
|
|
return serverPromise.then((server) => { |
|
|
|
|
85
|
|
|
return server.inject({ |
86
|
|
|
url: changePasswordUrl, |
87
|
|
|
method: 'PUT', |
88
|
|
|
payload: { password: shortPassword }, |
89
|
|
|
credentials: { username: 'testuser' }, |
90
|
|
|
}).then((response) => { |
91
|
|
|
expect(response.statusCode).toBe(400); |
92
|
|
|
}); |
93
|
|
|
}); |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
afterAll(() => User.remove(id)); |
97
|
|
|
}); |
98
|
|
|
|