-
Notifications
You must be signed in to change notification settings - Fork 9
/
database.users.js
193 lines (145 loc) · 6.59 KB
/
database.users.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
const should = require('should');
const requireNew = require('require-new');
const wipe = require('./fixture-helpers/wipe');
const DuplicateRecordError = require('../lib/errors').DuplicateRecordError;
describe("Database", function() {
before(wipe.wipeAllData);
describe("Users", function() {
const user1 = requireNew('./fixtures/user1.json');
const user2 = requireNew('./fixtures/user2.json');
describe("Create", function() {
it("Should be able to create a user", function(done) {
global.db.users.create(user1, function(err, result) {
should.not.exist(err);
should.exist(result);
result.should.have.properties(["insertId", "verificationToken"]);
result.should.have.property("email", user1.email);
result.should.have.property("displayName", user1.displayName);
done();
});
});
it("Should be able to create another user (with no displayName)", function(done) {
global.db.users.create(user2, function(err, result) {
should.not.exist(err);
should.exist(result);
result.should.have.properties(["insertId", "verificationToken"]);
result.should.have.properties({
email : user2.email,
displayName : user2.displayName
});
done();
});
});
it("Should not be able to create a duplicate user", function(done) {
global.db.users.create(user1, function(err, result) {
should.exist(err);
should.not.exist(result);
err.should.be.an.instanceOf(DuplicateRecordError);
err.should.have.property("data");
err.data.should.have.property("code", "ER_DUP_ENTRY");
done();
});
});
}); // End Create
describe("Find", function() {
let foundUser1 = null;
it("Should be able to find a user by email", function(done) {
global.db.users.findByEmail(user1.email, function(err, user) {
should.not.exist(err);
should.exist(user);
user.should.have.properties(["id", "password", "created", "modified"]);
user.should.have.properties({
email : user1.email,
displayName : user1.displayName
});
// remember this user so we can do the next test
foundUser1 = user;
done();
});
});
it("Should be able to find a user by ID", function(done) {
global.db.users.findById(foundUser1.id, function(err, user) {
should.not.exist(err);
should.exist(user);
// do a deep equal
should(user).eql(foundUser1);
done();
});
});
it("Should be able to find a user by email and password", function(done) {
global.db.users.findByEmailAndPassword(user1.email, user1.password, function(err, user) {
should.not.exist(err);
should.exist(user);
user.should.have.properties(["id", "password", "created", "modified"]);
user.should.have.properties({
email : user1.email,
displayName : user1.displayName
});
done();
});
});
it("Should not be able to find a user by a non-existent email", function(done) {
global.db.users.findByEmail("bogus", function(err, user) {
should.not.exist(err);
should.not.exist(user);
done();
});
});
it("Should not be able to find a user by a non-existent ID", function(done) {
global.db.users.findById(-1, function(err, user) {
should.not.exist(err);
should.not.exist(user);
done();
});
});
it("Should not be able to find a user by email and password with a non-existent email", function(done) {
global.db.users.findByEmailAndPassword("bogus", user1.password, function(err, user) {
should.not.exist(err);
should.not.exist(user);
done();
});
});
it("Should not be able to find a user by email and password with an incorrect password", function(done) {
global.db.users.findByEmailAndPassword(user2.email, "bogus", function(err, user) {
should.not.exist(err);
should.not.exist(user);
done();
});
});
}); // End Find
describe("Reset Password", function() {
let resetPasswordToken = null;
let foundUser = null;
const newPassword = 'this is my new password';
it("Should be able to create a reset password token", function(done) {
global.db.users.findByEmail(user1.email, function(err, user) {
should.not.exist(err);
should.exist(user);
// remember this user so we can compare later
foundUser = user;
global.db.users.createResetPasswordToken(user1.email, function(err, token) {
should.not.exist(err);
should.exist(token);
// remember the token so we can use it to reset the user's password
resetPasswordToken = token;
done();
});
});
});
it("Should be able to set the password using the reset password token", function(done) {
global.db.users.setPassword(resetPasswordToken, newPassword, function(err, wasSuccessful) {
should.not.exist(err);
should.exist(wasSuccessful);
wasSuccessful.should.eql(true);
// do a find on the user to verify that the password changed
global.db.users.findByEmail(user1.email, function(err, user) {
should.not.exist(err);
should.exist(user);
foundUser.password.should.not.eql(user.password);
done();
});
});
});
}); // End Reset Password
}); // End Users
}); // End Database