1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe Ci::GroupVariable do
6  subject { build(:ci_group_variable) }
7
8  it_behaves_like "CI variable"
9
10  it { is_expected.to include_module(Presentable) }
11  it { is_expected.to include_module(Ci::Maskable) }
12  it { is_expected.to include_module(HasEnvironmentScope) }
13  it { is_expected.to validate_uniqueness_of(:key).scoped_to([:group_id, :environment_scope]).with_message(/\(\w+\) has already been taken/) }
14
15  describe '.by_environment_scope' do
16    let!(:matching_variable) { create(:ci_group_variable, environment_scope: 'production ') }
17    let!(:non_matching_variable) { create(:ci_group_variable, environment_scope: 'staging') }
18
19    subject { Ci::GroupVariable.by_environment_scope('production') }
20
21    it { is_expected.to contain_exactly(matching_variable) }
22  end
23
24  describe '.unprotected' do
25    subject { described_class.unprotected }
26
27    context 'when variable is protected' do
28      before do
29        create(:ci_group_variable, :protected)
30      end
31
32      it 'returns nothing' do
33        is_expected.to be_empty
34      end
35    end
36
37    context 'when variable is not protected' do
38      let(:variable) { create(:ci_group_variable, protected: false) }
39
40      it 'returns the variable' do
41        is_expected.to contain_exactly(variable)
42      end
43    end
44  end
45end
46