mirror of
https://github.com/internetee/registry.git
synced 2025-06-06 20:55:44 +02:00
Merge branch 'master' into update-ruby-to-2-4
This commit is contained in:
commit
0962ea3fd0
5 changed files with 102 additions and 51 deletions
|
@ -10,12 +10,12 @@ class Registrar
|
||||||
@deposit = Deposit.new(deposit_params.merge(registrar: current_user.registrar))
|
@deposit = Deposit.new(deposit_params.merge(registrar: current_user.registrar))
|
||||||
@invoice = @deposit.issue_prepayment_invoice
|
@invoice = @deposit.issue_prepayment_invoice
|
||||||
|
|
||||||
if @invoice&.persisted?
|
if @invoice
|
||||||
flash[:notice] = t(:please_pay_the_following_invoice)
|
flash[:notice] = t(:please_pay_the_following_invoice)
|
||||||
redirect_to [:registrar, @invoice]
|
redirect_to [:registrar, @invoice]
|
||||||
else
|
else
|
||||||
flash.now[:alert] = t(:failed_to_create_record)
|
flash[:alert] = @deposit.errors.full_messages.join(', ')
|
||||||
render 'new'
|
redirect_to new_registrar_deposit_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,13 +4,17 @@ class Deposit
|
||||||
extend ActiveModel::Naming
|
extend ActiveModel::Naming
|
||||||
include DisableHtml5Validation
|
include DisableHtml5Validation
|
||||||
|
|
||||||
attr_accessor :amount, :description, :registrar, :registrar_id
|
attr_accessor :description, :registrar, :registrar_id
|
||||||
|
attr_writer :amount
|
||||||
|
|
||||||
validates :amount, :registrar, presence: true
|
validates :amount, :registrar, presence: true
|
||||||
validate :validate_amount
|
validate :validate_amount
|
||||||
|
|
||||||
def validate_amount
|
def validate_amount
|
||||||
return if BigDecimal.new(amount) >= Setting.minimum_deposit
|
minimum_allowed_amount = [0.01, Setting.minimum_deposit].max
|
||||||
errors.add(:amount, I18n.t(:is_too_small_minimum_deposit_is, amount: Setting.minimum_deposit, currency: 'EUR'))
|
return if amount >= minimum_allowed_amount
|
||||||
|
errors.add(:amount, I18n.t(:is_too_small_minimum_deposit_is, amount: minimum_allowed_amount,
|
||||||
|
currency: 'EUR'))
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(attributes = {})
|
def initialize(attributes = {})
|
||||||
|
@ -24,10 +28,12 @@ class Deposit
|
||||||
end
|
end
|
||||||
|
|
||||||
def amount
|
def amount
|
||||||
BigDecimal.new(@amount.to_s.sub(/,/, '.'))
|
return BigDecimal('0.0') if @amount.blank?
|
||||||
|
BigDecimal(@amount.to_s.tr(',', '.'), 10)
|
||||||
end
|
end
|
||||||
|
|
||||||
def issue_prepayment_invoice
|
def issue_prepayment_invoice
|
||||||
valid? && registrar.issue_prepayment_invoice(amount, description)
|
return unless valid?
|
||||||
|
registrar.issue_prepayment_invoice(amount, description)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
require 'rails_helper'
|
|
||||||
|
|
||||||
describe Deposit do
|
|
||||||
context 'with invalid attribute' do
|
|
||||||
before :all do
|
|
||||||
@deposit = Deposit.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should not be valid' do
|
|
||||||
@deposit.valid?
|
|
||||||
@deposit.errors.full_messages.should match_array([
|
|
||||||
"Registrar is missing"
|
|
||||||
])
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should have 0 amount' do
|
|
||||||
@deposit.amount.should == 0
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should not be presisted' do
|
|
||||||
@deposit.persisted?.should == false
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should replace comma with point for 0' do
|
|
||||||
@deposit.amount = '0,0'
|
|
||||||
@deposit.amount.should == 0.0
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should replace comma with points' do
|
|
||||||
@deposit.amount = '10,11'
|
|
||||||
@deposit.amount.should == 10.11
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should work with float as well' do
|
|
||||||
@deposit.amount = 0.123
|
|
||||||
@deposit.amount.should == 0.123
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
59
test/models/deposit_test.rb
Normal file
59
test/models/deposit_test.rb
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class DepositTest < ActiveSupport::TestCase
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
|
||||||
|
@deposit = Deposit.new(registrar: registrars(:bestnames))
|
||||||
|
@minimum_deposit = Setting.minimum_deposit
|
||||||
|
Setting.minimum_deposit = 1.00
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
super
|
||||||
|
|
||||||
|
Setting.minimum_deposit = @minimum_deposit
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_validate_amount_cannot_be_lower_than_0_01
|
||||||
|
Setting.minimum_deposit = 0.0
|
||||||
|
@deposit.amount = -10
|
||||||
|
refute(@deposit.valid?)
|
||||||
|
assert(@deposit.errors.full_messages.include?("Amount is too small. Minimum deposit is 0.01 EUR"))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_validate_amount_cannot_be_lower_than_minimum_deposit
|
||||||
|
@deposit.amount = 0.10
|
||||||
|
refute(@deposit.valid?)
|
||||||
|
|
||||||
|
assert(@deposit.errors.full_messages.include?("Amount is too small. Minimum deposit is 1.0 EUR"))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_registrar_must_be_set
|
||||||
|
deposit = Deposit.new(amount: 120)
|
||||||
|
refute(deposit.valid?)
|
||||||
|
|
||||||
|
assert(deposit.errors.full_messages.include?("Registrar is missing"))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_amount_is_converted_from_string
|
||||||
|
@deposit.amount = "12.00"
|
||||||
|
assert_equal(BigDecimal.new("12.00"), @deposit.amount)
|
||||||
|
|
||||||
|
@deposit.amount = "12,11"
|
||||||
|
assert_equal(BigDecimal.new("12.11"), @deposit.amount)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_amount_is_converted_from_float
|
||||||
|
@deposit.amount = 12.0044
|
||||||
|
assert_equal(BigDecimal.new("12.0044"), @deposit.amount)
|
||||||
|
|
||||||
|
@deposit.amount = 12.0144
|
||||||
|
assert_equal(BigDecimal.new("12.0144"), @deposit.amount)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_amount_is_converted_from_nil
|
||||||
|
@deposit.amount = nil
|
||||||
|
assert_equal(BigDecimal.new("0.00"), @deposit.amount)
|
||||||
|
end
|
||||||
|
end
|
|
@ -29,11 +29,10 @@ class NewInvoiceTest < ApplicationSystemTestCase
|
||||||
assert_text 'Pay invoice'
|
assert_text 'Pay invoice'
|
||||||
end
|
end
|
||||||
|
|
||||||
# This test case should fail once issue #651 gets fixed
|
def test_create_new_invoice_with_comma_in_number
|
||||||
def test_create_new_invoice_with_amount_0_goes_through
|
|
||||||
visit registrar_invoices_path
|
visit registrar_invoices_path
|
||||||
click_link_or_button 'Add deposit'
|
click_link_or_button 'Add deposit'
|
||||||
fill_in 'Amount', with: '0.00'
|
fill_in 'Amount', with: '200,00'
|
||||||
fill_in 'Description', with: 'My first invoice'
|
fill_in 'Description', with: 'My first invoice'
|
||||||
|
|
||||||
assert_difference 'Invoice.count', 1 do
|
assert_difference 'Invoice.count', 1 do
|
||||||
|
@ -42,7 +41,33 @@ class NewInvoiceTest < ApplicationSystemTestCase
|
||||||
|
|
||||||
assert_text 'Please pay the following invoice'
|
assert_text 'Please pay the following invoice'
|
||||||
assert_text 'Invoice no. 131050'
|
assert_text 'Invoice no. 131050'
|
||||||
assert_text 'Subtotal 0,00 €'
|
assert_text 'Subtotal 200,00 €'
|
||||||
assert_text 'Pay invoice'
|
assert_text 'Pay invoice'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_create_new_invoice_fails_when_amount_is_0
|
||||||
|
visit registrar_invoices_path
|
||||||
|
click_link_or_button 'Add deposit'
|
||||||
|
fill_in 'Amount', with: '0.00'
|
||||||
|
fill_in 'Description', with: 'My first invoice'
|
||||||
|
|
||||||
|
assert_no_difference 'Invoice.count' do
|
||||||
|
click_link_or_button 'Add'
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_text 'Amount is too small. Minimum deposit is 0.01 EUR'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_create_new_invoice_fails_when_amount_is_negative
|
||||||
|
visit registrar_invoices_path
|
||||||
|
click_link_or_button 'Add deposit'
|
||||||
|
fill_in 'Amount', with: '-120.00'
|
||||||
|
fill_in 'Description', with: 'My first invoice'
|
||||||
|
|
||||||
|
assert_no_difference 'Invoice.count' do
|
||||||
|
click_link_or_button 'Add'
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_text 'Amount is too small. Minimum deposit is 0.01 EUR'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue