var port = require('../../server').port;

var chai = require('chai');
var chaihttp = require('chai-http');
chai.use(chaihttp);
var expect = chai.expect;

var firstID, secondID;

describe('The users REST API', function() {

	it('Can create a new user', function(done) {

		chai.request('http://localhost:' + port)
		.post('/api/0_0_1/users')
		.req(function(req) {
			req.send({
				"email": "fakeaddress@fake.com",
				"password": "123"
			});
		})
		.res(function(res) {
			expect(res).to.have.status(200);
			firstID = res.body._id;
			done();
		});

	});

	it('Returns a JWT', function(done) {

		chai.request('http://localhost:' + port)
		.post('/api/0_0_1/users')
		.req(function(req) {
			req.send({
				"email": "fakeaddress2@fake.com",
				"password": "123"
			});
		})
		.res(function(res) {
			expect(res).to.have.status(200);
			expect(res.body).to.have.property('jwt');
			secondID = res.body._id;
			done();
		});

	});

	it('Can update a user', function(done) {
		chai.request('http://localhost:' + port)
		.put('/api/0_0_1/users/' + secondID)
		.req(function(req) {
			req.send({
				basic: {
					"email": "fakeaddress3@fake.com"
				}
			});
		})
		.res(function(res) {
			expect(res).to.have.status(202);
			expect(res.body.basic.email).to.eql('fakeaddress3@fake.com');
			done();
		});

	});

	it('Rejects duplicate users', function(done) {

		chai.request('http://localhost:' + port)
		.post('/api/0_0_1/users')
		.req(function(req) {
			req.send({
				"email": "fakeaddress@fake.com",
				"password": "123"
			});
		})
		.res(function(res) {
			expect(res).to.have.status(401);
			expect(res.body.message).to.eql('cannot create user');
			done();
		});

	});

	it('Can delete users', function(done) {

		chai.request('http://localhost:' + port)
		.del('/api/0_0_1/users/' + firstID)
		.res(function(res) {
			expect(res).to.have.status(200);
			expect(res.body.message).to.eql('deleted');
		});

		chai.request('http://localhost:' + port)
		.del('/api/0_0_1/users/' + secondID)
		.res(function(res) {
			expect(res).to.have.status(200);
			expect(res.body.message).to.eql('deleted');
			done();
		});

	});

});
@@ -159,25 +159,26 @@
 firstID,
- 
+%0A%09
 secondID
 ;%0A%0Adescr
@@ -169,16 +169,22 @@
 secondID
+,%0A%09jwt
 ;%0A%0Adescr
@@ -196,22 +196,16 @@
 The 
-users 
 REST API
 ', f
@@ -200,16 +200,26 @@
 REST API
+ for users
 ', funct
@@ -886,16 +886,39 @@
 'jwt');%0A
+%09%09%09jwt = res.body.jwt;%0A
 %09%09%09secon
@@ -953,32 +953,429 @@
 );%0A%09%09%7D);%0A%0A%09%7D);%0A%0A
+%09it('Can use that JWT to access the authenticated data API', function(done) %7B%0A%0A%09%09chai.request('http://localhost:' + port)%0A%09%09.get('/api/0_0_1/data')%0A%09%09.req(function(req) %7B%0A%09%09%09var testCredentials = %7B%7D;%0A%09%09%09testCredentials.jwt = jwt;%0A%09%09%09req.send(testCredentials);%0A%09%09%7D)%0A%09%09.res(function(res) %7B%0A%09%09%09expect(res).to.have.status(200);%0A%09%09%09expect(Array.isArray(res.body)).to.eql(true);%0A%09%09%09done();%0A%09%09%7D);%0A%0A%09%7D);%0A%0A
 %09it('Can update 
@@ -1400,16 +1400,17 @@
 done) %7B%0A
+%0A
 %09%09chai.r

Use the JWT to test that I can access the authenticated data API