-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
241 lines (227 loc) · 7.31 KB
/
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* Change our DB to 'test' for testing
* @type {String}
*/
process.env.DB_NAME = 'test'
var chai = require('chai');
var expect = chai.expect;
var request = require('supertest');
var JWT;
var tx;
describe('loading express', function() {
var server;
beforeEach(function() {
server = require('./index');
});
afterEach(function() {
server.close();
});
it('responds to / with a 404 error', function testSlash(done) {
request(server)
.get('/')
.expect(404, done);
});
it('attempts to signup a new user without params should fail with errors', function testSlash(done) {
request(server)
.post('/private/v1/auth/signup')
.end(function(err, res) {
expect(res.body.success).to.be.equal(false);
expect(res.body.errors).to.be.an('array').that.is.not.empty;
if (err) {
return done(err);
}
done();
});
});
it(`attempts to signup a new user with passwords that don't contain
at least 1 number, 1 lowercase char & 1 uppercase char should fail`, function testSlash(done) {
request(server)
.post('/private/v1/auth/signup')
.send({
email:'i@owale.co',
password:'simpl3',
confirmPassword:'simple3'
})
.end(function(err, res) {
expect(res.body.success).to.be.equal(false);
expect(res.body.errors).to.be.an('array').that.is.not.empty;
if (err) {
return done(err);
}
done();
});
});
it(`attempts to signup a new user with a valid email and Passwords
that don't match should be unsuccessful`, function testSlash(done) {
request(server)
.post('/private/v1/auth/signup')
.send({
email:'i@owale.co',
password:'SimplePass1',
confirmPassword:'SimplePass2'
})
.end(function(err, res) {
expect(res.body.success).to.be.equal(false);
if (err) {
return done(err);
}
done();
});
});
it(`attempts to signup a new user with a valid email and password
should be successful with a JWT token response`, function testSlash(done) {
request(server)
.post('/private/v1/auth/signup')
.send({
email:'i@owale.co',
password:'SimplePass1',
confirmPassword:'SimplePass1'
})
.end(function(err, res) {
expect(res.body).to.have.property('token');
expect(res.body.token).to.be.a('string');
JWT = res.body.token;
if (err) {
return done(err);
}
done();
});
});
it(`attempts to login a user with invalid credentials should
be unsuccessful and return an error response`, function testSlash(done) {
request(server)
.post('/private/v1/auth/login')
.send({
email:'i@owale.co',
password:'wrongpassword'
})
.end(function(err, res) {
expect(res.status).to.be.equal(401);
expect(res.body).to.have.property('case');
expect(res.body.case).to.be.equal('Invalid-Credentials');
expect(res.body).to.have.property('messages');
if (err) {
return done(err);
}
done();
});
});
it(`attempts to login a user with valid credentials should
be successful`, function testSlash(done) {
request(server)
.post('/private/v1/auth/login')
.send({
email:'i@owale.co',
password:'SimplePass1',
confirmPassword:'SimplePass1'
})
.end(function(err, res) {
JWT = res.body.token;
expect(res.status).to.be.equal(200);
expect(res.body).to.have.property('token');
expect(res.body.token).to.be.a('string');
if (err) {
return done(err);
}
done();
});
});
it(`the private/v1/auth/user endpoint should return the currently
authenticated user`, function testSlash(done) {
request(server)
.post('/private/v1/auth/user')
.set('Authorization', `Bearer ${JWT}`)
.end(function(err, res) {
expect(res.status).to.be.equal(200);
expect(res.body).to.have.property('_id');
expect(res.body).to.have.property('email');
expect(res.body.email).to.be.equal('i@owale.co');
if (err) {
return done(err);
}
done();
});
});
it(`POSTing the private/v1/auth/transactions endpoint should create a
new transaction successfully and return it as JSON`, function testSlash(done) {
request(server)
.post('/private/v1/transactions')
.set('Authorization', `Bearer ${JWT}`)
.send({
hash:'3913ca9e5k29ee39458ac800ed83b32ed275405cc8ad9c8977aa68f7ef67454a',
tags:'testTransaction',
})
.end(function(err, res) {
expect(res.status).to.be.equal(200);
expect(res.body).to.have.property('tags');
expect(res.body).to.have.property('hash');
expect(res.body).to.have.property('date');
expect(res.body).to.have.property('ownerId');
expect(res.body).to.have.property('_id');
expect(res.body.hash).to.be.equal('3913ca9e5k29ee39458ac800ed83b32ed275405cc8ad9c8977aa68f7ef67454a');
expect(res.body.tags).to.have.lengthOf(1);
if (err) {
return done(err);
}
done();
});
});
it(`POSTing the private/v1/auth/transactions endpoint with the same transaction hash
in an attempt to create dupes should return an error`, function testSlash(done) {
request(server)
.post('/private/v1/transactions')
.set('Authorization', `Bearer ${JWT}`)
.send({
hash:'3913ca9e5k29ee39458ac800ed83b32ed275405cc8ad9c8977aa68f7ef67454a',
tags:'testTransaction',
})
.end(function(err, res) {
expect(res.status).to.be.equal(400);
expect(res.body).to.have.property('success');
expect(res.body.success).to.be.equal(false);
expect(res.body).to.have.property('errors');
expect(res.body.errors).to.have.lengthOf(1);
if (err) {
return done(err);
}
done();
});
});
it(`GETing the private/v1/auth/transactions should return the user's transactions as JSON`, function testSlash(done) {
request(server)
.get('/private/v1/transactions')
.set('Authorization', `Bearer ${JWT}`)
.end(function(err, res) {
tx = res.body[0];
expect(res.status).to.be.equal(200);
expect(res.body).to.have.lengthOf(1);
expect(res.body[0]).to.have.property('date');
expect(res.body[0]).to.have.property('hash');
expect(res.body[0]).to.have.property('ownerId');
expect(res.body[0]).to.have.property('tags');
expect(res.body[0]).to.have.property('_id');
expect(res.body[0].hash).to.be.equal('3913ca9e5k29ee39458ac800ed83b32ed275405cc8ad9c8977aa68f7ef67454a');
if (err) {
return done(err);
}
done();
});
});
it(`DELETE-ing the private/v1/auth/transactions should return with status code 200 and an empty json object
to confirm deletion.`, function testSlash(done) {
request(server)
.delete('/private/v1/transactions')
.set('Authorization', `Bearer ${JWT}`)
.send({
id:tx._id
})
.end(function(err, res) {
expect(res.status).to.be.equal(200);
expect(res.body).to.be.empty;
if (err) {
return done(err);
}
done();
});
});
});