var auth = require('../lib/authentication.js'),
	users = require('../lib/users.js'),
	assert = require('assert'),
	sinon = require('sinon');

describe('authentication', function() {
	describe('when request is authenticated', function() {
		it('should call next', function(done) {
			var res = {};
			var req = { isAuthenticated : function() { return true; }};
			auth.ensure(req, res, done);
		});
	});

	describe('when request not authenticated but has valid token', function() {
		beforeEach(function() {
			sinon.stub(users,'byToken').yields(null, 'USER');
		});

		afterEach(function() {
			users.byToken.restore();
		});

		it('should call next', function(done) {
			var res = {};
			var req = { 
				isAuthenticated : function() { return false; },
				header: function(name) { return 'TOKEN'; }
			};

			auth.ensure(req, res, done);
		});
	});

	describe('when request not authenticated and has no token', function() {
		it('should redirect', function(done) {
			var res = { redirect : function(path) { done() }};
			var req = { 
				isAuthenticated : function() { return false; },
				header: function(name) { return null; }
			};

			auth.ensure(req, res, function() {
				assert(false, 'should not call next');
			});
		});
	});

	describe('when request not authenticated but has invalid token', function() {
		beforeEach(function() {
			sinon.stub(users,'byToken').yields(null, null);
		});

		afterEach(function() {
			users.byToken.restore();
		});

		it('should call redirect', function(done) {
			var res = { redirect : function(path) { done() }};
			var req = { 
				isAuthenticated : function() { return false; },
				header: function(name) { return 'TOKEN'; }
			};

			auth.ensure(req, res, function() {
				assert(false, 'should not call next');
			});
		});
	});
});
@@ -696,74 +696,145 @@
 q = 
-%7B %0A%09%09%09%09isAuthenticated : function() %7B return false; %7D,%0A%09%09%09%09header:
+sinon.stub();%0A%09%09%09req.header = function()%7B%7D;%0A%09%09%09sinon.stub(req, 'header').withArgs('usertoken').returns('TOKEN');%0A%09%09%09req.isAuthenticated =
  fun
@@ -831,38 +831,33 @@
 ated = function(
-name) 
+)
 %7B return 'TOKEN'
@@ -845,38 +845,30 @@
 n()%7B return 
-'TOKEN'; %7D%0A%09%09%09
+false;
 %7D;%0A%0A%09%09%09auth.
@@ -1089,74 +1089,142 @@
 q = 
-%7B %0A%09%09%09%09isAuthenticated : function() %7B return false; %7D,%0A%09%09%09%09header:
+sinon.stub();%0A%09%09%09req.header = function()%7B%7D;%0A%09%09%09sinon.stub(req, 'header').withArgs('usertoken').returns(null);%0A%09%09%09req.isAuthenticated =
  fun
@@ -1221,38 +1221,33 @@
 ated = function(
-name) 
+)
 %7B return null; %7D
@@ -1243,19 +1243,14 @@
 urn 
-null; %7D%0A%09%09%09
+false;
 %7D;%0A%0A
@@ -1664,32 +1664,33 @@
 h) %7B done() %7D%7D;%0A
+%0A
 %09%09%09var req = %7B %0A
@@ -1690,74 +1690,145 @@
 q = 
-%7B %0A%09%09%09%09isAuthenticated : function() %7B return false; %7D,%0A%09%09%09%09header:
+sinon.stub();%0A%09%09%09req.header = function()%7B%7D;%0A%09%09%09sinon.stub(req, 'header').withArgs('usertoken').returns('TOKEN');%0A%09%09%09req.isAuthenticated =
  fun
@@ -1837,14 +1837,9 @@
 ion(
-name) 
+)
 %7B re
@@ -1847,22 +1847,14 @@
 urn 
-'TOKEN'; %7D%0A%09%09%09
+false;
 %7D;%0A%0A

improve the authentication tests