edools_mymoip 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.gitignore +15 -0
  4. data/.travis.yml +4 -0
  5. data/CHANGELOG.md +128 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +20 -0
  8. data/README.md +3 -0
  9. data/Rakefile +10 -0
  10. data/lib/mymoip.rb +57 -0
  11. data/lib/mymoip/bank_debit.rb +22 -0
  12. data/lib/mymoip/commission.rb +54 -0
  13. data/lib/mymoip/credit_card.rb +73 -0
  14. data/lib/mymoip/exceptions.rb +15 -0
  15. data/lib/mymoip/formatter.rb +23 -0
  16. data/lib/mymoip/instruction.rb +134 -0
  17. data/lib/mymoip/json_parser.rb +11 -0
  18. data/lib/mymoip/payer.rb +75 -0
  19. data/lib/mymoip/payment.rb +10 -0
  20. data/lib/mymoip/payment_methods.rb +46 -0
  21. data/lib/mymoip/payment_slip.rb +74 -0
  22. data/lib/mymoip/payments/bank_debit_payment.rb +27 -0
  23. data/lib/mymoip/payments/credit_card_payment.rb +53 -0
  24. data/lib/mymoip/payments/payment_slip_payment.rb +12 -0
  25. data/lib/mymoip/purchase.rb +40 -0
  26. data/lib/mymoip/request.rb +34 -0
  27. data/lib/mymoip/requests/payment_request.rb +53 -0
  28. data/lib/mymoip/requests/transparent_request.rb +36 -0
  29. data/lib/mymoip/validators.rb +12 -0
  30. data/lib/mymoip/version.rb +3 -0
  31. data/mymoip.gemspec +30 -0
  32. data/test/fixtures/fixture.rb +73 -0
  33. data/test/fixtures/vcr_cassettes/payment_request.yml +37 -0
  34. data/test/fixtures/vcr_cassettes/payment_request_with_payment_slip.yml +32 -0
  35. data/test/fixtures/vcr_cassettes/transparent_request.yml +34 -0
  36. data/test/fixtures/vcr_cassettes/transparent_request_with_commissions.yml +35 -0
  37. data/test/lib/test_bank_debit.rb +36 -0
  38. data/test/lib/test_bank_debit_payment.rb +32 -0
  39. data/test/lib/test_commission.rb +121 -0
  40. data/test/lib/test_credit_card_payment.rb +107 -0
  41. data/test/lib/test_creditcard.rb +206 -0
  42. data/test/lib/test_formatter.rb +49 -0
  43. data/test/lib/test_instruction.rb +243 -0
  44. data/test/lib/test_mymoip.rb +79 -0
  45. data/test/lib/test_payer.rb +249 -0
  46. data/test/lib/test_payment.rb +15 -0
  47. data/test/lib/test_payment_methods.rb +54 -0
  48. data/test/lib/test_payment_request.rb +120 -0
  49. data/test/lib/test_payment_slip.rb +78 -0
  50. data/test/lib/test_payment_slip_payment.rb +8 -0
  51. data/test/lib/test_purchase.rb +109 -0
  52. data/test/lib/test_request.rb +88 -0
  53. data/test/lib/test_transparent_request.rb +71 -0
  54. data/test/lib/test_validators.rb +13 -0
  55. data/test/live_test.rb +4 -0
  56. data/test/test_helper.rb +17 -0
  57. metadata +251 -0
