Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/credit_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CreditMutation < ApplicationRecord
belongs_to :created_by, class_name: 'User'

validates :description, presence: true
validates :amount, presence: true, numericality: { less_than_or_equal_to: 1000 }
validates :amount, presence: true, numericality: { less_than_or_equal_to: 5000 }

validate :activity_not_locked

Expand Down
14 changes: 12 additions & 2 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class << self

validate :user_xor_invoice
validate :user_amount
validate :invoice_amount

scope :not_completed, -> { where.not(status: COMPLETE_STATUSES) }

Expand Down Expand Up @@ -74,10 +75,19 @@ def user_xor_invoice
errors.add(:payment, 'must belong to a user xor invoice') unless user.present? ^ invoice.present?
end

def user_amount
def user_amount # rubocop:disable Metrics/AbcSize
return unless user

min_amount = Rails.application.config.x.min_payment_amount
errors.add(:amount, "must be bigger than or equal to €#{format('%.2f', min_amount)}") unless amount && (amount >= min_amount)
max_amount = Rails.application.config.x.max_payment_amount
errors.add(:amount, "must be greater than or equal to €#{format('%.2f', min_amount)}") unless amount && (amount >= min_amount)
errors.add(:amount, "must be less than or equal to €#{format('%.2f', max_amount)}") unless amount && (amount <= max_amount)
end

def invoice_amount
return unless invoice

min_amount = Rails.application.config.x.min_invoice_amount
errors.add(:amount, "must be greater than or equal to €#{format('%.2f', min_amount)}") unless amount && (amount >= min_amount)
end
end
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class Application < Rails::Application
config.x.deposit_button_enabled = ENV.fetch('DEPOSIT_BUTTON_ENABLED', 'true') == 'true'

config.x.min_payment_amount = [ENV.fetch('MIN_PAYMENT_AMOUNT', '21.8').to_f, 0.01].max
config.x.max_payment_amount = ENV.fetch('MAX_PAYMENT_AMOUNT', '1000').to_f
config.x.min_invoice_amount = [ENV.fetch('MIN_INVOICE_AMOUNT', '1').to_f, 0.01].max

config.x.codes = {
beer: ENV.fetch('CODE_BEER', nil),
Expand Down
14 changes: 11 additions & 3 deletions spec/models/credit_mutation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@
it { expect(mutation).not_to be_valid }
end

context 'when with too high amount' do
subject(:mutation) { build_stubbed(:credit_mutation, amount: 1001) }
context 'when with boundary amounts' do
context 'when at maximum amount' do
subject(:mutation) { build_stubbed(:credit_mutation, amount: 5000) }

it { expect(mutation).not_to be_valid }
it { expect(mutation).to be_valid }
end

context 'when just over maximum amount' do
subject(:mutation) { build_stubbed(:credit_mutation, amount: 5000.01) }

it { expect(mutation).not_to be_valid }
end
end

context 'when with a locked activity' do
Expand Down
40 changes: 40 additions & 0 deletions spec/models/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,46 @@
end
end

context 'when with too high amount' do
context 'when with user' do
subject(:payment) { build_stubbed(:payment, amount: 1001) }

it { expect(payment).not_to be_valid }
end

context 'when with invoice and high amount' do
subject(:payment) { build_stubbed(:payment, :invoice, amount: 1001) }

it { expect(payment).to be_valid }
end
end

context 'when with boundary amounts' do
context 'when with user at max amount' do
subject(:payment) { build_stubbed(:payment, amount: 1000) }

it { expect(payment).to be_valid }
end

context 'when with user just over max amount' do
subject(:payment) { build_stubbed(:payment, amount: 1000.01) }

it { expect(payment).not_to be_valid }
end

context 'when with invoice below minimum' do
subject(:payment) { build_stubbed(:payment, :invoice, amount: 0.99) }

it { expect(payment).not_to be_valid }
end

context 'when with invoice at minimum' do
subject(:payment) { build_stubbed(:payment, :invoice, amount: 1) }

it { expect(payment).to be_valid }
end
end

context 'when without a status' do
subject(:payment) { build_stubbed(:payment, status: nil) }

Expand Down