Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adicionado Deprecation warnings na antiga API e refatorado os testes

  • Loading branch information...
commit 138227de26f16f247b8f232eda1106ef09c565ca 1 parent 12c0196
Rafael Mendonça França rafaelfranca authored

Showing 2 changed files with 112 additions and 48 deletions. Show diff stats Hide diff stats

  1. +23 0 lib/moip.rb
  2. +89 48 spec/moip_spec.rb
23 lib/moip.rb
... ... @@ -1,6 +1,7 @@
1 1 # encoding: utf-8
2 2 require "rubygems"
3 3 require 'active_support/core_ext/module/attribute_accessors'
  4 +require 'active_support/deprecation'
4 5
5 6 module MoIP
6 7 autoload :DirectPayment, 'moip/direct_payment'
@@ -21,4 +22,26 @@ def self.setup
21 22 end
22 23
23 24 STATUS = {1 => "authorized", 2 => "started", 3 => "printed", 4 => "completed", 5 => "canceled", 6 => "analysing"}
  25 +
  26 + class << self
  27 + def checkout(attributes = {})
  28 + ActiveSupport::Deprecation.warn("MoIP.checkout has been deprecated. Use MoIP::Client.checkout instead", caller)
  29 + MoIP::Client.checkout(attributes)
  30 + end
  31 +
  32 + def query(token)
  33 + ActiveSupport::Deprecation.warn("MoIP.query has been deprecated. Use MoIP::Client.query instead", caller)
  34 + MoIP::Client.query(token)
  35 + end
  36 +
  37 + def moip_page(token)
  38 + ActiveSupport::Deprecation.warn("MoIP.moip_page has been deprecated. use MoIP::Client.moip_page instead", caller)
  39 + MoIP::Client.moip_page(token)
  40 + end
  41 +
  42 + def notification(params)
  43 + ActiveSupport::Deprecation.warn("MoIP.notification has been deprecated. use MoIP::Client.notification instead", caller)
  44 + MoIP::Client.moip_page(token)
  45 + end
  46 + end
24 47 end
137 spec/moip_spec.rb
@@ -31,81 +31,104 @@
31 31 @credit = { :value => "8.90", :id_proprio => id, :forma => "CartaoCredito", :instituicao => "AmericanExpress", :numero => "345678901234564", :expiracao => "08/11", :codigo_seguranca => "1234", :nome => "João Silva", :identidade => "134.277.017.00", :telefone => "(21)9208-0547", :data_nascimento => "25/10/1980", :parcelas => "2", :recebimento => "AVista", :pagador => @pagador }
32 32 end
33 33
34   - context "make a billet checkout" do
35   - it "should have status Sucesso" do
  34 + context "checkout" do
  35 + before(:each) do
36 36 MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>{ "Resposta"=>{ "ID"=>Time.now.strftime("%y%m%d%H%M%S"), "Status"=>"Sucesso", "Token" => "T2N0L0X8E0S71217U2H3W1T4F4S4G4K731D010V0S0V0S080M010E0Q082X2" }})
37   - response = MoIP::Client.checkout(@billet)
38   - response["Status"].should == "Sucesso"
39 37 end
40   - end
41 38
42   - context "make a debit checkout" do
43   - it "should have status Sucesso" do
44   - MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>{ "Resposta"=>{ "ID"=>Time.now.strftime("%y%m%d%H%M%S"), "Status"=>"Sucesso", "Token" => "T2N0L0X8E0S71217U2H3W1T4F4S4G4K731D010V0S0V0S080M010E0Q082X2" }})
45   - response = MoIP::Client.checkout(@debit)
46   - response["Status"].should == "Sucesso"
  39 + it "with old api should be deprecated" do
  40 + deprecations = collect_deprecations{ MoIP.checkout(@billet) }
  41 +
  42 + deprecations.should_not be_empty
  43 + deprecations.any? {|w| w =~ /MoIP.checkout has been deprecated/ }.should be_true
47 44 end
48   - end
49 45
50   - context "make a debit checkout without pass a institution" do
51   - it "should have status Falha" do
52   - @incorrect_debit = { :value => "37.90", :id_proprio => id, :forma => "DebitoBancario", :pagador => @pagador }
53   - MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>{ "Resposta"=>{"Status"=>"Falha", "Erro"=>"Pagamento direto não é possível com a instituição de pagamento enviada" }})
54   - lambda { MoIP::Client.checkout(@incorrect_debit) }.should raise_error(StandardError, "Pagamento direto não é possível com a instituição de pagamento enviada")
  46 + context "when is a billet checkout" do
  47 + it "should have status 'Sucesso'" do
  48 + response = MoIP::Client.checkout(@billet)
  49 + response["Status"].should == "Sucesso"
  50 + end
