04-auth.test.js
6.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
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
242
243
244
245
246
247
248
249
250
// # tests - auth
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);
var agent = request.agent(app);
request = request(app);
// storage for context-specific variables throughout the tests
var context = {};
describe('auth', 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('GET /my-account — should redirect me to /login and show error without login', function(done) {
agent
.get('/my-account')
.accept('text/html')
.expect(302)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.headers.location).to.exist;
// Test the values make sense
res.headers.location.should.equal('/login');
done();
});
});
it('GET /signup — should show me email and password form fields', function(done) {
agent
.get('/signup')
.accept('text/html')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.text).to.exist;
var $ = cheerio.load(res.text);
var $container = $('.container form');
// Test the values make sense
$container.should.have.length.of(1);
$container.find('input[name="email"]').should.have.length.of.least(1);
$container.find('input[name="password"]').should.have.length.of.least(1);
$container.find('button[type="submit"]').should.have.length.of.least(1);
// Save for later use
context.csrf = $container.find('input[name="_csrf"]').val();
done();
});
});
it('POST /signup — should create an account and redirect me to /my-account', function(done) {
// This does take a couple of seconds on average
this.timeout(5000);
agent
.post('/signup')
.send({
_csrf: context.csrf,
email: 'test+something@example.com',
name: 'Test',
surname: 'Something',
password: '123a-c456'
})
.accept('text/html')
.expect(302)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.headers.location).to.exist;
// Test the values make sense
res.headers.location.should.equal('/my-account');
// Test we can fetch the user from the DB
User.findOne({
email: 'test+something@example.com'
}, function(err, user) {
if (err) return done(err);
expect(user).to.exist;
user.should.have.property('name');
user.name.should.equal('Test');
done();
});
});
});
it('GET /login — should redirect me to / if logged in', function(done) {
agent
.get('/login')
.accept('text/html')
.expect(302)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.headers.location).to.exist;
// Test the values make sense
res.headers.location.should.equal('/');
done();
});
});
it('GET /logout — should log me out and redirect me to /', function(done) {
agent
.get('/logout')
.accept('text/html')
.expect(302)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.headers.location).to.exist;
// Test the values make sense
res.headers.location.should.equal('/');
done();
});
});
it('GET /login — should show me email and password form fields', function(done) {
agent
.get('/login')
.accept('text/html')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.text).to.exist;
var $ = cheerio.load(res.text);
var $container = $('.container form');
// Test the values make sense
$container.should.have.length.of(1);
$container.find('input[name="email"]').should.have.length.of.least(1);
$container.find('input[name="password"]').should.have.length.of.least(1);
$container.find('button[type="submit"]').should.have.length.of.least(1);
// Save for later use
context.csrf = $container.find('input[name="_csrf"]').val();
done();
});
});
it('POST /login — should log me in and redirect me to /', function(done) {
agent
.post('/login')
.send({
_csrf: context.csrf,
email: 'test+something@example.com',
password: '123a-c456'
})
.accept('text/html')
.expect(302)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.headers.location).to.exist;
// Test the values make sense
res.headers.location.should.equal('/');
done();
});
});
it('GET /my-account — should show me my email', function(done) {
agent
.get('/my-account')
.accept('text/html')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
// Test the attributes exist
expect(res.text).to.exist;
var $ = cheerio.load(res.text);
var $container = $('.container');
// Test the values make sense
$container.should.have.length.of(1);
$container.find('h1').text().should.equal('My Account');
$container.find('h3').eq(0).text().should.equal('Email: test+something@example.com');
$container.find('h3').eq(1).text().should.equal('Name: Test Something');
done();
});
});
});