Skip to content

Instantly share code, notes, and snippets.

@kevinmstephens
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmstephens/57447f5a374bfbb850eb to your computer and use it in GitHub Desktop.
Save kevinmstephens/57447f5a374bfbb850eb to your computer and use it in GitHub Desktop.
Superagent testing of notes app user routes
var superagent = require('superagent');
var chai = require('chai'),
expect = chai.expect,
should = chai.should();
var app = require('../../server.js').app;
describe('Notes JSON api', function () {
var email;
var password;
var jwt_token;
it('can succesfully create a new user', function (done) {
email = 'kevinmstephens' + parseInt(Math.random()*100) + '@gmail.com';
password = 'pwpwpw' + parseInt(Math.random()*100) + '!';
superagent.post('http://localhost:3000/api/v1/users')
.send({
email: email,
password: password
})
.end(function (err, res) {
jwt_token = res.body.jwt_token;
expect(err).to.be.null;
expect(res.body.jwt_token).to.not.be.null;
done();
});
});
it('cant create user that already exists', function (done) {
superagent.post('http://localhost:3000/api/v1/users')
.send({
email: email,
password: 'abldags'
})
.end(function (err, res) {
expect(err).to.be.null;
expect(res.status).to.equal(401);
expect(res.body.msg).to.be.a('string');
done();
});
});
it('will assign a new token when user re-authenticates', function (done) {
superagent.get('http://localhost:3000/api/v1/users')
.auth(email, password)
.end(function (err, res) {
expect(err).to.be.null;
expect(res.body.jwt_token).to.be.a('string');
expect(res.body.jwt_token).to.not.equal(jwt_token);
console.log(res.body.jwt_token);
console.log(jwt_token);
done();
});
});
it('will reject bad password for existing user', function (done) {
superagent.get('http://localhost:3000/api/v1/users')
.auth(email, 'badpassword')
.end(function (err, res) {
expect(err).to.be.null;
expect(res.body.jwt_token).to.be.undefined;
expect(res.status).to.equal(401);
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment