Skip to content

Commit

Permalink
feat(server): added tests for user model
Browse files Browse the repository at this point in the history
  • Loading branch information
DaftMonk committed Feb 23, 2014
1 parent 2bda23d commit 4c894b6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
4 changes: 3 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,9 @@ Generator.prototype.serverFiles = function () {
this.template('../../templates/express/server.js', 'server.js');
this.copy('../../templates/express/jshintrc', 'lib/.jshintrc');
this.template('../../templates/express/controllers/api.js', 'lib/controllers/api.js');
this.template('../../templates/express/test/api/api.js', 'test/server/api/api.js');
this.template('../../templates/express/controllers/index.js', 'lib/controllers/index.js');
this.template('../../templates/express/routes.js', 'lib/routes.js');
this.template('../../templates/express/test/thing/api.js', 'test/server/thing/api.js');

this.template('../../templates/express/config/express.js', 'lib/config/express.js');
this.template('../../templates/express/config/config.js', 'lib/config/config.js');
Expand Down Expand Up @@ -520,4 +520,6 @@ Generator.prototype.mongoFiles = function () {
// controllers
this.template('../../templates/express/controllers/session.js', 'lib/controllers/session.js');
this.template('../../templates/express/controllers/users.js', 'lib/controllers/users.js');
// tests
this.template('../../templates/express/test/user/model.js', 'test/server/user/model.js');
};
27 changes: 19 additions & 8 deletions templates/common/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = function (grunt) {
mochaTest: {
files: ['test/server/{,*/}*.js'],
tasks: ['mochaTest']
},
},
jsTest: {
files: ['test/spec/{,*/}*.js'],
tasks: ['newer:jshint:test', 'karma']
Expand Down Expand Up @@ -428,12 +428,19 @@ module.exports = function (grunt) {
configFile: 'karma.conf.js',
singleRun: true
}
},
mochaTest: {
options: {
reporter: 'spec'
},
src: ['test/server/**/*.js']
},

mochaTest: {
options: {
reporter: 'spec'
},
src: ['test/server/**/*.js']
},

env: {
test: {
NODE_ENV: 'test'
}
}
});

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

grunt.registerTask('test', function(target) {
if (target === 'server') {
return grunt.task.run(['mochaTest']);
return grunt.task.run([
'env:test',
'mochaTest'
]);
}

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

grunt.task.run([
'env:test',
'mochaTest',
'clean:server',
'concurrent:test',
Expand Down
3 changes: 2 additions & 1 deletion templates/common/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"karma-ng-html2js-preprocessor": "~0.1.0",
"grunt-mocha-test": "~0.8.1",
"supertest": "~0.8.2",
"should": "~2.1.0"
"should": "~2.1.0",
"grunt-env": "~0.4.1"
},
"engines": {
"node": ">=0.10.0"
Expand Down
File renamed without changes.
60 changes: 60 additions & 0 deletions templates/express/test/user/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

var should = require('should'),
mongoose = require('mongoose'),
User = mongoose.model('User');

var user;

describe('User Model', function() {
before(function(done) {
user = new User({
provider: 'local',
name: 'Fake User',
email: 'test@test.com',
password: 'password'
});

// Clear users before testing
User.remove().exec();
done();
});

afterEach(function(done) {
User.remove().exec();
done();
});

it('should begin with no users', function(done) {
User.find({}, function(err, users) {
users.should.have.length(0);
done();
});
});

it('should fail when saving a duplicate user', function(done) {
user.save();
var userDup = new User(user);
userDup.save(function(err) {
should.exist(err);
done();
});
});

it('should fail when saving without an email', function(done) {
user.email = '';
user.save(function(err) {
should.exist(err);
done();
});
});

it("should authenticate user if password is valid", function() {
user.authenticate('password').should.be.true;
});

it("should not authenticate user if password is invalid", function() {
user.authenticate('blah').should.not.be.true;
});

});

0 comments on commit 4c894b6

Please sign in to comment.