<commit_before>'use strict';

const chai = require('chai');
const chaiHttp = require('chai-http');
chai.use(chaiHttp);

const login = (app) => {
    let agent = chai.request.agent(app); // create agent for storing cookies
    return new Promise((resolve, reject) => {
        agent
            .post('/login/')
            .send({'username': '<EMAIL>', 'password': <PASSWORD>})
            .end((err, res) => {
                if (err) {
                    reject(err);
                }

                // return agent for making further request in loggedIn state
                resolve({
                    agent,
                    res
                });
            });
    });
};

module.exports = {
    login
};<commit_msg>Modify mocha test to use carf token<commit_after>"use strict";

const chai = require("chai");
const chaiHttp = require("chai-http");
chai.use(chaiHttp);

const login = app => {
	let agent = chai.request.agent(app); // create agent for storing cookies

	return new Promise((resolve, reject) => {
		getCsrfToken(agent).then(csrf => {
			
			agent
				.post("/login/")
				.send({
					username: "<EMAIL>",
                    password: <PASSWORD>, 
                    _csrf: csrf
				})
				.end((err, res) => {
					if (err) {
						reject(err);
					}

					// return agent for making further request in loggedIn state
					resolve({
						agent,
						res
					});
				});
		});
	});
};

const getLoginPage = agent => {
	return new Promise((resolve, reject) => {
		agent.get("/login/").end((err, res) => {
			if (err) {
				reject(err);
			}

			// return agent for making further request in loggedIn state
			resolve({
				agent,
				res
			});
		});
	});
};

const extractCsrf = string => {
	if (!string) return undefined;
	let result = string.split('<meta name="csrfToken" content="', 2)[1];
	result = result.split('">', 2)[0];
	console.log(result);
	return result;
};

const getCsrfToken = agent => {
	return new Promise((resolve, reject) => {
		getLoginPage(agent).then(({ res }) => {
			let csrf = extractCsrf(res.text);
			if (csrf) {
				resolve(csrf);
			} else {
				reject(csrf);
			}
		});
	});
};

module.exports = {
	login
};

{'fraction_non_alphanumeric': 0.16306844281120808, 'fraction_numerical': 0.0018373909049150207, 'mean_word_length': 2.0981507823613086, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 22, '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': '4312548', 'n_tokens_mistral': 795, 'n_tokens_neox': 677, 'n_words': 168}