debian-mirror-gitlab/spec/support/shared_examples/url_validator_examples.rb

43 lines
1 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
RSpec.shared_examples 'url validator examples' do |schemes|
2019-03-02 22:35:43 +05:30
let(:validator) { described_class.new(attributes: [:link_url], **options) }
2018-11-08 19:23:39 +05:30
let!(:badge) { build(:badge, link_url: 'http://www.example.com') }
2019-07-31 22:56:46 +05:30
subject { validator.validate(badge) }
2018-11-08 19:23:39 +05:30
2019-07-31 22:56:46 +05:30
describe '#validate' do
2018-11-08 19:23:39 +05:30
context 'with no options' do
let(:options) { {} }
2019-07-31 22:56:46 +05:30
it "allows #{schemes.join(',')} schemes by default" do
expect(validator.options[:schemes]).to eq schemes
2018-11-08 19:23:39 +05:30
end
it 'checks that the url structure is valid' do
badge.link_url = "#{badge.link_url}:invalid_port"
subject
2019-07-31 22:56:46 +05:30
expect(badge.errors).to be_present
2018-11-08 19:23:39 +05:30
end
end
2019-07-31 22:56:46 +05:30
context 'with schemes' do
let(:options) { { schemes: %w(http) } }
2018-11-08 19:23:39 +05:30
2019-07-31 22:56:46 +05:30
it 'allows urls with the defined schemes' do
2018-11-08 19:23:39 +05:30
subject
2019-07-31 22:56:46 +05:30
expect(badge.errors).to be_empty
2018-11-08 19:23:39 +05:30
end
2019-07-31 22:56:46 +05:30
it 'add error if the url scheme does not match the selected ones' do
2018-11-08 19:23:39 +05:30
badge.link_url = 'https://www.example.com'
subject
2019-07-31 22:56:46 +05:30
expect(badge.errors).to be_present
2018-11-08 19:23:39 +05:30
end
end
end
end