02-users.test.js
5.5 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
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
// # tests - users
var util = require('util');
var request = require('supertest');
var app = require('../app');
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var expect = chai.expect;
var utils = require('./utils');
var async = require('async');
var IoC = require('electrolyte');
var cheerio = require('cheerio');
chai.should();
chai.use(sinonChai);
request = request(app);
// storage for context-specific variables throughout the tests
var context = {};
describe('/users', function() {
var User = IoC.create('models/user');
// Clean DB and add 3 sample users before tests start
before(function(done) {
async.waterfall([
utils.cleanDatabase,
function createTestUsers(callback) {
// Create 3 test users
async.timesSeries(3, function(i, _callback) {
var user = new User({
email: 'email+' + i + '@example.com',
name: 'User #' + i,
surname: 'Last Name #' + i,
password: '1234' + i
});
user.save(_callback);
}, callback);
}
], done);
});
// Clean DB after all tests are done
after(function(done) {
utils.cleanDatabase(done);
});
it('POST /users - should return 200 if user was created', function(done) {
this.timeout(3000); // The first request sometimes takes longer to complete
request
.post('/users')
.set({
'X-Requested-With': 'XMLHttpRequest'// We need to set this so CSRF is ignored when enabled
})
.accept('application/json')
.send({
email: util.format('niftylettuce+%s@gmail.com', new Date().getTime()),
name: 'Nifty',
surname: 'Lettuce',
password: 'abc123'
})
.expect(200)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.body).to.exist;
res.body.should.have.property('id');
res.body.should.have.property('name');
res.body.should.have.property('surname');
res.body.should.not.have.property('password');
// Test the values make sense
res.body.name.should.equal('Nifty');
res.body.surname.should.equal('Lettuce');
// Store this id to use later
context.userIdCreatedWithRequest = res.body.id;
done();
});
});
it('GET /users/:id — should return 200 if user was retrieved', function(done) {
request
.get(util.format('/users/%s', context.userIdCreatedWithRequest))
.accept('application/json')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.body).to.exist;
res.body.should.have.property('id');
res.body.should.have.property('name');
res.body.should.have.property('surname');
res.body.should.not.have.property('password');
// Test the values make sense
res.body.name.should.equal('Nifty');
res.body.surname.should.equal('Lettuce');
done();
});
});
it('PUT /users/:id - should return 200 if user was updated', function(done) {
request
.put(util.format('/users/%s', context.userIdCreatedWithRequest))
.set({
'X-Requested-With': 'XMLHttpRequest'// We need to set this so CSRF is ignored when enabled
})
.accept('application/json')
.send({
name: 'NiftyWhoa',
email: 'niftywhoa@gmail.com',
surname: 'LettuceWhoa'
})
.expect(200)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.body).to.exist;
res.body.should.have.property('id');
res.body.should.have.property('email');
res.body.should.have.property('name');
res.body.should.have.property('surname');
// Test the values make sense
res.body.email.should.equal('niftywhoa@gmail.com');
res.body.name.should.equal('NiftyWhoa');
res.body.surname.should.equal('LettuceWhoa');
done();
});
});
it('DELETE /users/:id - should return 200 if user was deleted', function(done) {
request
.del(util.format('/users/%s', context.userIdCreatedWithRequest))
.set({
'X-Requested-With': 'XMLHttpRequest'// We need to set this so CSRF is ignored when enabled
})
.accept('application/json')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.body).to.exist;
res.body.should.have.property('id');
res.body.should.have.property('deleted');
// Test the values make sense
res.body.id.should.equal(context.userIdCreatedWithRequest);
res.body.deleted.should.equal(true);
done();
});
});
it('GET /users - should return 200 if user index loads (JSON)', function(done) {
request
.get('/users')
.accept('application/json')
.expect(200, done);
});
it('GET /users - should return 200 if user index loads and shows 3 rows (HTML)', function(done) {
request
.get('/users')
.accept('text/html')
.expect(200)
.end(function(err, res) {
// Test the attributes exist
expect(res.text).to.exist;
var $ = cheerio.load(res.text);
var $userList = $('table');
var $userRows = $userList.find('tr');
// Test the values make sense
$userList.should.have.length.of(1);
$userRows.should.have.length.of.at.least(3);
done();
});
});
});