From 94298f5def79ee9a115022ae30d35aa9666f338c Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 5 Oct 2022 15:57:48 -0500 Subject: [PATCH] Refactor alt top up to use FormTemplate --- forms/admin_payment_methods.rb | 4 +- forms/alt_top_up.rb | 29 +++++++ forms/alt_top_up/btc_addresses.rb | 15 ++++ forms/alt_top_up/interac.rb | 7 ++ forms/alt_top_up/mailing_address.rb | 9 +++ forms/registration/mail.rb | 13 ++++ lib/alt_top_up_form.rb | 113 +++------------------------- lib/registration.rb | 23 ++---- test/test_add_bitcoin_address.rb | 4 +- test/test_alt_top_up_form.rb | 14 ++-- test/test_helper.rb | 4 +- test/test_registration.rb | 35 +++++++++ 12 files changed, 135 insertions(+), 135 deletions(-) create mode 100644 forms/alt_top_up.rb create mode 100644 forms/alt_top_up/btc_addresses.rb create mode 100644 forms/alt_top_up/interac.rb create mode 100644 forms/alt_top_up/mailing_address.rb create mode 100644 forms/registration/mail.rb diff --git a/forms/admin_payment_methods.rb b/forms/admin_payment_methods.rb index cb99c8f..033324f 100644 --- a/forms/admin_payment_methods.rb +++ b/forms/admin_payment_methods.rb @@ -5,6 +5,4 @@ unless @payment_methods.empty? field @payment_methods.to_list_single(label: "Credit Cards") end -AltTopUpForm::HasBitcoinAddresses.new(@btc_addresses, desc: nil).each do |spec| - field spec -end +render "alt_top_up/btc_addresses", btc_addresses: @btc_addresses diff --git a/forms/alt_top_up.rb b/forms/alt_top_up.rb new file mode 100644 index 0000000..b7f0602 --- /dev/null +++ b/forms/alt_top_up.rb @@ -0,0 +1,29 @@ +form! +title "Buy Account Credit" + +instructions( + "Besides credit cards, we support payment by Bitcoin, postal mail, " \ + "or in Canada by Interac e-Transfer." +) + +field( + type: "fixed", + value: "Current balance: $#{'%.4f' % @balance}" +) + +render "alt_top_up/mailing_address" +render "alt_top_up/interac" if @currency == :CAD +render "alt_top_up/btc_addresses" + +add_btc_label = if !@btc_addresses || @btc_addresses.empty? + "You have no Bitcoin addresses, would you like to create one?" +else + "Or, create a new Bitcoin address?" +end + +field( + var: "add_btc_address", + label: add_btc_label, + type: "boolean", + value: false +) diff --git a/forms/alt_top_up/btc_addresses.rb b/forms/alt_top_up/btc_addresses.rb new file mode 100644 index 0000000..381fe11 --- /dev/null +++ b/forms/alt_top_up/btc_addresses.rb @@ -0,0 +1,15 @@ +DESCRIPTION = + "You can make a Bitcoin payment of any amount to any " \ + "of these addresses and it will be credited to your " \ + "account at the Canadian Bitcoins exchange rate within 5 " \ + "minutes of your transaction reaching 3 confirmations.".freeze + +if @btc_addresses && !@btc_addresses.empty? + field( + var: "btc_addresses", + type: "fixed", + label: "Bitcoin Addresses", + description: DESCRIPTION, + value: @btc_addresses + ) +end diff --git a/forms/alt_top_up/interac.rb b/forms/alt_top_up/interac.rb new file mode 100644 index 0000000..18fb766 --- /dev/null +++ b/forms/alt_top_up/interac.rb @@ -0,0 +1,7 @@ +field( + var: "interac_email", + type: "fixed", + label: "Interac e-Transfer Address", + description: "Please include your Jabber ID in the note", + value: CONFIG[:interac] +) diff --git a/forms/alt_top_up/mailing_address.rb b/forms/alt_top_up/mailing_address.rb new file mode 100644 index 0000000..1feb7e6 --- /dev/null +++ b/forms/alt_top_up/mailing_address.rb @@ -0,0 +1,9 @@ +field( + var: "adr", + type: "fixed", + label: "Mailing Address", + description: + "Make payable to #{CONFIG[:payable]} and include your " \ + "Jabber ID in the mailing somewhere.", + value: CONFIG[:adr] +) diff --git a/forms/registration/mail.rb b/forms/registration/mail.rb new file mode 100644 index 0000000..eef0dfd --- /dev/null +++ b/forms/registration/mail.rb @@ -0,0 +1,13 @@ +result! +title "Activate by Mail or Interac e-Tranfer" + +instructions( + "Activate your account by sending at least " \ + "$#{CONFIG[:activation_amount]}\nWe support payment by " \ + "postal mail or, in Canada, by Interac e-Transfer.\n\n" \ + "You will receive a notification when your payment is complete." \ + "#{@final_message}" +) + +render "alt_top_up/mailing_address" +render "alt_top_up/interac" if @currency == :CAD diff --git a/lib/alt_top_up_form.rb b/lib/alt_top_up_form.rb index 7c41d09..f39c08b 100644 --- a/lib/alt_top_up_form.rb +++ b/lib/alt_top_up_form.rb @@ -3,29 +3,23 @@ class AltTopUpForm def self.for(customer) customer.btc_addresses.then do |addrs| - AltTopUpForm.new(customer, *[ - (IS_CAD if customer.currency == :CAD), - (HasBitcoinAddresses.new(addrs) unless addrs.empty?), - AddBtcAddressField.for(addrs) - ].compact) + AltTopUpForm.new(customer, addrs) end end - def initialize(customer, *fields) - @fields = fields + def initialize(customer, btc_addresses) @balance = customer.balance + @currency = customer.currency + @btc_addresses = btc_addresses end def form - form = Blather::Stanza::X.new(:result) - form.type = :form - form.title = "Buy Account Credit" - form.instructions = - "Besides credit cards, we support payment by Bitcoin, postal mail, " \ - "or in Canada by Interac e-Transfer." - - form.fields = fields.to_a - form + FormTemplate.render( + "alt_top_up", + balance: @balance, + currency: @currency, + btc_addresses: @btc_addresses + ) end def parse(form) @@ -35,91 +29,4 @@ class AltTopUpForm ) } end - - def balance - { - type: "fixed", - value: "Current balance: $#{'%.4f' % @balance}" - } - end - - MAILING_ADDRESS = { - var: "adr", - type: "fixed", - label: "Mailing Address", - description: - "Make payable to #{CONFIG[:payable]} and include your " \ - "Jabber ID in the mailing somewhere.", - value: CONFIG[:adr] - }.freeze - - def fields - Enumerator.new do |y| - y << balance - y << MAILING_ADDRESS - @fields.each do |fs| - fs.each { |f| y << f } - end - end - end - - IS_CAD = [{ - var: "adr", - type: "fixed", - label: "Interac e-Transfer Address", - description: "Please include your Jabber ID in the note", - value: CONFIG[:interac] - }].freeze - - class AddBtcAddressField - def self.for(addrs) - if addrs.empty? - AddNewBtcAddressField.new - else - new - end - end - - def each - yield( - var: "add_btc_address", - label: label, - type: "boolean", - value: false - ) - end - - def label - "Or, create a new Bitcoin address?" - end - - class AddNewBtcAddressField < AddBtcAddressField - def label - "You have no Bitcoin addresses, would you like to create one?" - end - end - end - - class HasBitcoinAddresses - def initialize(addrs, desc: DESCRIPTION) - @addrs = addrs - @desc = desc - end - - DESCRIPTION = - "You can make a Bitcoin payment of any amount to any " \ - "of these addresses and it will be credited to your " \ - "account at the Canadian Bitcoins exchange rate within 5 " \ - "minutes of your transaction reaching 3 confirmations." - - def each - yield( - var: "btc_address", - type: "fixed", - label: "Bitcoin Addresses", - description: @desc, - value: @addrs - ) - end - end end diff --git a/lib/registration.rb b/lib/registration.rb index 58c25ab..14fff79 100644 --- a/lib/registration.rb +++ b/lib/registration.rb @@ -406,24 +406,11 @@ class Registration end def form - form = Blather::Stanza::X.new(:result) - form.title = "Activate by Mail or Interac e-Transfer" - form.instructions = - "Activate your account by sending at least " \ - "$#{CONFIG[:activation_amount]}\nWe support payment by " \ - "postal mail or, in Canada, by Interac e-Transfer.\n\n" \ - "You will receive a notification when your payment is complete." \ - "#{@final_message}" - - form.fields = fields.to_a - form - end - - def fields - [ - AltTopUpForm::MAILING_ADDRESS, - AltTopUpForm::IS_CAD - ].flatten + FormTemplate.render( + "registration/mail", + currency: @customer.currency, + final_message: @final_message + ) end def write diff --git a/test/test_add_bitcoin_address.rb b/test/test_add_bitcoin_address.rb index 9576cb6..8fc0791 100644 --- a/test/test_add_bitcoin_address.rb +++ b/test/test_add_bitcoin_address.rb @@ -8,14 +8,14 @@ class AddBitcoinAddressTest < Minitest::Test def test_for iq = Blather::Stanza::Iq::Command.new cust = customer - AddBitcoinAddress.for(iq, AltTopUpForm.new(cust), cust) + AddBitcoinAddress.for(iq, AltTopUpForm.new(cust, []), cust) end def test_for_add_bitcoin iq = Blather::Stanza::Iq::Command.new iq.form.fields = [{ var: "add_btc_address", value: "true" }] cust = customer - AddBitcoinAddress.for(iq, AltTopUpForm.new(cust), cust) + AddBitcoinAddress.for(iq, AltTopUpForm.new(cust, []), cust) end def test_write diff --git a/test/test_alt_top_up_form.rb b/test/test_alt_top_up_form.rb index ab552d8..2ae9d43 100644 --- a/test/test_alt_top_up_form.rb +++ b/test/test_alt_top_up_form.rb @@ -48,8 +48,7 @@ class AltTopUpFormTest < Minitest::Test assert_kind_of( Blather::Stanza::X, AltTopUpForm.new( - customer, - AltTopUpForm::AddBtcAddressField.new + customer, ["some_addr"] ).form ) end @@ -58,8 +57,7 @@ class AltTopUpFormTest < Minitest::Test assert_kind_of( Blather::Stanza::X, AltTopUpForm.new( - customer, - AltTopUpForm::AddBtcAddressField::AddNewBtcAddressField.new + customer, [] ).form ) end @@ -69,7 +67,7 @@ class AltTopUpFormTest < Minitest::Test iq_form.fields = [ { var: "add_btc_address", value: "true" } ] - assert AltTopUpForm.new(customer).parse(iq_form)[:add_btc_address] + assert AltTopUpForm.new(customer, []).parse(iq_form)[:add_btc_address] end def test_parse_1 @@ -77,7 +75,7 @@ class AltTopUpFormTest < Minitest::Test iq_form.fields = [ { var: "add_btc_address", value: "1" } ] - assert AltTopUpForm.new(customer).parse(iq_form)[:add_btc_address] + assert AltTopUpForm.new(customer, []).parse(iq_form)[:add_btc_address] end def test_parse_false @@ -85,11 +83,11 @@ class AltTopUpFormTest < Minitest::Test iq_form.fields = [ { var: "add_btc_address", value: "false" } ] - refute AltTopUpForm.new(customer).parse(iq_form)[:add_btc_address] + refute AltTopUpForm.new(customer, []).parse(iq_form)[:add_btc_address] end def test_parse_not_presend iq_form = Blather::Stanza::X.new - refute AltTopUpForm.new(customer).parse(iq_form)[:add_btc_address] + refute AltTopUpForm.new(customer, []).parse(iq_form)[:add_btc_address] end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 0011f42..907b6fe 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -112,7 +112,9 @@ CONFIG = { }, bandwidth_site: "test_site", bandwidth_peer: "test_peer", - keepgo: { api_key: "keepgokey", access_token: "keepgotoken" } + keepgo: { api_key: "keepgokey", access_token: "keepgotoken" }, + adr: "A Mailing Address", + interac: "interac@example.com" }.freeze def panic(e) diff --git a/test/test_registration.rb b/test/test_registration.rb index 57ff55c..de4de5e 100644 --- a/test/test_registration.rb +++ b/test/test_registration.rb @@ -408,6 +408,41 @@ class RegistrationTest < Minitest::Test em :test_write end + class MailTest < Minitest::Test + def setup + @mail = Registration::Payment::Mail.new( + customer(plan_name: "test_cad"), + "+15555550000" + ) + end + + def test_write + result = execute_command do + Command::COMMAND_MANAGER.expect( + :write, + EMPromise.reject(:test_result), + [Matching.new do |reply| + assert_equal [:execute, :prev], reply.allowed_actions + refute reply.form.instructions.empty? + assert_equal( + "A Mailing Address", + reply.form.field("adr").value + ) + assert_equal( + "interac@example.com", + reply.form.field("interac_email").value + ) + end] + ) + + @mail.write.catch { |e| e } + end + + assert_equal :test_result, result + end + em :test_write + end + class ActivateTest < Minitest::Test Registration::Payment::CreditCard::Activate::Finish = Minitest::Mock.new -- 2.38.5