From e1041da50fb7f45b8b6c48552edac2f03ef6868f Mon Sep 17 00:00:00 2001 From: Priit Tark Date: Mon, 6 Apr 2015 13:47:18 +0300 Subject: [PATCH] Converted to use Rails Time.zone only to honor app time zone --- app/models/certificate.rb | 2 +- app/models/contact.rb | 4 ++-- app/models/domain.rb | 4 ++-- app/models/epp/domain.rb | 6 +++--- app/models/keyrelay.rb | 2 +- app/models/zonefile_setting.rb | 4 ++-- app/views/epp/sessions/greeting.xml.builder | 2 +- config/application-example.yml | 6 +++--- config/application.rb | 2 +- config/initializers/001_paper_trail.rb | 2 +- lib/tasks/import.rake | 12 ++++++------ spec/epp/contact_spec.rb | 2 +- spec/epp/domain_spec.rb | 11 +++++++---- spec/fabricators/keyrelay_fabricator.rb | 2 +- spec/models/keyrelay_spec.rb | 2 +- spec/support/epp_doc.rb | 2 +- 16 files changed, 34 insertions(+), 31 deletions(-) diff --git a/app/models/certificate.rb b/app/models/certificate.rb index 9a0cab4a1..c55f0fe77 100644 --- a/app/models/certificate.rb +++ b/app/models/certificate.rb @@ -29,7 +29,7 @@ class Certificate < ActiveRecord::Base @cached_status = SIGNED - if parsed_crt.not_before > Time.now.utc && parsed_crt.not_after < Time.now.utc + if parsed_crt.not_before > Time.zone.now.utc && parsed_crt.not_after < Time.zone.now.utc @cached_status = EXPIRED end diff --git a/app/models/contact.rb b/app/models/contact.rb index b1e1e568d..1df655f2e 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -81,9 +81,9 @@ class Contact < ActiveRecord::Base end def destroy_orphans - logger.info "#{Time.now.utc} - Destroying orphaned contacts\n" + logger.info "#{Time.zone.now.utc} - Destroying orphaned contacts\n" count = find_orphans.destroy_all.count - logger.info "#{Time.now.utc} - Successfully destroyed #{count} orphaned contacts\n" + logger.info "#{Time.zone.now.utc} - Successfully destroyed #{count} orphaned contacts\n" end end diff --git a/app/models/domain.rb b/app/models/domain.rb index 7c81736be..d6aa28718 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -49,7 +49,7 @@ class Domain < ActiveRecord::Base before_create :set_validity_dates before_save :touch_always_version def touch_always_version - self.updated_at = Time.now + self.updated_at = Time.zone.now end after_save :manage_automatic_statuses after_save :update_whois_body @@ -205,7 +205,7 @@ class Domain < ActiveRecord::Base def set_validity_dates self.registered_at = Time.zone.now - self.valid_from = Date.today + self.valid_from = Time.zone.now.to_date self.valid_to = valid_from + self.class.convert_period_to_time(period, period_unit) end diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index 723925188..359495ed6 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -92,7 +92,7 @@ class Epp::Domain < Domain at[:name] = frame.css('name').text if new_record? at[:registrar_id] = current_user.registrar.try(:id) - at[:registered_at] = Time.now if new_record? + at[:registered_at] = Time.zone.now if new_record? period = frame.css('period').text at[:period] = (period.to_i == 0) ? 1 : period.to_i @@ -593,7 +593,7 @@ class Epp::Domain < Domain transaction do kr = keyrelays.build( - pa_date: Time.now, + pa_date: Time.zone.now, key_data_flags: parsed_frame.css('flags').text, key_data_protocol: parsed_frame.css('protocol').text, key_data_alg: parsed_frame.css('alg').text, @@ -632,7 +632,7 @@ class Epp::Domain < Domain def validate_exp_dates(cur_exp_date) begin - return if cur_exp_date.to_date == valid_to + return if cur_exp_date.to_date == valid_to.to_date rescue add_epp_error('2306', 'curExpDate', cur_exp_date, I18n.t('errors.messages.epp_exp_dates_do_not_match')) return diff --git a/app/models/keyrelay.rb b/app/models/keyrelay.rb index 4b900b4b4..ba48991b2 100644 --- a/app/models/keyrelay.rb +++ b/app/models/keyrelay.rb @@ -45,7 +45,7 @@ class Keyrelay < ActiveRecord::Base end def status - if Time.now > expiry + if Time.zone.now > expiry return 'expired' else return 'pending' diff --git a/app/models/zonefile_setting.rb b/app/models/zonefile_setting.rb index c9cac6154..8747a11ca 100644 --- a/app/models/zonefile_setting.rb +++ b/app/models/zonefile_setting.rb @@ -12,7 +12,7 @@ class ZonefileSetting < ActiveRecord::Base def self.generate_zonefile(origin) filename = "#{origin}.zone" - STDOUT << "#{Time.now.utc} - Generating zonefile #{filename}\n" + STDOUT << "#{Time.zone.now.utc} - Generating zonefile #{filename}\n" zf = ActiveRecord::Base.connection.execute( "select generate_zonefile('#{origin}')" @@ -20,7 +20,7 @@ class ZonefileSetting < ActiveRecord::Base File.open("#{ENV['zonefile_export_dir']}/#{filename}", 'w') { |f| f.write(zf) } - STDOUT << "#{Time.now.utc} - Successfully generated zonefile #{filename}\n" + STDOUT << "#{Time.zone.now.utc} - Successfully generated zonefile #{filename}\n" end def to_s diff --git a/app/views/epp/sessions/greeting.xml.builder b/app/views/epp/sessions/greeting.xml.builder index 4fac143e7..c5a1a1a9e 100644 --- a/app/views/epp/sessions/greeting.xml.builder +++ b/app/views/epp/sessions/greeting.xml.builder @@ -1,7 +1,7 @@ xml.epp_head do xml.greeting do xml.svID 'EPP server (EIS)' - xml.svDate Time.now.utc.iso8601 + xml.svDate Time.zone.now.utc.iso8601 xml.svcMenu do xml.version '1.0' xml.lang 'en' diff --git a/config/application-example.yml b/config/application-example.yml index 32d56ca5c..22dcfeaca 100644 --- a/config/application-example.yml +++ b/config/application-example.yml @@ -1,6 +1,6 @@ # Be sure to restart your server when you modify settings. -app_name: .EE Registry +app_name: '.EE Registry' zonefile_export_dir: 'export/zonefiles' # Contact epp will not accept org value by default @@ -10,8 +10,8 @@ contact_org_enabled: 'false' # You can use `rake secret` to generate a secure secret key. # Your secret key is used for verifying the integrity of signed cookies. # If you change this key, all old signed cookies will become invalid! -secret_key_base: please-change-it-you-can-generate-it-with-rake-secret -devise_secret: please-change-it-you-can-generate-it-with-rake-secret +secret_key_base: 'please-change-it-you-can-generate-it-with-rake-secret' +devise_secret: 'please-change-it-you-can-generate-it-with-rake-secret' # Used by admin server, you can leave those empty for when running EPP server: openssl_config_path: '/etc/ssl/openssl.cnf' diff --git a/config/application.rb b/config/application.rb index f44bb80f0..2bcdc6b46 100644 --- a/config/application.rb +++ b/config/application.rb @@ -21,7 +21,7 @@ module Registry # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. - # config.time_zone = 'Central Time (US & Canada)' + config.time_zone = 'UTC' # NB! It should be defined, otherwise ActiveRecord usese other class. # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] diff --git a/config/initializers/001_paper_trail.rb b/config/initializers/001_paper_trail.rb index 0c99ca590..9dec5fb12 100644 --- a/config/initializers/001_paper_trail.rb +++ b/config/initializers/001_paper_trail.rb @@ -17,7 +17,7 @@ class PaperSession class << self attr_writer :session def session - @session ||= Time.now.to_s(:db) + @session ||= Time.zone.now.to_s(:db) end end end diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index c53ec493c..c378aa5d0 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -56,7 +56,7 @@ namespace :import do desc 'Import registrars' task registrars: :environment do - start = Time.now.to_f + start = Time.zone.now.to_f puts '-----> Importing registrars...' registrars = [] @@ -92,12 +92,12 @@ namespace :import do Registrar.import registrars, validate: false - puts "-----> Imported #{count} new registrars in #{(Time.now.to_f - start).round(2)} seconds" + puts "-----> Imported #{count} new registrars in #{(Time.zone.now.to_f - start).round(2)} seconds" end desc 'Import contacts' task contacts: :environment do - start = Time.now.to_f + start = Time.zone.now.to_f puts '-----> Importing contacts...' # 1;"RC";"born number" # not used @@ -183,12 +183,12 @@ namespace :import do end Contact.import contact_columns, contacts, validate: false - puts "-----> Imported #{count} new contacts in #{(Time.now.to_f - start).round(2)} seconds" + puts "-----> Imported #{count} new contacts in #{(Time.zone.now.to_f - start).round(2)} seconds" end desc 'Import domains' task domains: :environment do - start = Time.now.to_f + start = Time.zone.now.to_f puts '-----> Importing domains...' domain_columns = %w( @@ -449,6 +449,6 @@ namespace :import do x.save(validate: false) end - puts "-----> Imported #{count} new domains in #{(Time.now.to_f - start).round(2)} seconds" + puts "-----> Imported #{count} new domains in #{(Time.zone.now.to_f - start).round(2)} seconds" end end diff --git a/spec/epp/contact_spec.rb b/spec/epp/contact_spec.rb index b53f10f26..45a00120b 100644 --- a/spec/epp/contact_spec.rb +++ b/spec/epp/contact_spec.rb @@ -140,7 +140,7 @@ describe 'EPP Contact', epp: true do id.text.length.should == 8 # 5 seconds for what-ever weird lag reasons might happen - cr_date.text.to_time.should be_within(5).of(Time.now) + cr_date.text.to_time.should be_within(5).of(Time.zone.now) end it 'successfully saves custom code' do diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index cdb528e19..5063ee25b 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -308,7 +308,7 @@ describe 'EPP Domain', epp: true do response = epp_plain_request(xml, :xml) response[:msg].should == 'Command completed successfully' response[:result_code].should == '1000' - Domain.first.valid_to.should == Date.today + 1.year + Domain.first.valid_to.should == 1.year.since.to_date end it 'does not create a domain with invalid period' do @@ -1682,7 +1682,7 @@ describe 'EPP Domain', epp: true do ### RENEW ### it 'renews a domain' do - exp_date = (Date.today + 1.year) + exp_date = 1.year.since.to_date xml = @epp_xml.domain.renew( name: { value: domain.name }, curExpDate: { value: exp_date.to_s }, @@ -1690,6 +1690,9 @@ describe 'EPP Domain', epp: true do ) response = epp_plain_request(xml, :xml) + response[:results][0][:msg].should == 'Command completed successfully' + response[:results][0][:result_code].should == '1000' + ex_date = response[:parsed].css('renData exDate').text name = response[:parsed].css('renData name').text ex_date.should == "#{(exp_date + 1.year)} 00:00:00 UTC" @@ -1709,7 +1712,7 @@ describe 'EPP Domain', epp: true do end it 'returns an error when period is invalid' do - exp_date = (Date.today + 1.year) + exp_date = (1.year.since.to_date) xml = @epp_xml.domain.renew( name: { value: domain.name }, @@ -1718,8 +1721,8 @@ describe 'EPP Domain', epp: true do ) response = epp_plain_request(xml, :xml) - response[:results][0][:result_code].should == '2004' response[:results][0][:msg].should == 'Period must add up to 1, 2 or 3 years [period]' + response[:results][0][:result_code].should == '2004' response[:results][0][:value].should == '4' end diff --git a/spec/fabricators/keyrelay_fabricator.rb b/spec/fabricators/keyrelay_fabricator.rb index 6c30ea104..436518ef8 100644 --- a/spec/fabricators/keyrelay_fabricator.rb +++ b/spec/fabricators/keyrelay_fabricator.rb @@ -1,5 +1,5 @@ Fabricator(:keyrelay) do - pa_date { DateTime.now } + pa_date { Time.zone.now } expiry_relative 'P1W' key_data_public_key 'abc' key_data_flags 0 diff --git a/spec/models/keyrelay_spec.rb b/spec/models/keyrelay_spec.rb index 75dd0626c..040567c0e 100644 --- a/spec/models/keyrelay_spec.rb +++ b/spec/models/keyrelay_spec.rb @@ -62,7 +62,7 @@ describe Keyrelay do end it 'is in expired status' do - kr = Fabricate(:keyrelay, pa_date: DateTime.now - 2.weeks) + kr = Fabricate(:keyrelay, pa_date: Time.zone.now - 2.weeks) expect(kr.status).to eq('expired') end diff --git a/spec/support/epp_doc.rb b/spec/support/epp_doc.rb index 6315a9e14..2aa3b3451 100644 --- a/spec/support/epp_doc.rb +++ b/spec/support/epp_doc.rb @@ -7,7 +7,7 @@ class EppDoc def start(example_count) @output.puts '# EPP REQUEST - RESPONSE EXAMPLES' - @output.puts "GENERATED AT: #{Time.now} " + @output.puts "GENERATED AT: #{Time.zone.now} " @output.puts "EXAMPLE COUNT: #{example_count.count} " @output.puts "\n---\n\n" end