@@ -0,0 +1,49 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestFormatter < Test::Unit::TestCase
4
+ def test_cep_method_returns_the_given_cep_with_section_separator
5
+ assert_equal '92400-123', MyMoip::Formatter.cep('92400123')
6
+ end
7
+
8
+ def test_cep_method_raises_exception_with_nil_cep_given
9
+ assert_raise ArgumentError do
10
+ MyMoip::Formatter.cep(nil)
11
+ end
12
+ end
13
+
14
+ def test_phone_method_returns_the_given_8_digit_phone_with_section_separators
15
+ assert_equal '(51)3040-5060', MyMoip::Formatter.phone('5130405060')
16
+ end
17
+
18
+ def test_phone_method_returns_the_given_9_digit_phone_with_section_separators
19
+ assert_equal '(51)93040-5060', MyMoip::Formatter.phone('51930405060')
20
+ end
21
+
22
+ def test_phone_method_raises_exception_with_nil_phone_given
23
+ assert_raise ArgumentError do
24
+ MyMoip::Formatter.phone(nil)
25
+ end
26
+ end
27
+
28
+ def test_date_method_returns_the_given_date_in_the_format_expected
29
+ date = Date.new(2040, 10, 30)
30
+ assert_equal '30/10/2040', MyMoip::Formatter.date(date)
31
+ end
32
+
33
+ def test_date_method_raises_exception_with_nil_date_given
34
+ assert_raise ArgumentError do
35
+ MyMoip::Formatter.date(nil)
36
+ end
37
+ end
38
+
39
+ def test_cpf_method_returns_the_given_number_with_section_separators
40
+ cpf = '522.116.706-95'
41
+ assert_equal '522.116.706-95', MyMoip::Formatter.cpf('52211670695')
42
+ end
43
+
44
+ def test_cpf_method_raises_exception_with_nil_cpf_given
45
+ assert_raise ArgumentError do
46
+ MyMoip::Formatter.cpf(nil)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,243 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestInstruction < Test::Unit::TestCase
4
+ def test_getters_for_attributes
5
+ payer = Fixture.payer
6
+ commissions = [Fixture.commission]
7
+ installments = [{min: 2, max: 12, forward_taxes: true, fee: 1.99, receive_in_installments: true}]
8
+ instruction = MyMoip::Instruction.new(
9
+ id: "some id",
10
+ payment_reason: "some payment_reason",
11
+ values: [100.0, 200.0],
12
+ payer: payer,
13
+ commissions: commissions,
14
+ fee_payer_login: "fee_payer_login",
15
+ payment_receiver_login: "payment_receiver_login",
16
+ payment_receiver_name: "nick_fury",
17
+ installments: installments,
18
+ notification_url: 'http://please.notify.me',
19
+ return_url: 'http://return.to.my.address.com'
20
+ )
21
+
22
+ assert_equal "some id", instruction.id
23
+ assert_equal "some payment_reason", instruction.payment_reason
24
+ assert_equal [100.0, 200.0], instruction.values
25
+ assert_equal payer, instruction.payer
26
+ assert_equal commissions, instruction.commissions
27
+ assert_equal installments, instruction.installments
28
+ assert_equal "fee_payer_login", instruction.fee_payer_login
29
+ assert_equal "payment_receiver_login", instruction.payment_receiver_login
30
+ assert_equal "nick_fury", instruction.payment_receiver_name
31
+ assert_equal 'http://please.notify.me', instruction.notification_url
32
+ assert_equal 'http://return.to.my.address.com', instruction.return_url
33
+ end
34
+
35
+ def test_should_generate_a_string_when_converting_to_xml
36
+ payer = Fixture.payer
37
+ instruction = Fixture.instruction(payer: payer)
38
+
39
+ assert_equal String, instruction.to_xml.class
40
+ end
41
+
42
+ def test_xml_format
43
+ payer = Fixture.payer
44
+ instruction = Fixture.instruction(payer: payer)
45
+
46
+ expected_format = <<XML
47
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Parcelamentos><Parcelamento><MinimoParcelas>2</MinimoParcelas><MaximoParcelas>12</MaximoParcelas><Repassar>true</Repassar><Juros>1.99</Juros></Parcelamento></Parcelamentos><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
48
+ XML
49
+ assert_equal expected_format.rstrip, instruction.to_xml
50
+ end
51
+
52
+ def test_xml_format_with_urls
53
+ payer = Fixture.payer
54
+ instruction = Fixture.instruction(payer: payer)
55
+ instruction.notification_url = 'http://qwerty.me/nasp'
56
+ instruction.return_url = 'http://return.to.me'
57
+
58
+ expected_format = <<XML
59
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Parcelamentos><Parcelamento><MinimoParcelas>2</MinimoParcelas><MaximoParcelas>12</MaximoParcelas><Repassar>true</Repassar><Juros>1.99</Juros></Parcelamento></Parcelamentos><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador><URLNotificacao>http://qwerty.me/nasp</URLNotificacao><URLRetorno>http://return.to.me</URLRetorno></InstrucaoUnica></EnviarInstrucao>
60
+ XML
61
+ assert_equal expected_format.rstrip, instruction.to_xml
62
+ end
63
+
64
+ def test_xml_format_with_installments
65
+ payer = Fixture.payer
66
+ installments = [
67
+ {min: 5, max: 10, fee: 2.99, receive_in_installments: true}
68
+ ]
69
+ instruction = Fixture.instruction(payer: payer, installments: installments)
70
+
71
+ expected_format = <<XML
72
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Parcelamentos><Parcelamento><MinimoParcelas>5</MinimoParcelas><MaximoParcelas>10</MaximoParcelas><Juros>2.99</Juros><Recebimento>Parcelado</Recebimento></Parcelamento></Parcelamentos><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
73
+ XML
74
+ assert_equal expected_format.rstrip, instruction.to_xml
75
+ end
76
+
77
+ def test_xml_format_with_mutiple_installments
78
+ payer = Fixture.payer
79
+ installments = [
80
+ {min: 2, max: 6, fee: 1.99},
81
+ {min: 7, max: 12, fee: 2.99}
82
+ ]
83
+ instruction = Fixture.instruction(payer: payer, installments: installments)
84
+
85
+ expected_format = <<XML
86
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Parcelamentos><Parcelamento><MinimoParcelas>2</MinimoParcelas><MaximoParcelas>6</MaximoParcelas><Juros>1.99</Juros></Parcelamento><Parcelamento><MinimoParcelas>7</MinimoParcelas><MaximoParcelas>12</MaximoParcelas><Juros>2.99</Juros></Parcelamento></Parcelamentos><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
87
+ XML
88
+ assert_equal expected_format.rstrip, instruction.to_xml
89
+ end
90
+
91
+ def test_xml_format_with_commissions
92
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.15, fixed_value: nil)]
93
+ payer = Fixture.payer
94
+ instruction = Fixture.instruction(payer: payer, commissions: commissions, installments: nil)
95
+ expected_format = <<XML
96
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Comissoes><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorFixo>5.00</ValorFixo></Comissionamento><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorPercentual>15.00</ValorPercentual></Comissionamento></Comissoes><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
97
+ XML
98
+ assert_equal expected_format.rstrip, instruction.to_xml
99
+ end
100
+
101
+ def test_xml_format_with_commissions_and_fee_payer
102
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.15,fixed_value: nil)]
103
+ payer = Fixture.payer
104
+ instruction = Fixture.instruction(payer: payer, commissions: commissions, fee_payer_login: 'fee_payer_indentifier', installments: nil)
105
+ expected_format = <<XML
106
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Comissoes><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorFixo>5.00</ValorFixo></Comissionamento><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorPercentual>15.00</ValorPercentual></Comissionamento><PagadorTaxa><LoginMoIP>fee_payer_indentifier</LoginMoIP></PagadorTaxa></Comissoes><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
107
+ XML
108
+ assert_equal expected_format.rstrip, instruction.to_xml
109
+ end
110
+
111
+ def test_xml_format_with_commissions_and_payment_receiver
112
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.15, fixed_value: nil)]
113
+ payer = Fixture.payer
114
+ instruction = Fixture.instruction(payer: payer, commissions: commissions,
115
+ payment_receiver_login:'payment_receiver_indentifier',
116
+ payment_receiver_name: 'nick_fury', installments: nil)
117
+ expected_format = <<XML
118
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Comissoes><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorFixo>5.00</ValorFixo></Comissionamento><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorPercentual>15.00</ValorPercentual></Comissionamento></Comissoes><Recebedor><LoginMoIP>payment_receiver_indentifier</LoginMoIP><Apelido>nick_fury</Apelido></Recebedor><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
119
+ XML
120
+ assert_equal expected_format.rstrip, instruction.to_xml
121
+ end
122
+
123
+ def test_xml_format_with_commissions_and_payment_receiver_and_fee_payer
124
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.15, fixed_value: nil)]
125
+ payer = Fixture.payer
126
+ instruction = Fixture.instruction(payer: payer, commissions: commissions,
127
+ payment_receiver_login:'payment_receiver_indentifier',
128
+ payment_receiver_name: 'nick_fury',
129
+ fee_payer_login: 'fee_payer_indentifier',
130
+ installments: nil)
131
+ expected_format = <<XML
132
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Comissoes><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorFixo>5.00</ValorFixo></Comissionamento><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorPercentual>15.00</ValorPercentual></Comissionamento><PagadorTaxa><LoginMoIP>fee_payer_indentifier</LoginMoIP></PagadorTaxa></Comissoes><Recebedor><LoginMoIP>payment_receiver_indentifier</LoginMoIP><Apelido>nick_fury</Apelido></Recebedor><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
133
+ XML
134
+ assert_equal expected_format.rstrip, instruction.to_xml
135
+ end
136
+
137
+ def test_to_xml_method_raises_exception_when_called_with_some_invalid_comission
138
+ invalid_commission = Fixture.commission
139
+ invalid_commission.stubs(:invalid?).returns(true)
140
+ subject = Fixture.instruction(commissions: [Fixture.commission, invalid_commission])
141
+ assert_raise MyMoip::InvalidComission do
142
+ subject.to_xml
143
+ end
144
+ end
145
+
146
+ def test_to_xml_method_raises_exception_when_called_with_invalid_payer
147
+ subject = Fixture.instruction
148
+ MyMoip::Payer.any_instance.stubs(:invalid?).returns(true)
149
+ assert_raise MyMoip::InvalidPayer do
150
+ subject.to_xml
151
+ end
152
+ end
153
+
154
+ def test_to_xml_method_dont_raises_exception_when_called_with_valid_payer
155
+ subject = Fixture.instruction
156
+ MyMoip::Payer.any_instance.stubs(:invalid?).returns(false)
157
+ assert_nothing_raised MyMoip::InvalidPayer do
158
+ subject.to_xml
159
+ end
160
+ end
161
+
162
+ def test_to_xml_method_raises_exception_when_called_with_invalid_params
163
+ subject = Fixture.instruction
164
+ MyMoip::Instruction.any_instance.stubs(:invalid?).returns(true)
165
+ assert_raise MyMoip::InvalidInstruction do
166
+ subject.to_xml
167
+ end
168
+ end
169
+
170
+ def test_to_xml_method_dont_raises_exception_when_called_with_valid_params
171
+ subject = Fixture.instruction
172
+ MyMoip::Instruction.any_instance.stubs(:invalid?).returns(false)
173
+ assert_nothing_raised MyMoip::InvalidInstruction do
174
+ subject.to_xml
175
+ end
176
+ end
177
+
178
+ def test_validate_no_presence_of_payment_receiver_in_commissions
179
+ commissions = [Fixture.commission(receiver_login: 'payment_receiver_id')]
180
+ subject = Fixture.instruction(payment_receiver_login: 'payment_receiver_id', commissions: commissions)
181
+ assert subject.invalid? && subject.errors[:payment_receiver_login].present?,
182
+ "should be invalid with receiver present on commissions"
183
+ end
184
+
185
+ def test_gross_amount
186
+ subject = Fixture.instruction(values: [6, 5])
187
+ assert_equal 11, subject.gross_amount
188
+ end
189
+
190
+ def test_commissions_sum
191
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.1, fixed_value: nil)]
192
+ subject = Fixture.instruction(commissions: commissions, values: [10])
193
+ assert_equal 6, subject.commissions_sum
194
+ end
195
+
196
+ def test_validate_commissions_sum
197
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(fixed_value: 5)]
198
+ subject = Fixture.instruction(commissions: commissions, values: [6])
199
+ assert subject.invalid? && subject.errors[:commissions].present?,
200
+ "should be invalid with commissions sum greater than values sum"
201
+ end
202
+
203
+ def test_validate_presence_of_id_attribute
204
+ subject = Fixture.instruction
205
+ subject.id = nil
206
+ assert subject.invalid? && subject.errors[:id].present?,
207
+ 'should be invalid without an id'
208
+ end
209
+
210
+ def test_validate_presence_of_payment_reason_attribute
211
+ subject = Fixture.instruction
212
+ subject.payment_reason = nil
213
+ assert subject.invalid? && subject.errors[:payment_reason].present?,
214
+ 'should be invalid without a payment_reason'
215
+ subject.payment_reason = ''
216
+ assert subject.invalid? && subject.errors[:payment_reason].present?,
217
+ 'should be invalid without a payment_reason'
218
+ end
219
+
220
+ def test_validate_presence_of_values_attribute
221
+ subject = Fixture.instruction
222
+ subject.values = nil
223
+ assert subject.invalid? && subject.errors[:values].present?,
224
+ 'should be invalid without values'
225
+ end
226
+
227
+ def test_validate_presence_of_payer_attribute
228
+ subject = Fixture.instruction
229
+ subject.payer = nil
230
+ assert subject.invalid? && subject.errors[:payer].present?,
231
+ 'should be invalid without a payer'
232
+ end
233
+
234
+ def test_validate_url
235
+ subject = Fixture.instruction
236
+ assert subject.valid?
237
+
238
+ subject.notification_url = 'ftp://sdfsdfs.com'
239
+ subject.return_url = 'xxftpsdfsdfs.com'
240
+
241
+ assert subject.invalid? && subject.errors[:notification_url].present? && subject.errors[:return_url].present?
242
+ end
243
+ end
@@ -0,0 +1,79 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestMymoip < Test::Unit::TestCase
4
+ def setup
5
+ @default_environment = MyMoip.environment
6
+ @default_key = MyMoip.key
7
+ @default_token = MyMoip.token
8
+ @default_logger = MyMoip.logger
9
+ end
10
+
11
+ def teardown
12
+ MyMoip.environment = @default_environment
13
+ MyMoip.key = @default_key
14
+ MyMoip.token = @default_token
15
+ MyMoip.logger = @default_logger
16
+ end
17
+
18
+ def test_default_environment_is_sandbox
19
+ MyMoip.environment = 'sandbox'
20
+ assert_equal "sandbox", MyMoip.environment
21
+ end
22
+
23
+ def test_key_setter_updates_sandbox_and_production_keys
24
+ MyMoip.key = "my_key"
25
+ assert_equal "my_key", MyMoip.production_key
26
+ assert_equal "my_key", MyMoip.sandbox_key
27
+ end
28
+
29
+ def test_token_setter_updates_sandbox_and_production_tokens
30
+ MyMoip.token = "my_token"
31
+ assert_equal "my_token", MyMoip.production_token
32
+ assert_equal "my_token", MyMoip.sandbox_token
33
+ end
34
+
35
+ def test_current_auth_when_in_sandbox
36
+ MyMoip.sandbox_key = "sandbox_key"
37
+ MyMoip.sandbox_token = "sandbox_token"
38
+ MyMoip.environment = "sandbox"
39
+ assert_equal "sandbox_key", MyMoip.key
40
+ assert_equal "sandbox_token", MyMoip.token
41
+ end
42
+
43
+ def test_current_auth_when_in_production
44
+ MyMoip.production_key = "production_key"
45
+ MyMoip.production_token = "production_token"
46
+ MyMoip.environment = "production"
47
+ assert_equal "production_key", MyMoip.key
48
+ assert_equal "production_token", MyMoip.token
49
+ end
50
+
51
+ def test_default_referer_url_setter
52
+ MyMoip.default_referer_url = "http://localhost"
53
+ assert_equal "http://localhost", MyMoip.default_referer_url
54
+ end
55
+
56
+ def test_environment_setter
57
+ MyMoip.environment = "production"
58
+ assert_equal "production", MyMoip.environment
59
+ end
60
+
61
+ def test_choose_right_api_url_by_sandbox_environment
62
+ MyMoip.environment = "sandbox"
63
+ assert_equal "https://desenvolvedor.moip.com.br/sandbox", MyMoip.api_url
64
+ end
65
+
66
+ def test_choose_right_api_url_by_production_environment
67
+ MyMoip.environment = "production"
68
+ assert_equal "https://www.moip.com.br", MyMoip.api_url
69
+ end
70
+
71
+ def test_logger_initialization
72
+ assert MyMoip.logger.instance_of?(Logger)
73
+ end
74
+
75
+ def test_attribution_of_new_logger
76
+ MyMoip.logger = my_string = ""
77
+ assert_equal my_string, MyMoip.logger
78
+ end
79
+ end
@@ -0,0 +1,249 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestPayer < Test::Unit::TestCase
4
+ def test_getters_for_attributes
5
+ payer = MyMoip::Payer.new(
6
+ id: "some id",
7
+ name: "some name",
8
+ email: "some email",
9
+ address_street: "some address_street",
10
+ address_street_number: "some address_street_number",
11
+ address_street_extra: "some address_street_extra",
12
+ address_neighbourhood: "some address_neighbourhood",
13
+ address_city: "some address_city",
14
+ address_state: "RS",
15
+ address_country: "BRA",
16
+ address_cep: "92123456",
17
+ address_phone: "5130405060"
18
+ )
19
+
20
+ assert_equal "some id", payer.id
21
+ assert_equal "some name", payer.name
22
+ assert_equal "some email", payer.email
23
+ assert_equal "some address_street", payer.address_street
24
+ assert_equal "some address_street_number", payer.address_street_number
25
+ assert_equal "some address_street_extra", payer.address_street_extra
26
+ assert_equal "some address_neighbourhood", payer.address_neighbourhood
27
+ assert_equal "some address_city", payer.address_city
28
+ assert_equal "RS", payer.address_state
29
+ assert_equal "BRA", payer.address_country
30
+ assert_equal "92123456", payer.address_cep
31
+ assert_equal "5130405060", payer.address_phone
32
+ end
33
+
34
+ def test_initialization_and_setters_with_string_keys
35
+ payer = MyMoip::Payer.new(
36
+ 'id' => 'some id',
37
+ 'name' => 'some name',
38
+ 'email' => 'some email',
39
+ 'address_street' => 'some address_street',
40
+ 'address_street_number' => 'some address_street_number',
41
+ 'address_street_extra' => 'some address_street_extra',
42
+ 'address_neighbourhood' => 'some address_neighbourhood',
43
+ 'address_city' => 'some address_city',
44
+ 'address_state' => 'RS',
45
+ 'address_country' => 'BRA',
46
+ 'address_cep' => '92123456',
47
+ 'address_phone' => '5130405060'
48
+ )
49
+
50
+ assert_equal "some id", payer.id
51
+ assert_equal "some name", payer.name
52
+ assert_equal "some email", payer.email
53
+ assert_equal "some address_street", payer.address_street
54
+ assert_equal "some address_street_number", payer.address_street_number
55
+ assert_equal "some address_street_extra", payer.address_street_extra
56
+ assert_equal "some address_neighbourhood", payer.address_neighbourhood
57
+ assert_equal "some address_city", payer.address_city
58
+ assert_equal "RS", payer.address_state
59
+ assert_equal "BRA", payer.address_country
60
+ assert_equal "92123456", payer.address_cep
61
+ assert_equal "5130405060", payer.address_phone
62
+ end
63
+
64
+ def test_validate_presence_of_id_attribute
65
+ subject = Fixture.payer
66
+ subject.id = nil
67
+ assert subject.invalid?, 'should be invalid without an id'
68
+ subject.id = ''
69
+ assert subject.invalid?, 'should be invalid without an id'
70
+ end
71
+
72
+ def test_validate_presence_of_name_attribute
73
+ subject = Fixture.payer
74
+ subject.name = nil
75
+ assert subject.invalid?, 'should be invalid without an name'
76
+ subject.name = ''
77
+ assert subject.invalid?, 'should be invalid without an name'
78
+ end
79
+
80
+ def test_validate_presence_of_email_attribute
81
+ subject = Fixture.payer
82
+ subject.email = nil
83
+ assert subject.invalid?, 'should be invalid without an email'
84
+ subject.email = ''
85
+ assert subject.invalid?, 'should be invalid without an email'
86
+ end
87
+
88
+ def test_validate_presence_of_address_street_attribute
89
+ subject = Fixture.payer
90
+ subject.address_street = nil
91
+ assert subject.invalid?, 'should be invalid without an address_street'
92
+ subject.address_street = ''
93
+ assert subject.invalid?, 'should be invalid without an address_street'
94
+ end
95
+
96
+ def test_validate_presence_of_address_street_number_attribute
97
+ subject = Fixture.payer
98
+ subject.address_street_number = nil
99
+ assert subject.invalid?, 'should be invalid without an address_street_number'
100
+ subject.address_street_number = ''
101
+ assert subject.invalid?, 'should be invalid without an address_street_number'
102
+ end
103
+
104
+ def test_validate_presence_of_address_neighbourhood_attribute
105
+ subject = Fixture.payer
106
+ subject.address_neighbourhood = nil
107
+ assert subject.invalid?, 'should be invalid without an address_neighbourhood'
108
+ subject.address_neighbourhood = ''
109
+ assert subject.invalid?, 'should be invalid without an address_neighbourhood'
110
+ end
111
+
112
+ def test_validate_presence_of_address_city_attribute
113
+ subject = Fixture.payer
114
+ subject.address_city = nil
115
+ assert subject.invalid?, 'should be invalid without an address_city'
116
+ subject.address_city = ''
117
+ assert subject.invalid?, 'should be invalid without an address_city'
118
+ end
119
+
120
+ def test_validate_presence_of_address_state_attribute
121
+ subject = Fixture.payer
122
+ subject.address_state = nil
123
+ assert subject.invalid?, 'should be invalid without an address_state'
124
+ subject.address_state = ''
125
+ assert subject.invalid?, 'should be invalid without an address_state'
126
+ end
127
+
128
+ def test_validate_length_of_address_state_attribute_in_2_chars
129
+ subject = Fixture.payer
130
+ subject.address_state = 'RS'
131
+ assert subject.valid?, 'should accept 2 chars'
132
+ subject.address_state = 'RSS'
133
+ assert subject.invalid? && subject.errors[:address_state].present?,
134
+ 'should not accept strings with other than 2 chars'
135
+ end
136
+
137
+ def test_upcase_assigned_address_state
138
+ subject = Fixture.payer
139
+ subject.address_state = 'rs'
140
+ assert_equal 'RS', subject.address_state
141
+ end
142
+
143
+ def test_validate_presence_of_address_country_attribute
144
+ subject = Fixture.payer
145
+ subject.address_country = nil
146
+ assert subject.invalid?, 'should be invalid without an address_country'
147
+ subject.address_country = ''
148
+ assert subject.invalid?, 'should be invalid without an address_country'
149
+ end
150
+
151
+ def test_validate_length_of_address_country_attribute_in_3_chars
152
+ subject = Fixture.payer
153
+ subject.address_country = 'BRA'
154
+ assert subject.valid?, 'should accept 3 chars'
155
+ subject.address_country = 'BR'
156
+ assert subject.invalid? && subject.errors[:address_country].present?,
157
+ 'should not accept strings with other than 3 chars'
158
+ end
159
+
160
+ def test_upcase_assigned_address_country
161
+ subject = Fixture.payer
162
+ subject.address_country = 'bra'
163
+ assert_equal 'BRA', subject.address_country
164
+ end
165
+
166
+ def test_validate_presence_of_address_cep_attribute
167
+ subject = Fixture.payer
168
+ subject.address_cep = nil
169
+ assert subject.invalid?, 'should be invalid without an address_cep'
170
+ subject.address_cep = ''
171
+ assert subject.invalid?, 'should be invalid without an address_cep'
172
+ end
173
+
174
+ def test_validate_length_of_address_cep_attribute_in_2_chars
175
+ subject = Fixture.payer
176
+ subject.address_cep = '92123456'
177
+ assert subject.valid?, 'should accept 8 chars'
178
+ subject.address_cep = '921234560000'
179
+ assert subject.invalid? && subject.errors[:address_cep].present?,
180
+ 'should not accept strings with other than 8 chars'
181
+ end
182
+
183
+ def test_dont_count_dashes_in_the_address_cep_length_validation
184
+ subject = Fixture.payer
185
+ subject.address_cep = '92123-456'
186
+ assert subject.valid?
187
+ end
188
+
189
+ def test_remove_dashes_from_address_cep
190
+ subject = Fixture.payer
191
+ subject.address_cep = '92123-456'
192
+ assert_equal '92123456', subject.address_cep
193
+ end
194
+
195
+ def test_validate_presence_of_address_phone_attribute
196
+ subject = Fixture.payer
197
+ subject.address_phone = nil
198
+ assert subject.invalid?, 'should be invalid without an address_phone'
199
+ subject.address_phone = ''
200
+ assert subject.invalid?, 'should be invalid without an address_phone'
201
+ end
202
+
203
+ def test_validate_length_of_address_phone_attribute_in_10_or_11_chars
204
+ subject = Fixture.payer
205
+ subject.address_phone = '5130405060'
206
+ assert subject.valid?, 'should accept 10 chars'
207
+ subject.address_phone = '51930405060'
208
+ assert subject.valid?, 'should accept 11 chars'
209
+ subject.address_phone = '215130405060'
210
+ assert subject.invalid? && subject.errors[:address_phone].present?,
211
+ 'should not accept strings with other than 10 or 11 chars'
212
+ end
213
+
214
+ def test_remove_left_zeros_from_address_phone
215
+ subject = Fixture.payer
216
+ subject.address_phone = '05130405060'
217
+ assert_equal '5130405060', subject.address_phone
218
+ end
219
+
220
+ def test_remove_dashes_from_address_phone
221
+ subject = Fixture.payer
222
+ subject.address_phone = '513040-5060'
223
+ assert_equal '5130405060', subject.address_phone
224
+ subject.address_phone = '5193040-5060'
225
+ assert_equal '51930405060', subject.address_phone
226
+ end
227
+
228
+ def test_remove_parenthesis_from_address_phone
229
+ subject = Fixture.payer
230
+ subject.address_phone = '(51)30405060'
231
+ assert_equal '5130405060', subject.address_phone
232
+ subject.address_phone = '(51)930405060'
233
+ assert_equal '51930405060', subject.address_phone
234
+ end
235
+
236
+ def test_to_xml_method_uses_the_formatted_version_of_the_address_cep
237
+ subject = Fixture.payer(address_cep: '92000123')
238
+ formatter = stub_everything('formatter')
239
+ formatter.expects(:cep).with('92000123')
240
+ subject.to_xml(nil, formatter)
241
+ end
242
+
243
+ def test_to_xml_method_uses_the_formatted_version_of_the_address_phone
244
+ subject = Fixture.payer(address_phone: '5130405060')
245
+ formatter = stub_everything('formatter')
246
+ formatter.expects(:phone).with('5130405060')
247
+ subject.to_xml(nil, formatter)
248
+ end
249
+ end