<commit_before>var assert = require('assert');
var request = require('request');

var { URL_PREFIX, ADMIN_USER, TEST_USER } = require('../fixtures.js')

describe('auth api -- login with email', function() {

	it('succeeds', function (done) {
		const url = URL_PREFIX + `/login?action=login&ajax=1&email=${ADMIN_USER.email}&md5=${ADMIN_USER.md5}`;
		request.get(url, function(error, response, body) {
			assert.ifError(error);
			assert.equal(response.statusCode, 200);
			const cookies = ((response.headers || {})['set-cookie'] || []).join(' ');
			assert(/whydSid\=/.test(cookies));
			assert(JSON.parse(body).redirect);
			done();
		});
	});

	it('gives access to personal /stream', function (done) {
		const url = URL_PREFIX + `/login?action=login&ajax=1&email=${ADMIN_USER.email}&md5=${ADMIN_USER.md5}`;
		request.get(url, function(error, response, body) {
			assert.ifError(error);
			assert.equal(response.statusCode, 200);

			const cookie = request.cookie((response.headers || {})['set-cookie'][0]);
			const jar = request.jar();
			jar.setCookie(cookie, URL_PREFIX);
			request({ jar, url: URL_PREFIX + '/stream?format=json' }, function(error, response, body) {
				assert.ifError(error);
				assert.equal(response.statusCode, 200);
				var json = JSON.parse(body);
				assert.ifError(json.error);
				assert(json.join); // check that it's an array
				done();
			});
		});
	});

	it('fails if wrong email', function (done) {
		const url = URL_PREFIX + `/login?action=login&ajax=1&email=<EMAIL>&md5=${ADMIN_USER.md5}`;
		request.get(url, function(error, response, body) {
			assert.ifError(error);
			assert(/email/.test(JSON.parse(body).error));
			done();
		});
	});

	it('fails if wrong password', function (done) {
		const url = URL_PREFIX + `/login?action=login&ajax=1&email=${ADMIN_USER.email}&md5=aaa`;
		request.get(url, function(error, response, body) {
			assert.ifError(error);
			assert(JSON.parse(body).wrongPassword);
			done();
		});
	});
});<commit_msg>api test refacto: `loginAs()`<commit_after>var assert = require('assert');
var request = require('request');

var { URL_PREFIX, ADMIN_USER, TEST_USER } = require('../fixtures.js')

function loginAs(user, callback) {
	const url = URL_PREFIX + `/login?action=login&ajax=1&email=${user.email}&md5=${user.md5}`;
	request.get(url, function(error, response, body) {
		assert.ifError(error);
		assert.equal(response.statusCode, 200);
		const jar = request.jar();
		if (((response.headers || {})['set-cookie'] || []).length) {
			jar.setCookie(request.cookie(response.headers['set-cookie'][0]), URL_PREFIX);
		}
		callback(error, { response, body, jar });
	});
}

describe('auth api -- login with email', function() {

	it('succeeds', function (done) {
		loginAs(ADMIN_USER, function(error, { response, body }) {
			const cookies = ((response.headers || {})['set-cookie'] || []).join(' ');
			assert(/whydSid\=/.test(cookies));
			assert(JSON.parse(body).redirect);
			done();
		});
	});

	it('gives access to personal /stream', function (done) {
		loginAs(ADMIN_USER, function(error, { response, body, jar }) {
			request({ jar, url: URL_PREFIX + '/stream?format=json' }, function(error, response, body) {
				assert.ifError(error);
				assert.equal(response.statusCode, 200);
				var json = JSON.parse(body);
				assert.ifError(json.error);
				assert(json.join); // check that it's an array
				done();
			});
		});
	});

	it('fails if wrong email', function (done) {
		loginAs(Object.assign({}, ADMIN_USER, { email: 'qq' }), function(error, { response, body }) {
			assert(/email/.test(JSON.parse(body).error));
			done();
		});
	});

	it('fails if wrong password', function (done) {
		loginAs(Object.assign({}, ADMIN_USER, { md5: 'qq' }), function(error, { response, body }) {
			assert(JSON.parse(body).wrongPassword);
			done();
		});
	});
});
{'fraction_non_alphanumeric': 0.19973821989528795, 'fraction_numerical': 0.008376963350785341, 'mean_word_length': 4.58625730994152, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '2116987', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1293, 'n_words': 248}