2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-17 03:33:32 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Config::Entry::Validatable do
|
2017-08-24 08:54:27 -04:00
|
|
|
let(:entry) do
|
2018-11-29 06:44:48 -05:00
|
|
|
Class.new(Gitlab::Config::Entry::Node) do
|
|
|
|
include Gitlab::Config::Entry::Validatable
|
2017-08-24 08:54:27 -04:00
|
|
|
end
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.validator' do
|
|
|
|
before do
|
2016-11-14 04:31:45 -05:00
|
|
|
entry.class_eval do
|
2016-06-17 03:33:32 -04:00
|
|
|
attr_accessor :test_attribute
|
|
|
|
|
|
|
|
validations do
|
|
|
|
validates :test_attribute, presence: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns validator' do
|
2016-11-14 04:31:45 -05:00
|
|
|
expect(entry.validator.superclass)
|
2018-11-29 06:44:48 -05:00
|
|
|
.to be Gitlab::Config::Entry::Validator
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
|
2016-08-09 05:29:32 -04:00
|
|
|
it 'returns only one validator to mitigate leaks' do
|
2016-11-14 04:31:45 -05:00
|
|
|
expect { entry.validator }.not_to change { entry.validator }
|
2016-08-09 05:29:32 -04:00
|
|
|
end
|
|
|
|
|
2016-11-14 04:31:45 -05:00
|
|
|
context 'when validating entry instance' do
|
2017-08-24 08:54:27 -04:00
|
|
|
let(:entry_instance) { entry.new('something') }
|
2016-06-17 03:33:32 -04:00
|
|
|
|
|
|
|
context 'when attribute is valid' do
|
|
|
|
before do
|
2016-11-14 04:31:45 -05:00
|
|
|
entry_instance.test_attribute = 'valid'
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'instance of validator is valid' do
|
2016-11-14 04:31:45 -05:00
|
|
|
expect(entry.validator.new(entry_instance)).to be_valid
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when attribute is not valid' do
|
|
|
|
before do
|
2016-11-14 04:31:45 -05:00
|
|
|
entry_instance.test_attribute = nil
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'instance of validator is invalid' do
|
2016-11-14 04:31:45 -05:00
|
|
|
expect(entry.validator.new(entry_instance)).to be_invalid
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|