-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathtestUserCreation.test.js
More file actions
89 lines (73 loc) · 2.4 KB
/
testUserCreation.test.js
File metadata and controls
89 lines (73 loc) · 2.4 KB
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
// Import the dependencies for testing
const chai = require('chai');
const bcrypt = require('bcryptjs');
const chaiHttp = require('chai-http');
const db = require('../src/db');
const service = require('../src/service');
chai.use(chaiHttp);
chai.should();
const expect = chai.expect;
const should = chai.should();
describe('user creation', async () => {
let app;
let cookie;
const setCookie = function (res) {
res.headers['set-cookie'].forEach((x) => {
if (x.startsWith('connect')) {
const value = x.split(';')[0];
cookie = value;
}
});
};
before(async function () {
app = await service.start();
await db.deleteUser('login-test-user');
});
it('should be able to login', async function () {
const res = await chai.request(app).post('/api/auth/login').send({
username: 'admin',
password: 'admin',
});
expect(res).to.have.cookie('connect.sid');
res.should.have.status(200);
setCookie(res);
});
it('should be able to create a new user', async function () {
const res = await chai.request(app).post('/api/auth/profile').set('Cookie', `${cookie}`).send({
username: 'login-test-user',
email: 'paul.timothy.groves@gmail.com',
gitAccount: 'test123',
admin: true,
});
res.should.have.status(200);
}).skip();
it('logout', async function () {
const res = await chai.request(app).post('/api/auth/logout').set('Cookie', `${cookie}`);
res.should.have.status(200);
});
it('login as new user', async function () {
// we don't know the users tempoary password - so force update a
// pasword
const user = await db.findUser('login-test-user');
await bcrypt.hash('test1234', 10, async function (err, hash) {
user.password = hash;
await db.updateUser(user);
const res = await chai.request(app).post('/api/auth/login').send({
username: 'login-test-user',
password: 'test1234',
});
expect(res).to.have.cookie('connect.sid');
res.should.have.status(200);
setCookie(res);
});
});
it('should access the profile', async function () {
const res = await chai.request(app).get('/api/auth/profile').set('Cookie', `${cookie}`);
res.should.have.status(200);
res.body.username.should.equal('login-test-user');
should.not.exist(res.body.password);
});
after(async function () {
await service.httpServer.close();
});
});