-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-file.test.js
177 lines (151 loc) · 5.4 KB
/
3-file.test.js
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
import {
expect, use, should, request,
} from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import { ObjectId } from 'mongodb';
import app from '../server';
import dbClient from '../utils/db';
import redisClient from '../utils/redis';
use(chaiHttp);
should();
// User Endpoints ==============================================
describe('testing User Endpoints', () => {
const credentials = 'Basic Ym9iQGR5bGFuLmNvbTp0b3RvMTIzNCE=';
let token = '';
let userId = '';
const user = {
email: '[email protected]',
password: 'toto1234!',
};
before(async () => {
await redisClient.client.flushall('ASYNC');
await dbClient.usersCollection.deleteMany({});
await dbClient.filesCollection.deleteMany({});
});
after(async () => {
await redisClient.client.flushall('ASYNC');
await dbClient.usersCollection.deleteMany({});
await dbClient.filesCollection.deleteMany({});
});
// users
describe('pOST /users', () => {
it('returns the id and email of created user', async () => {
const response = await request(app).post('/users').send(user);
const body = JSON.parse(response.text);
expect(body.email).to.equal(user.email);
expect(body).to.have.property('id');
expect(response.statusCode).to.equal(201);
userId = body.id;
const userMongo = await dbClient.usersCollection.findOne({
_id: ObjectId(body.id),
});
expect(userMongo).to.exist;
});
it('fails to create user because password is missing', async () => {
const user = {
email: '[email protected]',
};
const response = await request(app).post('/users').send(user);
const body = JSON.parse(response.text);
expect(body).to.eql({ error: 'Missing password' });
expect(response.statusCode).to.equal(400);
});
it('fails to create user because email is missing', async () => {
const user = {
password: 'toto1234!',
};
const response = await request(app).post('/users').send(user);
const body = JSON.parse(response.text);
expect(body).to.eql({ error: 'Missing email' });
expect(response.statusCode).to.equal(400);
});
it('fails to create user because it already exists', async () => {
const user = {
email: '[email protected]',
password: 'toto1234!',
};
const response = await request(app).post('/users').send(user);
const body = JSON.parse(response.text);
expect(body).to.eql({ error: 'Already exist' });
expect(response.statusCode).to.equal(400);
});
});
// Connect
describe('gET /connect', () => {
it('fails if no user is found for credentials', async () => {
const response = await request(app).get('/connect').send();
const body = JSON.parse(response.text);
expect(body).to.eql({ error: 'Unauthorized' });
expect(response.statusCode).to.equal(401);
});
it('returns a token if user is for credentials', async () => {
const spyRedisSet = sinon.spy(redisClient, 'set');
const response = await request(app)
.get('/connect')
.set('Authorization', credentials)
.send();
const body = JSON.parse(response.text);
token = body.token;
expect(body).to.have.property('token');
expect(response.statusCode).to.equal(200);
expect(
spyRedisSet.calledOnceWithExactly(`auth_${token}`, userId, 24 * 3600),
).to.be.true;
spyRedisSet.restore();
});
it('token exists in redis', async () => {
const redisToken = await redisClient.get(`auth_${token}`);
expect(redisToken).to.exist;
});
});
// Disconnect
describe('gET /disconnect', () => {
after(async () => {
await redisClient.client.flushall('ASYNC');
});
it('should responde with unauthorized because there is no token for user', async () => {
const response = await request(app).get('/disconnect').send();
const body = JSON.parse(response.text);
expect(body).to.eql({ error: 'Unauthorized' });
expect(response.statusCode).to.equal(401);
});
it('should sign-out the user based on the token', async () => {
const response = await request(app)
.get('/disconnect')
.set('X-Token', token)
.send();
expect(response.text).to.be.equal('');
expect(response.statusCode).to.equal(204);
});
it('token no longer exists in redis', async () => {
const redisToken = await redisClient.get(`auth_${token}`);
expect(redisToken).to.not.exist;
});
});
describe('gET /users/me', () => {
before(async () => {
const response = await request(app)
.get('/connect')
.set('Authorization', credentials)
.send();
const body = JSON.parse(response.text);
token = body.token;
});
it('should return unauthorized because no token is passed', async () => {
const response = await request(app).get('/users/me').send();
const body = JSON.parse(response.text);
expect(body).to.be.eql({ error: 'Unauthorized' });
expect(response.statusCode).to.equal(401);
});
it('should retrieve the user base on the token used', async () => {
const response = await request(app)
.get('/users/me')
.set('X-Token', token)
.send();
const body = JSON.parse(response.text);
expect(body).to.be.eql({ id: userId, email: user.email });
expect(response.statusCode).to.equal(200);
});
});
});