Merge pull request #552 from internetee/registry-475

Registry 475
This commit is contained in:
Timo Võhmar 2017-06-16 10:39:39 +03:00 committed by GitHub
commit 8420ef6620
29 changed files with 272 additions and 233 deletions

View file

@ -37,7 +37,7 @@ class Epp::DomainsController < EppController
sum: @domain_pricelist.price.amount,
description: "#{I18n.t('create')} #{@domain.name}",
activity_type: AccountActivity::CREATE,
log_pricelist_id: @domain_pricelist.id
price: @domain_pricelist
})
render_epp_response '/epp/domains/create'
@ -124,7 +124,7 @@ class Epp::DomainsController < EppController
sum: @domain_pricelist.price.amount,
description: "#{I18n.t('renew')} #{@domain.name}",
activity_type: AccountActivity::RENEW,
log_pricelist_id: @domain_pricelist.id
price: @domain_pricelist
})
render_epp_response '/epp/domains/renew'

View file

@ -1,6 +1,7 @@
class Account < ActiveRecord::Base
include Versions
belongs_to :registrar
belongs_to :registrar, required: true
has_many :account_activities
validates :account_type, presence: true

View file

@ -2,10 +2,10 @@ require 'csv'
class AccountActivity < ActiveRecord::Base
include Versions
belongs_to :account
belongs_to :account, required: true
belongs_to :bank_transaction
belongs_to :invoice
belongs_to :price, class_name: 'Billing::Price'
CREATE = 'create'
RENEW = 'renew'

View file

@ -1,12 +1,11 @@
module Billing
class Price < ActiveRecord::Base
include Versions
include Concerns::Billing::Price::Expirable
has_paper_trail class_name: '::PriceVersion'
self.auto_html5_validation = false
belongs_to :zone, class_name: 'DNS::Zone', required: true
has_many :account_activities
validates :price, :valid_from, :operation_category, :duration, presence: true
validates :operation_category, inclusion: { in: Proc.new { |price| price.class.operation_categories } }
@ -69,24 +68,16 @@ module Billing
"#{operation_category} #{zone_name}"
end
def years_amount
duration.to_i
end
def price_decimal
price_cents / BigDecimal.new('100')
end
def zone_name
zone.origin
end
private
def to_partial_path
'price'
end
private
def init_values
return unless new_record?
self.valid_from = Time.zone.now.beginning_of_year unless valid_from

View file

@ -61,7 +61,6 @@ class Directo < ActiveRecord::Base
def self.send_monthly_invoices(debug: false)
@debug = debug
I18n.locale = :et
month = Time.now - 1.month
invoices_until = month.end_of_month
@ -87,29 +86,50 @@ class Directo < ActiveRecord::Base
# adding domains items
registrar_activities.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW]).each do |activity|
pricelist = load_activity_pricelist(activity)
unless pricelist
Rails.logger.error("[DIRECTO] Skipping activity #{activity.id} as pricelist not found")
next
end
price = load_price(activity)
pricelist.years_amount.times do |i|
year = i+1
hash = {
"ProductID" => DOMAIN_TO_PRODUCT[pricelist.category],
"Unit" => "tk",
"ProductName" => ".#{pricelist.category} registreerimine: #{pricelist.years_amount} aasta",
"UnitPriceWoVAT" => pricelist.price_decimal/pricelist.years_amount
}
hash["StartDate"] = (activity.created_at + (year-1).year).end_of_month.strftime(date_format) if year > 1
hash["EndDate"] = (activity.created_at + (year-1).year + 1).end_of_month.strftime(date_format) if year > 1
if price.duration.include?('year')
price.duration.to_i.times do |i|
year = i+1
hash = {
"ProductID" => DOMAIN_TO_PRODUCT[price.zone_name],
"Unit" => "tk",
"ProductName" => ".#{price.zone_name} registreerimine: #{price.duration.to_i} aasta",
"UnitPriceWoVAT" => price.price.amount / price.duration.to_i
}
hash["StartDate"] = (activity.created_at + (year-1).year).end_of_month.strftime(date_format) if year > 1
hash["EndDate"] = (activity.created_at + (year-1).year + 1).end_of_month.strftime(date_format) if year > 1
if items.has_key?(hash)
items[hash]["Quantity"] += 1
else
items[hash] = {"RN"=>counter.next, "RR" => counter.now - i, "Quantity"=> 1}
if items.has_key?(hash)
items[hash]["Quantity"] += 1
else
items[hash] = { "RN" => counter.next, "RR" => counter.now - i, "Quantity" => 1 }
end
end
else
1.times do |i|
quantity = price.account_activities
.where(account_id: registrar.account_ids)
.where(created_at: month.beginning_of_month..month.end_of_month)
.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW])
.count
hash = {
"ProductID" => DOMAIN_TO_PRODUCT[price.zone_name],
"Unit" => "tk",
"ProductName" => ".#{price.zone_name} registreerimine: #{price.duration.to_i} kuud",
"UnitPriceWoVAT" => price.price.amount,
}
if items.has_key?(hash)
#items[hash]["Quantity"] += 1
else
items[hash] = { "RN" => counter.next, "RR" => counter.now - i, "Quantity" => quantity }
end
end
end
end
#adding prepaiments
@ -143,10 +163,11 @@ class Directo < ActiveRecord::Base
end
data = builder.to_xml.gsub("\n",'')
response = RestClient::Request.execute(url: ENV['directo_invoice_url'], method: :post, payload: {put: "1", what: "invoice", xmldata: data}, verify_ssl: false).to_s
if @debug
if debug
STDOUT << "#{Time.zone.now.utc} - Directo xml had to be sent #{data}\n"
else
response = RestClient::Request.execute(url: ENV['directo_invoice_url'], method: :post, payload: {put: "1", what: "invoice", xmldata: data}, verify_ssl: false).to_s
Setting.directo_monthly_number_last = directo_next
Nokogiri::XML(response).css("Result").each do |res|
Directo.create!(request: data, response: res.as_json.to_h, invoice_number: directo_next)
@ -161,19 +182,10 @@ class Directo < ActiveRecord::Base
STDOUT << "#{Time.zone.now.utc} - Directo invoices sending finished. #{invoice_counter.now} are sent\n"
end
def self.load_activity_pricelist activity
def self.load_price(account_activity)
@pricelists ||= {}
return @pricelists[activity.log_pricelist_id] if @pricelists.has_key?(activity.log_pricelist_id)
pricelist = Pricelist.find_by(id: activity.log_pricelist_id) || PricelistVersion.find_by(item_id: activity.log_pricelist_id).try(:reify)
unless pricelist
@pricelists[activity.log_pricelist_id] = nil
Rails.logger.info("[DIRECTO] AccountActivity #{activity.id} cannot be sent as pricelist wasn't found #{activity.log_pricelist_id}")
return
end
@pricelists[activity.log_pricelist_id] = pricelist.version_at(activity.created_at) || pricelist
return @pricelists[account_activity.price_id] if @pricelists.has_key?(account_activity.price_id)
@pricelists[account_activity.price_id] = account_activity.price
end
end

View file

@ -1,4 +0,0 @@
class PriceVersion < PaperTrail::Version
self.table_name = :log_pricelists
self.sequence_name = :log_pricelists_id_seq
end

View file

@ -2,3 +2,7 @@ et:
username: 'Kasutajanimi'
password: 'Parool'
log_in: 'Logi sisse'
date:
# Don't forget the nil at the beginning; there's no such thing as a 0th month
month_names: [~, Jaanuar, Veebruar, Märts, Aprill, Mai, Juuni, Juuli, August, September, Oktoober, November, Detsember]

View file

@ -54,6 +54,10 @@ if @cron_group == 'registry'
runner 'DomainCron.start_redemption_grace_period'
end
every '0 0 1 * *' do
runner 'Directo.send_monthly_invoices'
end
every :day, at: '19:00pm' do
runner 'Directo.send_receipts'
end if @environment == 'production'

View file

@ -0,0 +1,5 @@
class RemovePriceDesc < ActiveRecord::Migration
def change
remove_column :prices, :desc, :string
end
end

View file

@ -0,0 +1,6 @@
class RenameAccountActivityLogPricelistIdToPriceId < ActiveRecord::Migration
def change
rename_column :account_activities, :log_pricelist_id, :price_id
add_foreign_key :account_activities, :prices
end
end

View file

@ -0,0 +1,6 @@
class AddAccountActivityAccountIdForeignKey < ActiveRecord::Migration
def change
change_column :account_activities, :account_id, :integer, null: false
add_foreign_key :account_activities, :accounts
end
end

View file

@ -0,0 +1,5 @@
class AddAccountActivityInvoiceIdFk < ActiveRecord::Migration
def change
add_foreign_key :account_activities, :invoices
end
end

View file

@ -0,0 +1,5 @@
class AddAccountActivityBankTransactionIdFk < ActiveRecord::Migration
def change
add_foreign_key :account_activities, :bank_transactions
end
end

View file

@ -0,0 +1,6 @@
class AddAccountRegistrarFk < ActiveRecord::Migration
def change
change_column :accounts, :registrar_id, :integer, null: false
add_foreign_key :accounts, :registrars
end
end

View file

@ -0,0 +1,5 @@
class RemoveLogPricelists < ActiveRecord::Migration
def change
drop_table :log_pricelists
end
end

View file

@ -287,7 +287,7 @@ SET default_with_oids = false;
CREATE TABLE account_activities (
id integer NOT NULL,
account_id integer,
account_id integer NOT NULL,
invoice_id integer,
sum numeric(10,2),
currency character varying,
@ -298,7 +298,7 @@ CREATE TABLE account_activities (
creator_str character varying,
updator_str character varying,
activity_type character varying,
log_pricelist_id integer
price_id integer
);
@ -327,7 +327,7 @@ ALTER SEQUENCE account_activities_id_seq OWNED BY account_activities.id;
CREATE TABLE accounts (
id integer NOT NULL,
registrar_id integer,
registrar_id integer NOT NULL,
account_type character varying,
balance numeric(10,2) DEFAULT 0.0 NOT NULL,
created_at timestamp without time zone,
@ -2068,43 +2068,6 @@ CREATE SEQUENCE log_nameservers_id_seq
ALTER SEQUENCE log_nameservers_id_seq OWNED BY log_nameservers.id;
--
-- Name: log_pricelists; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE log_pricelists (
id integer NOT NULL,
item_type character varying NOT NULL,
item_id integer NOT NULL,
event character varying NOT NULL,
whodunnit character varying,
object json,
object_changes json,
created_at timestamp without time zone,
session character varying,
uuid character varying
);
--
-- Name: log_pricelists_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE log_pricelists_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: log_pricelists_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE log_pricelists_id_seq OWNED BY log_pricelists.id;
--
-- Name: log_registrars; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
@ -2453,7 +2416,6 @@ ALTER SEQUENCE people_id_seq OWNED BY people.id;
CREATE TABLE prices (
id integer NOT NULL,
"desc" character varying,
price_cents integer NOT NULL,
valid_from timestamp without time zone,
valid_to timestamp without time zone,
@ -3207,13 +3169,6 @@ ALTER TABLE ONLY log_messages ALTER COLUMN id SET DEFAULT nextval('log_messages_
ALTER TABLE ONLY log_nameservers ALTER COLUMN id SET DEFAULT nextval('log_nameservers_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY log_pricelists ALTER COLUMN id SET DEFAULT nextval('log_pricelists_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
@ -3714,14 +3669,6 @@ ALTER TABLE ONLY log_nameservers
ADD CONSTRAINT log_nameservers_pkey PRIMARY KEY (id);
--
-- Name: log_pricelists_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
ALTER TABLE ONLY log_pricelists
ADD CONSTRAINT log_pricelists_pkey PRIMARY KEY (id);
--
-- Name: log_registrars_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
@ -4678,6 +4625,46 @@ ALTER TABLE ONLY prices
ADD CONSTRAINT fk_rails_78c376257f FOREIGN KEY (zone_id) REFERENCES zones(id);
--
-- Name: fk_rails_86cd2b09f5; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY account_activities
ADD CONSTRAINT fk_rails_86cd2b09f5 FOREIGN KEY (account_id) REFERENCES accounts(id);
--
-- Name: fk_rails_b80dbb973d; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY account_activities
ADD CONSTRAINT fk_rails_b80dbb973d FOREIGN KEY (bank_transaction_id) REFERENCES bank_transactions(id);
--
-- Name: fk_rails_c9f635c0b3; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY accounts
ADD CONSTRAINT fk_rails_c9f635c0b3 FOREIGN KEY (registrar_id) REFERENCES registrars(id);
--
-- Name: fk_rails_ce38d749f6; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY account_activities
ADD CONSTRAINT fk_rails_ce38d749f6 FOREIGN KEY (invoice_id) REFERENCES invoices(id);
--
-- Name: fk_rails_d2cc3c2fa9; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY account_activities
ADD CONSTRAINT fk_rails_d2cc3c2fa9 FOREIGN KEY (price_id) REFERENCES prices(id);
--
-- PostgreSQL database dump complete
--
@ -5158,8 +5145,22 @@ INSERT INTO schema_migrations (version) VALUES ('20170423225333');
INSERT INTO schema_migrations (version) VALUES ('20170424115801');
INSERT INTO schema_migrations (version) VALUES ('20170506144743');
INSERT INTO schema_migrations (version) VALUES ('20170506155009');
INSERT INTO schema_migrations (version) VALUES ('20170506162952');
INSERT INTO schema_migrations (version) VALUES ('20170506205356');
INSERT INTO schema_migrations (version) VALUES ('20170506205946');
INSERT INTO schema_migrations (version) VALUES ('20170506212014');
INSERT INTO schema_migrations (version) VALUES ('20170509215614');
INSERT INTO schema_migrations (version) VALUES ('20170604182521');
INSERT INTO schema_migrations (version) VALUES ('20170606133501');
INSERT INTO schema_migrations (version) VALUES ('20170606150352');

View file

@ -166,17 +166,6 @@
<ellipse fill="none" stroke="#e88904" cx="349.694" cy="-125.352" rx="4.00002" ry="4.00002"/>
<polygon fill="#e88904" stroke="#e88904" points="1479.91,-20.9262 1490.02,-25.1909 1484.91,-20.8092 1489.91,-20.6922 1489.91,-20.6922 1489.91,-20.6922 1484.91,-20.8092 1489.8,-16.1934 1479.91,-20.9262 1479.91,-20.9262"/>
</g>
<!-- PricelistVersion -->
<g id="node15" class="node"><title>PricelistVersion</title>
<ellipse fill="none" stroke="black" cx="2888.99" cy="-91" rx="67.6881" ry="18"/>
<text text-anchor="middle" x="2888.99" y="-87.3" font-family="Times,serif" font-size="14.00">PricelistVersion</text>
</g>
<!-- PricelistVersion&#45;&gt;VersionAssociation -->
<g id="edge13" class="edge"><title>PricelistVersion&#45;&gt;VersionAssociation</title>
<path fill="none" stroke="#66b357" d="M2833.71,-76.4421C2826.77,-75.0923 2819.74,-73.8934 2812.99,-73 2589.58,-43.3999 1904.87,-26.195 1658.49,-20.8299"/>
<ellipse fill="none" stroke="#66b357" cx="2837.87" cy="-77.2894" rx="4.00001" ry="4.00001"/>
<polygon fill="#66b357" stroke="#66b357" points="1658.15,-20.8225 1648.25,-16.1071 1653.15,-20.7143 1648.15,-20.606 1648.15,-20.606 1648.15,-20.606 1653.15,-20.7143 1648.05,-25.105 1658.15,-20.8225 1658.15,-20.8225"/>
</g>
<!-- MessageVersion -->
<g id="node16" class="node"><title>MessageVersion</title>
<ellipse fill="none" stroke="black" cx="1093.99" cy="-253" rx="69.5877" ry="18"/>
@ -1229,13 +1218,6 @@
<ellipse fill="none" stroke="black" cx="4191.99" cy="-523" rx="51.9908" ry="18"/>
<text text-anchor="middle" x="4191.99" y="-519.3" font-family="Times,serif" font-size="14.00">EppSession</text>
</g>
<!-- Pricelist&#45;&gt;PricelistVersion -->
<g id="edge79" class="edge"><title>Pricelist&#45;&gt;PricelistVersion</title>
<path fill="none" stroke="#499b9a" d="M3250.87,-173.392C3244.19,-157.303 3233,-137.406 3215.99,-127 3168.48,-97.9313 3021.11,-117.025 2965.99,-109 2959.86,-108.107 2953.49,-106.998 2947.15,-105.777"/>
<ellipse fill="none" stroke="#499b9a" cx="3252.35" cy="-177.198" rx="4.00001" ry="4.00001"/>
<polygon fill="#499b9a" stroke="#499b9a" points="2947.07,-105.76 2938.16,-99.371 2942.17,-104.771 2937.27,-103.782 2937.27,-103.782 2937.27,-103.782 2942.17,-104.771 2936.37,-108.193 2947.07,-105.76 2947.07,-105.76"/>
<text text-anchor="middle" x="3267.99" y="-141.3" font-family="Times,serif" font-size="14.00">versions</text>
</g>
<!-- InvoiceItem -->
<g id="node55" class="node"><title>InvoiceItem</title>
<ellipse fill="none" stroke="black" cx="201.994" cy="-199" rx="53.8905" ry="18"/>

Before

Width:  |  Height:  |  Size: 132 KiB

After

Width:  |  Height:  |  Size: 130 KiB

Before After
Before After

View file

@ -330,27 +330,6 @@
<ellipse fill="none" stroke="#3eeac6" cx="420.555" cy="-269.403" rx="4.00001" ry="4.00001"/>
<polygon fill="#3eeac6" stroke="#3eeac6" points="1667.41,-20.0428 1677.47,-24.4139 1672.41,-19.9786 1677.41,-19.9143 1677.41,-19.9143 1677.41,-19.9143 1672.41,-19.9786 1677.35,-15.4146 1667.41,-20.0428 1667.41,-20.0428"/>
</g>
<!-- PricelistVersion -->
<g id="node15" class="node"><title>PricelistVersion</title>
<path fill="none" stroke="black" d="M2986,-81C2986,-81 3092,-81 3092,-81 3098,-81 3104,-87 3104,-93 3104,-93 3104,-235 3104,-235 3104,-241 3098,-247 3092,-247 3092,-247 2986,-247 2986,-247 2980,-247 2974,-241 2974,-235 2974,-235 2974,-93 2974,-93 2974,-87 2980,-81 2986,-81"/>
<text text-anchor="middle" x="3039" y="-231.8" font-family="Times,serif" font-size="14.00">PricelistVersion</text>
<polyline fill="none" stroke="black" points="2974,-224 3104,-224 "/>
<text text-anchor="start" x="2982" y="-208.8" font-family="Times,serif" font-size="14.00">id :integer</text>
<text text-anchor="start" x="2982" y="-193.8" font-family="Times,serif" font-size="14.00">item_type :string</text>
<text text-anchor="start" x="2982" y="-178.8" font-family="Times,serif" font-size="14.00">item_id :integer</text>
<text text-anchor="start" x="2982" y="-163.8" font-family="Times,serif" font-size="14.00">event :string</text>
<text text-anchor="start" x="2982" y="-148.8" font-family="Times,serif" font-size="14.00">whodunnit :string</text>
<text text-anchor="start" x="2982" y="-133.8" font-family="Times,serif" font-size="14.00">object :json</text>
<text text-anchor="start" x="2982" y="-118.8" font-family="Times,serif" font-size="14.00">object_changes :json</text>
<text text-anchor="start" x="2982" y="-103.8" font-family="Times,serif" font-size="14.00">created_at :datetime</text>
<text text-anchor="start" x="2982" y="-88.8" font-family="Times,serif" font-size="14.00">session :string</text>
</g>
<!-- PricelistVersion&#45;&gt;VersionAssociation -->
<g id="edge13" class="edge"><title>PricelistVersion&#45;&gt;VersionAssociation</title>
<path fill="none" stroke="#219849" d="M2970.55,-76.1769C2968.73,-75.0528 2966.88,-73.9914 2965,-73 2880.55,-28.4331 2846.3,-59.9044 2751,-54 2417.69,-33.35 2020.69,-23.9253 1846.46,-20.5507"/>
<ellipse fill="none" stroke="#219849" cx="2974.09" cy="-78.5702" rx="4.00002" ry="4.00002"/>
<polygon fill="#219849" stroke="#219849" points="1846.31,-20.548 1836.4,-15.8571 1841.31,-20.4521 1836.31,-20.3562 1836.31,-20.3562 1836.31,-20.3562 1841.31,-20.4521 1836.23,-24.8554 1846.31,-20.548 1846.31,-20.548"/>
</g>
<!-- MessageVersion -->
<g id="node16" class="node"><title>MessageVersion</title>
<path fill="none" stroke="black" d="M1120,-733.5C1120,-733.5 1226,-733.5 1226,-733.5 1232,-733.5 1238,-739.5 1238,-745.5 1238,-745.5 1238,-902.5 1238,-902.5 1238,-908.5 1232,-914.5 1226,-914.5 1226,-914.5 1120,-914.5 1120,-914.5 1114,-914.5 1108,-908.5 1108,-902.5 1108,-902.5 1108,-745.5 1108,-745.5 1108,-739.5 1114,-733.5 1120,-733.5"/>
@ -1976,13 +1955,6 @@
<text text-anchor="start" x="4306.5" y="-2451.8" font-family="Times,serif" font-size="14.00">updated_at :datetime</text>
<text text-anchor="start" x="4306.5" y="-2436.8" font-family="Times,serif" font-size="14.00">registrar_id :integer</text>
</g>
<!-- Pricelist&#45;&gt;PricelistVersion -->
<g id="edge79" class="edge"><title>Pricelist&#45;&gt;PricelistVersion</title>
<path fill="none" stroke="#7416af" d="M3665.06,-472.647C3650.73,-402.404 3620.54,-318.837 3557,-273 3516.96,-244.115 3157.21,-276.99 3113,-255 3111.44,-254.226 3109.91,-253.402 3108.39,-252.533"/>
<ellipse fill="none" stroke="#7416af" cx="3665.88" cy="-476.781" rx="4.00001" ry="4.00001"/>
<polygon fill="#7416af" stroke="#7416af" points="3108.36,-252.513 3102.4,-243.31 3104.16,-249.802 3099.96,-247.091 3099.96,-247.091 3099.96,-247.091 3104.16,-249.802 3097.52,-250.872 3108.36,-252.513 3108.36,-252.513"/>
<text text-anchor="middle" x="3682" y="-360.3" font-family="Times,serif" font-size="14.00">versions</text>
</g>
<!-- InvoiceItem -->
<g id="node55" class="node"><title>InvoiceItem</title>
<path fill="none" stroke="black" d="M160.5,-503.5C160.5,-503.5 267.5,-503.5 267.5,-503.5 273.5,-503.5 279.5,-509.5 279.5,-515.5 279.5,-515.5 279.5,-672.5 279.5,-672.5 279.5,-678.5 273.5,-684.5 267.5,-684.5 267.5,-684.5 160.5,-684.5 160.5,-684.5 154.5,-684.5 148.5,-678.5 148.5,-672.5 148.5,-672.5 148.5,-515.5 148.5,-515.5 148.5,-509.5 154.5,-503.5 160.5,-503.5"/>

Before

Width:  |  Height:  |  Size: 239 KiB

After

Width:  |  Height:  |  Size: 236 KiB

Before After
Before After

View file

@ -10,8 +10,37 @@ namespace :dev do
FactoryGirl.find_definitions
PaperTrail.enabled = false
Domain.paper_trail_on!
Contact.paper_trail_on!
with_random_data = args[:random].present?
def create_domain(name:, registrar:, registrant:, account:, price:, reg_time:)
duration = price.duration.sub('mons', 'months').split("\s")
period = duration.first.to_i
period_unit = duration.second[0]
create(:domain,
name: name,
period: period,
period_unit: period_unit,
registered_at: reg_time,
valid_from: reg_time,
expire_time: reg_time + period.send(duration.second.to_sym),
created_at: reg_time,
updated_at: reg_time,
registrar: registrar,
registrant: registrant)
create(:account_activity,
account: account,
sum: -price.price.amount,
activity_type: AccountActivity::CREATE,
created_at: reg_time,
updated_at: reg_time,
price: price)
end
def generate_default_data
create(:admin_user, username: 'test', password: 'testtest', password_confirmation: 'testtest')
@ -19,29 +48,54 @@ namespace :dev do
registrar = create(:registrar, name: 'test')
registrant = create(:registrant, name: 'test', registrar: registrar)
create(:account, registrar: registrar, balance: 1_000_000)
create(:api_user, username: 'test', password: 'testtest', registrar: registrar)
create(:domain,
name: 'test.test',
period: 1,
period_unit: 'y',
registered_at: Time.zone.now,
valid_from: Time.zone.now,
expire_time: Time.zone.now + 10.years,
registrar: registrar,
registrant: registrant)
account = create(:account, registrar: registrar, balance: 1_000_000)
api_user = create(:api_user, username: 'test', password: 'testtest', registrar: registrar)
epp_session = build(:epp_session, registrar: registrar)
epp_session[:api_user_id] = api_user.id
epp_session.registrar_id = registrar.id
epp_session.save!
domain_counter = 1.step
Billing::Price.durations.each do |duration|
Billing::Price.operation_categories.each do |operation_category|
create(:price,
price: Money.from_amount(1),
valid_from: Time.zone.now.beginning_of_day,
valid_to: Time.zone.now + 10.years,
duration: duration,
operation_category: operation_category,
zone: zone)
price = create(:price,
price: Money.from_amount(duration.to_i * 10),
valid_from: Time.zone.now - rand(1).months,
valid_to: Time.zone.now + rand(1).months,
duration: duration,
operation_category: operation_category,
zone: zone)
next if operation_category == 'renew'
(rand(3) + 1).times do
create_domain(name: "test#{domain_counter.next}.test",
registrar: registrar,
registrant: registrant,
account: account,
price: price,
reg_time: 1.month.ago)
end
(rand(3) + 1).times do
create_domain(name: "test#{domain_counter.next}.test",
registrar: registrar,
registrant: registrant,
account: account,
price: price,
reg_time: Time.zone.now)
end
end
end
create_domain(name: 'test.test',
registrar: registrar,
registrant: registrant,
account: account,
price: Billing::Price.first,
reg_time: Time.zone.now)
end
def generate_random_data

View file

@ -1,5 +0,0 @@
Fabricator(:account_activity) do
invoice
currency 'EUR'
sum 110.0
end

View file

@ -2,5 +2,5 @@ Fabricator(:account) do
account_type { Account::CASH }
balance 0.0
currency 'EUR'
# account_activities(count: 2)
registrar
end

View file

@ -3,5 +3,6 @@ FactoryGirl.define do
account_type Account::CASH
balance 1
currency 'EUR'
registrar
end
end

View file

@ -0,0 +1,6 @@
FactoryGirl.define do
factory :account_activity do
sum 1.0
account
end
end

View file

@ -0,0 +1,5 @@
FactoryGirl.define do
factory :epp_session do
end
end

View file

@ -0,0 +1,13 @@
require 'rails_helper'
RSpec.describe AccountActivity do
describe 'account validation', db: false do
subject(:account_activity) { described_class.new }
it 'rejects absent' do
account_activity.account = nil
account_activity.validate
expect(account_activity.errors).to have_key(:account)
end
end
end

View file

@ -1,48 +1,21 @@
require 'rails_helper'
describe Account do
context 'with invalid attribute' do
before :all do
@account = Account.new
end
it 'should not be valid' do
@account.valid?
@account.errors.full_messages.should match_array(["Account type is missing"])
end
it 'should not have any versions' do
@account.versions.should == []
RSpec.describe Account do
it 'has versions' do
with_versioning do
price = build(:account)
price.save!
expect(price.versions.size).to be(1)
end
end
context 'with valid attributes' do
before :all do
@account = Fabricate(:account)
end
describe 'registrar validation', db: false do
subject(:account) { described_class.new }
it 'should be valid' do
@account.valid?
@account.errors.full_messages.should match_array([])
s = 0.0
@account.activities.map { |x| s += x.sum }
@account.balance.should == s
end
it 'should be valid twice' do
@account = Fabricate(:account)
@account.valid?
@account.errors.full_messages.should match_array([])
end
it 'should have one version' do
with_versioning do
@account.versions.should == []
@account.account_type = 'new_type'
@account.save
@account.errors.full_messages.should match_array([])
@account.versions.size.should == 1
end
it 'rejects absent' do
account.registrar = nil
account.validate
expect(account.errors).to have_key(:registrar)
end
end
end

View file

@ -2,18 +2,9 @@ require 'rails_helper'
RSpec.describe Billing::Price do
it { is_expected.to monetize(:price) }
it { is_expected.to be_versioned }
it { is_expected.to alias_attribute(:effect_time, :valid_from) }
it { is_expected.to alias_attribute(:expire_time, :valid_to) }
it 'should have one version' do
with_versioning do
price = build(:price)
price.save!
price.versions.size.should == 1
end
end
describe '::operation_categories', db: false do
it 'returns operation categories' do
categories = %w[create renew]

View file

@ -17,20 +17,20 @@ RSpec.describe 'admin zone create', settings: false do
text_attributes.each do |attr_name|
it "saves #{attr_name}" do
post admin_zones_path, { zone: attributes_for(:zone, attr_name => 'test') }
post admin_zones_path, zone: attributes_for(:zone, attr_name => 'test')
expect(zone.send(attr_name)).to eq('test')
end
end
integer_attributes.each do |attr_name|
it "saves #{attr_name}" do
post admin_zones_path, { zone: attributes_for(:zone, attr_name => '1') }
post admin_zones_path, zone: attributes_for(:zone, attr_name => '1')
expect(zone.send(attr_name)).to eq(1)
end
end
it 'redirects to :index' do
post admin_zones_path, { zone: attributes_for(:zone) }
post admin_zones_path, zone: attributes_for(:zone)
expect(response).to redirect_to admin_zones_url
end
end

View file

@ -33,7 +33,7 @@ RSpec.describe 'admin zone update', settings: false do
it 'redirects to :index' do
zone = create(:zone)
patch admin_zone_path(zone), { zone: attributes_for(:zone) }
patch admin_zone_path(zone), zone: attributes_for(:zone)
expect(response).to redirect_to admin_zones_url
end