<commit_before>require 'spec_helper'

shared_examples 'Charge API' do

  it "creates a stripe charge item with a card token" do
    charge = Stripe::Charge.create(
      amount: 999,
      currency: 'USD',
      card: 'card_token_abcde',
      description: 'card charge'
    )

    expect(charge.id).to match(/^test_ch/)
    expect(charge.amount).to eq(999)
    expect(charge.description).to eq('card charge')
  end


  it "stores a created stripe charge in memory" do
    charge = Stripe::Charge.create({
      amount: 333,
      currency: 'USD',
      card: 'card_token_333'
    })
    charge2 = Stripe::Charge.create({
      amount: 777,
      currency: 'USD',
      card: 'card_token_777'
    })
    data = test_data_source(:charges)
    expect(data[charge.id]).to_not be_nil
    expect(data[charge.id][:amount]).to eq(333)

    expect(data[charge2.id]).to_not be_nil
    expect(data[charge2.id][:amount]).to eq(777)
  end


  it "retrieves a stripe charge" do
    original = Stripe::Charge.create({
      amount: 777,
      currency: 'USD',
      card: 'card_token_abc'
    })
    charge = Stripe::Charge.retrieve(original.id)

    expect(charge.id).to eq(original.id)
    expect(charge.amount).to eq(original.amount)
  end

  it "cannot retrieve a charge that doesn't exist" do
    expect { Stripe::Charge.retrieve('nope') }.to raise_error {|e|
      expect(e).to be_a Stripe::InvalidRequestError
      expect(e.param).to eq('charge')
      expect(e.http_status).to eq(404)
    }
  end


  context "With strict mode toggled off" do

    before { StripeMock.toggle_strict(false) }

    it "retrieves a stripe charge with an id that doesn't exist" do
      charge = Stripe::Charge.retrieve('test_charge_x')
      expect(charge.id).to eq('test_charge_x')
      expect(charge.amount).to_not be_nil
      expect(charge.card).to_not be_nil
    end
  end


  describe 'captured status value' do
    it "reports captured by default" do
      charge = Stripe::Charge.create({
        amount: 777,
        currency: 'USD',
        card: 'card_token_abc'
      })

      expect(charge.captured).to be_true
    end

    it "reports captured if capture requested" do
      charge = Stripe::Charge.create({
        amount: 777,
        currency: 'USD',
        card: 'card_token_abc',
        capture: true
      })

      expect(charge.captured).to be_true
    end

    it "reports not captured if capture: false requested" do
      charge = Stripe::Charge.create({
        amount: 777,
        currency: 'USD',
        card: 'card_token_abc',
        capture: false
      })

      expect(charge.captured).to be_false
    end
  end

end<commit_msg>Spec desired behavior upon Charge#capture<commit_after>require 'spec_helper'

shared_examples 'Charge API' do

  it "creates a stripe charge item with a card token" do
    charge = Stripe::Charge.create(
      amount: 999,
      currency: 'USD',
      card: 'card_token_abcde',
      description: 'card charge'
    )

    expect(charge.id).to match(/^test_ch/)
    expect(charge.amount).to eq(999)
    expect(charge.description).to eq('card charge')
  end


  it "stores a created stripe charge in memory" do
    charge = Stripe::Charge.create({
      amount: 333,
      currency: 'USD',
      card: 'card_token_333'
    })
    charge2 = Stripe::Charge.create({
      amount: 777,
      currency: 'USD',
      card: 'card_token_777'
    })
    data = test_data_source(:charges)
    expect(data[charge.id]).to_not be_nil
    expect(data[charge.id][:amount]).to eq(333)

    expect(data[charge2.id]).to_not be_nil
    expect(data[charge2.id][:amount]).to eq(777)
  end


  it "retrieves a stripe charge" do
    original = Stripe::Charge.create({
      amount: 777,
      currency: 'USD',
      card: 'card_token_abc'
    })
    charge = Stripe::Charge.retrieve(original.id)

    expect(charge.id).to eq(original.id)
    expect(charge.amount).to eq(original.amount)
  end

  it "cannot retrieve a charge that doesn't exist" do
    expect { Stripe::Charge.retrieve('nope') }.to raise_error {|e|
      expect(e).to be_a Stripe::InvalidRequestError
      expect(e.param).to eq('charge')
      expect(e.http_status).to eq(404)
    }
  end


  context "With strict mode toggled off" do

    before { StripeMock.toggle_strict(false) }

    it "retrieves a stripe charge with an id that doesn't exist" do
      charge = Stripe::Charge.retrieve('test_charge_x')
      expect(charge.id).to eq('test_charge_x')
      expect(charge.amount).to_not be_nil
      expect(charge.card).to_not be_nil
    end
  end


  describe 'captured status value' do
    it "reports captured by default" do
      charge = Stripe::Charge.create({
        amount: 777,
        currency: 'USD',
        card: 'card_token_abc'
      })

      expect(charge.captured).to be_true
    end

    it "reports captured if capture requested" do
      charge = Stripe::Charge.create({
        amount: 777,
        currency: 'USD',
        card: 'card_token_abc',
        capture: true
      })

      expect(charge.captured).to be_true
    end

    it "reports not captured if capture: false requested" do
      charge = Stripe::Charge.create({
        amount: 777,
        currency: 'USD',
        card: 'card_token_abc',
        capture: false
      })

      expect(charge.captured).to be_false
    end
  end

  describe "two-step charge (auth, then capture)" do
    it "changes captured status upon #capture" do
      charge = Stripe::Charge.create({
        amount: 777,
        currency: 'USD',
        card: 'card_token_abc',
        capture: false
      })

      ret = charge.capture
      expect(charge.captured).to be_true
      expect ret.to_eq charge
    end
  end
end
{'fraction_non_alphanumeric': 0.1159062885326757, 'fraction_numerical': 0.015324995596265633, 'mean_word_length': 2.6727037516170764, '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': '4221294', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1807, 'n_words': 462}