<commit_before>require 'spec_helper'

shared_examples "check total store credit from payments" do
  context "with valid payments" do
    let(:order)           { payment.order }
    let!(:payment)        { create(:store_credit_payment) }
    let!(:second_payment) { create(:store_credit_payment, order: order) }

    subject { order }

    it "returns the sum of the payment amounts" do
      subject.total_applicable_store_credit.should eq (payment.amount + second_payment.amount)
    end
  end

  context "without valid payments" do
    let(:order) { create(:order) }

    subject { order }

    it "returns 0" do
      subject.total_applicable_store_credit.should be_zero
    end
  end
end

describe "Order" do
  describe "#create_gift_cards" do
    let(:order) { create(:order_with_line_items) }
    let(:line_item) { order.line_items.first }
    subject { order.create_gift_cards }

    context "the line item is a gift card" do
      before do
        line_item.stub(:gift_card?).and_return(true)
        line_item.stub(:quantity).and_return(3)
      end

      it 'creates a gift card for each gift card in the line item' do
        expect { subject }.to change { Spree::VirtualGiftCard.count }.by(line_item.quantity)
      end

      it 'sets the purchaser, amount, and currency' do
        Spree::VirtualGiftCard.should_receive(:create!).exactly(3).times.with(amount: line_item.price, currency: line_item.currency, purchaser: order.user, line_item: line_item)
        subject
      end
    end

    context "the line item is not a gift card" do
      before { line_item.stub(:gift_card?).and_return(false) }

      it 'does not create a gift card' do
        Spree::VirtualGiftCard.should_not_receive(:create!)
        subject
      end
    end
  end

  describe "#add_store_credit_payments" do
    let(:order_total) { 500.00 }

    before { create(:store_credit_payment_method) }

    subject { order.add_store_credit_payments }

    context "there is no store credit" do
      let(:order)       { create(:store_credits_order_without_user, total: order_total) }

      context "there is a credit card payment" do
        let!(:cc_payment) { create(:payment, order: order) }

        before do
          # callbacks recalculate total based on line items
          # this ensures the total is what we expect
          order.update_column(:total, order_total)
          subject
          order.reload
        end

        it "charges the outstanding balance to the credit card" do
          order.payments.count.should eq 1
          order.payments.first.source.should be_a(Spree::CreditCard)
          order.payments.first.amount.should eq order_total
        end
      end

      context "there are no other payments" do
        it "adds an error to the model" do
          expect(subject).to be false
          order.errors.full_messages.should include(Spree.t("store_credits.errors.unable_to_fund"))
        end
      end

      context "there is a payment of an unknown type" do
        let!(:check_payment) { create(:check_payment, order: order) }

        it "raises an error" do
          expect { subject }.to raise_error
        end
      end
    end

    context "there is enough store credit to pay for the entire order" do
      let(:store_credit) { create(:store_credit, amount: order_total) }
      let(:order)        { create(:order, user: store_credit.user, total: order_total) }

      context "there are no other payments" do
        before do
          subject
          order.reload
        end

        it "creates a store credit payment for the full amount" do
          order.payments.count.should eq 1
          order.payments.first.should be_store_credit
          order.payments.first.amount.should eq order_total
        end
      end

      context "there is a credit card payment" do
        let!(:cc_payment) { create(:payment, order: order) }

        before { subject }

        it "should invalidate the credit card payment" do
          cc_payment.reload.should be_invalid
        end
      end

      context "there is a payment of an unknown type" do
        let!(:check_payment) { create(:check_payment, order: order) }

        it "raises an error" do
          expect { subject }.to raise_error
        end
      end
    end

    context "the available store credit is not enough to pay for the entire order" do
      let(:expected_cc_total)  { 100.0 }
      let(:store_credit_total) { order_total - expected_cc_total }
      let(:store_credit)       { create(:store_credit, amount: store_credit_total) }
      let(:order)              { create(:order, user: store_credit.user, total: order_total) }
<commit_msg>Compare state directly in specs.<commit_after>require 'spec_helper'

