item_spec.rb 3.97 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::Ci::Variables::Collection::Item do
4 5 6 7
  let(:variable_key) { 'VAR' }
  let(:variable_value) { 'something' }
  let(:expected_value) { variable_value }

8
  let(:variable) do
9
    { key: variable_key, value: variable_value, public: true, masked: false }
10 11
  end

12
  describe '.new' do
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    context 'when unknown keyword is specified' do
      it 'raises error' do
        expect { described_class.new(key: variable_key, value: 'abc', files: true) }
          .to raise_error ArgumentError, 'unknown keyword: files'
      end
    end

    context 'when required keywords are not specified' do
      it 'raises error' do
        expect { described_class.new(key: variable_key) }
          .to raise_error ArgumentError, 'missing keyword: value'
      end
    end

    shared_examples 'creates variable' do
      subject { described_class.new(key: variable_key, value: variable_value) }

      it 'saves given value' do
        expect(subject[:key]).to eq variable_key
        expect(subject[:value]).to eq expected_value
      end
    end

    shared_examples 'raises error for invalid type' do
      it do
        expect { described_class.new(key: variable_key, value: variable_value) }
39
          .to raise_error ArgumentError, /`#{variable_key}` must be of type String or nil value, while it was:/
40
      end
41 42
    end

43 44 45 46 47 48
    it_behaves_like 'creates variable'

    context "when it's nil" do
      let(:variable_value) { nil }
      let(:expected_value) { nil }

49
      it_behaves_like 'creates variable'
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    end

    context "when it's an empty string" do
      let(:variable_value) { '' }
      let(:expected_value) { '' }

      it_behaves_like 'creates variable'
    end

    context 'when provided value is not a string' do
      [1, false, [], {}, Object.new].each do |val|
        context "when it's #{val}" do
          let(:variable_value) { val }

          it_behaves_like 'raises error for invalid type'
        end
      end
67 68 69
    end
  end

70 71 72 73 74 75 76 77
  describe '.fabricate' do
    it 'supports using a hash' do
      resource = described_class.fabricate(variable)

      expect(resource).to be_a(described_class)
      expect(resource).to eq variable
    end

78 79 80 81 82 83 84 85
    it 'supports using a hash with stringified values' do
      variable = { 'key' => 'VARIABLE', 'value' => 'my value' }

      resource = described_class.fabricate(variable)

      expect(resource).to eq(key: 'VARIABLE', value: 'my value')
    end

86
    it 'supports using an active record resource' do
87 88
      variable = create(:ci_variable, key: 'CI_VAR', value: '123')
      resource = described_class.fabricate(variable)
89 90

      expect(resource).to be_a(described_class)
91
      expect(resource).to eq(key: 'CI_VAR', value: '123', public: false, masked: false)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    end

    it 'supports using another collection item' do
      item = described_class.new(**variable)

      resource = described_class.fabricate(item)

      expect(resource).to be_a(described_class)
      expect(resource).to eq variable
      expect(resource.object_id).not_to eq item.object_id
    end
  end

  describe '#==' do
    it 'compares a hash representation of a variable' do
      expect(described_class.new(**variable) == variable).to be true
    end
  end

111 112 113 114 115 116 117 118
  describe '#[]' do
    it 'behaves like a hash accessor' do
      item = described_class.new(**variable)

      expect(item[:key]).to eq 'VAR'
    end
  end

119
  describe '#to_runner_variable' do
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    context 'when variable is not a file-related' do
      it 'returns a runner-compatible hash representation' do
        runner_variable = described_class
          .new(**variable)
          .to_runner_variable

        expect(runner_variable).to eq variable
      end
    end

    context 'when variable is file-related' do
      it 'appends file description component' do
        runner_variable = described_class
          .new(key: 'VAR', value: 'value', file: true)
          .to_runner_variable
135

136
        expect(runner_variable)
137 138 139
          .to eq(key: 'VAR', value: 'value', public: true, file: true, masked: false)
      end
    end
140 141
  end
end