From c42c8ebc64ebf860a013e91982a11e2ce70b73e2 Mon Sep 17 00:00:00 2001 From: Amos Haviv Date: Tue, 10 Sep 2013 16:28:47 +0300 Subject: [PATCH] Fixing style, adding article test --- .jshintrc | 79 +++++++++++++++--------------- Makefile | 1 - app/controllers/articles.js | 17 +++++-- app/models/article.js | 7 +++ config/routes.js | 10 +++- gruntfile.js | 2 +- package.json | 94 ++++++++++++++++++------------------ public/js/init.js | 4 +- test/article/model.js | 58 ++++++++++++++++++++++ test/unit/user/model.test.js | 51 ------------------- test/user/model.js | 47 ++++++++++++++++++ 11 files changed, 222 insertions(+), 148 deletions(-) create mode 100644 test/article/model.js delete mode 100644 test/unit/user/model.test.js create mode 100644 test/user/model.js diff --git a/.jshintrc b/.jshintrc index c83be70aa9..8a7233d7a3 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,41 +1,40 @@ -// http://www.jshint.com/docs/ { - "node": true, // Enable globals available when code is running inside of the NodeJS runtime environment. - "browser": true, // Standard browser globals e.g. `window`, `document`. - "es5": true, // Allow EcmaScript 5 syntax. - "esnext": true, // Allow ES.next specific features such as `const` and `let`. - "bitwise": false, // Prohibit bitwise operators (&, |, ^, etc.). - "camelcase": false, // Permit only camelcase for `var` and `object indexes`. - "curly": false, // Require {} for every new block or scope. - "eqeqeq": true, // Require triple equals i.e. `===`. - "immed": true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` - "latedef": true, // Prohibit variable use before definition. - "newcap": true, // Require capitalization of all constructor functions e.g. `new F()`. - "noarg": true, // Prohibit use of `arguments.caller` and `arguments.callee`. - "quotmark": "single", // Define quotes to string values. - "regexp": true, // Prohibit `.` and `[^...]` in regular expressions. - "undef": true, // Require all non-global variables be declared before they are used. - "unused": true, // Warn unused variables. - "strict": false, // Require `use strict` pragma in every file. - "trailing": true, // Prohibit trailing whitespaces. - "smarttabs": false, // Suppresses warnings about mixed tabs and spaces - "globals": { // Globals variables. - "angular": false - }, - "predef": [ // Extra globals. - "define", - "require", - "exports", - "module", - "describe", - "before", - "beforeEach", - "after", - "afterEach", - "it" - ], - "indent": 2, // Specify indentation spacing - "maxlen": 120, // Max line lenght - "devel": false, // Allow development statements e.g. `console.log();`. - "noempty": true // Prohibit use of empty blocks. -} + "node": true, // Enable globals available when code is running inside of the NodeJS runtime environment. + "browser": true, // Standard browser globals e.g. `window`, `document`. + "es5": true, // Allow EcmaScript 5 syntax. + "esnext": true, // Allow ES.next specific features such as `const` and `let`. + "bitwise": false, // Prohibit bitwise operators (&, |, ^, etc.). + "camelcase": false, // Permit only camelcase for `var` and `object indexes`. + "curly": false, // Require {} for every new block or scope. + "eqeqeq": true, // Require triple equals i.e. `===`. + "immed": true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` + "latedef": true, // Prohibit variable use before definition. + "newcap": true, // Require capitalization of all constructor functions e.g. `new F()`. + "noarg": true, // Prohibit use of `arguments.caller` and `arguments.callee`. + "quotmark": "single", // Define quotes to string values. + "regexp": true, // Prohibit `.` and `[^...]` in regular expressions. + "undef": true, // Require all non-global variables be declared before they are used. + "unused": true, // Warn unused variables. + "strict": false, // Require `use strict` pragma in every file. + "trailing": true, // Prohibit trailing whitespaces. + "smarttabs": false, // Suppresses warnings about mixed tabs and spaces + "globals": { // Globals variables. + "angular": true + }, + "predef": [ // Extra globals. + "define", + "require", + "exports", + "module", + "describe", + "before", + "beforeEach", + "after", + "afterEach", + "it" + ], + "indent": 4, // Specify indentation spacing + "maxlen": 120, // Max line lenght + "devel": false, // Allow development statements e.g. `console.log();`. + "noempty": true // Prohibit use of empty blocks. +} \ No newline at end of file diff --git a/Makefile b/Makefile index 9ac702f6a7..ec3333e671 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ REPORTER = spec NODEARGS = test: -# @./node_modules/grunt-contrib-jshint/node_modules/.bin/jshint ./**/*.js --config .jshintrc &2> /dev/null @if [ ! -n "$(NODE_ENV)" ]; then NODE_ENV=test NODE_PATH=lib ./node_modules/grunt-nodemon/node_modules/.bin/nodemon -x ./node_modules/.bin/mocha -R $(REPORTER) -t 15000 --recursive test $(NODEARGS); else NODE_PATH=lib ./node_modules/.bin/mocha -R $(REPORTER) -t 15000 --recursive test $(NODEARGS); fi start: diff --git a/app/controllers/articles.js b/app/controllers/articles.js index 16a7fad551..3f9d9c191e 100644 --- a/app/controllers/articles.js +++ b/app/controllers/articles.js @@ -22,12 +22,21 @@ exports.article = function(req, res, next, id) { /** * Create a article */ -exports.create = function(req, res) { +exports.create = function(req, res) { var article = new Article(req.body); - article.user = req.user; - article.save(); - res.jsonp(article); + + article.save(function(err) { + if (err) { + return res.send('users/signup', { + errors: err.errors, + article: article + }); + } + else { + res.jsonp(article); + } + }); }; /** diff --git a/app/models/article.js b/app/models/article.js index 0d56c12051..d53ee851ee 100644 --- a/app/models/article.js +++ b/app/models/article.js @@ -30,6 +30,13 @@ var ArticleSchema = new Schema({ } }); +/** + * Validations + */ +ArticleSchema.path('title').validate(function(title) { + return title.length; +}, 'Title cannot be blank'); + /** * Statics */ diff --git a/config/routes.js b/config/routes.js index 91ed957638..0092e50a0c 100755 --- a/config/routes.js +++ b/config/routes.js @@ -9,10 +9,12 @@ module.exports = function(app, passport, auth) { //Setting up the users api app.post('/users', users.create); + app.post('/users/session', passport.authenticate('local', { failureRedirect: '/signin', failureFlash: 'Invalid email or password.' }), users.session); + app.get('/users/me', users.me); app.get('/users/:userId', users.show); @@ -21,6 +23,7 @@ module.exports = function(app, passport, auth) { scope: ['email', 'user_about_me'], failureRedirect: '/signin' }), users.signin); + app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/signin' }), users.authCallback); @@ -29,6 +32,7 @@ module.exports = function(app, passport, auth) { app.get('/auth/github', passport.authenticate('github', { failureRedirect: '/signin' }), users.signin); + app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/signin' }), users.authCallback); @@ -37,6 +41,7 @@ module.exports = function(app, passport, auth) { app.get('/auth/twitter', passport.authenticate('twitter', { failureRedirect: '/signin' }), users.signin); + app.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/signin' }), users.authCallback); @@ -45,10 +50,11 @@ module.exports = function(app, passport, auth) { app.get('/auth/google', passport.authenticate('google', { failureRedirect: '/signin', scope: [ - 'https://www.googleapis.com/auth/userinfo.profile', - 'https://www.googleapis.com/auth/userinfo.email' + 'https://www.googleapis.com/auth/userinfo.profile', + 'https://www.googleapis.com/auth/userinfo.email' ] }), users.signin); + app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/signin' }), users.authCallback); diff --git a/gruntfile.js b/gruntfile.js index bc099b5eed..395acdbd99 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -31,7 +31,7 @@ module.exports = function(grunt) { } }, jshint: { - all: ['gruntfile.js'] + all: ['gruntfile.js', 'public/js/**/*.js', 'test/**/*.js', 'app/**/*.js'] }, compass: { //Task dist: { //Target diff --git a/package.json b/package.json index 18e0b5c05d..e834b0e587 100755 --- a/package.json +++ b/package.json @@ -1,48 +1,48 @@ { - "name": "mean", - "description": "Mongo", - "version": "1.0.0", - "private": false, - "author": "MEAN - A Modern Stack: MongoDB, ExpressJS, AngularJS, NodeJS. (BONUS: Passport User Support).", - "engines": { - "node": "0.10.x", - "npm": "1.2.x" - }, - "scripts": { - "start": "make start", - "test": "make test", - "postinstall": "node_modules/bower/bin/bower install" - }, - "dependencies": { - "express": "latest", - "jade": "latest", - "mongoose": "latest", - "connect-mongo": "latest", - "connect-flash": "latest", - "passport": "latest", - "passport-local": "latest", - "passport-facebook": "latest", - "passport-twitter": "latest", - "passport-github": "latest", - "passport-google-oauth": "latest", - "underscore": "latest", - "async": "latest", - "view-helpers": "latest", - "mean-logger": "latest", - "bower": "latest", - "forever": "latest", - "foreman": "0.0.25" - }, - "devDependencies": { - "supertest": "latest", - "should": "latest", - "mocha": "latest", - "grunt": "~0.4.1", - "grunt-contrib-compass": "~0.3.0", - "grunt-contrib-watch": "~0.4.4", - "grunt-contrib-jshint": "~0.6.0", - "grunt-nodemon": "0.0.8", - "grunt-concurrent": "~0.3.0", - "web-mocha": "0.0.10" - } -} + "name": "mean", + "description": "Mongo", + "version": "1.0.0", + "private": false, + "author": "MEAN - A Modern Stack: MongoDB, ExpressJS, AngularJS, NodeJS. (BONUS: Passport User Support).", + "engines": { + "node": "0.10.x", + "npm": "1.2.x" + }, + "scripts": { + "start": "make start", + "test": "make test", + "postinstall": "node_modules/bower/bin/bower install" + }, + "dependencies": { + "express": "latest", + "jade": "latest", + "mongoose": "latest", + "connect-mongo": "latest", + "connect-flash": "latest", + "passport": "latest", + "passport-local": "latest", + "passport-facebook": "latest", + "passport-twitter": "latest", + "passport-github": "latest", + "passport-google-oauth": "latest", + "underscore": "latest", + "async": "latest", + "view-helpers": "latest", + "mean-logger": "latest", + "bower": "latest", + "forever": "latest", + "foreman": "0.0.25" + }, + "devDependencies": { + "supertest": "latest", + "should": "latest", + "mocha": "latest", + "web-mocha": "0.0.10", + "grunt": "~0.4.1", + "grunt-contrib-compass": "~0.3.0", + "grunt-contrib-watch": "~0.4.4", + "grunt-contrib-jshint": "~0.6.0", + "grunt-nodemon": "0.0.8", + "grunt-concurrent": "~0.3.0" + } +} \ No newline at end of file diff --git a/public/js/init.js b/public/js/init.js index e2537929ca..fa2955c850 100644 --- a/public/js/init.js +++ b/public/js/init.js @@ -1,10 +1,10 @@ window.bootstrap = function() { angular.bootstrap(document, ['mean']); -} +}; window.init = function() { window.bootstrap(); -} +}; $(document).ready(function() { //Fixing facebook bug with redirect diff --git a/test/article/model.js b/test/article/model.js new file mode 100644 index 0000000000..e31351abae --- /dev/null +++ b/test/article/model.js @@ -0,0 +1,58 @@ +/** + * Module dependencies. + */ +var should = require('should'), + app = require('../../server'), + mongoose = require('mongoose'), + User = mongoose.model('User'), + Article = mongoose.model('Article'); + +//Globals +var user; +var article; + +//The tests +describe('', function() { + describe('Model Article:', function() { + beforeEach(function(done) { + user = new User({ + name: 'Full name', + email: 'test@test.com', + username: 'user', + password: 'password' + }); + + user.save(function(err) { + article = new Article({ + title: 'Article Title', + content: 'Article Content', + user: user + }); + + done(); + }); + }); + + describe('Method Save', function() { + it('should be able to save whithout problems', function(done) { + return article.save(function(err) { + should.not.exist(err); + done(); + }); + }); + + it('should be able to show an error when try to save witout title', function(done) { + article.title = ''; + + return article.save(function(err) { + should.exist(err); + done(); + }); + }); + }); + + afterEach(function(done) { + done(); + }); + }); +}); \ No newline at end of file diff --git a/test/unit/user/model.test.js b/test/unit/user/model.test.js deleted file mode 100644 index 528bc5b3c7..0000000000 --- a/test/unit/user/model.test.js +++ /dev/null @@ -1,51 +0,0 @@ -var - should = require('should'), - app = require('../../../server'), - mongoose = require('mongoose'); - -describe('', function() { - - describe('Model User:', function() { - var User = mongoose.model('User'), - user; - - beforeEach(function (done) { - user = new User({ - name: 'Full name', - email: 'test@test.com', - username: 'user', - password: 'password' - }); - done(); - }); - - afterEach(function (done) { - - done(); - - }); - - describe('Method Save', function() { - - it('should be able to save whithout problems', function (done) { - - return user.save(function (err) { - should.not.exist(err); - done(); - }); - - }); - - it('should be able to show an erro when try to save witout name', function (done) { - user.name = ''; - return user.save(function (err) { - should.exist(err); - done(); - }); - }); - - }); - - }); - -}); diff --git a/test/user/model.js b/test/user/model.js new file mode 100644 index 0000000000..4919b7b543 --- /dev/null +++ b/test/user/model.js @@ -0,0 +1,47 @@ +/** + * Module dependencies. + */ +var should = require('should'), + app = require('../../server'), + mongoose = require('mongoose'), + User = mongoose.model('User'); + +//Globals +var user; + +//The tests +describe('', function() { + describe('Model User:', function() { + before(function(done) { + user = new User({ + name: 'Full name', + email: 'test@test.com', + username: 'user', + password: 'password' + }); + + done(); + }); + + describe('Method Save', function() { + it('should be able to save whithout problems', function(done) { + return user.save(function(err) { + should.not.exist(err); + done(); + }); + }); + + it('should be able to show an error when try to save witout name', function(done) { + user.name = ''; + return user.save(function(err) { + should.exist(err); + done(); + }); + }); + }); + + after(function(done) { + done(); + }); + }); +}); \ No newline at end of file