<commit_before>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');
			});
		});
	});
});<commit_msg>improve the authentication tests<commit_after>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 = sinon.stub();
			req.header = function(){};
			sinon.stub(req, 'header').withArgs('usertoken').returns('TOKEN');
			req.isAuthenticated = function(){ return false;};

			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 = sinon.stub();
			req.header = function(){};
			sinon.stub(req, 'header').withArgs('usertoken').returns(null);
			req.isAuthenticated = function(){ return false;};

			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 = sinon.stub();
			req.header = function(){};
			sinon.stub(req, 'header').withArgs('usertoken').returns('TOKEN');
			req.isAuthenticated = function(){ return false;};

			auth.ensure(req, res, function() {
				assert(false, 'should not call next');
			});
		});
	});
});
{'fraction_non_alphanumeric': 0.1857666143380429, 'fraction_numerical': 0.0, 'mean_word_length': 3.8887468030690537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4001592', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1199, 'n_words': 286}