shared_examples "check total store credit from payments" do
  context "with valid payments" do
    let(:order)           { payment.order }
    let!(:payment)        { create(:store_credit_payment) }
    let!(:second_payment) { create(:store_credit_payment, order: order) }

    subject { order }

    it "returns the sum of the payment amounts" do
      subject.total_applicable_store_credit.should eq (payment.amount + second_payment.amount)
    end
  end

  context "without valid payments" do
    let(:order) { create(:order) }

    subject { order }

    it "returns 0" do
      subject.total_applicable_store_credit.should be_zero
    end
  end
end

describe "Order" do
  describe "#create_gift_cards" do
    let(:order) { create(:order_with_line_items) }
    let(:line_item) { order.line_items.first }
    subject { order.create_gift_cards }

    context "the line item is a gift card" do
      before do
        line_item.stub(:gift_card?).and_return(true)
        line_item.stub(:quantity).and_return(3)
      end

      it 'creates a gift card for each gift card in the line item' do
        expect { subject }.to change { Spree::VirtualGiftCard.count }.by(line_item.quantity)
      end

      it 'sets the purchaser, amount, and currency' do
        Spree::VirtualGiftCard.should_receive(:create!).exactly(3).times.with(amount: line_item.price, currency: line_item.currency, purchaser: order.user, line_item: line_item)
        subject
      end
    end

    context "the line item is not a gift card" do
      before { line_item.stub(:gift_card?).and_return(false) }

      it 'does not create a gift card' do
        Spree::VirtualGiftCard.should_not_receive(:create!)
        subject
      end
    end
  end

  describe "#add_store_credit_payments" do
    let(:order_total) { 500.00 }

    before { create(:store_credit_payment_method) }

    subject { order.add_store_credit_payments }

    context "there is no store credit" do
      let(:order)       { create(:store_credits_order_without_user, total: order_total) }

      context "there is a credit card payment" do
        let!(:cc_payment) { create(:payment, order: order) }

        before do
          # callbacks recalculate total based on line items
          # this ensures the total is what we expect
          order.update_column(:total, order_total)
          subject
          order.reload
        end

        it "charges the outstanding balance to the credit card" do
          order.payments.count.should eq 1
          order.payments.first.source.should be_a(Spree::CreditCard)
          order.payments.first.amount.should eq order_total
        end
      end

      context "there are no other payments" do
        it "adds an error to the model" do
          expect(subject).to be false
          order.errors.full_messages.should include(Spree.t("store_credits.errors.unable_to_fund"))
        end
      end

      context "there is a payment of an unknown type" do
        let!(:check_payment) { create(:check_payment, order: order) }

        it "raises an error" do
          expect { subject }.to raise_error
        end
      end
    end

    context "there is enough store credit to pay for the entire order" do
      let(:store_credit) { create(:store_credit, amount: order_total) }
      let(:order)        { create(:order, user: store_credit.user, total: order_total) }

      context "there are no other payments" do
        before do
          subject
          order.reload
        end

        it "creates a store credit payment for the full amount" do
          order.payments.count.should eq 1
          order.payments.first.should be_store_credit
          order.payments.first.amount.should eq order_total
        end
      end

      context "there is a credit card payment" do
        let!(:cc_payment) { create(:payment, order: order) }

        before { subject }

        it "should invalidate the credit card payment" do
          cc_payment.reload.state.should == 'invalid'
        end
      end

      context "there is a payment of an unknown type" do
        let!(:check_payment) { create(:check_payment, order: order) }

        it "raises an error" do
          expect { subject }.to raise_error
        end
      end
    end

    context "the available store credit is not enough to pay for the entire order" do
      let(:expected_cc_total)  { 100.0 }
      let(:store_credit_total) { order_total - expected_cc_total }
      let(:store_credit)       { create(:store_credit, amount: store_credit_total) }
      let(:order)              { create(:order, user: store_credit.user, total: order_total) }

{'fraction_non_alphanumeric': 0.08262870598308894, 'fraction_numerical': 0.0029968960719255057, 'mean_word_length': 2.5420773313116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1447036', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2639, 'n_words': 822}