Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/concerns')
-rw-r--r--spec/models/concerns/cache_markdown_field_spec.rb2
-rw-r--r--spec/models/concerns/integrations/reset_secret_fields_spec.rb19
-rw-r--r--spec/models/concerns/issuable_spec.rb28
-rw-r--r--spec/models/concerns/pg_full_text_searchable_spec.rb13
-rw-r--r--spec/models/concerns/reactive_caching_spec.rb2
-rw-r--r--spec/models/concerns/schedulable_spec.rb10
-rw-r--r--spec/models/concerns/sha256_attribute_spec.rb91
-rw-r--r--spec/models/concerns/sha_attribute_spec.rb135
8 files changed, 132 insertions, 168 deletions
diff --git a/spec/models/concerns/cache_markdown_field_spec.rb b/spec/models/concerns/cache_markdown_field_spec.rb
index 1c1efab2889..d46f22b2216 100644
--- a/spec/models/concerns/cache_markdown_field_spec.rb
+++ b/spec/models/concerns/cache_markdown_field_spec.rb
@@ -149,7 +149,7 @@ RSpec.describe CacheMarkdownField, :clean_gitlab_redis_cache do
it 'saves the changes' do
expect(thing)
.to receive(:save_markdown)
- .with("description_html" => updated_html, "title_html" => "", "cached_markdown_version" => cache_version)
+ .with({ "description_html" => updated_html, "title_html" => "", "cached_markdown_version" => cache_version })
thing.refresh_markdown_cache!
end
diff --git a/spec/models/concerns/integrations/reset_secret_fields_spec.rb b/spec/models/concerns/integrations/reset_secret_fields_spec.rb
new file mode 100644
index 00000000000..a372550c70f
--- /dev/null
+++ b/spec/models/concerns/integrations/reset_secret_fields_spec.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Integrations::ResetSecretFields do
+ let(:described_class) do
+ Class.new(Integration) do
+ field :username, type: 'text'
+ field :url, type: 'text', exposes_secrets: true
+ field :api_url, type: 'text', exposes_secrets: true
+ field :password, type: 'password'
+ field :token, type: 'password'
+ end
+ end
+
+ let(:integration) { described_class.new }
+
+ it_behaves_like Integrations::ResetSecretFields
+end
diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb
index b38135fc0b2..e8e9c263d23 100644
--- a/spec/models/concerns/issuable_spec.rb
+++ b/spec/models/concerns/issuable_spec.rb
@@ -474,11 +474,11 @@ RSpec.describe Issuable do
issue.update!(labels: [label])
issue.assignees << user
issue.spend_time(duration: 2, user_id: user.id, spent_at: Time.current)
- expect(Gitlab::HookData::IssuableBuilder)
+ expect(Gitlab::DataBuilder::Issuable)
.to receive(:new).with(issue).and_return(builder)
end
- it 'delegates to Gitlab::HookData::IssuableBuilder#build and does not set labels, assignees, nor total_time_spent' do
+ it 'delegates to Gitlab::DataBuilder::Issuable#build and does not set labels, assignees, nor total_time_spent' do
expect(builder).to receive(:build).with(
user: user,
changes: {})
@@ -493,11 +493,11 @@ RSpec.describe Issuable do
before do
issue.update!(labels: [labels[1]])
- expect(Gitlab::HookData::IssuableBuilder)
+ expect(Gitlab::DataBuilder::Issuable)
.to receive(:new).with(issue).and_return(builder)
end
- it 'delegates to Gitlab::HookData::IssuableBuilder#build' do
+ it 'delegates to Gitlab::DataBuilder::Issuable#build' do
expect(builder).to receive(:build).with(
user: user,
changes: hash_including(
@@ -512,11 +512,11 @@ RSpec.describe Issuable do
before do
issue.spend_time(duration: 2, user_id: user.id, spent_at: Time.current)
issue.save!
- expect(Gitlab::HookData::IssuableBuilder)
+ expect(Gitlab::DataBuilder::Issuable)
.to receive(:new).with(issue).and_return(builder)
end
- it 'delegates to Gitlab::HookData::IssuableBuilder#build' do
+ it 'delegates to Gitlab::DataBuilder::Issuable#build' do
expect(builder).to receive(:build).with(
user: user,
changes: hash_including(
@@ -532,11 +532,11 @@ RSpec.describe Issuable do
before do
issue.assignees << user << user2
- expect(Gitlab::HookData::IssuableBuilder)
+ expect(Gitlab::DataBuilder::Issuable)
.to receive(:new).with(issue).and_return(builder)
end
- it 'delegates to Gitlab::HookData::IssuableBuilder#build' do
+ it 'delegates to Gitlab::DataBuilder::Issuable#build' do
expect(builder).to receive(:build).with(
user: user,
changes: hash_including(
@@ -554,11 +554,11 @@ RSpec.describe Issuable do
before do
merge_request.update!(assignees: [user])
merge_request.update!(assignees: [user, user2])
- expect(Gitlab::HookData::IssuableBuilder)
+ expect(Gitlab::DataBuilder::Issuable)
.to receive(:new).with(merge_request).and_return(builder)
end
- it 'delegates to Gitlab::HookData::IssuableBuilder#build' do
+ it 'delegates to Gitlab::DataBuilder::Issuable#build' do
expect(builder).to receive(:build).with(
user: user,
changes: hash_including(
@@ -574,11 +574,11 @@ RSpec.describe Issuable do
before do
issue.update!(issuable_severity_attributes: { severity: 'low' })
- expect(Gitlab::HookData::IssuableBuilder)
+ expect(Gitlab::DataBuilder::Issuable)
.to receive(:new).with(issue).and_return(builder)
end
- it 'delegates to Gitlab::HookData::IssuableBuilder#build' do
+ it 'delegates to Gitlab::DataBuilder::Issuable#build' do
expect(builder).to receive(:build).with(
user: user,
changes: hash_including(
@@ -596,10 +596,10 @@ RSpec.describe Issuable do
before do
issue.escalation_status.update!(status: acknowledged)
- expect(Gitlab::HookData::IssuableBuilder).to receive(:new).with(issue).and_return(builder)
+ expect(Gitlab::DataBuilder::Issuable).to receive(:new).with(issue).and_return(builder)
end
- it 'delegates to Gitlab::HookData::IssuableBuilder#build' do
+ it 'delegates to Gitlab::DataBuilder::Issuable#build' do
expect(builder).to receive(:build).with(
user: user,
changes: hash_including(
diff --git a/spec/models/concerns/pg_full_text_searchable_spec.rb b/spec/models/concerns/pg_full_text_searchable_spec.rb
index db7f652f494..b6da481024a 100644
--- a/spec/models/concerns/pg_full_text_searchable_spec.rb
+++ b/spec/models/concerns/pg_full_text_searchable_spec.rb
@@ -54,12 +54,23 @@ RSpec.describe PgFullTextSearchable do
end
context 'when specified columns are not changed' do
- it 'does not enqueue worker' do
+ it 'does not call update_search_data!' do
expect(model).not_to receive(:update_search_data!)
model.update!(description: 'A new description')
end
end
+
+ context 'when model is updated twice within a transaction' do
+ it 'calls update_search_data!' do
+ expect(model).to receive(:update_search_data!)
+
+ model.transaction do
+ model.update!(title: 'A new title')
+ model.update!(updated_at: Time.current)
+ end
+ end
+ end
end
describe '.pg_full_text_search' do
diff --git a/spec/models/concerns/reactive_caching_spec.rb b/spec/models/concerns/reactive_caching_spec.rb
index 4f3b95e43cd..5468699f9dd 100644
--- a/spec/models/concerns/reactive_caching_spec.rb
+++ b/spec/models/concerns/reactive_caching_spec.rb
@@ -237,7 +237,7 @@ RSpec.describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end
it 'does not raise the exception' do
- expect { go! }.not_to raise_exception(ReactiveCaching::ExceededReactiveCacheLimit)
+ expect { go! }.not_to raise_exception
end
end
diff --git a/spec/models/concerns/schedulable_spec.rb b/spec/models/concerns/schedulable_spec.rb
index 62acd12e267..b98dcf1c174 100644
--- a/spec/models/concerns/schedulable_spec.rb
+++ b/spec/models/concerns/schedulable_spec.rb
@@ -57,6 +57,16 @@ RSpec.describe Schedulable do
it_behaves_like '.runnable_schedules'
end
+ context 'for a packages cleanup policy' do
+ # let! is used to reset the next_run_at value before each spec
+ let(:object) { create(:packages_cleanup_policy, :runnable) }
+ let(:non_runnable_object) { create(:packages_cleanup_policy) }
+
+ it_behaves_like '#schedule_next_run!'
+ it_behaves_like 'before_save callback'
+ it_behaves_like '.runnable_schedules'
+ end
+
describe '#next_run_at' do
let(:schedulable_instance) do
Class.new(ActiveRecord::Base) do
diff --git a/spec/models/concerns/sha256_attribute_spec.rb b/spec/models/concerns/sha256_attribute_spec.rb
deleted file mode 100644
index 02947325bf4..00000000000
--- a/spec/models/concerns/sha256_attribute_spec.rb
+++ /dev/null
@@ -1,91 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-RSpec.describe Sha256Attribute do
- let(:model) { Class.new(ApplicationRecord) { include Sha256Attribute } }
-
- before do
- columns = [
- double(:column, name: 'name', type: :text),
- double(:column, name: 'sha256', type: :binary)
- ]
-
- allow(model).to receive(:columns).and_return(columns)
- end
-
- describe '#sha_attribute' do
- context 'when in non-production' do
- before do
- stub_rails_env('development')
- end
-
- context 'when the table exists' do
- before do
- allow(model).to receive(:table_exists?).and_return(true)
- end
-
- it 'defines a SHA attribute for a binary column' do
- expect(model).to receive(:attribute)
- .with(:sha256, an_instance_of(Gitlab::Database::Sha256Attribute))
-
- model.sha256_attribute(:sha256)
- end
-
- it 'raises ArgumentError when the column type is not :binary' do
- expect { model.sha256_attribute(:name) }.to raise_error(ArgumentError)
- end
- end
-
- context 'when the table does not exist' do
- it 'allows the attribute to be added and issues a warning' do
- allow(model).to receive(:table_exists?).and_return(false)
-
- expect(model).not_to receive(:columns)
- expect(model).to receive(:attribute)
- expect(model).to receive(:warn)
-
- model.sha256_attribute(:name)
- end
- end
-
- context 'when the column does not exist' do
- it 'allows the attribute to be added and issues a warning' do
- allow(model).to receive(:table_exists?).and_return(true)
-
- expect(model).to receive(:columns)
- expect(model).to receive(:attribute)
- expect(model).to receive(:warn)
-
- model.sha256_attribute(:no_name)
- end
- end
-
- context 'when other execeptions are raised' do
- it 'logs and re-rasises the error' do
- allow(model).to receive(:table_exists?).and_raise(ActiveRecord::NoDatabaseError.new('does not exist'))
-
- expect(model).not_to receive(:columns)
- expect(model).not_to receive(:attribute)
- expect(Gitlab::AppLogger).to receive(:error)
-
- expect { model.sha256_attribute(:name) }.to raise_error(ActiveRecord::NoDatabaseError)
- end
- end
- end
-
- context 'when in production' do
- before do
- stub_rails_env('production')
- end
-
- it 'defines a SHA attribute' do
- expect(model).not_to receive(:table_exists?)
- expect(model).not_to receive(:columns)
- expect(model).to receive(:attribute).with(:sha256, an_instance_of(Gitlab::Database::Sha256Attribute))
-
- model.sha256_attribute(:sha256)
- end
- end
- end
-end
diff --git a/spec/models/concerns/sha_attribute_spec.rb b/spec/models/concerns/sha_attribute_spec.rb
index 1bcf3dc8b61..790e6936803 100644
--- a/spec/models/concerns/sha_attribute_spec.rb
+++ b/spec/models/concerns/sha_attribute_spec.rb
@@ -3,86 +3,101 @@
require 'spec_helper'
RSpec.describe ShaAttribute do
- let(:model) { Class.new(ActiveRecord::Base) { include ShaAttribute } }
+ let(:model) do
+ Class.new(ActiveRecord::Base) do
+ include ShaAttribute
- before do
- columns = [
- double(:column, name: 'name', type: :text),
- double(:column, name: 'sha1', type: :binary)
- ]
-
- allow(model).to receive(:columns).and_return(columns)
+ self.table_name = 'merge_requests'
+ end
end
- describe '#sha_attribute' do
- context 'when in development' do
- before do
- stub_rails_env('development')
- end
+ let(:binary_column) { :merge_ref_sha }
+ let(:text_column) { :target_branch }
- context 'when the table exists' do
- before do
- allow(model).to receive(:table_exists?).and_return(true)
- end
+ describe '.sha_attribute' do
+ it 'defines a SHA attribute with Gitlab::Database::ShaAttribute type' do
+ expect(model).to receive(:attribute)
+ .with(binary_column, an_instance_of(Gitlab::Database::ShaAttribute))
+ .and_call_original
- it 'defines a SHA attribute for a binary column' do
- expect(model).to receive(:attribute)
- .with(:sha1, an_instance_of(Gitlab::Database::ShaAttribute))
+ model.sha_attribute(binary_column)
+ end
+ end
- model.sha_attribute(:sha1)
- end
+ describe '.sha256_attribute' do
+ it 'defines a SHA256 attribute with Gitlab::Database::ShaAttribute type' do
+ expect(model).to receive(:attribute)
+ .with(binary_column, an_instance_of(Gitlab::Database::Sha256Attribute))
+ .and_call_original
- it 'raises ArgumentError when the column type is not :binary' do
- expect { model.sha_attribute(:name) }.to raise_error(ArgumentError)
- end
- end
+ model.sha256_attribute(binary_column)
+ end
+ end
- context 'when the table does not exist' do
- it 'allows the attribute to be added' do
- allow(model).to receive(:table_exists?).and_return(false)
+ describe '.load_schema!' do
+ # load_schema! is not a documented class method, so use a documented method
+ # that we know will call load_schema!
+ def load_schema!
+ expect(model).to receive(:load_schema!).and_call_original
- expect(model).not_to receive(:columns)
- expect(model).to receive(:attribute)
+ model.new
+ end
- model.sha_attribute(:name)
- end
- end
+ using RSpec::Parameterized::TableSyntax
- context 'when the column does not exist' do
- it 'allows the attribute to be added' do
- allow(model).to receive(:table_exists?).and_return(true)
+ where(:column_name, :environment, :expected_error) do
+ ref(:binary_column) | 'development' | :no_error
+ ref(:binary_column) | 'production' | :no_error
+ ref(:text_column) | 'development' | :sha_mismatch_error
+ ref(:text_column) | 'production' | :no_error
+ :__non_existent_column | 'development' | :no_error
+ :__non_existent_column | 'production' | :no_error
+ end
- expect(model).to receive(:columns)
- expect(model).to receive(:attribute)
+ let(:sha_mismatch_error) do
+ [
+ described_class::ShaAttributeTypeMismatchError,
+ /#{column_name}.* should be a :binary column/
+ ]
+ end
- model.sha_attribute(:no_name)
- end
+ with_them do
+ before do
+ stub_rails_env(environment)
end
- context 'when other execeptions are raised' do
- it 'logs and re-rasises the error' do
- allow(model).to receive(:table_exists?).and_raise(ActiveRecord::NoDatabaseError.new('does not exist'))
-
- expect(model).not_to receive(:columns)
- expect(model).not_to receive(:attribute)
- expect(Gitlab::AppLogger).to receive(:error)
-
- expect { model.sha_attribute(:name) }.to raise_error(ActiveRecord::NoDatabaseError)
+ context 'with sha_attribute' do
+ before do
+ model.sha_attribute(column_name)
end
- end
- end
- context 'when in production' do
- before do
- stub_rails_env('production')
+ it 'validates column type' do
+ if expected_error == :no_error
+ expect { load_schema! }.not_to raise_error
+ elsif expected_error == :sha_mismatch_error
+ expect { load_schema! }.to raise_error(
+ described_class::ShaAttributeTypeMismatchError,
+ /sha_attribute.*#{column_name}.* should be a :binary column/
+ )
+ end
+ end
end
- it 'defines a SHA attribute' do
- expect(model).not_to receive(:table_exists?)
- expect(model).not_to receive(:columns)
- expect(model).to receive(:attribute).with(:sha1, an_instance_of(Gitlab::Database::ShaAttribute))
+ context 'with sha256_attribute' do
+ before do
+ model.sha256_attribute(column_name)
+ end
- model.sha_attribute(:sha1)
+ it 'validates column type' do
+ if expected_error == :no_error
+ expect { load_schema! }.not_to raise_error
+ elsif expected_error == :sha_mismatch_error
+ expect { load_schema! }.to raise_error(
+ described_class::Sha256AttributeTypeMismatchError,
+ /sha256_attribute.*#{column_name}.* should be a :binary column/
+ )
+ end
+ end
end
end
end