diff options
author | Shinya Maeda <shinya@gitlab.com> | 2017-06-28 16:16:19 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2017-06-28 16:16:19 +0900 |
commit | bbb3babf8a17c719fa5cd2c56dd24f00d842fed1 (patch) | |
tree | 9fb84a266322c4996c0eaa5d162287bc08e7854f | |
parent | 90610c2a45a892f9f2797a9c23f856135fa76e52 (diff) | |
parent | de893b19c30acf83ce43dd42376783505d704763 (diff) | |
download | gitlab-ce-bbb3babf8a17c719fa5cd2c56dd24f00d842fed1.tar.gz |
Merged HasVariable
-rw-r--r-- | app/models/ci/variable.rb | 19 | ||||
-rw-r--r-- | app/models/concerns/has_variable.rb | 23 | ||||
-rw-r--r-- | spec/models/ci/project_variable_spec.rb | 23 | ||||
-rw-r--r-- | spec/models/concerns/has_variable_spec.rb (renamed from spec/models/ci/variable_spec.rb) | 31 |
4 files changed, 51 insertions, 45 deletions
diff --git a/app/models/ci/variable.rb b/app/models/ci/variable.rb index f235260208f..96d6e120998 100644 --- a/app/models/ci/variable.rb +++ b/app/models/ci/variable.rb @@ -1,27 +1,12 @@ module Ci class Variable < ActiveRecord::Base extend Ci::Model + include HasVariable belongs_to :project - validates :key, - presence: true, - uniqueness: { scope: :project_id }, - length: { maximum: 255 }, - format: { with: /\A[a-zA-Z0-9_]+\z/, - message: "can contain only letters, digits and '_'." } + validates :key, uniqueness: { scope: :project_id } - scope :order_key_asc, -> { reorder(key: :asc) } scope :unprotected, -> { where(protected: false) } - - attr_encrypted :value, - mode: :per_attribute_iv_and_salt, - insecure_mode: true, - key: Gitlab::Application.secrets.db_key_base, - algorithm: 'aes-256-cbc' - - def to_runner_variable - { key: key, value: value, public: false } - end end end diff --git a/app/models/concerns/has_variable.rb b/app/models/concerns/has_variable.rb new file mode 100644 index 00000000000..9585b5583dc --- /dev/null +++ b/app/models/concerns/has_variable.rb @@ -0,0 +1,23 @@ +module HasVariable + extend ActiveSupport::Concern + + included do + validates :key, + presence: true, + length: { maximum: 255 }, + format: { with: /\A[a-zA-Z0-9_]+\z/, + message: "can contain only letters, digits and '_'." } + + scope :order_key_asc, -> { reorder(key: :asc) } + + attr_encrypted :value, + mode: :per_attribute_iv_and_salt, + insecure_mode: true, + key: Gitlab::Application.secrets.db_key_base, + algorithm: 'aes-256-cbc' + + def to_runner_variable + { key: key, value: value, public: false } + end + end +end diff --git a/spec/models/ci/project_variable_spec.rb b/spec/models/ci/project_variable_spec.rb index 2c72c401f04..3c2d6a19cf5 100644 --- a/spec/models/ci/project_variable_spec.rb +++ b/spec/models/ci/project_variable_spec.rb @@ -3,5 +3,28 @@ require 'spec_helper' describe Ci::ProjectVariable, models: true do subject { build(:ci_project_variable) } + it { is_expected.to include_module(HasVariable) } it { is_expected.to validate_uniqueness_of(:key).scoped_to(:project_id) } + + describe '.unprotected' do + subject { described_class.unprotected } + + context 'when variable is protected' do + before do + create(:ci_variable, :protected) + end + + it 'returns nothing' do + is_expected.to be_empty + end + end + + context 'when variable is not protected' do + let(:variable) { create(:ci_variable, protected: false) } + + it 'returns the variable' do + is_expected.to contain_exactly(variable) + end + end + end end diff --git a/spec/models/ci/variable_spec.rb b/spec/models/concerns/has_variable_spec.rb index 3f18681a56a..f4b24e6d1d9 100644 --- a/spec/models/ci/variable_spec.rb +++ b/spec/models/concerns/has_variable_spec.rb @@ -1,10 +1,7 @@ require 'spec_helper' -describe Ci::Variable, models: true do - set(:project) { create(:empty_project) } - let(:secret_value) { 'secret' } - - subject { build(:ci_variable, project_id: project.id) } +describe HasVariable do + subject { build(:ci_variable) } it { is_expected.to validate_presence_of(:key) } it { is_expected.to validate_length_of(:key).is_at_most(255) } @@ -12,31 +9,9 @@ describe Ci::Variable, models: true do it { is_expected.not_to allow_value('foo bar').for(:key) } it { is_expected.not_to allow_value('foo/bar').for(:key) } - describe '.unprotected' do - subject { described_class.unprotected } - - context 'when variable is protected' do - before do - create(:ci_variable, :protected) - end - - it 'returns nothing' do - is_expected.to be_empty - end - end - - context 'when variable is not protected' do - let(:variable) { create(:ci_variable, protected: false) } - - it 'returns the variable' do - is_expected.to contain_exactly(variable) - end - end - end - describe '#value' do before do - subject.value = secret_value + subject.value = 'secret' end it 'stores the encrypted value' do |