<commit_before>import should from 'should'; // monkeypatch the world~!1

import Particle from '../src/Particle';
import Defaults from '../src/Defaults';
import Client from '../src/Client';
import { createServer } from 'http';
import EventStream from '../src/EventStream';
import FakeAgent from './FakeAgent';
import {sinon, expect} from './test-setup';

let api;
let server;

const props = {
	url: 'http://www.zombo.com/',
	password: '<PASSWORD>',
	data: { sentient: true },
	isPrivate: true,
	username: 'test-user',
	argument: 'noThanks',
	shouldUpdate: 'duh',
	name: 'specialName',
	productId: '9001',
	deviceId: '1337',
	key: 'c1a55e5',
	event: 'main',
	token: 'Y',
	signal: '1',
	auth: 'X',
	accountInfo : { first_name: 'John', last_name: 'Scully', business_account: true, company_name: 'Apple Inc.' },
	files: {
		'app.ino': new Buffer('void() {}\nsetup() {}\n')
	},
	targetVersion: '0.4.7',
	requestType: 'GET',
	headers: {
		test: 'header'
	},
	query: {
		q: 'p'
	},
	form: {
		f: 'd'
	},
	json: {
		j: 'd'
	},
	body: '{{data}}',
	responseTopic: 'topic',
	responseTemplate: 'template',
	webhookAuth: {
		username: 'u',
		password: 'p'
	},
	rejectUnauthorized: true,
	noDefaults: true,
	hookId: 'hook-1234567890',
	integrationId: 'integration-1234567890',
	clientId: 'client-123',
	type: 'web',
	redirect_uri: 'https://example.com',
	scope: '',
	deviceName: 'test-device',
	page: 5,
	perPage: 50,
	sortAttr: 'deviceId',
	sortDir: 'asc',
	deny: false,
	deviceIds: ['abc', 'xyz'],
	development: false,
	notes: 'A fancy Photon',
	desiredFirmwareVersion: 42,
	flash: false,
	version: 42,
	title: 'prod',
	description: 'ready for production',
	file: new Buffer('ELF...'),
	country: 'RO',
	iccid: '1234567890',
	iccids: ['1234567890', '9876543210'],
	serialNumber: 'PH-123456',
	settings: {
		url: 'http://example.com',
	},
};

const product = 'ze-product-v1';

const propsWithProduct = Object.assign({ product }, props);

class Common {

	static expectCredentials({ form }) {
		form.username.should.equal(props.username);
		form.password.should.equal(<PASSWORD>);
	}
	static expectDeviceUrlAndToken(results) {
		results.uri.should.containEql(props.deviceId);
		results.auth.should.equal(props.auth);
	}
}