55 51 end
56   - end
57 52
58   - context "make a debit checkout without pass the payer informations" do
59   - it "should raise an exception" do
60   - @incorrect_debit = { :value => "37.90", :id_proprio => id, :forma => "DebitoBancario", :instituicao => "BancoDoBrasil" }
61   - lambda { MoIP::Client.checkout(@incorrect_debit) }.should raise_error(StandardError, "É obrigatório passar as informações do pagador")
  53 + context "when is a debit checkout" do
  54 + it "should have status 'Sucesso' with valid arguments" do
  55 + response = MoIP::Client.checkout(@debit)
  56 + response["Status"].should == "Sucesso"
  57 + end
  58 +
  59 + it "should have status 'Falha' when a instituition is not passed as argument" do
  60 + @incorrect_debit = { :value => "37.90", :id_proprio => id, :forma => "DebitoBancario", :pagador => @pagador }
  61 +
  62 + MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>{ "Resposta"=>{"Status"=>"Falha", "Erro"=>"Pagamento direto não é possível com a instituição de pagamento enviada" }})
  63 +
  64 + lambda { MoIP::Client.checkout(@incorrect_debit) }.should raise_error(StandardError, "Pagamento direto não é possível com a instituição de pagamento enviada")
  65 + end
  66 +
  67 + it "should raise an exception if payer isformations is not passed" do
  68 + @incorrect_debit = { :value => "37.90", :id_proprio => id, :forma => "DebitoBancario", :instituicao => "BancoDoBrasil" }
  69 + lambda { MoIP::Client.checkout(@incorrect_debit) }.should raise_error(StandardError, "É obrigatório passar as informações do pagador")
  70 + end
62 71 end
63   - end
64 72
65   - context "make a credit card checkout" do
66   - it "should have status Sucesso" do
67   - MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>{ "Resposta"=>{ "ID"=>Time.now.strftime("%y%m%d%H%M%S"), "Status"=>"Sucesso", "Token" => "T2N0L0X8E0S71217U2H3W1T4F4S4G4K731D010V0S0V0S080M010E0Q082X2" }})
68   - response = MoIP::Client.checkout(@credit)
69   - response["Status"].should == "Sucesso"
  73 + context "when is a credit card checkout" do
  74 + it "should have status 'Sucesso' with valid arguments" do
  75 + response = MoIP::Client.checkout(@credit)
  76 + response["Status"].should == "Sucesso"
  77 + end
  78 +
  79 + it "should have status 'Falha' when the card informations is not passed as argument" do
  80 + @incorrect_credit = { :value => "8.90", :id_proprio => id, :forma => "CartaoCredito", :pagador => @pagador }
  81 + MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>{ "Resposta"=>{"Status"=>"Falha", "Erro"=>"Pagamento direto não é possível com a instituição de pagamento enviada" }})
  82 + lambda { MoIP::Client.checkout(@incorrect_credit) }.should raise_error(StandardError, "Pagamento direto não é possível com a instituição de pagamento enviada")
  83 + end
70 84 end
71   - end
72 85
73   - context "make a credit card checkout without pass card informations" do
74   - it "should have status Falha" do
75   - @incorrect_credit = { :value => "8.90", :id_proprio => id, :forma => "CartaoCredito", :pagador => @pagador }
76   - MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>{ "Resposta"=>{"Status"=>"Falha", "Erro"=>"Pagamento direto não é possível com a instituição de pagamento enviada" }})
77   - lambda { MoIP::Client.checkout(@incorrect_credit) }.should raise_error(StandardError, "Pagamento direto não é possível com a instituição de pagamento enviada")
  86 + context "in error scenario" do
  87 + it "should raise an exception if response is nil" do
  88 + MoIP::Client.stub!(:post).and_return(nil)
  89 + lambda { MoIP::Client.checkout(@billet) }.should raise_error(StandardError, "Ocorreu um erro ao chamar o webservice")
  90 + end
  91 +
  92 + it "should raise an exception if status is fail" do
  93 + MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>{ "Resposta"=>{"Status"=>"Falha", "Erro"=>"O status da resposta é Falha" }})
  94 + lambda { MoIP::Client.checkout(@billet) }.should raise_error(StandardError, "O status da resposta é Falha")
  95 + end
78 96 end
79 97 end
80 98
81   - context "in error scenario" do
82   - it "should raise an exception if response is nil" do
83   - MoIP::Client.stub!(:post).and_return(nil)
84   - lambda { MoIP::Client.checkout(@billet) }.should raise_error(StandardError, "Ocorreu um erro ao chamar o webservice")
  99 + context "query a transaction token" do
  100 + before(:each) do
  101 + MoIP::Client.stub!(:get).and_return("ns1:ConsultarTokenResponse"=>{ "RespostaConsultar"=>{"Status"=>"Sucesso", "ID"=>"201010291031001210000000046760" }})
