This repository was archived by the owner on Jul 29, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathuser.model.spec(mongooseModels).js
More file actions
213 lines (176 loc) · 6.24 KB
/
Copy pathuser.model.spec(mongooseModels).js
File metadata and controls
213 lines (176 loc) · 6.24 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
'use strict';
import app from '../..';
import User from './user.model';
var user;
var genUser = function() {
user = new User({
provider: 'local',
name: 'Fake User',
email: 'test@example.com',
password: 'password'
});
return user;
};
describe('User Model', function() {
before(function() {
// Clear users before testing
return User.remove();
});
beforeEach(function() {
genUser();
});
afterEach(function() {
return User.remove();
});
it('should begin with no users', function() {
return <%= expect() %>User.find({}).exec()<%= to() %>
.eventually.have.length(0);
});
it('should fail when saving a duplicate user', function() {
return <%= expect() %>user.save()
.then(function() {
var userDup = genUser();
return userDup.save();
})<%= to() %>.be.rejected;
});
describe('#email', function() {
it('should fail when saving with a blank email', function() {
user.email = '';
return <%= expect() %>user.save()<%= to() %>.be.rejected;
});
it('should fail when saving with a null email', function() {
user.email = null;
return <%= expect() %>user.save()<%= to() %>.be.rejected;
});
it('should fail when saving without an email', function() {
user.email = undefined;
return <%= expect() %>user.save()<%= to() %>.be.rejected;
});
<%_ if(filters.githubAuth) { -%>
describe('given user provider is github', function() {
beforeEach(function() {
user.provider = 'github';
});
it('should succeed when saving without an email', function() {
user.email = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %>
<%_ if(filters.oauth && filters.googleAuth) { -%>
describe('given user provider is google', function() {
beforeEach(function() {
user.provider = 'google';
});
it('should succeed when saving without an email', function() {
user.email = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %><% if(filters.oauth && filters.facebookAuth) { %>
describe('given user provider is facebook', function() {
beforeEach(function() {
user.provider = 'facebook';
});
it('should succeed when saving without an email', function() {
user.email = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %><% if(filters.oauth && filters.twitterAuth) { %>
describe('given user provider is twitter', function() {
beforeEach(function() {
user.provider = 'twitter';
});
it('should succeed when saving without an email', function() {
user.email = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %><% if(filters.oauth) { %>
describe('given user provider is github', function() {
beforeEach(function() {
user.provider = 'github';
});
it('should succeed when saving without an email', function() {
user.email = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %>
});
describe('#password', function() {
it('should fail when saving with a blank password', function() {
user.password = '';
return <%= expect() %>user.save()<%= to() %>.be.rejected;
});
it('should fail when saving with a null password', function() {
user.password = null;
return <%= expect() %>user.save()<%= to() %>.be.rejected;
});
it('should fail when saving without a password', function() {
user.password = undefined;
return <%= expect() %>user.save()<%= to() %>.be.rejected;
});
describe('given the user has been previously saved', function() {
beforeEach(function() {
return user.save();
});
it('should authenticate user if valid', function() {
<%= expect() %>user.authenticate('password')<%= to() %>.be.true;
});
it('should not authenticate user if invalid', function() {
<%= expect() %>user.authenticate('blah')<%= to() %>.not.be.true;
});
it('should remain the same hash unless the password is updated', function() {
user.name = 'Test User';
return <%= expect() %>user.save()
.then(function(u) {
return u.authenticate('password');
})<%= to() %>.eventually.be.true;
});
});
<%_ if(filters.oauth && filters.githubAuth) { -%>
describe('given user provider is github', function() {
beforeEach(function() {
user.provider = 'github';
});
it('should succeed when saving without a password', function() {
user.password = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %>
<%_ if(filters.oauth && filters.googleAuth) { -%>
describe('given user provider is google', function() {
beforeEach(function() {
user.provider = 'google';
});
it('should succeed when saving without a password', function() {
user.password = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %><% if(filters.oauth && filters.facebookAuth) { %>
describe('given user provider is facebook', function() {
beforeEach(function() {
user.provider = 'facebook';
});
it('should succeed when saving without a password', function() {
user.password = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %><% if(filters.oauth && filters.twitterAuth) { %>
describe('given user provider is twitter', function() {
beforeEach(function() {
user.provider = 'twitter';
});
it('should succeed when saving without a password', function() {
user.password = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %><% if(filters.oauth) { %>
describe('given user provider is github', function() {
beforeEach(function() {
user.provider = 'github';
});
it('should succeed when saving without a password', function() {
user.password = null;
return <%= expect() %>user.save()<%= to() %>.be.fulfilled;
});
});<% } %>
});
});