This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
221 lines (183 loc) · 5.98 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
/*jshint node:true, mocha:true */
"use strict";
var assert = require('assert');
var crypto = require('./lib/crypto');
var auth = require('./index');
process.env.NTB_USER_AGENT = 'turbasen-auth/v' + require('./package.json').version;
describe('lib/crypto', function() {
var user = null;
beforeEach(function() {
user = {
_password: 'Pa$w0rd',
navn: 'Foo Bar',
epost: '[email protected]',
pbkdf2: {
prf: 'HMAC-SHA1',
itrs: 100,
salt: 'XO6rZj9WG1UsLEsAGQH16qgZpCM9D7VylFQzwpSmOEo=',
dkLen: 32,
hash: 'Ir/5WTFgyBJoI3pJ8SaH8qWxdgZ0my6qcOPVPHnYJQ4='
}
};
});
describe('pbkdf2()', function() {
it('returns pbkdf2 hash', function(done) {
var pwd = user._password;
var itrs = user.pbkdf2.itrs;
var dkLen = user.pbkdf2.dkLen;
var salt = user.pbkdf2.salt;
var hash = user.pbkdf2.hash;
crypto.pbkdf2(pwd, salt, itrs, dkLen, function(err, h) {
assert.ifError(err);
assert.equal(h, hash);
done();
});
});
});
describe('salt()', function() {
it('returns random salt', function() {
assert.equal(crypto.salt().length, 172);
assert.notEqual(crypto.salt(), crypto.salt());
});
});
describe('authenticate()', function() {
it('returns true for correct email and password', function(done) {
var email = user.epost;
var pass = user._password;
crypto.authenticate(email, pass, user, function(isAuth, code, msg) {
assert.equal(isAuth, true);
assert.equal(code, undefined);
assert.equal(msg, undefined);
done();
});
});
it('returns code AUTH001 when email does not match', function(done) {
var email = 'foo';
var pass = user._password;
crypto.authenticate(email, pass, user, function(isAuth, code, msg) {
assert.equal(isAuth, false);
assert.equal(code, 'AUTH001');
assert.equal(msg, 'User email did not match');
done();
});
});
it('returns code AUTH002 for invalid auth schema', function(done) {
var email = user.epost;
var pass = user._password;
user.pbkdf2 = undefined;
crypto.authenticate(email, pass, user, function(isAuth, code, msg) {
assert.equal(isAuth, false);
assert.equal(code, 'AUTH002');
assert.equal(msg, 'Unknown authentication schema');
done();
});
});
it('returns code AUTH003 when pbkdf2 fails', function(done) {
var email = user.epost;
var pass = user._password;
user.pbkdf2.dkLen = -1;
crypto.authenticate(email, pass, user, function(isAuth, code, msg) {
assert.equal(isAuth, false);
assert.equal(code, 'AUTH003');
assert(/TypeError: Bad key length/.test(msg));
done();
});
});
it('returns code AUTH004 when hash does not match', function(done) {
var email = user.epost;
var pass = 'foo';
crypto.authenticate(email, pass, user, function(isAuth, code, msg) {
assert.equal(isAuth, false);
assert.equal(code, 'AUTH004');
assert.equal(msg, 'User password hash did not match');
done();
});
});
});
});
describe('#authenticate()', function() {
var _user;
beforeEach(function() {
_user = {
_password: process.env.INTEGRATION_TEST_PASSW,
navn: process.env.INTEGRATION_TEST_NAME,
epost: process.env.INTEGRATION_TEST_EMAIL,
gruppe: {
_id: '52407f3c4ec4a138150001d7',
navn: 'Destinasjon Trysil'
}
};
});
it('returns false for invalid user email', function(done) {
this.timeout(10000);
auth.authenticate('[email protected]', _user._password, function(err, user) {
assert.ifError(err);
assert.equal(user, false);
done();
});
});
it('returns false for invalid user password', function(done) {
this.timeout(10000);
auth.authenticate(_user.epost, 'foobar', function(err, user) {
assert.ifError(err);
assert.equal(user, false);
done();
});
});
it('returns user data for valid user credentials', function(done) {
this.timeout(10000);
auth.authenticate(_user.epost, _user._password, function(err, user) {
assert.ifError(err);
assert.equal(user.navn, _user.navn);
assert.equal(user.epost, _user.epost);
assert.equal(user.gruppe._id, _user.gruppe._id);
assert.equal(user.gruppe.navn, _user.gruppe.navn);
done();
});
});
});
describe('#middleware()', function() {
this.timeout(10000);
var agent = require('supertest').agent(require('./examples/server'));
it('returns 401 for invalid user credentials', function(done) {
agent.post('/authenticate')
.send({email: 'foo', password: 'bar'})
.expect(401, done);
});
it('returns 200 for valid user credentials', function(done) {
var email = process.env.INTEGRATION_TEST_EMAIL;
var passw = process.env.INTEGRATION_TEST_PASSW;
agent.post('/authenticate')
.send({email: email, password: passw})
.expect(200, done);
});
});
describe('#createUserAuth()', function() {
var salt = crypto.salt;
before(function() {
crypto.salt = function() { return 'aaaaabbbbbcccc1111122223333'; };
});
after(function() {
crypto.salt = salt;
});
it('returns auth for new user', function(done) {
this.timeout(10000);
var name = 'Random User';
var email = '[email protected]';
var password = 'Pa$sw0rd';
auth.createUserAuth(name, email, password, function(err, user) {
assert.ifError(err);
assert.equal(user.navn, name);
assert.equal(user.epost, email);
assert.equal(user.pbkdf2.prf, 'HMAC-SHA1');
assert.equal(user.pbkdf2.itrs, 131072);
assert.equal(user.pbkdf2.salt, 'aaaaabbbbbcccc1111122223333');
assert.equal(user.pbkdf2.dkLen, 256);
assert.equal(crypto.md5(user.pbkdf2.hash), '15f45542331087a598e7222335b67083');
crypto.authenticate(email, password, user, function(auth) {
assert.equal(auth, true);
done();
});
});
});
});