mastodon/spec/validators/url_validator_spec.rb

67 lines
1.7 KiB
Ruby
Raw Normal View History

2019-01-03 04:10:20 +00:00
# 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 }
2019-01-03 04:10:20 +00:00
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
2019-01-03 04:10:20 +00:00
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
2019-01-03 04:10:20 +00:00
context 'with an unparseable value' do
it 'adds errors' do
record.profile = 'https://host:port/page' # non-numeric port string causes invalid uri error
2019-01-03 04:10:20 +00:00
expect(record).to_not be_valid
expect(record.errors.first.attribute).to eq(:profile)
expect(record.errors.first.type).to eq(:invalid)
2019-01-03 04:10:20 +00:00
end
end
context 'with a valid url' do
it 'does not add errors' do
record.profile = 'https://example.com/page'
2019-01-03 04:10:20 +00:00
expect(record).to be_valid
expect(record.errors).to be_empty
2019-01-03 04:10:20 +00:00
end
end
end
end