summaryrefslogtreecommitdiff
path: root/spec/validators/qualified_domain_array_validator_spec.rb
blob: a96b00bfd1d0b9bac4d8f95ffe4beea1bfba3f73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

require 'spec_helper'

describe QualifiedDomainArrayValidator do
  class TestClass
    include ActiveModel::Validations

    attr_accessor :domain_array

    def initialize(domain_array)
      self.domain_array = domain_array
    end
  end

  let!(:record) do
    TestClass.new(['gitlab.com'])
  end

  subject { validator.validate(record) }

  shared_examples 'cannot be blank' do
    it 'returns error when attribute is blank' do
      record.domain_array = []

      subject

      expect(record.errors).to be_present
      expect(record.errors.first[1]).to eq 'entries cannot be blank'
    end
  end

  shared_examples 'can be nil' do
    it 'allows when attribute is nil' do
      record.domain_array = nil

      subject

      expect(record.errors).to be_empty
    end
  end

  describe 'validations' do
    let(:validator) { described_class.new(attributes: [:domain_array]) }

    it_behaves_like 'cannot be blank'

    it 'returns error when attribute is nil' do
      record.domain_array = nil

      subject

      expect(record.errors).to be_present
    end

    it 'allows when domain is valid' do
      subject

      expect(record.errors).to be_empty
    end

    it 'returns error when domain contains unicode' do
      record.domain_array = ['ğitlab.com']

      subject

      expect(record.errors).to be_present
      expect(record.errors.first[1]).to eq 'unicode domains should use IDNA encoding'
    end

    it 'returns error when entry is larger than 255 chars' do
      record.domain_array = ['a' * 256]

      subject

      expect(record.errors).to be_present
      expect(record.errors.first[1]).to eq 'entries cannot be larger than 255 characters'
    end

    it 'returns error when entry contains HTML tags' do
      record.domain_array = ['gitlab.com<h1>something</h1>']

      subject

      expect(record.errors).to be_present
      expect(record.errors.first[1]).to eq 'entries cannot contain HTML tags'
    end
  end

  context 'when allow_nil is set to true' do
    let(:validator) { described_class.new(attributes: [:domain_array], allow_nil: true) }

    it_behaves_like 'can be nil'

    it_behaves_like 'cannot be blank'
  end

  context 'when allow_blank is set to true' do
    let(:validator) { described_class.new(attributes: [:domain_array], allow_blank: true) }

    it_behaves_like 'can be nil'

    it 'allows when attribute is blank' do
      record.domain_array = []

      subject

      expect(record.errors).to be_empty
    end
  end
end