Add coverage for `URLValidator` (#25591)

main-unfiltered
Matt Jankowski 2023-07-28 17:12:25 -04:00 committed by GitHub
parent 6602edf064
commit 660993b415
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 23 deletions

View File

@ -1,16 +1,31 @@
# frozen_string_literal: true # frozen_string_literal: true
class URLValidator < ActiveModel::EachValidator class URLValidator < ActiveModel::EachValidator
VALID_SCHEMES = %w(http https).freeze
def validate_each(record, attribute, value) def validate_each(record, attribute, value)
record.errors.add(attribute, :invalid) unless compliant?(value) @value = value
record.errors.add(attribute, :invalid) unless compliant_url?
end end
private private
def compliant?(url) def compliant_url?
parsed_url = Addressable::URI.parse(url) parsed_url.present? && valid_url_scheme? && valid_url_host?
parsed_url && %w(http https).include?(parsed_url.scheme) && parsed_url.host end
def parsed_url
Addressable::URI.parse(@value)
rescue Addressable::URI::InvalidURIError rescue Addressable::URI::InvalidURIError
false false
end end
def valid_url_scheme?
VALID_SCHEMES.include?(parsed_url.scheme)
end
def valid_url_host?
parsed_url.host.present?
end
end end

View File

@ -2,32 +2,64 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe URLValidator, type: :validator do describe URLValidator do
describe '#validate_each' do let(:record_class) do
before do Class.new do
allow(validator).to receive(:compliant?).with(value) { compliant } include ActiveModel::Validations
validator.validate_each(record, attribute, value) attr_accessor :profile
validates :profile, url: true
end end
end
let(:record) { record_class.new }
let(:validator) { described_class.new(attributes: [attribute]) } describe '#validate_each' do
let(:record) { instance_double(Webhook, errors: errors) } context 'with a nil value' do
let(:errors) { instance_double(ActiveModel::Errors, add: nil) } it 'adds errors' do
let(:value) { '' } record.profile = nil
let(:attribute) { :foo }
context 'when not compliant?' do expect(record).to_not be_valid
let(:compliant) { false } expect(record.errors.first.attribute).to eq(:profile)
expect(record.errors.first.type).to eq(:invalid)
it 'calls errors.add' do
expect(errors).to have_received(:add).with(attribute, :invalid)
end end
end end
context 'when compliant?' do context 'with an invalid url scheme' do
let(:compliant) { true } it 'adds errors' do
record.profile = 'ftp://example.com/page'
it 'not calls errors.add' do expect(record).to_not be_valid
expect(errors).to_not have_received(:add).with(attribute, any_args) 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
end end