Merge branch 'master' of github.com:domify/registry

This commit is contained in:
Priit Tark 2015-07-03 10:33:14 +03:00
commit bacddaf2d1
11 changed files with 188 additions and 12 deletions

View file

@ -21,11 +21,18 @@ class Epp::DomainsController < EppController
def create
authorize! :create, Epp::Domain
@domain = Epp::Domain.new_from_epp(params[:parsed_frame], current_user)
@domain.valid?
if @domain.errors.any? || !@domain.save
handle_errors(@domain)
else
render_epp_response '/epp/domains/create'
handle_errors(@domain) and return if @domain.errors.any?
handle_errors and return unless balance_ok?('create')
ActiveRecord::Base.transaction do
if @domain.save
current_user.registrar.debit!(@domain_price, "#{I18n.t('create')} #{@domain.name}")
render_epp_response '/epp/domains/create'
else
handle_errors(@domain)
end
end
end
@ -185,4 +192,16 @@ class Epp::DomainsController < EppController
msg: "#{I18n.t(:client_side_status_editing_error)}: status [status]"
}
end
def balance_ok?(operation)
@domain_price = @domain.price(operation).amount
if current_user.registrar.balance < @domain_price
epp_errors << {
code: '2104',
msg: I18n.t('billing_failure_credit_balance_low')
}
return false
end
true
end
end

View file

@ -3,7 +3,9 @@ class BankTransaction < ActiveRecord::Base
belongs_to :bank_statement
has_one :account_activity
scope :unbinded, -> { where('id NOT IN (SELECT bank_transaction_id FROM account_activities)') }
scope :unbinded, -> {
where('id NOT IN (SELECT bank_transaction_id FROM account_activities where bank_transaction_id IS NOT NULL)')
}
def binded?
account_activity.present?

View file

@ -372,6 +372,17 @@ class Domain < ActiveRecord::Base
DomainMailer.pending_deleted(self).deliver_now
end
def price(operation)
zone = name.split('.').drop(1).join('.')
p = period / 365 if period_unit == 'd'
p = period / 12 if period_unit == 'm'
p = period if period_unit == 'y'
p = "#{p}year"
Pricelist.price_for(zone, operation, p)
end
### VALIDATIONS ###
def validate_nameserver_ips

View file

@ -1,6 +1,8 @@
class Pricelist < ActiveRecord::Base
include Versions # version/pricelist_version.rb
scope :valid, -> { where("valid_from <= ? AND valid_to >= ? OR valid_to IS NULL", Time.zone.now, Time.zone.now) }
monetize :price_cents
validates :price_cents, :price_currency, :price,
@ -13,10 +15,18 @@ class Pricelist < ActiveRecord::Base
after_initialize :init_values
def init_values
return unless new_record?
self.valid_from = Time.zone.now.beginning_of_year
self.valid_from = Time.zone.now.beginning_of_year unless valid_from
end
def name
"#{operation_category} #{category}"
end
class << self
def price_for(zone, operation, period)
lists = valid.where(category: zone, operation_category: operation, duration: period)
return lists.first.price if lists.count == 1
lists.where('valid_to IS NOT NULL').order(valid_from: :desc).first.price
end
end
end

View file

