Sha256: 85fdd8fa49c3b5af81de5962178171c538d8f021ecb71ec396acebe1c57536c7

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

require 'test_plugin_helper'

class ForemanHostExtraValidatorTest < ActiveSupport::TestCase
  setup do
    Setting::ForemanHostExtraValidator.load_defaults
    disable_orchestration
    User.current = FactoryBot.build(:user, :admin)
  end

  context 'with validation regex' do
    setup do
      Setting[:host_name_validation_regex] = '^[0-9]+$'
      @host = FactoryBot.build(:host)
    end

    test 'host should validate from settings' do
      @host.name = '054354'
      assert_valid @host
    end

    test 'host should not validate when host name does not match regex' do
      @host.name = 'invalidhostname'
      refute_valid @host
      assert_includes @host.errors[:name], "must match regex /#{Setting[:host_name_validation_regex]}/"
    end

    test 'host should validate from hostgroup parameter' do
      hostgroup = FactoryBot.create(:hostgroup)
      @host.hostgroup = hostgroup
      FactoryBot.create(:hostgroup_parameter, :name => 'host_name_validation_regex', :value => '^[a-z]+$', :hostgroup => hostgroup)

      assert_equal '^[a-z]+$', @host.send(:validate_name_regex)
      @host.hostname = 'abcdef'
      assert_valid @host

      @host.hostname = '1234'
      refute_valid @host
      assert_includes @host.errors[:name], "must match regex /^[a-z]+$/"
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
foreman_host_extra_validator-0.2.0 test/unit/foreman_host_extra_validator_test.rb
foreman_host_extra_validator-0.1.0 test/unit/foreman_host_extra_validator_test.rb