test/test_helper.rb in pagarme-2.0.2 vs test/test_helper.rb in pagarme-2.1.0
- old
+ new
@@ -1,192 +1,69 @@
-require_relative '../lib/pagarme'
require 'test/unit'
require 'shoulda'
+require 'webmock'
+require 'pry'
+require 'vcr'
-def test_transaction(params = {})
- return PagarMe::Transaction.new({
- :card_number => "4901720080344448",
- :card_holder_name => "Jose da Silva",
- :card_expiration_month => "10",
- :card_expiration_year => "15",
- :card_cvv => "314",
- :amount => 1000
- }.merge(params))
-end
+require_relative '../lib/pagarme'
+require_relative 'assertions'
+require_relative 'fixtures'
-def test_transfer(params = {})
- return PagarMe::Transfer.new({
- amount: 1000,
- }.merge(params))
+VCR.configure do |config|
+ config.cassette_library_dir = 'test/vcr_cassettes'
+ config.default_cassette_options[:match_requests_on] = [:method, :uri, :host, :path, :query, :body, :headers]
+ config.default_cassette_options[:record] = :new_episodes
+ config.hook_into :webmock
end
-def test_card(params = {})
- return PagarMe::Card.new({
- :card_number => '4111111111111111',
- :card_holder_name => 'Jose da Silva',
- :card_expiration_month => '10',
- :card_expiration_year => '15',
- :card_cvv => '314'
- }.merge(params))
-end
+class Test::Unit::TestCase
+ FIXED_API_KEY = 'ak_test_Q2D2qDYGJSyeR1KbI4sLzGACEr73MF'
-def test_subscription_without_plan(params = {})
- return PagarMe::Subscription.new({
- :payment_method => 'credit_card',
- :card_number => "4901720080344448",
- :card_holder_name => "Jose da Silva",
- :card_expiration_month => "10",
- :card_expiration_year => "15",
- :card_cvv => "314",
- :postback_url => "http://test.com/postback",
- :customer => {
- :email => 'customer@pagar.me'
- }
- }.merge(params))
-end
+ include Fixtures::Helpers
+ include Assertions
-def test_plan(params = {})
- return PagarMe::Plan.new({
- :name => "Plano gold",
- :trial_days => 5,
- :days => 30,
- :amount => 3000,
- }.merge(params))
-end
+ def setup
+ PagarMe.api_key = temporary_api_key
+ end
-def test_bank_account(params = {})
- return PagarMe::BankAccount.new({
- :bank_code => '237',
- :agencia => '1935',
- :agencia_dv => '9',
- :conta => '23398',
- :conta_dv => '9',
- :legal_name => 'foo bar loem',
- :document_number => '00000000000000'
- }.merge(params))
-end
+ def teardown
+ PagarMe.api_key = nil
+ end
-def test_subscription(params = {})
- return PagarMe::Subscription.new({
- :payment_method => 'credit_card',
- :card_number => "4901720080344448",
- :card_holder_name => "Jose da Silva",
- :card_expiration_month => "10",
- :card_expiration_year => "15",
- :card_cvv => "314",
- :postback_url => "http://test.com/postback",
- :customer => {
- email: 'customer@pagar.me'
- }
- }.merge(params))
-end
+ protected
+ def ensure_positive_balance
+ VCR.use_cassette 'TestCase/ensure_positive_balance' do
+ transaction = PagarMe::Transaction.charge transaction_with_boleto_params(amount: 100_000_00)
+ transaction.status = :paid
+ transaction.save
+ end
+ end
-def test_transaction_with_customer(params = {})
- return PagarMe::Transaction.new({
- :amount => 1000,
- :card_number => '4901720080344448',
- :card_holder_name => "Jose da Silva",
- :card_expiration_month => "11",
- :card_expiration_year => "19",
- :card_cvv => "356",
- :customer => {
- :name => "Jose da Silva",
- :document_number => "36433809847",
- :email => "henrique+test@pagar.me",
- :address => {
- :street => 'Av. Brigadeiro Faria Lima',
- :neighborhood => 'Itaim bibi',
- :zipcode => '01452000',
- :street_number => "2941",
- },
- :phone => {
- :ddd => "12",
- :number => '981433533',
- },
- :sex => 'M',
- :born_at => '1970-10-11'
- }
- }.merge(params))
-end
+ def ensure_waiting_funds
+ VCR.use_cassette 'TestCase/ensure_waiting_funds' do
+ PagarMe::Transaction.create transaction_with_customer_with_card_params(amount: 10_000_00, installments: 12)
+ end
+ end
-def test_subscription_with_customer(params = {})
- return PagarMe::Subscription.new({
- :amount => 1000,
- :card_number => '4901720080344448',
- :card_holder_name => "Jose da silva",
- :card_expiration_month => "11",
- :card_expiration_year => "19",
- :card_cvv => "356",
- :customer => {
- :name => "Jose da Silva",
- :document_number => "36433809847",
- :email => "henrique@pagar.me",
- :address => {
- :street => 'Av. Brigadeiro Faria Lima',
- :neighborhood => 'Itaim bibi',
- :zipcode => '01452000',
- :street_number => "1000",
- } ,
- :phone => {
- :ddd => "12",
- :number => '981444444',
- } ,
- :sex => 'M',
- :born_at => '1970-10-11'
- }
- }.merge(params))
-end
+ def fixed_api_key
+ PagarMe.api_key = FIXED_API_KEY
+ yield
+ PagarMe.api_key = temporary_api_key
+ end
+ def temporary_api_key
+ return FIXED_API_KEY
-def test_customer_response(customer)
- assert customer.id
- assert customer.document_type == 'cpf'
- assert customer.name == 'Jose da Silva'
- assert customer.born_at
- assert customer.id
-end
-
-def test_subscription_transaction_response(transaction, amount = nil, installments = nil)
- amount ||= 2000
- installments ||= 1
- assert transaction.id
- assert !transaction.refuse_reason
- assert transaction.date_created
- assert transaction.amount == amount
- assert transaction.installments.to_i == installments
- # assert transaction.card_brand == 'visa'
- assert transaction.payment_method == 'credit_card'
- assert transaction.status == 'paid'
- assert !transaction.boleto_url
- assert !transaction.boleto_barcode
-end
-
-def test_transaction_response(transaction)
- assert transaction.id
- assert transaction.card_holder_name
- assert !transaction.refuse_reason
- assert transaction.date_created
- assert transaction.amount == 1000
- assert transaction.installments.to_i == 1
- assert transaction.card_holder_name == 'Jose da Silva'
- # assert transaction.card_brand == 'visa'
- assert transaction.payment_method == 'credit_card'
- assert transaction.status == 'paid'
-end
-
-def test_plan_response(plan)
- assert plan.id
- assert plan.name == 'Plano gold'
- assert plan.trial_days == 5
- assert plan.days == 30
- assert plan.amount = 3000
-end
-
-class Test::Unit::TestCase
- def setup
- PagarMe.api_key="ak_test_Rw4JR98FmYST2ngEHtMvVf5QJW7Eoo"
+ # TODO: Unfortunately, it's right now impossible to create
+ # temporary companies properly pre-configured to run all tests
+ VCR.use_cassette 'TestCase/tmp_company_api_key' do
+ PagarMe.api_key = FIXED_API_KEY
+ Company.temporary.api_key.test
+ end
end
- def teardown
- PagarMe.api_key=nil
+ # Monkey Patch that adds VCR everywhere
+ def self.should(description, &block)
+ cassette_name = "#{ self.name.split('::').last }/should_#{ description.gsub /\s+/, '_' }"
+ super(description){ VCR.use_cassette(cassette_name){ self.instance_exec &block } }
end
end