Read about our upcoming Code of Conduct on this issue

variable_spec.rb 1.12 KB
Newer Older
Douwe Maan's avatar
Douwe Maan committed
1
2
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
3
describe Ci::Variable, models: true do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
4
  subject { Ci::Variable.new }
Douwe Maan's avatar
Douwe Maan committed
5
6
7

  let(:secret_value) { 'secret' }

8
9
10
11
12
13
14
  it { is_expected.to validate_presence_of(:key) }
  it { is_expected.to validate_uniqueness_of(:key).scoped_to(:gl_project_id) }
  it { is_expected.to validate_length_of(:key).is_at_most(255) }
  it { is_expected.to allow_value('foo').for(:key) }
  it { is_expected.not_to allow_value('foo bar').for(:key) }
  it { is_expected.not_to allow_value('foo/bar').for(:key) }

Douwe Maan's avatar
Douwe Maan committed
15
16
17
18
  before :each do
    subject.value = secret_value
  end

19
  describe '#value' do
Douwe Maan's avatar
Douwe Maan committed
20
    it 'stores the encrypted value' do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
21
      expect(subject.encrypted_value).not_to be_nil
Douwe Maan's avatar
Douwe Maan committed
22
23
24
    end

    it 'stores an iv for value' do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
25
      expect(subject.encrypted_value_iv).not_to be_nil
Douwe Maan's avatar
Douwe Maan committed
26
27
28
    end

    it 'stores a salt for value' do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
29
      expect(subject.encrypted_value_salt).not_to be_nil
Douwe Maan's avatar
Douwe Maan committed
30
31
32
    end

    it 'fails to decrypt if iv is incorrect' do
33
      subject.encrypted_value_iv = SecureRandom.hex
Douwe Maan's avatar
Douwe Maan committed
34
      subject.instance_variable_set(:@value, nil)
35
36
      expect { subject.value }.
        to raise_error(OpenSSL::Cipher::CipherError, 'bad decrypt')
Douwe Maan's avatar
Douwe Maan committed
37
38
39
    end
  end
end