2019-04-11 06:29:07 +00:00
|
|
|
RSpec.shared_examples 'url validator examples' do |schemes|
|
2019-01-16 12:09:29 +00:00
|
|
|
let(:validator) { described_class.new(attributes: [:link_url], **options) }
|
2018-06-01 11:43:53 +00:00
|
|
|
let!(:badge) { build(:badge, link_url: 'http://www.example.com') }
|
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
subject { validator.validate(badge) }
|
2018-06-01 11:43:53 +00:00
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
describe '#validate' do
|
2018-06-01 11:43:53 +00:00
|
|
|
context 'with no options' do
|
|
|
|
let(:options) { {} }
|
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
it "allows #{schemes.join(',')} schemes by default" do
|
|
|
|
expect(validator.options[:schemes]).to eq schemes
|
2018-06-01 11:43:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks that the url structure is valid' do
|
|
|
|
badge.link_url = "#{badge.link_url}:invalid_port"
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
expect(badge.errors).to be_present
|
2018-06-01 11:43:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
context 'with schemes' do
|
|
|
|
let(:options) { { schemes: %w(http) } }
|
2018-06-01 11:43:53 +00:00
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
it 'allows urls with the defined schemes' do
|
2018-06-01 11:43:53 +00:00
|
|
|
subject
|
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
expect(badge.errors).to be_empty
|
2018-06-01 11:43:53 +00:00
|
|
|
end
|
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
it 'add error if the url scheme does not match the selected ones' do
|
2018-06-01 11:43:53 +00:00
|
|
|
badge.link_url = 'https://www.example.com'
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
expect(badge.errors).to be_present
|
2018-06-01 11:43:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|