<commit_before>'use strict';

const DmmAccount = require('../../scripts/model/dmmAccount');
const sinon = require('sinon');
const rp = require('request-promise');
const sprintf = require("sprintf-js").sprintf
const async = require('async');

describe('DMM account', function() {

	it('empty email', sinon.test(function() {
		var account = new DmmAccount('', '1234');
		expectError(account);
		account = new DmmAccount('     ', '1234');
		expectError(account);
	}))

	it('empty password', sinon.test(function() {
		var email = '<EMAIL>';
		var account = new DmmAccount(email, '');
		expectError(account);
		account = new DmmAccount(email, '      ');
		expectError(account);
	}))

	it('scrape login token from DMM login page', sinon.test(function() {
		var procedure = this.stub(async, 'waterfall');
		
		var account = new DmmAccount('<EMAIL>', '1234');
		account.login();
		
		var task = procedure.firstCall.args[0][0];
		var loginToken = mockSrapeLoginPageToken();
		var httpRequest = this.stub(rp, 'get').returns(loginToken.response);

		var spyDone = this.spy();
		task(spyDone);
		assert.isTrue(spyDone.calledOnce);

		var returnVal = spyDone.firstCall.args;
		assert.isNull(returnVal[0]);
		assert.equal(returnVal[1], loginToken.fakeToken.dmmToken);
		assert.equal(returnVal[2], loginToken.fakeToken.dataToken);

		var httpParam = httpRequest.firstCall.args[0];
		assert.include(httpParam.uri, 'https://www.dmm.com/my/-/login');
	}))

	it('authroize token', sinon.test(function() {
		var procedure = this.stub(async, 'waterfall');
		
		var account = new DmmAccount('<EMAIL>', '1234');
		account.login();
		
		var task = procedure.firstCall.args[0][1];
		var authroizedToken = mockAuthorizeDmmToken();
		var httpRequest = this.stub(rp, 'post').returns(authroizedToken.response);

		var spyDone = this.spy();
		var fake_dmm_token = 'dmm_token', fake_data_token = 'data_token';
		task(fake_dmm_token, fake_data_token, spyDone);
		assert.isTrue(spyDone.calledOnce);

		var returnVal = spyDone.firstCall.args;
		assert.isNull(returnVal[0]);
		assert.equal(returnVal[1].token, authroizedToken.fakeToken.token);
		assert.equal(returnVal[1].login_id, authroizedToken.fakeToken.login_id);
		assert.equal(returnVal[1].password, authroizedToken.fakeToken.password);

		var httpParam = httpRequest.firstCall.args[0];
		assert.include(httpParam.uri, 'https://www.dmm.com/my/-/login/ajax-get-token');
		assert.equal(httpParam.headers['DMM_TOKEN'], fake_dmm_token);
		assert.match(httpParam.headers['x-requested-with'], /XMLHttpRequest/i);
		assert.equal(httpParam.form['token'], fake_data_token);
	}))

	it('authentication success', sinon.test(function() {
		var procedure = this.stub(async, 'waterfall');
		
		var email = '<EMAIL>', password = '<PASSWORD>';
		var account = new DmmAccount(email, password);
		account.login();
		
		var task = procedure.firstCall.args[0][2];
		var auth = mockAuthentication(true);
		var httpRequest = this.stub(rp, 'post').returns(auth.response);

		var spyDone = this.spy();
		var tokenJson = {
			token: 'a',
			login_id: 'b',
			password: 'c'
		}
		task(JSON.stringify(tokenJson), spyDone);
		assert.isTrue(spyDone.calledOnce);

		var returnVal = spyDone.firstCall.args;
		assert.isNull(returnVal[0], 'should have no errors (be null)');
		assert.isBoolean(returnVal[1], 'should indicate whether the login is success (email and password are correct)');
		assert.isTrue(returnVal[1], 'success should be true');
		assert.equal(returnVal[2], auth.fakeCookie, 'cookie should match and not altered');

		var httpParam = httpRequest.firstCall.args[0];
		assert.startsWith(httpParam.uri, 'https://www.dmm.com/my/-/login/auth');
		assertRememberMe();
		assertToken();


		function assertRememberMe() {
			['save_login_id', 'save_password', 'use_auto_login'].forEach(function(prop) {
				var msg = sprintf('inside http POST form should have property "%s" with integer either 0 or 1', prop);
				assert.isTrue(httpParam.form.hasOwnProperty(prop), msg);
				assert.isAtLeast(httpParam.form[prop], 0);
				assert.isAtMost(httpParam.form[prop], 1);
			});
		}

		function assertToken() {
			assert.equal(httpParam.form.token, tokenJson.token, 'token should be put into form.token');
			assert.equal(httpParam.form.login_id, email, 'email should be put into form.login_id');
			assert.equal(httpParam.form.password, password, 'password should be put into form.password');

			assert.isTrue(httpParam.form.hasOwnProperty(tokenJson.login_id), 'should have property named from token.login_id');
			assert.equal(httpParam.form[tokenJson.login_id], email, 'should have the same value as email address');

			assert.isTrue(httpParam.form.hasOwnProperty(tokenJson.password), 'should have property named from token.password');
			assert.equal(httpParam.form[tokenJson.password], password, 'should have the same value as password');
		}	
	})) 
})

