added test for jobs, validators and helper

This commit is contained in:
Oleg Hasjanov 2021-03-03 18:00:14 +02:00
parent d53d4f2412
commit 14b8dd6ade
6 changed files with 170 additions and 4 deletions

View file

@ -0,0 +1,14 @@
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
def test_env_style_when_pic_present
assert_dom_equal %{<body style={"background-image: url(#{image_path("registrar/bg-#{unstable_env}.png")});"}>},
%{<body style={"#{env_style}"}>}
end
def test_env_style_return_nil
env_style = ''
assert_dom_equal %{<body style=''>},
%{<body style={"#{env_style}"}>}
end
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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