~cytrogen/masto-fe

ref: 9af24835f6f7ecc6e71c9767562d78bd4d90dcac masto-fe/spec/validators/url_validator_spec.rb -rw-r--r-- 1.7 KiB
9af24835 — Claire Merge pull request #2434 from ClearlyClaire/glitch-soc/merge-upstream 2 years ago
                                                                                
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
# frozen_string_literal: true

require 'rails_helper'

describe URLValidator do
  let(:record_class) do
    Class.new do
      include ActiveModel::Validations
      attr_accessor :profile

      validates :profile, url: true
    end
  end
  let(:record) { record_class.new }

  describe '#validate_each' do
    context 'with a nil value' do
      it 'adds errors' do
        record.profile = nil

        expect(record).to_not be_valid
        expect(record.errors.first.attribute).to eq(:profile)
        expect(record.errors.first.type).to eq(:invalid)
      end
    end

    context 'with an invalid url scheme' do
      it 'adds errors' do
        record.profile = 'ftp://example.com/page'

        expect(record).to_not be_valid
        expect(record.errors.first.attribute).to eq(:profile)
        expect(record.errors.first.type).to eq(:invalid)
      end
    end

    context 'without a hostname' do
      it 'adds errors' do
        record.profile = 'https:///page'

        expect(record).to_not be_valid
        expect(record.errors.first.attribute).to eq(:profile)
        expect(record.errors.first.type).to eq(:invalid)
      end
    end

    context 'with an unparseable value' do
      it 'adds errors' do
        record.profile = 'https://host:port/page' # non-numeric port string causes invalid uri error

        expect(record).to_not be_valid
        expect(record.errors.first.attribute).to eq(:profile)
        expect(record.errors.first.type).to eq(:invalid)
      end
    end

    context 'with a valid url' do
      it 'does not add errors' do
        record.profile = 'https://example.com/page'

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