Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhutchison committed Jul 7, 2015
1 parent 9ca71c8 commit 67e38ae
Showing 1 changed file with 45 additions and 44 deletions.
89 changes: 45 additions & 44 deletions modules/articles/tests/server/article.server.routes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ var app, agent, credentials, user, article;
/**
* Article routes tests
*/
describe('Article CRUD tests', function() {
before(function(done) {
describe('Article CRUD tests', function () {
before(function (done) {
// Get application
app = express.init(mongoose);
agent = request.agent(app);

done();
});

beforeEach(function(done) {
beforeEach(function (done) {
// Create user credentials
credentials = {
username: 'username',
Expand All @@ -44,7 +44,7 @@ describe('Article CRUD tests', function() {
});

// Save a user to the test db and create new article
user.save(function() {
user.save(function () {
article = {
title: 'Article Title',
content: 'Article Content'
Expand All @@ -54,11 +54,11 @@ describe('Article CRUD tests', function() {
});
});

it('should be able to save an article if logged in', function(done) {
it('should be able to save an article if logged in', function (done) {
agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function(signinErr, signinRes) {
.end(function (signinErr, signinRes) {
// Handle signin error
if (signinErr) done(signinErr);

Expand All @@ -69,13 +69,13 @@ describe('Article CRUD tests', function() {
agent.post('/api/articles')
.send(article)
.expect(200)
.end(function(articleSaveErr, articleSaveRes) {
.end(function (articleSaveErr, articleSaveRes) {
// Handle article save error
if (articleSaveErr) done(articleSaveErr);

// Get a list of articles
agent.get('/api/articles')
.end(function(articlesGetErr, articlesGetRes) {
.end(function (articlesGetErr, articlesGetRes) {
// Handle article save error
if (articlesGetErr) done(articlesGetErr);

Expand All @@ -93,24 +93,24 @@ describe('Article CRUD tests', function() {
});
});

it('should not be able to save an article if not logged in', function(done) {
it('should not be able to save an article if not logged in', function (done) {
agent.post('/api/articles')
.send(article)
.expect(403)
.end(function(articleSaveErr, articleSaveRes) {
.end(function (articleSaveErr, articleSaveRes) {
// Call the assertion callback
done(articleSaveErr);
});
});

it('should not be able to save an article if no title is provided', function(done) {
it('should not be able to save an article if no title is provided', function (done) {
// Invalidate title field
article.title = '';

agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function(signinErr, signinRes) {
.end(function (signinErr, signinRes) {
// Handle signin error
if (signinErr) done(signinErr);

Expand All @@ -121,7 +121,7 @@ describe('Article CRUD tests', function() {
agent.post('/api/articles')
.send(article)
.expect(400)
.end(function(articleSaveErr, articleSaveRes) {
.end(function (articleSaveErr, articleSaveRes) {
// Set message assertion
(articleSaveRes.body.message).should.match('Title cannot be blank');

Expand All @@ -131,11 +131,11 @@ describe('Article CRUD tests', function() {
});
});

it('should be able to update an article if signed in', function(done) {
it('should be able to update an article if signed in', function (done) {
agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function(signinErr, signinRes) {
.end(function (signinErr, signinRes) {
// Handle signin error
if (signinErr) done(signinErr);

Expand All @@ -146,7 +146,7 @@ describe('Article CRUD tests', function() {
agent.post('/api/articles')
.send(article)
.expect(200)
.end(function(articleSaveErr, articleSaveRes) {
.end(function (articleSaveErr, articleSaveRes) {
// Handle article save error
if (articleSaveErr) done(articleSaveErr);

Expand All @@ -157,7 +157,7 @@ describe('Article CRUD tests', function() {
agent.put('/api/articles/' + articleSaveRes.body._id)
.send(article)
.expect(200)
.end(function(articleUpdateErr, articleUpdateRes) {
.end(function (articleUpdateErr, articleUpdateRes) {
// Handle article update error
if (articleUpdateErr) done(articleUpdateErr);

Expand All @@ -172,17 +172,17 @@ describe('Article CRUD tests', function() {
});
});

it('should be able to get a list of articles if not signed in', function(done) {
it('should be able to get a list of articles if not signed in', function (done) {
// Create new article model instance
var articleObj = new Article(article);

// Save the article
articleObj.save(function() {
articleObj.save(function () {
// Request articles
request(app).get('/api/articles')
.end(function(req, res) {
.end(function (req, res) {
// Set assertion
res.body.should.be.an.Array.with.lengthOf(1);
res.body.should.be.instanceof(Array).and.have.lengthOf(1);

// Call the assertion callback
done();
Expand All @@ -192,39 +192,40 @@ describe('Article CRUD tests', function() {
});


it('should be able to get a single article if not signed in', function(done) {
it('should be able to get a single article if not signed in', function (done) {
// Create new article model instance
var articleObj = new Article(article);

// Save the article
articleObj.save(function() {
articleObj.save(function () {
request(app).get('/api/articles/' + articleObj._id)
.end(function(req, res) {
.end(function (req, res) {
// Set assertion
res.body.should.be.an.Object.with.property('title', article.title);
res.body.should.be.instanceof(Object).and.have.property('title', article.title);

// Call the assertion callback
done();
});
});
});

it('should return proper error for single article which doesnt exist, if not signed in', function(done) {
request(app).get('/articles/test')
.end(function(req, res) {
it('should return proper error for single article which doesnt exist, if not signed in', function (done) {
request(app).get('/api/articles/test')
.end(function (req, res) {
console.log(res.body);
// Set assertion
res.body.should.be.an.Object.with.property('message', 'Article is invalid');
res.body.should.be.instanceof(Object).and.have.property('message', 'Article is invalid');

// Call the assertion callback
done();
});
});

it('should be able to delete an article if signed in', function(done) {
it('should be able to delete an article if signed in', function (done) {
agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function(signinErr, signinRes) {
.end(function (signinErr, signinRes) {
// Handle signin error
if (signinErr) done(signinErr);

Expand All @@ -235,15 +236,15 @@ describe('Article CRUD tests', function() {
agent.post('/api/articles')
.send(article)
.expect(200)
.end(function(articleSaveErr, articleSaveRes) {
.end(function (articleSaveErr, articleSaveRes) {
// Handle article save error
if (articleSaveErr) done(articleSaveErr);

// Delete an existing article
agent.delete('/api/articles/' + articleSaveRes.body._id)
.send(article)
.expect(200)
.end(function(articleDeleteErr, articleDeleteRes) {
.end(function (articleDeleteErr, articleDeleteRes) {
// Handle article error error
if (articleDeleteErr) done(articleDeleteErr);

Expand All @@ -257,31 +258,31 @@ describe('Article CRUD tests', function() {
});
});

it('should not be able to delete an article if not signed in', function(done) {
it('should not be able to delete an article if not signed in', function (done) {
// Set article user
article.user = user;

// Create new article model instance
var articleObj = new Article(article);

// Save the article
articleObj.save(function() {
articleObj.save(function () {
// Try deleting article
request(app).delete('/api/articles/' + articleObj._id)
.expect(403)
.end(function(articleDeleteErr, articleDeleteRes) {
// Set message assertion
(articleDeleteRes.body.message).should.match('User is not authorized');
.expect(403)
.end(function (articleDeleteErr, articleDeleteRes) {
// Set message assertion
(articleDeleteRes.body.message).should.match('User is not authorized');

// Handle article error error
done(articleDeleteErr);
});
// Handle article error error
done(articleDeleteErr);
});

});
});

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

0 comments on commit 67e38ae

Please sign in to comment.