Add balance auto reload

Closes #329
This commit is contained in:
Artur Beljajev 2018-09-06 12:09:57 +03:00
parent 19f9a4eb71
commit 62c38d1f99
29 changed files with 660 additions and 16 deletions

View file

@ -0,0 +1,32 @@
require 'test_helper'
class RegistrarAreaSettingsBalanceAutoReloadIntegrationTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
setup do
@registrar = registrars(:bestnames)
sign_in users(:api_bestnames)
end
def test_updates_balance_auto_reload_setting
amount = 100
threshold = 10
assert_nil @registrar.settings['balance_auto_reload']
patch registrar_settings_balance_auto_reload_path, { type: { amount: amount,
threshold: threshold } }
@registrar.reload
assert_equal amount, @registrar.settings['balance_auto_reload']['type']['amount']
assert_equal threshold, @registrar.settings['balance_auto_reload']['type']['threshold']
end
def test_disables_balance_auto_reload_setting
@registrar.update!(settings: { balance_auto_reload: { amount: 'any', threshold: 'any' } })
delete registrar_settings_balance_auto_reload_path
@registrar.reload
assert_nil @registrar.settings['balance_auto_reload']
end
end

View file

@ -0,0 +1,62 @@
require 'test_helper'
class BalanceAutoReloadTypes::ThresholdTest < ActiveSupport::TestCase
setup do
@original_min_reload_amount = Setting.minimum_deposit
end
teardown do
Setting.minimum_deposit = @original_min_reload_amount
end
def test_valid_fixture_is_valid
assert valid_type.valid?
end
def test_invalid_without_amount
type = valid_type
type.amount = nil
assert type.invalid?
end
def test_invalid_when_amount_is_smaller_than_required_minimum
type = valid_type
Setting.minimum_deposit = 0.02
type.amount = 0.01
assert type.invalid?
end
def test_valid_when_amount_equals_allowed_minimum
type = valid_type
Setting.minimum_deposit = 0.02
type.amount = 0.02
assert type.valid?
end
def test_invalid_without_threshold
type = valid_type
type.threshold = nil
assert type.invalid?
end
def test_invalid_when_threshold_is_less_than_zero
type = valid_type
type.threshold = -1
assert type.invalid?
end
def test_serializes_to_json
type = BalanceAutoReloadTypes::Threshold.new(amount: 100, threshold: 10)
assert_equal ({ name: 'threshold', amount: 100, threshold: 10 }).to_json, type.to_json
end
private
def valid_type
BalanceAutoReloadTypes::Threshold.new(amount: Setting.minimum_deposit, threshold: 0)
end
end

View file

@ -1,8 +1,13 @@
require 'test_helper'
class BankTransactionTest < ActiveSupport::TestCase
def test_matches_against_invoice_reference_number
invoices(:one).update!(account_activity: nil, number: '2222', total: 10, reference_no: '1111')
setup do
@registrar = registrars(:bestnames)
@invoice = invoices(:one)
end
def test_matches_against_invoice_number_and_reference_number
create_payable_invoice(number: '2222', total: 10, reference_no: '1111')
transaction = BankTransaction.new(description: 'invoice #2222', sum: 10, reference_no: '1111')
assert_difference 'AccountActivity.count' do
@ -10,8 +15,19 @@ class BankTransactionTest < ActiveSupport::TestCase
end
end
def test_resets_pending_registrar_balance_reload
registrar = registrar_with_pending_balance_auto_reload
create_payable_invoice(number: '2222', total: 10, reference_no: '1111')
transaction = BankTransaction.new(description: 'invoice #2222', sum: 10, reference_no: '1111')
transaction.autobind_invoice
registrar.reload
assert_nil registrar.settings['balance_auto_reload']['pending']
end
def test_does_not_match_against_registrar_reference_number
registrars(:bestnames).update!(reference_no: '1111')
@registrar.update!(reference_no: '1111')
transaction = BankTransaction.new(description: 'invoice #2222', sum: 10, reference_no: '1111')
assert_no_difference 'AccountActivity.count' do
@ -20,7 +36,7 @@ class BankTransactionTest < ActiveSupport::TestCase
end
def test_underpayment_is_not_matched_with_invoice
invoices(:one).update!(account_activity: nil, number: '2222', total: 10)
create_payable_invoice(number: '2222', total: 10)
transaction = BankTransaction.new(sum: 9)
assert_no_difference 'AccountActivity.count' do
@ -30,7 +46,7 @@ class BankTransactionTest < ActiveSupport::TestCase
end
def test_overpayment_is_not_matched_with_invoice
invoices(:one).update!(account_activity: nil, number: '2222', total: 10)
create_payable_invoice(number: '2222', total: 10)
transaction = BankTransaction.new(sum: 11)
assert_no_difference 'AccountActivity.count' do
@ -40,7 +56,7 @@ class BankTransactionTest < ActiveSupport::TestCase
end
def test_cancelled_invoice_is_not_matched
invoices(:one).update!(account_activity: nil, number: '2222', total: 10, cancelled_at: '2010-07-05')
@invoice.update!(account_activity: nil, number: '2222', total: 10, cancelled_at: '2010-07-05')
transaction = BankTransaction.new(sum: 10)
assert_no_difference 'AccountActivity.count' do
@ -48,4 +64,17 @@ class BankTransactionTest < ActiveSupport::TestCase
end
assert transaction.errors.full_messages.include?('Cannot bind cancelled invoice')
end
private
def create_payable_invoice(attributes)
payable_attributes = { account_activity: nil }
@invoice.update!(payable_attributes.merge(attributes))
@invoice
end
def registrar_with_pending_balance_auto_reload
@registrar.update!(settings: { balance_auto_reload: { pending: true } })
@registrar
end
end

