TwoToc code
[YouAndWeb_TwoToc] / server / api / user / __user.model.spec.js
1 'use strict';
2
3 var app = require('../..');
4 var User = require('./user.model');
5 var user;
6 var genUser = function() {
7   user = new User({
8     provider: 'local',
9     name: 'Fake User',
10     email: 'test@example.com',
11     password: 'password'
12   });
13   return user;
14 };
15
16 describe('User Model', function() {
17   before(function() {
18     // Clear users before testing
19     return User.removeAsync();
20   });
21
22   beforeEach(function() {
23     genUser();
24   });
25
26   afterEach(function() {
27     return User.removeAsync();
28   });
29
30   it('should begin with no users', function() {
31     return User.findAsync({}).should
32       .eventually.have.length(0);
33   });
34
35   it('should fail when saving a duplicate user', function() {
36     return user.saveAsync()
37       .then(function() {
38         var userDup = genUser();
39         return userDup.saveAsync();
40       }).should.be.rejected;
41   });
42
43   describe('#email', function() {
44     it('should fail when saving without an email', function() {
45       user.email = '';
46       return user.saveAsync().should.be.rejected;
47     });
48   });
49
50   describe('#password', function() {
51     beforeEach(function() {
52       return user.saveAsync();
53     });
54
55     it('should authenticate user if valid', function() {
56       user.authenticate('password').should.be.true;
57     });
58
59     it('should not authenticate user if invalid', function() {
60       user.authenticate('blah').should.not.be.true;
61     });
62
63     it('should remain the same hash unless the password is updated', function() {
64       user.name = 'Test User';
65       return user.saveAsync()
66         .spread(function(u) {
67           return u.authenticate('password');
68         }).should.eventually.be.true;
69     });
70   });
71
72 });