it('should not be able to save an article if not logged in', function (done) { Server.inject({ method: 'POST', url: '/articles', payload: article }, function (response) { Code.expect(response.statusCode, response.result.message).to.equal(401); done(); }); });
experiment('getting started with hapi testing', () => { test('test if the route returns JSON by default', done => { const options = { method: 'GET', url: '/' } server.inject(options, response => { const payload = JSON.parse(response.payload || '{}') Code.expect(response.statusCode).to.equal(200) Code.expect(payload.name).to.equal('Marcus') Code.expect(payload.isDeveloper).to.equal(true) done() }) }) })
test('Should not update user data: url is wrong', function (done) { User.run().then(function (users) { const options = { url: '/profile', method: 'POST', payload: { url: '123' }, credentials: users[ 0 ] } server.inject(options, (reply) => { Code.expect(reply.statusCode).to.equal(400) done() }) }) })
it('should return 404 page for single article which doesnt exist, if not signed in', function (done) { Server.inject({ method: 'GET', url: '/articles/test' }, function (response) { // Set assertion Code.expect(response.result).to.be.a.string(). and.to.include('Page Not Found'); // Call the assertion callback done(); }); });
test('test user create', function (done) { const options = { url: '/signup', method: 'POST', payload: { email: 'info@futurestud.io', password: 'password' } } server.inject(options, () => { User.findByEmail('info@futurestud.io').then(function (user) { Code.expect(user).to.be.an.object() done() }) }) })
it('should be able to get a list of articles if not signed in', function (done) { // Save the article Article.create(article, function (err, article) { if (err) return done(err); // Request articles Server.inject({ method: 'GET', url: '/articles' }, function (response) { // Set assertion Code.expect(response.result).to.be.an.array().and.to.have.length(1); // Call the assertion callback done(); }); }); });
it('should be able to get a single article if not signed in', function (done) { // Save the article Article.create(article, function (err, article) { if (err) return done(err); Server.inject({ method: 'GET', url: '/articles/' + article.id.toString() }, function (response) { // Set assertion // For some reason it returns document object instead of normal object Code.expect(response.result.toJSON()).to.be.an.object(). and.to.include({'title': article.title}); // Call the assertion callback done(); }); }); });
test('Should not create user without email', function (done) { const options = { url: '/signup', method: 'POST', payload: { password: 'password' } } server.inject(options, (reply) => { Code.expect(reply.statusCode).to.equal(400) User.run().then(function (users) { Code.expect(users.length).to.equal(1) done() }) }) })
it('should not be able to save an article if no title is provided', function (done) { // Invalidate title field article.title = ''; // Save a new article Server.inject({ method: 'POST', url: '/articles', payload: article, credentials: user }, function (response) { Code.expect(response.statusCode, response.result.message).to.equal(400); // Set message assertion Code.expect(response.result.message).to.equal('Title cannot be blank'); // Handle article save error done(); }); });
test('Should not create user without password', function (done) { const options = { url: '/signup', method: 'POST', payload: { email: 'info@futurestud.io' } } server.inject(options, (reply) => { Code.expect(reply.statusCode).to.equal(400) User.run().then(function (users) { Code.expect(users.length).to.equal(1) done() }) }) })
test('Should not change password: old password wrong', function (done) { User.run().then(function (users) { const options = { url: '/profile/change-password', method: 'POST', payload: { old_password: 'wrongpassword', new_password: 'doesntmatter', confirm_new_password: 'doesntmatter' }, credentials: users[ 0 ] } server.inject(options, (reply) => { Code.expect(reply.statusCode).to.equal(400) done() }) }) })
test('Should not change password: confirm is different to new', function (done) { User.run().then(function (users) { const options = { url: '/profile/change-password', method: 'POST', payload: { old_password: 'password', new_password: 'newpassword', confirm_new_password: 'newpassword-wrong' }, credentials: users[ 0 ] } server.inject(options, (reply) => { Code.expect(reply.statusCode).to.equal(400) done() }) }) })
test('Should change the password', function (done) { User.run().then(function (users) { const options = { url: '/profile/change-password', method: 'POST', payload: { old_password: 'password', new_password: 'newpassword', confirm_new_password: 'newpassword' }, credentials: users[ 0 ] } server.inject(options, (reply) => { Code.expect(reply.statusCode).to.equal(200) done() }) }) })
test('Should not create a second user with the same email address', function (done) { const options = { url: '/signup', method: 'POST', payload: { email: 'info@futurestud.io', password: 'password' } } server.inject(options, (reply) => { Code.expect(reply.statusCode).to.equal(409) User.run().then(function (users) { Code.expect(users.length).to.equal(1) done() }) }) })
test('Should not change password: new too short', function (done) { User.run().then(function (users) { const options = { url: '/profile/change-password', method: 'POST', payload: { old_password: 'password', new_password: 'short', confirm_new_password: 'short' }, credentials: users[ 0 ] } server.inject(options, (reply) => { Code.expect(reply.statusCode).to.equal(400) done() }) }) })