85 102 end
86 103
87   - it "should raise an exception if status is fail" do
88   - MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>{ "Resposta"=>{"Status"=>"Falha", "Erro"=>"O status da resposta é Falha" }})
89   - lambda { MoIP::Client.checkout(@billet) }.should raise_error(StandardError, "O status da resposta é Falha")
  104 + it "with old api should be deprecated" do
  105 + deprecations = collect_deprecations{ MoIP.query(token) }
  106 +
  107 + deprecations.should_not be_empty
  108 + deprecations.any? {|w| w =~ /MoIP.query has been deprecated/ }.should be_true
90 109 end
91   - end
92 110
93   - context "query a transaction token" do
94 111 it "should retrieve the transaction" do
95   - MoIP::Client.stub!(:get).and_return("ns1:ConsultarTokenResponse"=>{ "RespostaConsultar"=>{"Status"=>"Sucesso", "ID"=>"201010291031001210000000046760" }})
96 112 response = MoIP::Client.query(token)
97 113 response["Status"].should == "Sucesso"
98 114 end
99   - end
100 115
101   - context "query a transaction token in a error scenario" do
102   - it "should retrieve status Falha" do
103   - MoIP::Client.stub!(:get).and_return("ns1:ConsultarTokenResponse"=>{ "RespostaConsultar"=>{"Status"=>"Falha", "Erro"=>"Instrução não encontrada", "ID"=>"201010291102522860000000046768"}})
104   - lambda { MoIP::Client.query("000000000000000000000000000000000000000000000000000000000000") }.should raise_error(StandardError, "Instrução não encontrada")
  116 + context "in a error scenario" do
  117 + it "should retrieve status 'Falha'" do
  118 + MoIP::Client.stub!(:get).and_return("ns1:ConsultarTokenResponse"=>{ "RespostaConsultar"=>{"Status"=>"Falha", "Erro"=>"Instrução não encontrada", "ID"=>"201010291102522860000000046768"}})
  119 + lambda { MoIP::Client.query("000000000000000000000000000000000000000000000000000000000000") }.should raise_error(StandardError, "Instrução não encontrada")
  120 + end
105 121 end
106 122 end
107 123
108 124 context "build the MoIP URL" do
  125 + it "with old api should be deprecated" do
  126 + deprecations = collect_deprecations{ MoIP.moip_page(token) }
  127 +
  128 + deprecations.should_not be_empty
  129 + deprecations.any? {|w| w =~ /MoIP.moip_page has been deprecated/ }.should be_true
  130 + end
  131 +
109 132 it "should build the correct URL" do
110 133 MoIP::Client.moip_page(token).should == "https://desenvolvedor.moip.com.br/sandbox/Instrucao.do?token=#{token}"
111 134 end
@@ -124,6 +147,13 @@
124 147 @params = { "id_transacao" => "Pag62", "valor" => "8.90", "status_pagamento" => "3", "cod_moip" => "001", "forma_pagamento" => "73", "tipo_pagamento" => "BoletoBancario", "email_consumidor" => "presidente@planalto.gov.br" }
125 148 end
126 149
  150 + it "with old api should be deprecated" do
  151 + deprecations = collect_deprecations{ MoIP.notification(@param) }
  152 +
  153 + deprecations.should_not be_empty
  154 + deprecations.any? {|w| w =~ /MoIP.notification has been deprecated/ }.should be_true
  155 + end
  156 +
127 157 it "should return a hash with the params extracted from NASP" do
128 158 response = { :transaction_id => "Pag62", :amount => "8.90", :status => "printed", :code => "001", :payment_type => "BoletoBancario", :email => "presidente@planalto.gov.br" }
129 159 MoIP::Client.notification(@params).should == response
@@ -147,4 +177,15 @@ def token
147 177 "T2X0Q1N021E0B2S9U1P0V3Y0G1F570Y2P4M0P000M0Z0F0J0G0U4N6C7W5T9"
148 178 end
149 179
  180 + def collect_deprecations
  181 + old_behavior = ActiveSupport::Deprecation.behavior
  182 + deprecations = []
  183 + ActiveSupport::Deprecation.behavior = Proc.new do |message, callstack|
  184 + deprecations << message
  185 + end
  186 + result = yield
  187 + deprecations
  188 + ensure
  189 + ActiveSupport::Deprecation.behavior = old_behavior
  190 + end
150 191 end

0 comments on commit 138227d

Please sign in to comment.
Something went wrong with that request. Please try again.