function expectError(account) {<commit_msg>[refactor] stub async.waterfall() and login account before each tests<commit_after>'use strict';

const DmmAccount = require('../../scripts/model/dmmAccount');
const sinon = require('sinon');
const rp = require('request-promise');
const sprintf = require("sprintf-js").sprintf
const async = require('async');

describe('DMM account', function() {

	var taskList, fakeAccount;
	beforeEach(sinon.test(function() {
		var procedure = this.stub(async, 'waterfall');
		fakeAccount = new DmmAccount('<EMAIL>', '1234');
		fakeAccount.login();
		taskList = procedure.firstCall.args[0];
	}))

	it('empty email', sinon.test(function() {
		var account = new DmmAccount('', '1234');
		expectError(account);
		account = new DmmAccount('     ', '1234');
		expectError(account);
	}))

	it('empty password', sinon.test(function() {
		var email = '<EMAIL>';
		var account = new DmmAccount(email, '');
		expectError(account);
		account = new DmmAccount(email, '      ');
		expectError(account);
	}))

	it('scrape login token from DMM login page', sinon.test(function() {
		
		var scrpeTask = taskList[0];
		var loginToken = mockSrapeLoginPageToken();
		var httpRequest = this.stub(rp, 'get').returns(loginToken.response);

		var spyDone = this.spy();
		scrpeTask(spyDone);
		assert.isTrue(spyDone.calledOnce);

		var returnVal = spyDone.firstCall.args;
		assert.isNull(returnVal[0]);
		assert.equal(returnVal[1], loginToken.fakeToken.dmmToken);
		assert.equal(returnVal[2], loginToken.fakeToken.dataToken);

		var httpParam = httpRequest.firstCall.args[0];
		assert.include(httpParam.uri, 'https://www.dmm.com/my/-/login');
	}))

	it('authroize token', sinon.test(function() {
		var taskAuthorize = taskList[1];
		var authroizedToken = mockAuthorizeDmmToken();
		var httpRequest = this.stub(rp, 'post').returns(authroizedToken.response);

		var spyDone = this.spy();
		var fake_dmm_token = 'dmm_token', fake_data_token = 'data_token';
		taskAuthorize(fake_dmm_token, fake_data_token, spyDone);
		assert.isTrue(spyDone.calledOnce);

		var returnVal = spyDone.firstCall.args;
		assert.isNull(returnVal[0]);
		assert.equal(returnVal[1].token, authroizedToken.fakeToken.token);
		assert.equal(returnVal[1].login_id, authroizedToken.fakeToken.login_id);
		assert.equal(returnVal[1].password, <PASSWORD>roizedToken.fakeToken.password);

		var httpParam = httpRequest.firstCall.args[0];
		assert.include(httpParam.uri, 'https://www.dmm.com/my/-/login/ajax-get-token');
		assert.equal(httpParam.headers['DMM_TOKEN'], fake_dmm_token);
		assert.match(httpParam.headers['x-requested-with'], /XMLHttpRequest/i);
		assert.equal(httpParam.form['token'], fake_data_token);
	}))

	it('authentication success', sinon.test(function() {
		var taskAuthenticate = taskList[2];
		var auth = mockAuthentication(true);
		var httpRequest = this.stub(rp, 'post').returns(auth.response);

		var spyDone = this.spy();
		var tokenJson = {
			token: 'a',
			login_id: 'b',
			password: 'c'
		}
		taskAuthenticate(JSON.stringify(tokenJson), spyDone);
		assert.isTrue(spyDone.calledOnce);

		var returnVal = spyDone.firstCall.args;
		assert.isNull(returnVal[0], 'should have no errors (be null)');
		assert.isBoolean(returnVal[1], 'should indicate whether the login is success (email and password are correct)');
		assert.isTrue(returnVal[1], 'success should be true');
		assert.equal(returnVal[2], auth.fakeCookie, 'cookie should match and not altered');

		var httpParam = httpRequest.firstCall.args[0];
		assert.startsWith(httpParam.uri, 'https://www.dmm.com/my/-/login/auth');
		assertRememberMe();
		assertToken();


		function assertRememberMe() {
			['save_login_id', 'save_password', 'use_auto_login'].forEach(function(prop) {
				var msg = sprintf('inside http POST form should have property "%s" with integer either 0 or 1', prop);
				assert.isTrue(httpParam.form.hasOwnProperty(prop), msg);
				assert.isAtLeast(httpParam.form[prop], 0);
				assert.isAtMost(httpParam.form[prop], 1);
			});
		}

		function assertToken() {
			assert.equal(httpParam.form.token, tokenJson.token, 'token should be put into form.token');
			assert.equal(httpParam.form.login_id, fakeAccount.email, 'email should be put into form.login_id');
			assert.equal(httpParam.form.password, <PASSWORD>, 'password should be put into form.password');

			assert.isTrue(httpParam.form.hasOwnProperty(tokenJson.login_id), 'should have property named from token.login_id');
			assert.equal(httpParam.form[tokenJson.login_id], fakeAccount.email, 'should have the same value as email address');

			assert.isTrue(httpParam.form.hasOwnProperty(tokenJson.password), 'should have property named from token.password');
			assert.equal(httpParam.form[tokenJson.password], fakeAccount.password, 'should have the same value as password');
		}	
	})) 
})

function expectError(account) {
{'fraction_non_alphanumeric': 0.15191831683168316, 'fraction_numerical': 0.007632013201320132, 'mean_word_length': 5.176433121019108, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 6, 'lorem ipsum': 0, 'www.': 6, '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': '7365677', 'n_tokens_mistral': 3416, 'n_tokens_neox': 3125, 'n_words': 638}