Permalink
Browse files

Replaced supertest with supertest-as-promised

  • Loading branch information...
camjackson committed Dec 27, 2014
1 parent bba7384 commit 04d3f3a9df11a65d72a7b279c683612cc70add22
Showing with 127 additions and 145 deletions.
  1. +1 −1 package.json
  2. +126 −144 spec/integration/integrationSpec.js
@@ -33,7 +33,7 @@
"q": "^1.0.1",
"sinon": "^1.10.3",
"sinon-chai": "^2.5.0",
"supertest": "^0.13.0"
"supertest-as-promised": "^1.0.0"
},
"scripts": {
"test": "./node_modules/mocha/bin/mocha --recursive spec"
@@ -1,4 +1,4 @@
var request = require('supertest');
var request = require('supertest-as-promised');
var sinon = require('sinon');
var chai = require('chai');
var expect = chai.expect;
@@ -56,101 +56,89 @@ describe('Integration Test', function() {
describe('endpoints that do not need auth', function() {
var app = new WriteItDown({}).app;
describe('GET /login', function() {
it('renders the login page successfully', function(done) {
request(app)
.get('/login')
.end(function (err, res) {
expect(res.statusCode).to.equal(200);
expect(res.text).to.include('<title>integration title<\/title>');
expect(res.text).to.include('name="password"');
expect(res.text).to.include('<input type="submit"');
done();
});
it('renders the login page successfully', function() {
var req = request(app).get('/login');
return req.then(function(res) {
expect(res.statusCode).to.equal(200);
expect(res.text).to.include('<title>integration title<\/title>');
expect(res.text).to.include('name="password"');
expect(res.text).to.include('<input type="submit"');
});
});
});

describe('POST /login', function() {
it('redirects to the profile page when credentials are valid', function(done) {
request(app)
.post('/login')
it('redirects to the profile page when credentials are valid', function() {
var req = request(app).post('/login')
.type('form')
.send({
username: 'test-user',
password: 'test-password'
})
.end(function (err, res) {
expect(res.statusCode).to.equal(302); //TODO: This should be 303. Pending passport pull request #298
expect(res.headers.location).to.equal('/profile');
done();
});
return req.then(function(res) {
expect(res.statusCode).to.equal(302); //TODO: This should be 303. Pending passport pull request #298
expect(res.headers.location).to.equal('/profile');
});
});

it('redirects to the login page when credentials are invalid', function(done) {
request(app)
it('redirects to the login page when credentials are invalid', function() {
var req = request(app)
.post('/login')
.type('form')
.send({
username: 'bad-user',
password: 'bad-password'
})
.end(function (err, res) {
expect(res.statusCode).to.equal(302); //TODO: This should be 303. Pending passport pull request #298
expect(res.headers.location).to.equal('/login');
done();
});
return req.then(function(res) {
expect(res.statusCode).to.equal(302); //TODO: This should be 303. Pending passport pull request #298
expect(res.headers.location).to.equal('/login');
});
});
});

describe('POST /logout', function() {
it('redirects to the home page', function(done) {
request(app)
it('redirects to the home page', function() {
var req = request(app)
.post('/logout')
.type('form')
.send({})
.end(function (err, res) {
expect(res.statusCode).to.equal(303);
expect(res.headers.location).to.equal('/');
done();
});
.send({});
return req.then(function(res) {
expect(res.statusCode).to.equal(303);
expect(res.headers.location).to.equal('/');
});
})
});

describe('GET /', function () {
it('renders the home page successfully', function(done) {
request(app)
.get('/')
.end(function (err, res) {
expect(res.statusCode).to.equal(200);
expect(res.text).to.include('<title>integration title</title>');
expect(res.text).to.include('<h1 id="heading">integration heading</h1>');
expect(res.text).to.include('<em>emphasised</em>');
expect(res.text).to.include('<strong>strong</strong>');
done();
});
describe('GET /', function() {
it('renders the home page successfully', function() {
var req = request(app).get('/');
return req.then(function(res) {
expect(res.statusCode).to.equal(200);
expect(res.text).to.include('<title>integration title</title>');
expect(res.text).to.include('<h1 id="heading">integration heading</h1>');
expect(res.text).to.include('<em>emphasised</em>');
expect(res.text).to.include('<strong>strong</strong>');
});
});
});

describe('GET /post/:slug', function() {
it('renders the post page successfully', function(done) {
request(app)
.get('/post/post-slug')
.end(function(req, res) {
expect(res.text).to.include('<title>integration title</title>');
expect(res.text).to.include('<em>emphasised</em>');
expect(res.text).not.to.include('<strong>strong</strong>');
done();
});
it('renders the post page successfully', function() {
var req = request(app).get('/post/post-slug');
return req.then(function(res) {
expect(res.text).to.include('<title>integration title</title>');
expect(res.text).to.include('<em>emphasised</em>');
expect(res.text).not.to.include('<strong>strong</strong>');
});
});
});

describe('non-existent endpoint', function () {
it('gives a 404', function (done) {
request(app)
.get('/does-not-exist')
.end(function(err, res) {
describe('non-existent endpoint', function() {
it('gives a 404', function() {
var req = request(app).get('/does-not-exist');
return req.then(function(res) {
expect(res.statusCode).to.equal(404);
done();
});
});
});
});
});
@@ -166,116 +154,110 @@ describe('Integration Test', function() {
});
var app = new WriteItDown({authHandler: authHandler}).app;

describe('GET /profile', function () {
it('renders the profile page successfully', function (done) {
request(app)
.get('/profile')
.end(function(err, res) {
expect(res.statusCode).to.equal(200);
expect(res.text).to.include('<title>integration title<\/title>');
expect(res.text).to.include('Welcome, test-user!');
expect(res.text).to.include('name="confirmPassword"');
expect(res.text).to.include('<input type="submit"');
done();
});
describe('GET /profile', function() {
it('renders the profile page successfully', function() {
var req = request(app).get('/profile');
return req.then(function(res) {
expect(res.statusCode).to.equal(200);
expect(res.text).to.include('<title>integration title<\/title>');
expect(res.text).to.include('Welcome, test-user!');
expect(res.text).to.include('name="confirmPassword"');
expect(res.text).to.include('<input type="submit"');
});
});
});

describe('PUT /user/:username', function () {
it('updates the given user', function (done) {
request(app)
.post('/user/test-user')
describe('PUT /user/:username', function() {
it('updates the given user', function() {
var req = request(app).post('/user/test-user')
.type('form')
.send({
_method: 'PUT',
username: 'new-username',
password: 'new-password',
confirmPassword: 'new-password'
})
.end(function (err, res) {
expect(res.statusCode).to.equal(303);
expect(res.headers.location).to.equal('/profile');

User.find().exec().then(function(users) {
expect(users).to.have.length(1);
expect(users[0].username).to.equal('new-username');
expect(bcrypt.compareSync('new-password', users[0].password)).to.be.true;
done();
});
})
});
return req.then(function(res) {
expect(res.statusCode).to.equal(303);
expect(res.headers.location).to.equal('/profile');
return User.find().exec();
}).then(function(users) {
expect(users).to.have.length(1);
expect(users[0].username).to.equal('new-username');
expect(bcrypt.compareSync('new-password', users[0].password)).to.be.true;
});
});
});

describe('GET /write', function () {
it('renders the post creation page successfully', function(done) {
request(app)
.get('/write')
.end(function(err, res) {
expect(res.statusCode).to.equal(200);
expect(res.text).to.include('<title>integration title<\/title>');
expect(res.text).to.include('integration.com');
expect(res.text).to.include('name="slug"');
expect(res.text).to.include('<input type="submit"');
done();
});
})
describe('GET /write', function() {
it('renders the post creation page successfully', function() {
var req = request(app).get('/write');
return req.then(function(res) {
expect(res.statusCode).to.equal(200);
expect(res.text).to.include('<title>integration title<\/title>');
expect(res.text).to.include('integration.com');
expect(res.text).to.include('name="slug"');
expect(res.text).to.include('<input type="submit"');
});
});
});

describe('PUT /posts/', function() {
describe('when there is no post with the given slug', function() {
it('creates a new post and redirects to it', function(done) {
request(app)
.post('/posts/')
it('creates a new post and redirects to it', function() {
var req = request(app).post('/posts/')
.type('form')
.send({
_method: 'PUT',
title: 'New Post',
slug: 'new-post',
text: 'This is my newest post.'
})
.end(function (err, res) {
expect(res.statusCode).to.equal(303);
expect(res.headers.location).to.equal('/post/new-post');

Post.find({}).exec().then(function(posts) {
expect(posts).to.have.length(3);
return Post.findOne({slug: 'new-post'}).exec()
}).then(function(post) {
expect(post.title).to.equal('New Post');
expect(post.slug).to.equal('new-post');
expect(post.text).to.equal('This is my newest post.');
done();
});
});
return req.then(function(res) {
expect(res.statusCode).to.equal(303);
expect(res.headers.location).to.equal('/post/new-post');
return Post.find({}).exec();
}).then(function(posts) {
expect(posts).to.have.length(3);
return Post.find({slug: 'new-post'}).exec();
}).then(function(posts) {
expect(posts).to.have.length(1);
expect(posts[0].title).to.equal('New Post');
expect(posts[0].slug).to.equal('new-post');
expect(posts[0].text).to.equal('This is my newest post.');
var timeSincePosted = Date.now() - posts[0].posted;
expect(timeSincePosted).to.be.within(1, 10); //ms
});
});
});

describe('when there is an existing post with the given slug', function() {
it('overwrites the existing post', function(done) {
request(app)
.post('/posts/')
.type('form')
.send({
_method: 'PUT',
title: 'Post title (updated)',
slug: 'post-slug',
text: '*still emphasised*'
})
.end(function (err, res) {
expect(res.statusCode).to.equal(303);
expect(res.headers.location).to.equal('/post/post-slug');

Post.find({}).exec().then(function(posts) {
expect(posts).to.have.length(2);
return Post.find({slug: 'post-slug'}).exec()
}).then(function(posts) {
expect(posts).to.have.length(1);
expect(posts[0].title).to.equal('Post title (updated)');
expect(posts[0].slug).to.equal('post-slug');
expect(posts[0].text).to.equal('*still emphasised*');
done();
it('overwrites the existing post without modifying the time', function() {
var originalTime;
return Post.findOne({slug: 'post-slug'}).exec().then(function(post) {
originalTime = post.posted;
return request(app).post('/posts/')
.type('form')
.send({
_method: 'PUT',
title: 'Post title (updated)',
slug: 'post-slug',
text: '*still emphasised*'
});
});
}).then(function(res) {
expect(res.statusCode).to.equal(303);
expect(res.headers.location).to.equal('/post/post-slug');
return Post.find({}).exec();
}).then(function(posts) {
expect(posts).to.have.length(2);
return Post.find({slug: 'post-slug'}).exec();
}).then(function(posts) {
expect(posts).to.have.length(1);
expect(posts[0].title).to.equal('Post title (updated)');
expect(posts[0].slug).to.equal('post-slug');
expect(posts[0].text).to.equal('*still emphasised*');
expect(posts[0].posted.getTime()).to.equal(originalTime.getTime());
});
})
});
});

0 comments on commit 04d3f3a

Please sign in to comment.