<commit_before>'use strict';

let chai = require("chai");
let expect = require('chai').expect;
let moment = require('moment');
let proxyquire = require('proxyquire').noCallThru();
let sinon = require('sinon');

let UserBilling = proxyquire('../../../models/userBilling', {
	"./subscriptions": {},
	"./billingAddress": {},
	"./paypal.js": {},
	"./invoice.js": {},
	"../response_codes.js": {},
	"./counter": {},
	'./role': {},
	'../mailer/mailer': {},
	'./user': {}
});

let C = require('../../../constants');

describe('UserBilling', function(){

	describe('.getNextPaymentDate', function(){

		it('should = 2016-02-01 if payment date is 2016-01-01', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-01')).toISOString()).to.equal('2016-02-01T00:00:00.000Z');
		});

		it('should = 2016-03-01 if payment date is 2016-01-30', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-30')).toISOString()).to.equal('2016-03-01T00:00:00.000Z');
		});

		it('should = 2016-03-01 if payment date is 2016-01-31', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-31')).toISOString()).to.equal('2016-03-01T00:00:00.000Z');
		});

		it('should = 2016-02-29 if payment date is 2016-01-29', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-29')).toISOString()).to.equal('2016-02-29T00:00:00.000Z');
		});

		it('should = 2016-02-28 if payment date is 2016-02-28', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-28')).toISOString()).to.equal('2016-02-28T00:00:00.000Z');
		});

		it('should = 2015-03-01 if payment date is 2015-01-29', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2015-01-29')).toISOString()).to.equal('2015-03-01T00:00:00.000Z');
		});

		it('should = 2015-02-28 if payment date is 2015-01-28', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2015-01-28')).toISOString()).to.equal('2015-02-28T00:00:00.000Z');
		});

		it('should = 2016-05-01 if payment date is 2016-03-31', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-03-31')).toISOString()).to.equal('2016-05-01T00:00:00.000Z');
		});

		it('should = 2016-08-31 if payment date is 2016-07-31', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-07-31')).toISOString()).to.equal('2016-08-31T00:00:00.000Z');
		});

		it('should = 2016-09-30 if payment date is 2016-08-30', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-08-30')).toISOString()).to.equal('2016-09-30T00:00:00.000Z');
		});

	});


	function createPaymentTest(data){


		let instanceProp = {
			subscriptions : data.currentSubscriptions,
			billingInfo: {
				countryCode: data.country,
				vat: data.isBusiness ? 'ABC123' : null
			}
		}

		if(data.currentSubscriptions.paypal.length > 0) {
			//Add a billing agreement Id into the instance if we have paypal subscriptions
			instanceProp.billingAgreementId = "I-5WJWJKFH40KD";
		}
		if(data.lastAnniversaryDate){
			instanceProp.lastAnniversaryDate = data.lastAnniversaryDate.toDate();
			instanceProp.nextPaymentDate =  data.nextPaymentDate;
		}

		return UserBilling.methods.writeSubscriptionChanges.call(instanceProp, data.newLicences).then( changes => {

			let paymentData = UserBilling.methods.calculateAmounts.call(instanceProp, data.paymentDate, changes);

			let proRataPayment = paymentData.payments.find(payment => payment.type === C.PRO_RATA_PAYMENT);
			let regularPayment = paymentData.payments.find(payment => payment.type === C.REGULAR_PAYMENT);
			
			return {changes, proRataPayment, regularPayment, paymentDate: paymentData.paymentDate};
		});
<commit_msg>ISSUE #859 check paypal entry exists before checking length<commit_after>'use strict';

let chai = require("chai");
let expect = require('chai').expect;
let moment = require('moment');
let proxyquire = require('proxyquire').noCallThru();
let sinon = require('sinon');

let UserBilling = proxyquire('../../../models/userBilling', {
	"./subscriptions": {},
	"./billingAddress": {},
	"./paypal.js": {},
	"./invoice.js": {},
	"../response_codes.js": {},
	"./counter": {},
	'./role': {},
	'../mailer/mailer': {},
	'./user': {}
});

let C = require('../../../constants');

describe('UserBilling', function(){

	describe('.getNextPaymentDate', function(){

		it('should = 2016-02-01 if payment date is 2016-01-01', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-01')).toISOString()).to.equal('2016-02-01T00:00:00.000Z');
		});

		it('should = 2016-03-01 if payment date is 2016-01-30', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-30')).toISOString()).to.equal('2016-03-01T00:00:00.000Z');
		});

		it('should = 2016-03-01 if payment date is 2016-01-31', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-31')).toISOString()).to.equal('2016-03-01T00:00:00.000Z');
		});

		it('should = 2016-02-29 if payment date is 2016-01-29', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-29')).toISOString()).to.equal('2016-02-29T00:00:00.000Z');
		});

		it('should = 2016-02-28 if payment date is 2016-02-28', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-01-28')).toISOString()).to.equal('2016-02-28T00:00:00.000Z');
		});

		it('should = 2015-03-01 if payment date is 2015-01-29', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2015-01-29')).toISOString()).to.equal('2015-03-01T00:00:00.000Z');
		});

		it('should = 2015-02-28 if payment date is 2015-01-28', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2015-01-28')).toISOString()).to.equal('2015-02-28T00:00:00.000Z');
		});

		it('should = 2016-05-01 if payment date is 2016-03-31', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-03-31')).toISOString()).to.equal('2016-05-01T00:00:00.000Z');
		});

		it('should = 2016-08-31 if payment date is 2016-07-31', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-07-31')).toISOString()).to.equal('2016-08-31T00:00:00.000Z');
		});

		it('should = 2016-09-30 if payment date is 2016-08-30', function(){
			expect(UserBilling.statics.getNextPaymentDate(new Date('2016-08-30')).toISOString()).to.equal('2016-09-30T00:00:00.000Z');
		});

	});


	function createPaymentTest(data){


		let instanceProp = {
			subscriptions : data.currentSubscriptions,
			billingInfo: {
				countryCode: data.country,
				vat: data.isBusiness ? 'ABC123' : null
			}
		}

		if(data.currentSubscriptions.paypal && data.currentSubscriptions.paypal.length > 0) {
			//Add a billing agreement Id into the instance if we have paypal subscriptions
			instanceProp.billingAgreementId = "I-5WJWJKFH40KD";
		}
		if(data.lastAnniversaryDate){
			instanceProp.lastAnniversaryDate = data.lastAnniversaryDate.toDate();
			instanceProp.nextPaymentDate =  data.nextPaymentDate;
		}

		return UserBilling.methods.writeSubscriptionChanges.call(instanceProp, data.newLicences).then( changes => {

			let paymentData = UserBilling.methods.calculateAmounts.call(instanceProp, data.paymentDate, changes);

			let proRataPayment = paymentData.payments.find(payment => payment.type === C.PRO_RATA_PAYMENT);
			let regularPayment = paymentData.payments.find(payment => payment.type === C.REGULAR_PAYMENT);
			
			return {changes, proRataPayment, regularPayment, paymentDate: paymentData.paymentDate};
		});

{'fraction_non_alphanumeric': 0.18685258964143425, 'fraction_numerical': 0.11115537848605578, 'mean_word_length': 4.753246753246753, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 11, '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': '6182932', 'n_tokens_mistral': 3324, 'n_tokens_neox': 2578, 'n_words': 401}