<commit_before>require 'rails_helper'

RSpec.describe ChargesController, type: :controller do
  let(:template) { CardTemplate.create(greeting: "<NAME>", image_file: "wn.png") }
  let(:card) { Card.create({
       card_template_id: template.id,
       custom_message: 'errrr.....',
       signature: '-neemur neemur',
       recipient_name: 'the bees',
       street_address: 'over yonder',
       city: 'Oakland',
       state: 'CA',
       zip_code: '04294'
     }) }

  describe 'POST create' do
    it 'sets @amount to 499 cents' do
      post :create, params: { card_id: card.id }
      expect(assigns(:amount)).to eq 499
    end

    describe 'if there is a Stripe error' do
      it 'redirects to the show page for the card if possible' do
        post :create, params: { card_id: card.id }
        expect(response).to redirect_to card_template_card_path(
          id: card.id,
          card_template_id: card.card_template_id
        )
      end

      it 'redirects to the home page if no card exists' do
        post :create, params: { card_id: -1 }
        expect(response).to redirect_to '/'
      end
    end
  end
end<commit_msg>Add test for fetching a card token from Stripe<commit_after>require 'rails_helper'
require 'stripe'

RSpec.describe ChargesController, type: :controller do
  let(:template) { CardTemplate.create(greeting: "<NAME>", image_file: "wn.png") }
  let(:card) { Card.create({
       card_template_id: template.id,
       custom_message: 'errrr.....',
       signature: '-neemur neemur',
       recipient_name: 'the bees',
       street_address: 'over yonder',
       city: 'Oakland',
       state: 'CA',
       zip_code: '04294'
     }) }
  let(:token) { get_test_stripe_token }

  describe 'POST create' do
    it 'sets @amount to 499 cents' do
      post :create, params: { card_id: card.id }
      expect(assigns(:amount)).to eq 499
    end

    describe 'if there is a Stripe error' do
      it 'redirects to the show page for the card if possible' do
        post :create, params: { card_id: card.id }
        expect(response).to redirect_to card_template_card_path(
          id: card.id,
          card_template_id: card.card_template_id
        )
      end

      it 'redirects to the home page if no card exists' do
        post :create, params: { card_id: -1 }
        expect(response).to redirect_to '/'
      end

      describe 'if Stripe returns a proper token' do
        it 'should be a test token' do
          expect(token[:object]).to eq "token"
          expect(token[:type]).to eq "card"
          expect(token[:livemode]).to eq false
        end
      end
    end
  end
end

def get_test_stripe_token
  Stripe.api_key = ENV['STRIPE_TEST_PUBLISHABLE_KEY']

  token = Stripe::Token.create(
    :card => {
      :number => "4242424242424242",
      :exp_month => 10,
      :exp_year => 2018,
      :cvc => "314"
    },
  )
end
{'fraction_non_alphanumeric': 0.11246095105866019, 'fraction_numerical': 0.017007983339118363, 'mean_word_length': 2.5018226002430133, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '814107', 'n_tokens_mistral': 971, 'n_tokens_neox': 915, 'n_words': 269}