View file

@ -176,6 +176,12 @@ class RegistrarTest < ActiveSupport::TestCase
assert_equal vat_country, registrar.vat_country
end
def test_returns_iban_for_e_invoice_delivery_channel
iban = 'GB33BUKB20201555555555'
registrar = Registrar.new(iban: iban)
assert_equal iban, registrar.e_invoice_iban
end
private
def valid_registrar

View file

@ -0,0 +1,74 @@
require 'test_helper'
class RegistrarAreaSettingsBalanceAutoReloadTest < ApplicationSystemTestCase
setup do
@registrar = registrars(:bestnames)
@user = users(:api_bestnames)
sign_in @user
end
def test_enables_balance_auto_reload
amount = 100
threshold = 10
assert_nil @registrar.settings['balance_auto_reload']
visit registrar_account_path
click_on 'Enable'
fill_in 'Amount', with: amount
fill_in 'Threshold', with: threshold
click_button 'Save'
assert_current_path registrar_account_path
assert_text 'Balance Auto-Reload setting has been updated'
# Using `number_to_currency` leads to `expected to find text "Reload 100,00 € when your balance
# drops to 10,00 €" in "...Reload 100,00 € when your balance drops to 10,00 €...`
assert_text 'Reload 100,00 € when your balance drops to 10,00 €'
end
def test_disables_balance_auto_reload
@registrar.update!(settings: { balance_auto_reload: { type: {} } })
visit registrar_account_path
click_on 'Disable'
assert_current_path registrar_account_path
assert_text 'Balance Auto-Reload setting has been disabled'
end
def test_edits_balance_auto_reload
@registrar.update!(settings: { balance_auto_reload: { type: { name: 'threshold',
amount: 100,
threshold: 10 } } })
visit registrar_account_path
within '.balance-auto-reload' do
click_on 'Edit'
end
fill_in 'Amount', with: '101'
fill_in 'Threshold', with: '11'
click_button 'Save'
assert_current_path registrar_account_path
assert_text 'Balance Auto-Reload setting has been updated'
end
def test_form_is_pre_populated_when_editing
amount = 100
threshold = 10
@registrar.update!(settings: { balance_auto_reload: { type: { name: 'threshold',
amount: amount,
threshold: threshold } } })
visit edit_registrar_settings_balance_auto_reload_path
assert_field 'Amount', with: amount
assert_field 'Threshold', with: threshold
end
def test_user_of_epp_role_cannot_edit_balance_auto_reload_setting
@user.update!(roles: [ApiUser::EPP])
visit registrar_account_path
assert_no_text 'Balance Auto-Reload'
end
end

View file

@ -0,0 +1,79 @@
require 'test_helper'
class ReloadBalanceTaskTest < ActiveSupport::TestCase
include ActionView::Helpers::NumberHelper
setup do
@registrar = registrars(:bestnames)
EInvoice.provider = EInvoice::Providers::TestProvider.new
end
def test_issues_invoice_when_auto_reload_is_enabled_and_threshold_reached
reload_amount = 100
registrar = registrar_with_auto_reload_enabled_and_threshold_reached(reload_amount)
assert_difference -> { registrar.invoices.count } do
capture_io { run_task }
end
invoice = registrar.invoices.last
assert_equal reload_amount, invoice.subtotal
end
def test_skips_issuing_invoice_when_threshold_is_not_reached
registrar = registrar_with_auto_reload_enabled_and_threshold_not_reached
assert_no_difference -> { registrar.invoices.count } do
capture_io { run_task }
end
end
def test_skips_issuing_invoice_when_balance_reload_is_pending
registrar = registrar_with_auto_reload_enabled_and_threshold_reached
registrar.settings['balance_auto_reload']['pending'] = true
registrar.save!
assert_no_difference -> { registrar.invoices.count } do
capture_io { run_task }
end
end
def test_marks_registrar_as_pending_balance_reload
registrar = registrar_with_auto_reload_enabled_and_threshold_reached
capture_io { run_task }
registrar.reload
assert registrar.settings['balance_auto_reload']['pending']
end
def test_output
reload_amount = 100
registrar = registrar_with_auto_reload_enabled_and_threshold_reached(reload_amount)
assert_equal 'Best Names', registrar.name
assert_output %(Registrar "Best Names" got #{number_to_currency(reload_amount, unit: 'EUR')}\nInvoiced total: 1\n) do
run_task
end
end
private
def registrar_with_auto_reload_enabled_and_threshold_reached(reload_amount = 100)
auto_reload_type = BalanceAutoReloadTypes::Threshold.new(amount: reload_amount, threshold: 10)
@registrar.update!(settings: { balance_auto_reload: { type: auto_reload_type } })
@registrar.accounts.first.update!(balance: 10)
@registrar
end
def registrar_with_auto_reload_enabled_and_threshold_not_reached
auto_reload_type = BalanceAutoReloadTypes::Threshold.new(amount: 100, threshold: 10)
@registrar.update!(settings: { balance_auto_reload: { type: auto_reload_type } })
@registrar.accounts.first.update!(balance: 11)
@registrar
end
def run_task
Rake::Task['registrars:reload_balance'].execute
end
end