<commit_before>// Test tools
import { expect } from 'chai';
import nock from 'nock';
import * as utils from './utils';

// Components needed for writing the test
import models from '../server/models';

// What's being tested
import creditcard from '../server/paymentProviders/stripe/creditcard';

async function createOrderWithPaymentMethod(paymentMethodName, orderParams = {}) {
  const user = await models.User.createUserWithCollective({
    name: 'TestMcTesterson',
    email: '<EMAIL>',
  });
  const host = await models.Collective.create({ name: 'Host Collective' });
  const tier = await models.Tier.create({ name: 'backer' });
  const collective = await models.Collective.create({ name: 'Parcel!!' });
  collective.addHost(host);
  const connectedAccount = await models.ConnectedAccount.create({
    service: 'stripe',
    token: 'tok_1Be9noDjPFcHOcTmT574CrEv',
    CollectiveId: host.id,
  });
  const paymentMethod = await models.PaymentMethod.create({
    name: paymentMethodName,
    token: 'tok_123456781234567812345678',
    service: 'stripe',
    type: 'creditcard',
    data: { expMonth: 11, expYear: 2025 },
    monthlyLimitPerMember: 10000,
    CollectiveId: collective.id,
  });
  const order = await models.Order.create(
    Object.assign(
      {
        CreatedByUserId: user.id,
        FromCollectiveId: user.CollectiveId,
        CollectiveId: collective.id,
        PaymentMethodId: paymentMethod.id,
        TierId: tier.id,
        totalAmount: 1000,
        currency: 'USD',
      },
      orderParams,
    ),
  );
  order.fromCollective = user.collective;
  order.collective = collective;
  order.createdByUser = user;
  order.paymentMethod = paymentMethod;
  return { order, user, collective, paymentMethod, connectedAccount };
}

describe('stripe.creditcard', () => {
  describe('#processOrder()', async () => {
    let secondCallToCreateCustomer;

    beforeEach(() => utils.resetTestDB());

    beforeEach(() => {
      // Call performed by getOrCreateCustomerOnPlatformAccount
      nock('https://api.stripe.com:443')
        .post('/v1/customers')
        .reply(200, {});

      // Calls performed by getOrCreateCustomerIdForHost
      secondCallToCreateCustomer = nock('https://api.stripe.com:443')
        .post('/v1/customers')
        .reply(200, {});
      nock('https://api.stripe.com:443')
        .post('/v1/tokens')
        .reply(200, {});

      // Calls performed by createChargeAndTransactions
      nock('https://api.stripe.com:443')
        .post('/v1/charges')
        .reply(200, {
          id: 'ch_1B5j91D8MNtzsDcgNMsUgI8L',
          balance_transaction: 'txn_1B5j92D8MNtzsDcgQzIcmfrn',
        });
      nock('https://api.stripe.com:443')
        .get('/v1/balance/history/txn_1B5j92D8MNtzsDcgQzIcmfrn')
        .reply(200, { amount: 1000, fee: 0, fee_details: [] });
    });

    afterEach(() => nock.cleanAll());

    it('should not create a new customer id for a host for pre-migration users', async () => {
      const { order } = await createOrderWithPaymentMethod(null);
      await creditcard.processOrder(order);
      expect(secondCallToCreateCustomer.isDone()).to.be.false;
    });

    it('should create a new customer id for a host for post-migration users', async () => {
      const { order } = await createOrderWithPaymentMethod('name');
      await creditcard.processOrder(order);
      expect(secondCallToCreateCustomer.isDone()).to.be.true;
    });

    it('has tax information stored in transaction', async () => {
      const taxAmount = 100;
      const { order } = await createOrderWithPaymentMethod('name', { taxAmount });
      console.log(order.dataValues);
      const transaction = await creditcard.processOrder(order);
      expect(transaction.taxAmount).to.be.equal(taxAmount);
    });
  });
});<commit_msg>chore: Remove forgotten console.log in test<commit_after>// Test tools
import { expect } from 'chai';
import nock from 'nock';
import * as utils from './utils';