describe('ParticleAPI', () => {
	beforeEach(() => {
		api = new Particle();
	});

	describe('operations', () => {
		beforeEach(() => {
			api.agent = new FakeAgent();
		});

		describe('constructor', () => {
			it('sets the defaults', () => {
				Object.keys(Defaults).forEach((setting) => {
					api[setting].should.equal(Defaults[setting]);
				});
			});
		});
		describe('trackingIdentity', () => {
			const context = { tool: { name: 'cli', version:'1.2.3' } };
			it('full', () => {
				return api.trackingIdentity({auth: 'X', full: true, context})
					.then((req) => {
						expect(req).to.have.property('uri').eql('/v1/user/identify');
						expect(req).to.have.property('method').eql('get');
						expect(req).to.have.property('query').eql(undefined);
						expect(req).to.have.property('context').eql(context);
						expect(req).to.have.property('auth').eql('X');
					});
			});
			it('id only', () => {
				return api.trackingIdentity().then(req => {
					expect(req).to.have.property('uri').eql('/v1/user/identify');
					expect(req).to.have.property('method').eql('get');
					expect(req).to.have.property('context').eql({});
					expect(req).to.have.property('query').eql({tracking:1});
				});
			});
		});
		describe('.login', () => {
			it('sends credentials', () => {
				return api.login(props).then(Common.expectCredentials);
			});
		});
		describe('.loginAsClientOwner', () => {
			it('sends client ID and secret', () => {
				let clientApi = new Particle({
					clientId: 'foo',
					clientSecret: 'bar'
				});
				clientApi.agent = new FakeAgent();
				return clientApi.loginAsClientOwner({}).then(req => {
					expect(req.form).to.have.property('client_id').eql('foo');
					expect(req.form).to.have.property('client_secret').eql('bar');
					expect(req.form).to.have.property('grant_type').eql('client_credentials');
				});
			});
		});
		describe('.createCustomer', () => {
			it('sends client ID and secret', () => {
				let clientApi = new Particle({
					clientId: 'foo',
					clientSecret: 'bar'
				});
				clientApi.agent = new FakeAgent();
				return clientApi.loginAsClientOwner({}).then(req => {
					expect(req.form).to.have.property('client_id').eql('foo');
					expect(req.form).to.have.property('client_secret').eql('bar');
					expect(req.form).to.have.property('grant_type').eql('client_credentials');
				});<commit_msg>Add tests fo MFA methods<commit_after>import should from 'should'; // monkeypatch the world~!1

import Particle from '../src/Particle';
import Defaults from '../src/Defaults';
import Client from '../src/Client';
import { createServer } from 'http';
import EventStream from '../src/EventStream';
import FakeAgent from './FakeAgent';
import {sinon, expect} from './test-setup';

let api;
let server;
let mfa;

const props = {
	url: 'http://www.zombo.com/',
	password: '<PASSWORD>',
	data: { sentient: true },
	isPrivate: true,
	username: 'test-user',
	argument: 'noThanks',
	shouldUpdate: 'duh',
	name: 'specialName',
	productId: '9001',
	deviceId: '1337',
	key: 'c1a55e5',
	event: 'main',
	token: 'Y',
	signal: '1',
	auth: 'X',
	accountInfo : { first_name: 'John', last_name: 'Scully', business_account: true, company_name: 'Apple Inc.' },
	files: {
		'app.ino': new Buffer('void() {}\nsetup() {}\n')
	},
	targetVersion: '0.4.7',
	requestType: 'GET',
	headers: {
		test: 'header'
	},
	query: {
		q: 'p'
	},
	form: {
		f: 'd'
	},
	json: {
		j: 'd'
	},
	body: '{{data}}',
	responseTopic: 'topic',
	responseTemplate: 'template',
	webhookAuth: {
		username: 'u',
		password: 'p'
	},
	rejectUnauthorized: true,
	noDefaults: true,
	hookId: 'hook-1234567890',
	integrationId: 'integration-1234567890',
	clientId: 'client-123',
	type: 'web',
	redirect_uri: 'https://example.com',
	scope: '',
	deviceName: 'test-device',
	page: 5,
	perPage: 50,
	sortAttr: 'deviceId',
	sortDir: 'asc',
	deny: false,
	deviceIds: ['abc', 'xyz'],
	development: false,
	notes: 'A fancy Photon',
	desiredFirmwareVersion: 42,
	flash: false,
	version: 42,
	title: 'prod',
	description: 'ready for production',
	file: new Buffer('ELF...'),
	country: 'RO',
	iccid: '1234567890',
	iccids: ['1234567890', '9876543210'],
	serialNumber: 'PH-123456',
	settings: {
		url: 'http://example.com',
	},
};

const product = 'ze-product-v1';

const propsWithProduct = Object.assign({ product }, props);

class Common {

	static expectCredentials({ form }) {
		form.username.should.equal(props.username);
		form.password.should.equal(props.password);
	}
	static expectDeviceUrlAndToken(results) {
		results.uri.should.containEql(props.deviceId);
		results.auth.should.equal(props.auth);
	}
}

describe('ParticleAPI', () => {
	beforeEach(() => {
		api = new Particle();
	});

	describe('operations', () => {
		beforeEach(() => {
			api.agent = new FakeAgent();
		});

		describe('constructor', () => {
			it('sets the defaults', () => {
				Object.keys(Defaults).forEach((setting) => {
					api[setting].should.equal(Defaults[setting]);
				});
			});
		});
		describe('trackingIdentity', () => {
			const context = { tool: { name: 'cli', version:'1.2.3' } };
			it('full', () => {
				return api.trackingIdentity({auth: 'X', full: true, context})
					.then((req) => {
						expect(req).to.have.property('uri').eql('/v1/user/identify');
						expect(req).to.have.property('method').eql('get');
						expect(req).to.have.property('query').eql(undefined);
						expect(req).to.have.property('context').eql(context);
						expect(req).to.have.property('auth').eql('X');
					});
			});
			it('id only', () => {
				return api.trackingIdentity().then(req => {
					expect(req).to.have.property('uri').eql('/v1/user/identify');
					expect(req).to.have.property('method').eql('get');
					expect(req).to.have.property('context').eql({});
					expect(req).to.have.property('query').eql({tracking:1});
				});
			});
		});
		describe('.login', () => {
			it('sends credentials', () => {
				return api.login(props).then(Common.expectCredentials);
			});
		});
		describe('.loginAsClientOwner', () => {
			it('sends client ID and secret', () => {
				let clientApi = new Particle({
					clientId: 'foo',
					clientSecret: 'bar'
				});
				clientApi.agent = new FakeAgent();
				return clientApi.loginAsClientOwner({}).then(req => {
					expect(req.form).to.have.property('client_id').eql('foo');
					expect(req.form).to.have.property('client_secret').eql('bar');
					expect(req.form).to.have.property('grant_type').eql('client_credentials');
				});
			});
		});
		describe('.enableMfa', () => {
			it('sends request to begin mfa enrollment', () => {
				return api.enableMfa(props).then((results) => {
					results.auth.should.equal(props.auth);
				});
			});
		});
		describe('.confirmMfa', () => {
			it('sends request to confirm mfa enrollment', () => {
				const otp = '123456';
				const mfaToken = '<KEY>';
				const params = Object.assign({}, props, { otp, mfaToken });

				return api.confirmMfa(params).then((results) => {
					results.data.should.be.instanceOf(Object);
					results.data.otp.should.equal(otp);
					results.data.mfa_token.should.equal(mfaToken);
					results.auth.should.equal(props.auth);
				});
			});
		});
		describe('.createCustomer', () => {
			it('sends client ID and secret', () => {
				let clientApi = new Particle({
					clientId: 'foo',
					clientSecret: 'bar'
				});
				clientApi.agent = new FakeAgent();
				return clientApi.loginAsClientOwner({}).then(req => {
					expect(req.form).to.have.property('client_id').eql('foo');
					expect(req.form).to.have.property('client_secret').eql('bar');
					expect(req.form).to.have.property('grant_type').eql('client_credentials');
				});
{'fraction_non_alphanumeric': 0.20950543312684067, 'fraction_numerical': 0.018889001726414136, 'mean_word_length': 3.921539230384808, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 53, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3355866', 'n_tokens_mistral': 4036, 'n_tokens_neox': 3442, 'n_words': 623}