forked from Relief-1/Relief1
/
test-login.js
133 lines (121 loc) · 3.77 KB
/
test-login.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
var crypto = require('crypto');
var testCase = require('nodeunit').testCase;
var settings = require('../lib/settings');
settings.__reload('test');
var db = require('../lib/db').db;
var login = require('../lib/login');
var invalidEmails = [
'invalid.email',
'@invalid.email',
'a@invalid',
'c@.pl'
]
var validEmails = [
'valid@email.com',
'valid+test@email.com',
'v@e.pl',
'CAPS@MAIL.ORG',
'test@multiple.sub.domains.org'
]
module.exports = testCase({
setUp: function (callback) {
var hash = crypto.createHash(settings.loginManager.hash);
var salt = 'salt';
this.login = new login.Login();
this.email = 'test@example.com';
this.password = 'password';
hash.update(salt + this.password);
this.user = {
hash: hash.digest('hex'),
salt: salt
};
var obj = this;
db.save(encodeURIComponent('user-' + this.email), this.user,
function (err, res) {
if (err)
callback(err);
obj.id = res.id;
obj.rev = res.rev;
callback();
});
},
testCorrectLogin: function (test) {
test.expect(2);
var obj = this;
this.login.userLogin(this.email, this.password, function (err, doc) {
test.equal(err, null, 'there should be no error with correct credentials');
test.equal(doc.id, obj.id);
test.done();
});
},
testWrongPassword: function (test) {
test.expect(2);
this.login.userLogin(this.email, this.password + '-not-really',
function (err, doc) {
test.ok(err, 'should return error when wrong password is supplied');
test.equal(doc, null, 'there should be no document returned');
test.done();
});
},
testWrongLogin: function (test) {
test.expect(2);
this.login.userLogin(this.email + '-not-really', this.password,
function (err, doc) {
test.ok(err, 'should return error when wrong login is supplied');
test.equal(doc, null, 'there should be no document returned');
test.done();
});
},
testSaltGeneration: function (test) {
var salt = this.login.saltGenerate();
test.equal(salt.length, settings.loginManager.saltLength);
test.done();
},
testUserRegister: function (test) {
// TODO: test for invalid emails
test.expect(3);
var email = 'some-email@test.com';
var id = 'user-' + encodeURIComponent(email);
var password = 'ninja';
this.login.userRegister(email, password, function (err, res) {
test.ok(!err, "shouldn't return error with valid data");
var rev = res.rev;
db.get(id, function (err, doc) {
test.ok(!err, 'there should be no error when getting new user from DB');
test.equal(
doc.salt.length,
settings.loginManager.saltLength,
"salt's length should be equal to one in settings"
);
db.remove(id, rev, function () {});
test.done();
});
});
},
testRegisterExistingUser: function (test) {
var self = this;
test.expect(3);
this.login.userRegister(this.email, 'whatever', function (err, res) {
test.ok(err, 'should return error when trying to register existing user');
test.equal(err.code, login.Login.USER_EXISTS, 'should return proper error code');
db.get(self.id, function (err, doc) {
test.equal(doc._rev, self.rev, 'document should not get overriden');
test.done();
});
});
},
testEmailValidation: function (test) {
var self = this;
test.expect(validEmails.length + invalidEmails.length);
validEmails.forEach(function (email) {
test.ok(self.login.emailValidate(email));
});
invalidEmails.forEach(function (email) {
test.ok(!self.login.emailValidate(email));
});
test.done();
},
tearDown: function (callback) {
db.remove(this.id, this.rev, callback);
}
});