Last active
August 29, 2015 14:02
-
-
Save kevinmstephens/57447f5a374bfbb850eb to your computer and use it in GitHub Desktop.
Superagent testing of notes app user routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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