From 14b8dd6adef9b9e7a85abac5c2334aeee86b0232 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 3 Mar 2021 18:00:14 +0200 Subject: [PATCH] added test for jobs, validators and helper --- .../application_helper_test.rb | 14 +++++ test/jobs/domain_delete_job_test.rb | 20 +++++++ .../regenerate_registrar_whoises_job_test.rb | 13 +++++ ...egistrant_change_expired_email_job_test.rb | 16 ++++++ .../date_time_iso8601_validator_test.rb | 54 ++++++++++++++++-- .../duration_iso8601_validator_test.rb | 57 +++++++++++++++++++ 6 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 test/integration/registrant_area/application_helper_test.rb create mode 100644 test/jobs/domain_delete_job_test.rb create mode 100644 test/jobs/regenerate_registrar_whoises_job_test.rb create mode 100644 test/jobs/registrant_change_expired_email_job_test.rb create mode 100644 test/lib/validators/duration_iso8601_validator_test.rb diff --git a/test/integration/registrant_area/application_helper_test.rb b/test/integration/registrant_area/application_helper_test.rb new file mode 100644 index 000000000..d915baf61 --- /dev/null +++ b/test/integration/registrant_area/application_helper_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class ApplicationHelperTest < ActionView::TestCase + def test_env_style_when_pic_present + assert_dom_equal %{}, + %{} + end + + def test_env_style_return_nil + env_style = '' + assert_dom_equal %{}, + %{} + end +end diff --git a/test/jobs/domain_delete_job_test.rb b/test/jobs/domain_delete_job_test.rb new file mode 100644 index 000000000..a6f7d9065 --- /dev/null +++ b/test/jobs/domain_delete_job_test.rb @@ -0,0 +1,20 @@ +require "test_helper" + +class DomainDeleteJobTest < ActiveSupport::TestCase + setup do + travel_to Time.zone.parse('2010-07-05') + @domain = domains(:shop) + @domain.update!(delete_date:'2010-07-05') + @domain.reload + end + + def test_delete_domain + dom = Domain.find_by(id: @domain.id) + assert dom + + DomainDeleteJob.run(@domain.id) + + dom = Domain.find_by(id: @domain.id) + assert_nil dom + end +end \ No newline at end of file diff --git a/test/jobs/regenerate_registrar_whoises_job_test.rb b/test/jobs/regenerate_registrar_whoises_job_test.rb new file mode 100644 index 000000000..3fe94612b --- /dev/null +++ b/test/jobs/regenerate_registrar_whoises_job_test.rb @@ -0,0 +1,13 @@ +require "test_helper" + +class RegenerateRegistrarWhoisesJobTest < ActiveSupport::TestCase + setup do + travel_to Time.zone.parse('2010-07-05 10:00') + @registrar = registrars(:bestnames) + end + + def test_job_return_true + # if return false, then job was failes + assert RegenerateRegistrarWhoisesJob.run(@registrar.id) + end +end \ No newline at end of file diff --git a/test/jobs/registrant_change_expired_email_job_test.rb b/test/jobs/registrant_change_expired_email_job_test.rb new file mode 100644 index 000000000..bd96bd034 --- /dev/null +++ b/test/jobs/registrant_change_expired_email_job_test.rb @@ -0,0 +1,16 @@ +require "test_helper" + +class RegistrantChangeExpiredEmailJobTest < ActiveSupport::TestCase + include ActionMailer::TestHelper + + setup do + ActionMailer::Base.deliveries.clear + @domain = domains(:shop) + end + + def test_delivers_email + # This job doesn't use anymore, but I'll leave it here for statistics + assert RegistrantChangeExpiredEmailJob.enqueue(@domain.id) + assert_emails 0 + end +end \ No newline at end of file diff --git a/test/lib/validators/date_time_iso8601_validator_test.rb b/test/lib/validators/date_time_iso8601_validator_test.rb index f16474707..ca397c6e5 100644 --- a/test/lib/validators/date_time_iso8601_validator_test.rb +++ b/test/lib/validators/date_time_iso8601_validator_test.rb @@ -2,15 +2,61 @@ require 'test_helper' class DateTimeIso8601Validatable include ActiveModel::Validations - validates_with DateTimeIso8601Validator, :attributes=>[:code] - attr_accessor :code - validates :code, iso8601: { date_only: true } + validates_with DateTimeIso8601Validator, :attributes=>[:errors] + attr_accessor :code, :type + validates :code, iso8601: { date_only: true }, if: :birthday? + + def birthday? + type == "birthday" + end + + def empty? + code.empty? + end end class DateTimeIso8601ValidatorTest < ActiveSupport::TestCase - def test_check_invalid_date + def test_check_invalid_reverse_date obj = DateTimeIso8601Validatable.new + obj.type = "birthday" obj.code = "22-12-2020" assert_not obj.valid? end + + def test_check_date_without_separate_symbols + obj = DateTimeIso8601Validatable.new + obj.type = "birthday" + obj.code = "24521012" + assert_not obj.valid? + end + + def test_check_empty_date + obj = DateTimeIso8601Validatable.new + obj.type = "birthday" + obj.code = "" + assert_not obj.valid? + end + + def test_check_valid_date + obj = DateTimeIso8601Validatable.new + obj.code = Date.new(2000,5,25).iso8601 + obj.type = "birthday" + assert obj.valid? + end + + def test_return_code_2005_in_epp_validate + obj = DateTimeIso8601Validatable.new + obj.code = Date.new(2000,5,25).iso8601 + obj.type = "birthday" + epp_resp = DateTimeIso8601Validator.validate_epp(obj, obj.code) + assert_equal epp_resp[:msg], "Expiry absolute must be compatible to ISO 8601" + end + + def test_epp_request_with_empty_data + obj = DateTimeIso8601Validatable.new + obj.code = "" + obj.type = "birthday" + epp_resp = DateTimeIso8601Validator.validate_epp(obj, obj.code) + assert_nil epp_resp + end end \ No newline at end of file diff --git a/test/lib/validators/duration_iso8601_validator_test.rb b/test/lib/validators/duration_iso8601_validator_test.rb new file mode 100644 index 000000000..a4acaad60 --- /dev/null +++ b/test/lib/validators/duration_iso8601_validator_test.rb @@ -0,0 +1,57 @@ +require 'test_helper' + +class DurationIso8601Validatable + include ActiveModel::Validations + validates_with DurationIso8601Validator, :attributes=>[:errors] + attr_accessor :duration + validates :duration, inclusion: { in: Proc.new { |price| price.class.durations } } + + def self.durations + [ + '3 mons', + '6 mons', + '9 mons', + '1 year', + '2 years', + '3 years', + '4 years', + '5 years', + '6 years', + '7 years', + '8 years', + '9 years', + '10 years', + ] + end + + def empty? + duration.empty? + end +end + +class DurationIso8601ValidatorTest < ActiveSupport::TestCase + def test_valid_duration + dura = DurationIso8601Validatable.new + dura.duration = '1 year' + assert dura.valid? + end + + def test_invalid_duration + dura = DurationIso8601Validatable.new + dura.duration = 'one millinons years' + assert_not dura.valid? + end + + def test_empty_duration + dura = DurationIso8601Validatable.new + dura.duration = '' + assert_not dura.valid? + end + + def test_return_epp_response_code_2005 + dura = DurationIso8601Validatable.new + dura.duration = '1 year' + epp_resp = DurationIso8601Validator.validate_epp(dura, dura.duration) + assert_equal epp_resp[:msg], "Expiry relative must be compatible to ISO 8601" + end +end