@ -12,6 +12,8 @@ class Registrar < ActiveRecord::Base
has_many :priv_contacts, -> { privs }, class_name: 'Contact'
has_many :white_ips, dependent: :destroy
delegate :balance, to: :cash_account
validates :name, :reg_no, :country_code, :email, :code, presence: true
validates :name, :reg_no, :reference_no, :code, uniqueness: true
validate :forbidden_codes
@ -121,6 +123,22 @@ class Registrar < ActiveRecord::Base
accounts.find_by(account_type: Account::CASH)
end
def debit!(sum, description)
cash_account.account_activities.create!(
sum: -sum,
currency: 'EUR',
description: description
)
end
def credit!(sum, description)
cash_account.account_activities.create!(
sum: sum,
currency: 'EUR',
description: description
)
end
def domain_transfers
at = DomainTransfer.arel_table
DomainTransfer.where(

View file

@ -20,7 +20,7 @@
- if x.invoice
%td= link_to(x.invoice, [:registrar, x.invoice])
- else
%td \-
%td -
- c = x.sum > 0.0 ? 'text-success' : 'text-danger'
- s = x.sum > 0.0 ? "+#{x.sum} #{x.currency}" : "#{x.sum} #{x.currency}"
%td{class: c}= s

View file

@ -854,3 +854,5 @@ en:
registry_zip: 'Postcode'
registry_country_code: 'Country'
blocked_domains: 'Blocked domains'
billing_failure_credit_balance_low: 'Billing failure - credit balance low'
create: 'Create'

View file

@ -5,7 +5,9 @@ describe 'EPP Domain', epp: true do
@xsd = Nokogiri::XML::Schema(File.read('doc/schemas/domain-eis-1.0.xsd'))
@epp_xml = EppXml.new(cl_trid: 'ABC-12345')
@registrar1 = Fabricate(:registrar1, code: 'REGDOMAIN1')
@registrar1.credit!(10000, '')
@registrar2 = Fabricate(:registrar2, code: 'REGDOMAIN2')
@registrar2.credit!(10000, '')
Fabricate(:api_user, username: 'registrar1', registrar: @registrar1)
Fabricate(:api_user, username: 'registrar2', registrar: @registrar2)
@ -17,6 +19,7 @@ describe 'EPP Domain', epp: true do
Fabricate(:contact, code: 'FIXED:JURIDICAL_1234', ident_type: 'bic')
Fabricate(:reserved_domain)
Fabricate(:blocked_domain)
Fabricate(:pricelist)
@uniq_no = proc { @i ||= 0; @i += 1 }
end

View file

@ -1,8 +1,8 @@
Fabricator(:pricelist) do
valid_from 1.year.ago
valid_to 1.year.since
category '.ee'
category 'ee'
duration '1year'
operation_category 'new'
operation_category 'create'
price 10
end

View file

@ -182,6 +182,26 @@ describe Domain do
@domain.force_delete_at.should be_nil
end
it 'should know its price' do
Fabricate(:pricelist, {
category: 'ee',
operation_category: 'create',
duration: '1year',
price: 1.50,
valid_from: Time.zone.parse('2015-01-01'),
valid_to: nil
})
domain = Fabricate(:domain)
domain.price('create').should == 1.50
domain = Fabricate(:domain, period: 12, period_unit: 'm')
domain.price('create').should == 1.50
domain = Fabricate(:domain, period: 365, period_unit: 'd')
domain.price('create').should == 1.50
end
context 'about registrant update confirm' do
before :all do
@domain.registrant_verification_token = 123

View file

@ -36,7 +36,6 @@ describe Pricelist do
it 'should not have name' do
@pricelist.name.should == ' '
end
end
context 'with valid attributes' do
@ -69,4 +68,96 @@ describe Pricelist do
end
end
end
it 'should return correct price' do
expect { Pricelist.price_for('ee', 'create', '1year') }.to raise_error(NoMethodError)
Fabricate(:pricelist, {
category: 'ee',
operation_category: 'create',
duration: '1year',
price: 1.50,
valid_from: Time.zone.parse('2198-01-01'),
valid_to: Time.zone.parse('2199-01-01')
})
expect { Pricelist.price_for('ee', 'create', '1year') }.to raise_error(NoMethodError)
Fabricate(:pricelist, {
category: 'ee',
operation_category: 'create',
duration: '1year',
price: 1.50,
valid_from: Time.zone.parse('2015-01-01'),
valid_to: nil
})
Pricelist.price_for('ee', 'create', '1year').should == 1.50
Fabricate(:pricelist, {
category: 'ee',
operation_category: 'create',
duration: '1year',
price: 1.30,
valid_from: Time.zone.parse('2015-01-01'),
valid_to: Time.zone.parse('2999-01-01')
})
Pricelist.price_for('ee', 'create', '1year').should == 1.30
Fabricate.create(:pricelist, {
category: 'ee',
operation_category: 'create',
duration: '1year',
price: 1.20,
valid_from: Time.zone.parse('2015-06-01'),
valid_to: Time.zone.parse('2999-01-01')
})
Pricelist.price_for('ee', 'create', '1year').should == 1.20
Fabricate.create(:pricelist, {
category: 'ee',
operation_category: 'create',
duration: '1year',
price: 1.10,
valid_from: Time.zone.parse('2014-01-01'),
valid_to: Time.zone.parse('2999-01-01')
})
Pricelist.price_for('ee', 'create', '1year').should == 1.20
Fabricate.create(:pricelist, {
category: 'ee',
operation_category: 'create',
duration: '1year',
price: 1.10,
valid_from: Time.zone.parse('2999-02-01'),
valid_to: Time.zone.parse('2999-01-01')
})
Pricelist.price_for('ee', 'create', '1year').should == 1.20
Fabricate.create(:pricelist, {
category: 'ee',
operation_category: 'create',
duration: '1year',
price: 1.10,
valid_from: Time.zone.parse('2015-06-02'),
valid_to: nil
})
Pricelist.price_for('ee', 'create', '1year').should == 1.20
Fabricate.create(:pricelist, {
category: 'ee',
operation_category: 'create',
duration: '1year',
price: 1.10,
valid_from: Time.zone.parse('2015-07-01'),
valid_to: Time.zone.parse('2999-01-01')
})
Pricelist.price_for('ee', 'create', '1year').should == 1.10
end
end