forked from angular-fullstack/generator-angular-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.model.spec(mongooseModels).js
78 lines (65 loc) · 1.85 KB
/
user.model.spec(mongooseModels).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
'use strict';
import app from '../..';
import mongoose from 'mongoose';
import User from './user.model';
var user;
var genUser = function() {
user = new User({
provider: 'local',
name: 'Fake User',
email: '[email protected]',
password: 'password'
});
return user;
};
describe('User Model', function() {
before(function() {
// Clear users before testing
return User.removeAsync();
});
beforeEach(function() {
genUser();
});
afterEach(function() {
return User.removeAsync();
});
after(function() {
app.angularFullstack.close();
mongoose.connection.close();
});
it('should begin with no users', function() {
return <%= expect() %>User.findAsync({})<%= to() %>
.eventually.have.length(0);
});
it('should fail when saving a duplicate user', function() {
return <%= expect() %>user.saveAsync()
.then(function() {
var userDup = genUser();
return userDup.saveAsync();
})<%= to() %>.be.rejected;
});
describe('#email', function() {
it('should fail when saving without an email', function() {
user.email = '';
return <%= expect() %>user.saveAsync()<%= to() %>.be.rejected;
});
});
describe('#password', function() {
beforeEach(function() {
return user.saveAsync();
});
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.saveAsync()
.spread(function(u) {
return u.authenticate('password');
})<%= to() %>.eventually.be.true;
});
});
});