From 7eb509b9a8219717fb836c3df71d3e61c27820d4 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Fri, 3 Jul 2015 10:50:37 +0300 Subject: [PATCH 1/3] Fix tests #2741 --- app/controllers/epp/domains_controller.rb | 10 +++++++--- spec/models/pricelist_spec.rb | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/epp/domains_controller.rb b/app/controllers/epp/domains_controller.rb index 3bb8f50e1..5ca4f0a0e 100644 --- a/app/controllers/epp/domains_controller.rb +++ b/app/controllers/epp/domains_controller.rb @@ -18,16 +18,18 @@ class Epp::DomainsController < EppController render_epp_response '/epp/domains/info' end + # rubocop: disable Metrics/PerceivedComplexity + # rubocop: disable Metrics/CyclomaticComplexity def create authorize! :create, Epp::Domain @domain = Epp::Domain.new_from_epp(params[:parsed_frame], current_user) - @domain.valid? - handle_errors(@domain) and return if @domain.errors.any? + handle_errors(@domain) and return if @domain.valid? && @domain.errors.any? + handle_errors and return unless balance_ok?('create') ActiveRecord::Base.transaction do - if @domain.save + if @domain.save # TODO: Maybe use validate: false here because we have already validated the domain? current_user.registrar.debit!(@domain_price, "#{I18n.t('create')} #{@domain.name}") render_epp_response '/epp/domains/create' else @@ -35,6 +37,8 @@ class Epp::DomainsController < EppController end end end + # rubocop: enable Metrics/PerceivedComplexity + # rubocop: enable Metrics/CyclomaticComplexity def update authorize! :update, @domain, @password diff --git a/spec/models/pricelist_spec.rb b/spec/models/pricelist_spec.rb index 9323960b7..d6dc79070 100644 --- a/spec/models/pricelist_spec.rb +++ b/spec/models/pricelist_spec.rb @@ -55,7 +55,7 @@ describe Pricelist do end it 'should have name' do - @pricelist.name.should == 'new .ee' + @pricelist.name.should == 'create ee' end it 'should have one version' do From 527f65ebfbe0fc2389593ddbdfd28362a3d2965e Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Fri, 3 Jul 2015 11:03:45 +0300 Subject: [PATCH 2/3] Add some tests #2741 --- spec/epp/domain_spec.rb | 8 ++++++++ spec/models/registrar_spec.rb | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index fae65e7f6..c17440800 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -325,15 +325,21 @@ describe 'EPP Domain', epp: true do end it 'creates a domain with period in days' do + old_balance = @registrar1.balance + old_activities = @registrar1.cash_account.account_activities.count xml = domain_create_xml(period_value: 365, period_unit: 'd') response = epp_plain_request(xml) response[:msg].should == 'Command completed successfully' response[:result_code].should == '1000' Domain.first.valid_to.should be_within(60).of(1.year.since) + @registrar1.balance.should be < old_balance + @registrar1.cash_account.account_activities.count.should == old_activities + 1 end it 'does not create a domain with invalid period' do + old_balance = @registrar1.balance + old_activities = @registrar1.cash_account.account_activities.count xml = domain_create_xml({ period: { value: '367', attrs: { unit: 'd' } } }) @@ -342,6 +348,8 @@ describe 'EPP Domain', epp: true do response[:results][0][:result_code].should == '2306' response[:results][0][:msg].should == 'Period must add up to 1, 2 or 3 years [period]' response[:results][0][:value].should == '367' + @registrar1.balance.should == old_balance + @registrar1.cash_account.account_activities.count.should == old_activities end it 'creates a domain with multiple dnskeys' do diff --git a/spec/models/registrar_spec.rb b/spec/models/registrar_spec.rb index c169a5b73..0cf04b2d2 100644 --- a/spec/models/registrar_spec.rb +++ b/spec/models/registrar_spec.rb @@ -144,5 +144,21 @@ describe Registrar do it 'should not have priv contacts' do @registrar.priv_contacts.size.should == 0 end + + it 'should credit and debit registrar cash account' do + @registrar.credit!(13.32, 'Add money') + @registrar.balance.should == BigDecimal.new('13.32') + @registrar.cash_account.account_activities.count.should == 1 + a = @registrar.cash_account.account_activities.last + a.description.should == 'Add money' + a.sum.should == BigDecimal.new('13.32') + + @registrar.debit!(10.31, 'Remove money') + @registrar.balance.should == BigDecimal.new('3.01') + @registrar.cash_account.account_activities.count.should == 2 + a = @registrar.cash_account.account_activities.last + a.description.should == 'Remove money' + a.sum.should == -BigDecimal.new('10.31') + end end end From fa77056f8102b871f627610b1349fd6aee8009fe Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Fri, 3 Jul 2015 12:11:35 +0300 Subject: [PATCH 3/3] Increase precision of pricelist #2741 --- ...3084632_increase_precision_of_pricelist.rb | 5 ++ db/structure.sql | 51 +++---------------- spec/epp/domain_spec.rb | 20 +++++++- 3 files changed, 29 insertions(+), 47 deletions(-) create mode 100644 db/migrate/20150703084632_increase_precision_of_pricelist.rb diff --git a/db/migrate/20150703084632_increase_precision_of_pricelist.rb b/db/migrate/20150703084632_increase_precision_of_pricelist.rb new file mode 100644 index 000000000..6d60d4786 --- /dev/null +++ b/db/migrate/20150703084632_increase_precision_of_pricelist.rb @@ -0,0 +1,5 @@ +class IncreasePrecisionOfPricelist < ActiveRecord::Migration + def change + change_column :pricelists, :price_cents, :decimal, precision: 10, scale: 2 + end +end diff --git a/db/structure.sql b/db/structure.sql index 271e8d8ae..2ef6146ff 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -74,7 +74,7 @@ CREATE FUNCTION generate_zonefile(i_origin character varying) RETURNS text SELECT concat(d.name_puny, '. IN NS ', ns.hostname, '.') FROM domains d JOIN nameservers ns ON ns.domain_id = d.id - WHERE d.name LIKE include_filter AND d.name NOT LIKE exclude_filter OR d.name = i_origin + WHERE d.name LIKE include_filter AND d.name NOT LIKE exclude_filter ORDER BY d.name ), chr(10) @@ -2366,7 +2366,7 @@ CREATE TABLE pricelists ( id integer NOT NULL, "desc" character varying, category character varying, - price_cents numeric(8,2) DEFAULT 0.0 NOT NULL, + price_cents numeric(10,2) DEFAULT 0.0 NOT NULL, price_currency character varying DEFAULT 'EUR'::character varying NOT NULL, valid_from timestamp without time zone, valid_to timestamp without time zone, @@ -2404,8 +2404,8 @@ ALTER SEQUENCE pricelists_id_seq OWNED BY pricelists.id; CREATE TABLE que_jobs ( priority smallint DEFAULT 100 NOT NULL, - run_at timestamp with time zone DEFAULT now() NOT NULL, - job_id bigint NOT NULL, + run_at timestamp without time zone DEFAULT '2015-06-30 14:16:50.905537'::timestamp without time zone NOT NULL, + job_id bigint DEFAULT 0 NOT NULL, job_class text NOT NULL, args json DEFAULT '[]'::json NOT NULL, error_count integer DEFAULT 0 NOT NULL, @@ -2414,32 +2414,6 @@ CREATE TABLE que_jobs ( ); --- --- Name: TABLE que_jobs; Type: COMMENT; Schema: public; Owner: - --- - -COMMENT ON TABLE que_jobs IS '3'; - - --- --- Name: que_jobs_job_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE que_jobs_job_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: que_jobs_job_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE que_jobs_job_id_seq OWNED BY que_jobs.job_id; - - -- -- Name: registrant_verifications; Type: TABLE; Schema: public; Owner: -; Tablespace: -- @@ -3195,13 +3169,6 @@ ALTER TABLE ONLY people ALTER COLUMN id SET DEFAULT nextval('people_id_seq'::reg ALTER TABLE ONLY pricelists ALTER COLUMN id SET DEFAULT nextval('pricelists_id_seq'::regclass); --- --- Name: job_id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY que_jobs ALTER COLUMN job_id SET DEFAULT nextval('que_jobs_job_id_seq'::regclass); - - -- -- Name: id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3721,14 +3688,6 @@ ALTER TABLE ONLY pricelists ADD CONSTRAINT pricelists_pkey PRIMARY KEY (id); --- --- Name: que_jobs_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: --- - -ALTER TABLE ONLY que_jobs - ADD CONSTRAINT que_jobs_pkey PRIMARY KEY (queue, priority, run_at, job_id); - - -- -- Name: registrant_verifications_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- @@ -4851,3 +4810,5 @@ INSERT INTO schema_migrations (version) VALUES ('20150612123111'); INSERT INTO schema_migrations (version) VALUES ('20150701074344'); +INSERT INTO schema_migrations (version) VALUES ('20150703084632'); + diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index c17440800..c71e4117e 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -19,11 +19,24 @@ describe 'EPP Domain', epp: true do Fabricate(:contact, code: 'FIXED:JURIDICAL_1234', ident_type: 'bic') Fabricate(:reserved_domain) Fabricate(:blocked_domain) - Fabricate(:pricelist) + Fabricate(:pricelist, valid_to: nil) @uniq_no = proc { @i ||= 0; @i += 1 } end + it 'should return error if balance low' do + f = Fabricate(:pricelist, valid_to: Time.zone.now + 1.day, price: 100000) + + dn = next_domain_name + response = epp_plain_request(domain_create_xml({ + name: { value: dn } + })) + + response[:msg].should == "Billing failure - credit balance low" + response[:result_code].should == '2104' + f.delete + end + it 'returns error if contact does not exists' do response = epp_plain_request(domain_create_xml({ registrant: { value: 'FIXED:CITIZEN_1234' }, @@ -332,9 +345,12 @@ describe 'EPP Domain', epp: true do response = epp_plain_request(xml) response[:msg].should == 'Command completed successfully' response[:result_code].should == '1000' - Domain.first.valid_to.should be_within(60).of(1.year.since) + Domain.last.valid_to.should be_within(60).of(1.year.since) @registrar1.balance.should be < old_balance @registrar1.cash_account.account_activities.count.should == old_activities + 1 + a = @registrar1.cash_account.account_activities.last + a.description.should == "Create #{Domain.last.name}" + a.sum.should == -BigDecimal.new('10.0') end it 'does not create a domain with invalid period' do