From 14c0db6fb87f341b7f4a94111159aadd51d98cef Mon Sep 17 00:00:00 2001 From: Madhusudhan Srinivasa Date: Mon, 23 Nov 2015 04:26:47 +0100 Subject: [PATCH] update tests to ava --- test/_test-articles-get.js | 54 +++++++++++ test/helper.js | 3 +- test/test-articles-create.js | 86 +++++++++++++++++ test/test-articles.js | 177 ----------------------------------- test/test-users-create.js | 72 ++++++++++++++ test/test-users.js | 106 --------------------- 6 files changed, 214 insertions(+), 284 deletions(-) create mode 100644 test/_test-articles-get.js create mode 100644 test/test-articles-create.js delete mode 100644 test/test-articles.js create mode 100644 test/test-users-create.js delete mode 100644 test/test-users.js diff --git a/test/_test-articles-get.js b/test/_test-articles-get.js new file mode 100644 index 000000000..13992cc03 --- /dev/null +++ b/test/_test-articles-get.js @@ -0,0 +1,54 @@ +'use strict'; + +/** + * Module dependencies. + */ + +const mongoose = require('mongoose'); +const test = require('ava'); +const request = require('supertest'); +const app = require('../server'); +const cleanup = require('./helper').cleanup; +const User = mongoose.model('User'); +const Article = mongoose.model('Article'); +const agent = request.agent(app); + +test.beforeEach(t => cleanup(t.end)); + +test('GET /articles - should respond with Content-Type text/html', t => { + agent + .get('/articles') + .expect('Content-Type', /html/) + .expect(200) + .expect(/Articles/) + .end(t.end); +}); + +test('GET /articles/new - When not logged in - should redirect to /login', t => { + agent + .get('/articles/new') + .expect('Content-Type', /plain/) + .expect(302) + .expect('Location', '/login') + .expect(/Moved Temporarily/) + .end(t.end); +}); + +test.before(t => { + // login the user + agent + .post('/users/session') + .field('email', 'foobar@example.com') + .field('password', 'foobar') + .end(t.end); +}); + +test('GET /articles/new - When logged in - should respond with Content-Type text/html', t => { + agent + .get('/articles/new') + .expect('Content-Type', /html/) + .expect(200) + .expect(/New Article/) + .end(t.end); +}); + diff --git a/test/helper.js b/test/helper.js index 8eec417b7..3850bae49 100644 --- a/test/helper.js +++ b/test/helper.js @@ -16,7 +16,8 @@ const User = mongoose.model('User'); * @api public */ -exports.clearDb = function (done) { +exports.cleanup = function (done) { + done = done || () => {}; async.parallel([ cb => User.collection.remove(cb), cb => Article.collection.remove(cb) diff --git a/test/test-articles-create.js b/test/test-articles-create.js new file mode 100644 index 000000000..008032948 --- /dev/null +++ b/test/test-articles-create.js @@ -0,0 +1,86 @@ +'use strict'; + +/** + * Module dependencies. + */ + +const mongoose = require('mongoose'); +const test = require('ava'); +const request = require('supertest'); +const app = require('../server'); +const cleanup = require('./helper').cleanup; +const User = mongoose.model('User'); +const Article = mongoose.model('Article'); +const agent = request.agent(app); + +const _user = { + email: 'foobar@example.com', + name: 'Foo bar', + username: 'foobar', + password: 'foobar' +}; + +test.beforeEach(t => cleanup(t.end)); + +test('POST /articles - when not logged in - should redirect to /login', t => { + request(app) + .get('/articles/new') + .expect('Content-Type', /plain/) + .expect(302) + .expect('Location', '/login') + .expect(/Redirecting/) + .end(t.end); +}); + +// user login +test.before(async t => { + const user = new User(_user); + await user.save(); + + agent + .post('/users/session') + .field('email', _user.email) + .field('password', _user.password) + .expect('Location', '/') + .expect('Content-Type', /html/) + .end(err => { + console.log(err); + t.ifError(err); + t.end(err); + }); +}); + +test('POST /articles - invalid form - should respond with error', t => { + agent + .post('/articles') + .field('title', '') + .field('body', 'foo') + .expect('Content-Type', /text/) + .expect(302) + .expect(/Article title cannot be blank/) + .end(async (err, res) => { + const count = await Article.count().exec(); + t.ifError(err); + t.same(count, 0, 'Count should be 0'); + t.end(); + }); +}); + +test('POST /articles - valid form - should redirect to the new article page', t => { + agent + .post('/articles') + .field('title', 'foo') + .field('body', 'bar') + .expect('Content-Type', /plain/) + .expect('Location', /\/articles\//) + .expect(302) + .expect(/Redirecting/) + .end(async err => { + const count = await Article.count().exec(); + t.ifError(err); + t.same(count, 1, 'Count should be 0'); + t.end(); + }); +}); + +test.afterEach(t => cleanup(t.end)); diff --git a/test/test-articles.js b/test/test-articles.js deleted file mode 100644 index 314b93ba8..000000000 --- a/test/test-articles.js +++ /dev/null @@ -1,177 +0,0 @@ -'use strict'; - -/** - * Module dependencies. - */ - -const mongoose = require('mongoose'); -const should = require('should'); -const request = require('supertest'); -const app = require('../server'); -const context = describe; -const User = mongoose.model('User'); -const Article = mongoose.model('Article'); -const agent = request.agent(app); - -let count; - -/** - * Articles tests - */ - -describe('Articles', function () { - before(function (done) { - // create a user - var user = new User({ - email: 'foobar@example.com', - name: 'Foo bar', - username: 'foobar', - password: 'foobar' - }); - user.save(done); - }); - - describe('GET /articles', function () { - it('should respond with Content-Type text/html', function (done) { - agent - .get('/articles') - .expect('Content-Type', /html/) - .expect(200) - .expect(/Articles/) - .end(done); - }); - }); - - describe('GET /articles/new', function () { - context('When not logged in', function () { - it('should redirect to /login', function (done) { - agent - .get('/articles/new') - .expect('Content-Type', /plain/) - .expect(302) - .expect('Location', '/login') - .expect(/Moved Temporarily/) - .end(done); - }); - }); - - context('When logged in', function () { - before(function (done) { - // login the user - agent - .post('/users/session') - .field('email', 'foobar@example.com') - .field('password', 'foobar') - .end(done); - }); - - it('should respond with Content-Type text/html', function (done) { - agent - .get('/articles/new') - .expect('Content-Type', /html/) - .expect(200) - .expect(/New Article/) - .end(done); - }); - }); - }); - - describe('POST /articles', function () { - context('When not logged in', function () { - it('should redirect to /login', function (done) { - request(app) - .get('/articles/new') - .expect('Content-Type', /plain/) - .expect(302) - .expect('Location', '/login') - .expect(/Moved Temporarily/) - .end(done); - }); - }); - - context('When logged in', function () { - before(function (done) { - // login the user - agent - .post('/users/session') - .field('email', 'foobar@example.com') - .field('password', 'foobar') - .end(done); - }); - - describe('Invalid parameters', function () { - before(function (done) { - Article.count(function (err, cnt) { - count = cnt; - done(err); - }); - }); - - it('should respond with error', function (done) { - agent - .post('/articles') - .field('title', '') - .field('body', 'foo') - .expect('Content-Type', /html/) - .expect(200) - .expect(/Article title cannot be blank/) - .end(done); - }); - - it('should not save to the database', function (done) { - Article.count(function (err, cnt) { - count.should.equal(cnt); - done(err); - }); - }); - }); - - describe('Valid parameters', function () { - before(function (done) { - Article.count(function (err, cnt) { - count = cnt; - done(); - }); - }); - - it('should redirect to the new article page', function (done) { - agent - .post('/articles') - .field('title', 'foo') - .field('body', 'bar') - .expect('Content-Type', /plain/) - .expect('Location', /\/articles\//) - .expect(302) - .expect(/Moved Temporarily/) - .end(done); - }); - - it('should insert a record to the database', function (done) { - Article.count(function (err, cnt) { - cnt.should.equal(count + 1); - done(err); - }); - }); - - it('should save the article to the database', function (done) { - Article - .findOne({ title: 'foo'}) - .populate('user') - .exec(function (err, article) { - should.not.exist(err); - article.should.be.an.instanceOf(Article); - article.title.should.equal('foo'); - article.body.should.equal('bar'); - article.user.email.should.equal('foobar@example.com'); - article.user.name.should.equal('Foo bar'); - done(); - }); - }); - }); - }); - }); - - after(function (done) { - require('./helper').clearDb(done); - }); -}); diff --git a/test/test-users-create.js b/test/test-users-create.js new file mode 100644 index 000000000..2c55ca822 --- /dev/null +++ b/test/test-users-create.js @@ -0,0 +1,72 @@ +'use strict'; + +/** + * Module dependencies. + */ + +const mongoose = require('mongoose'); +const test = require('ava'); +const request = require('supertest'); +const app = require('../server'); +const cleanup = require('./helper').cleanup; +const User = mongoose.model('User'); + +test.beforeEach(t => cleanup(t.end)); + +test('no email - should respond with errors', t => { + request(app) + .post('/users') + .field('name', 'Foo bar') + .field('username', 'foobar') + .field('email', '') + .field('password', 'foobar') + .expect('Content-Type', /html/) + .expect(200) + .expect(/Email cannot be blank/) + .end(async err => { + const count = await User.count().exec(); + t.ifError(err); + t.same(count, 0, 'count of users should be 0'); + t.end(); + }); +}); + +test('no name - should respond with errors', t => { + request(app) + .post('/users') + .field('name', '') + .field('username', 'foobar') + .field('email', 'foobar@example.com') + .field('password', 'foobar') + .expect('Content-Type', /html/) + .expect(200) + .expect(/Name cannot be blank/) + .end(async err => { + const count = await User.count().exec(); + t.ifError(err); + t.same(count, 0, 'count of users should be 0'); + t.end(); + }); +}); + +test('valid signup - should redirect to /', t => { + request(app) + .post('/users') + .field('name', 'Foo bar') + .field('username', 'foobar') + .field('email', 'foobar@example.com') + .field('password', 'foobar') + .expect('Content-Type', /plain/) + .expect('Location', /\//) + .expect(302) + .end(async err => { + const count = await User.count().exec(); + const user = await User.findOne({ username: 'foobar' }).exec(); + t.ifError(err); + t.same(count, 1, 'count of users should be 1'); + t.same(user.email, 'foobar@example.com'); + t.end(); + }); +}); + +test.afterEach(t => cleanup(t.end)); diff --git a/test/test-users.js b/test/test-users.js deleted file mode 100644 index 2398a5d62..000000000 --- a/test/test-users.js +++ /dev/null @@ -1,106 +0,0 @@ -'use strict'; - -/** - * Module dependencies. - */ - -const mongoose = require('mongoose'); -const should = require('should'); -const request = require('supertest'); -const app = require('../server'); -const User = mongoose.model('User'); - -let count; - -/** - * Users tests - */ - -describe('Users', function () { - describe('POST /users', function () { - describe('Invalid parameters', function () { - before(function (done) { - User.count(function (err, cnt) { - count = cnt; - done(err); - }); - }); - - it('no email - should respond with errors', function (done) { - request(app) - .post('/users') - .field('name', 'Foo bar') - .field('username', 'foobar') - .field('email', '') - .field('password', 'foobar') - .expect('Content-Type', /html/) - .expect(200) - // .expect(/Email cannot be blank/) - .end(done); - }); - - it('no name - should respond with errors', function (done) { - request(app) - .post('/users') - .field('name', '') - .field('username', 'foobar') - .field('email', 'foobar@example.com') - .field('password', 'foobar') - .expect('Content-Type', /html/) - .expect(200) - // .expect(/Name cannot be blank/) - .end(done); - }); - - it('should not save the user to the database', function (done) { - User.count(function (err, cnt) { - count.should.equal(cnt); - done(err); - }); - }); - }); - - describe('Valid parameters', function () { - before(function (done) { - User.count(function (err, cnt) { - count = cnt; - done(); - }); - }); - - it('should redirect to /articles', function (done) { - request(app) - .post('/users') - .field('name', 'Foo bar') - .field('username', 'foobar') - .field('email', 'foobar@example.com') - .field('password', 'foobar') - .expect('Content-Type', /plain/) - .expect('Location', /\//) - .expect(302) - .expect(/Moved Temporarily/) - .end(done); - }); - - it('should insert a record to the database', function (done) { - User.count(function (err, cnt) { - cnt.should.equal(count + 1); - done(err); - }); - }); - - it('should save the user to the database', function (done) { - User.findOne({ username: 'foobar' }).exec(function (err, user) { - should.not.exist(err); - user.should.be.an.instanceOf(User); - user.email.should.equal('foobar@example.com'); - done(); - }); - }); - }); - }); - - after(function (done) { - require('./helper').clearDb(done); - }); -});