// Components needed for writing the test
import models from '../server/models';

// What's being tested
import creditcard from '../server/paymentProviders/stripe/creditcard';

async function createOrderWithPaymentMethod(paymentMethodName, orderParams = {}) {
  const user = await models.User.createUserWithCollective({
    name: 'TestMcTesterson',
    email: '<EMAIL>',
  });
  const host = await models.Collective.create({ name: 'Host Collective' });
  const tier = await models.Tier.create({ name: 'backer' });
  const collective = await models.Collective.create({ name: 'Parcel!!' });
  collective.addHost(host);
  const connectedAccount = await models.ConnectedAccount.create({
    service: 'stripe',
    token: 'tok_1Be9noDjPFcHOcTmT574CrEv',
    CollectiveId: host.id,
  });
  const paymentMethod = await models.PaymentMethod.create({
    name: paymentMethodName,
    token: 'tok_123456781234567812345678',
    service: 'stripe',
    type: 'creditcard',
    data: { expMonth: 11, expYear: 2025 },
    monthlyLimitPerMember: 10000,
    CollectiveId: collective.id,
  });
  const order = await models.Order.create(
    Object.assign(
      {
        CreatedByUserId: user.id,
        FromCollectiveId: user.CollectiveId,
        CollectiveId: collective.id,
        PaymentMethodId: paymentMethod.id,
        TierId: tier.id,
        totalAmount: 1000,
        currency: 'USD',
      },
      orderParams,
    ),
  );
  order.fromCollective = user.collective;
  order.collective = collective;
  order.createdByUser = user;
  order.paymentMethod = paymentMethod;
  return { order, user, collective, paymentMethod, connectedAccount };
}

describe('stripe.creditcard', () => {
  describe('#processOrder()', async () => {
    let secondCallToCreateCustomer;

    beforeEach(() => utils.resetTestDB());

    beforeEach(() => {
      // Call performed by getOrCreateCustomerOnPlatformAccount
      nock('https://api.stripe.com:443')
        .post('/v1/customers')
        .reply(200, {});

      // Calls performed by getOrCreateCustomerIdForHost
      secondCallToCreateCustomer = nock('https://api.stripe.com:443')
        .post('/v1/customers')
        .reply(200, {});
      nock('https://api.stripe.com:443')
        .post('/v1/tokens')
        .reply(200, {});

      // Calls performed by createChargeAndTransactions
      nock('https://api.stripe.com:443')
        .post('/v1/charges')
        .reply(200, {
          id: 'ch_1B5j91D8MNtzsDcgNMsUgI8L',
          balance_transaction: 'txn_1B5j92D8MNtzsDcgQzIcmfrn',
        });
      nock('https://api.stripe.com:443')
        .get('/v1/balance/history/txn_1B5j92D8MNtzsDcgQzIcmfrn')
        .reply(200, { amount: 1000, fee: 0, fee_details: [] });
    });

    afterEach(() => nock.cleanAll());

    it('should not create a new customer id for a host for pre-migration users', async () => {
      const { order } = await createOrderWithPaymentMethod(null);
      await creditcard.processOrder(order);
      expect(secondCallToCreateCustomer.isDone()).to.be.false;
    });

    it('should create a new customer id for a host for post-migration users', async () => {
      const { order } = await createOrderWithPaymentMethod('name');
      await creditcard.processOrder(order);
      expect(secondCallToCreateCustomer.isDone()).to.be.true;
    });

    it('has tax information stored in transaction', async () => {
      const taxAmount = 100;
      const { order } = await createOrderWithPaymentMethod('name', { taxAmount });
      const transaction = await creditcard.processOrder(order);
      expect(transaction.taxAmount).to.be.equal(taxAmount);
    });
  });
});
{'fraction_non_alphanumeric': 0.1381448739939306, 'fraction_numerical': 0.027180366803008314, 'mean_word_length': 3.2921857304643263, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 10, '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': '4660693', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2320, 'n_words': 482}