summaryrefslogtreecommitdiff
path: root/spec/validators
diff options
context:
space:
mode:
authorMatija Čupić <matteeyah@gmail.com>2018-02-13 17:52:33 +0100
committerMatija Čupić <matteeyah@gmail.com>2018-02-13 17:52:33 +0100
commite5d9f4a3744acaa035809176b78bed8d81802130 (patch)
tree1e0132dffd48c3ffd4c7a13c0dbe9e8666acc0b1 /spec/validators
parentc047fb860bba74acf49e67c228d1f83a18b691ad (diff)
downloadgitlab-ce-e5d9f4a3744acaa035809176b78bed8d81802130.tar.gz
Add specs for VariableDuplicates validator
Diffstat (limited to 'spec/validators')
-rw-r--r--spec/validators/variable_duplicates_validator_spec.rb67
1 files changed, 67 insertions, 0 deletions
diff --git a/spec/validators/variable_duplicates_validator_spec.rb b/spec/validators/variable_duplicates_validator_spec.rb
new file mode 100644
index 00000000000..be80b901b88
--- /dev/null
+++ b/spec/validators/variable_duplicates_validator_spec.rb
@@ -0,0 +1,67 @@
+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
+
+ expect(project.errors[:variables]).to include("Duplicate variables: #{project.variables.last.key}")
+ 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
+
+ expect(project.errors[:variables]).to include("Duplicate variables: #{project.variables.last.key}")
+ end
+ end
+ end
+ end
+end