Skip to content

Commit 4c894b6

Browse files
committed
feat(server): added tests for user model
1 parent 2bda23d commit 4c894b6

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

app/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ Generator.prototype.serverFiles = function () {
473473
this.template('../../templates/express/server.js', 'server.js');
474474
this.copy('../../templates/express/jshintrc', 'lib/.jshintrc');
475475
this.template('../../templates/express/controllers/api.js', 'lib/controllers/api.js');
476-
this.template('../../templates/express/test/api/api.js', 'test/server/api/api.js');
477476
this.template('../../templates/express/controllers/index.js', 'lib/controllers/index.js');
478477
this.template('../../templates/express/routes.js', 'lib/routes.js');
478+
this.template('../../templates/express/test/thing/api.js', 'test/server/thing/api.js');
479479

480480
this.template('../../templates/express/config/express.js', 'lib/config/express.js');
481481
this.template('../../templates/express/config/config.js', 'lib/config/config.js');
@@ -520,4 +520,6 @@ Generator.prototype.mongoFiles = function () {
520520
// controllers
521521
this.template('../../templates/express/controllers/session.js', 'lib/controllers/session.js');
522522
this.template('../../templates/express/controllers/users.js', 'lib/controllers/users.js');
523+
// tests
524+
this.template('../../templates/express/test/user/model.js', 'test/server/user/model.js');
523525
};

templates/common/Gruntfile.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module.exports = function (grunt) {
6565
mochaTest: {
6666
files: ['test/server/{,*/}*.js'],
6767
tasks: ['mochaTest']
68-
},
68+
},
6969
jsTest: {
7070
files: ['test/spec/{,*/}*.js'],
7171
tasks: ['newer:jshint:test', 'karma']
@@ -428,12 +428,19 @@ module.exports = function (grunt) {
428428
configFile: 'karma.conf.js',
429429
singleRun: true
430430
}
431-
},
432-
mochaTest: {
433-
options: {
434-
reporter: 'spec'
435-
},
436-
src: ['test/server/**/*.js']
431+
},
432+
433+
mochaTest: {
434+
options: {
435+
reporter: 'spec'
436+
},
437+
src: ['test/server/**/*.js']
438+
},
439+
440+
env: {
441+
test: {
442+
NODE_ENV: 'test'
443+
}
437444
}
438445
});
439446

@@ -476,7 +483,10 @@ module.exports = function (grunt) {
476483

477484
grunt.registerTask('test', function(target) {
478485
if (target === 'server') {
479-
return grunt.task.run(['mochaTest']);
486+
return grunt.task.run([
487+
'env:test',
488+
'mochaTest'
489+
]);
480490
}
481491

482492
if (target === 'client') {
@@ -489,6 +499,7 @@ module.exports = function (grunt) {
489499
}
490500

491501
grunt.task.run([
502+
'env:test',
492503
'mochaTest',
493504
'clean:server',
494505
'concurrent:test',

templates/common/_package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"karma-ng-html2js-preprocessor": "~0.1.0",
5757
"grunt-mocha-test": "~0.8.1",
5858
"supertest": "~0.8.2",
59-
"should": "~2.1.0"
59+
"should": "~2.1.0",
60+
"grunt-env": "~0.4.1"
6061
},
6162
"engines": {
6263
"node": ">=0.10.0"

templates/express/test/user/model.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
var should = require('should'),
4+
mongoose = require('mongoose'),
5+
User = mongoose.model('User');
6+
7+
var user;
8+
9+
describe('User Model', function() {
10+
before(function(done) {
11+
user = new User({
12+
provider: 'local',
13+
name: 'Fake User',
14+
15+
password: 'password'
16+
});
17+
18+
// Clear users before testing
19+
User.remove().exec();
20+
done();
21+
});
22+
23+
afterEach(function(done) {
24+
User.remove().exec();
25+
done();
26+
});
27+
28+
it('should begin with no users', function(done) {
29+
User.find({}, function(err, users) {
30+
users.should.have.length(0);
31+
done();
32+
});
33+
});
34+
35+
it('should fail when saving a duplicate user', function(done) {
36+
user.save();
37+
var userDup = new User(user);
38+
userDup.save(function(err) {
39+
should.exist(err);
40+
done();
41+
});
42+
});
43+
44+
it('should fail when saving without an email', function(done) {
45+
user.email = '';
46+
user.save(function(err) {
47+
should.exist(err);
48+
done();
49+
});
50+
});
51+
52+
it("should authenticate user if password is valid", function() {
53+
user.authenticate('password').should.be.true;
54+
});
55+
56+
it("should not authenticate user if password is invalid", function() {
57+
user.authenticate('blah').should.not.be.true;
58+
});
59+
60+
});

0 commit comments

Comments
 (0)