2019-10-13 20:06:38 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-13 11:52:33 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe VariableDuplicatesValidator do
|
|
|
|
let(:validator) { described_class.new(attributes: [:variables], **options) }
|
|
|
|
|
|
|
|
describe '#validate_each' do
|
|
|
|
let(:project) { build(:project) }
|
|
|
|
|
|
|
|
subject { validator.validate_each(project, :variables, project.variables) }
|
|
|
|
|
|
|
|
context 'with no scope' do
|
|
|
|
let(:options) { {} }
|
|
|
|
let(:variables) { build_list(:ci_variable, 2, project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.variables << variables
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not have any errors' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(project.errors.empty?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with duplicates' do
|
|
|
|
before do
|
|
|
|
project.variables.build(key: variables.first.key, value: 'dummy_value')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a duplicate key error' do
|
|
|
|
subject
|
|
|
|
|
2018-02-13 13:46:02 -05:00
|
|
|
expect(project.errors).to have_key(:variables)
|
2018-02-13 11:52:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a scope attribute' do
|
|
|
|
let(:options) { { scope: :environment_scope } }
|
|
|
|
let(:first_variable) { build(:ci_variable, key: 'test_key', environment_scope: '*', project: project) }
|
|
|
|
let(:second_variable) { build(:ci_variable, key: 'test_key', environment_scope: 'prod', project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.variables << first_variable
|
|
|
|
project.variables << second_variable
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not have any errors' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(project.errors.empty?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with duplicates' do
|
|
|
|
before do
|
|
|
|
project.variables.build(key: second_variable.key, value: 'dummy_value', environment_scope: second_variable.environment_scope)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a duplicate key error' do
|
|
|
|
subject
|
|
|
|
|
2018-02-13 13:46:02 -05:00
|
|
|
expect(project.errors).to have_key(:variables)
|
2018-02-13 11:52:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|