Sha256: ea4648c417233a83205763e5b840098515f157543ae48809b21638089c894da4

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

require 'spec_helper'

describe CatarsePagarme::CreditCardsController do
  before do
    controller.stub(:current_user).and_return(user)
  end

  let(:project) { create(:project, goal: 10_000, state: 'online') }
  let(:contribution) { create(:contribution, value: 10, project: project) }
  let(:payment) { contribution.payments.first }

  describe 'pay with credit card' do
    context  'without an user' do
      let(:user) { nil }

      it 'should raise a error' do
        expect {
          post :create, { locale: :pt, id: contribution.id, use_route: 'catarse_pagarme' }
        }.to raise_error('invalid user')
      end
    end

    context 'with an user' do
      let(:user) { payment.user }
      context "with valid card data" do
        before do
          post :create, {
            locale: :pt, id: contribution.id, use_route: 'catarse_pagarme',
            card_hash: sample_card_hash }
        end

        it 'and payment_status is not failed' do
          expect(ActiveSupport::JSON.decode(response.body)['payment_status']).not_to eq('failed')
        end
      end

      context 'with invalid card data' do
        before do
          post :create, {
            locale: :pt, id: contribution.id, use_route: 'catarse_pagarme', card_hash: "abcd" }
        end

        it 'payment_status should be failed' do
          expect(ActiveSupport::JSON.decode(response.body)['payment_status']).to eq('failed')
        end
      end

      context "when charges fails" do
        before do
          allow_any_instance_of(PagarMe::Transaction).to receive(:charge).and_raise(PagarMe::PagarMeError)
          post :create, {
            locale: :pt, id: contribution.id, use_route: 'catarse_pagarme',
            card_hash: sample_card_hash }
        end

        it "should have created a payment" do
          expect(contribution.payments.size).to eq(2)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
catarse_pagarme-2.7.4 spec/controllers/catarse_pagarme/credit_cards_controller_spec.rb
catarse_pagarme-2.7.3 spec/controllers/catarse_pagarme/credit_cards_controller_spec.rb
catarse_pagarme-2.7.2 spec/controllers/catarse_pagarme/credit_cards_controller_spec.rb