diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
commit | a09983ae35713f5a2bbb100981116d31ce99826e (patch) | |
tree | 2ee2af7bd104d57086db360a7e6d8c9d5d43667a /spec/lib/gitlab/background_migration | |
parent | 18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff) |
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'spec/lib/gitlab/background_migration')
53 files changed, 492 insertions, 361 deletions
diff --git a/spec/lib/gitlab/background_migration/add_merge_request_diff_commits_count_spec.rb b/spec/lib/gitlab/background_migration/add_merge_request_diff_commits_count_spec.rb index b77c67b120f..1e72b249c19 100644 --- a/spec/lib/gitlab/background_migration/add_merge_request_diff_commits_count_spec.rb +++ b/spec/lib/gitlab/background_migration/add_merge_request_diff_commits_count_spec.rb @@ -2,13 +2,15 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::AddMergeRequestDiffCommitsCount, schema: 20180105212544 do +RSpec.describe Gitlab::BackgroundMigration::AddMergeRequestDiffCommitsCount do + let(:namespaces_table) { table(:namespaces) } let(:projects_table) { table(:projects) } let(:merge_requests_table) { table(:merge_requests) } let(:merge_request_diffs_table) { table(:merge_request_diffs) } let(:merge_request_diff_commits_table) { table(:merge_request_diff_commits) } - let(:project) { projects_table.create!(name: 'gitlab', path: 'gitlab-org/gitlab-ce') } + let(:namespace) { namespaces_table.create!(name: 'gitlab-org', path: 'gitlab-org') } + let(:project) { projects_table.create!(name: 'gitlab', path: 'gitlab-org/gitlab-ce', namespace_id: namespace.id) } let(:merge_request) do merge_requests_table.create!(target_project_id: project.id, target_branch: 'master', diff --git a/spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb b/spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb index 5c8dcb38511..7991ad69007 100644 --- a/spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb +++ b/spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::ArchiveLegacyTraces, schema: 20180529152628 do +RSpec.describe Gitlab::BackgroundMigration::ArchiveLegacyTraces do include TraceHelpers let(:namespaces) { table(:namespaces) } diff --git a/spec/lib/gitlab/background_migration/backfill_deployment_clusters_from_deployments_spec.rb b/spec/lib/gitlab/background_migration/backfill_deployment_clusters_from_deployments_spec.rb index fdabc8e8f7c..2be9c03e5bd 100644 --- a/spec/lib/gitlab/background_migration/backfill_deployment_clusters_from_deployments_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_deployment_clusters_from_deployments_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::BackfillDeploymentClustersFromDeployments, :migration, schema: 20200227140242 do +RSpec.describe Gitlab::BackgroundMigration::BackfillDeploymentClustersFromDeployments, :migration, schema: 20200227140242 do subject { described_class.new } describe '#perform' do diff --git a/spec/lib/gitlab/background_migration/backfill_environment_id_deployment_merge_requests_spec.rb b/spec/lib/gitlab/background_migration/backfill_environment_id_deployment_merge_requests_spec.rb index 34ac70071bb..550bdc484c9 100644 --- a/spec/lib/gitlab/background_migration/backfill_environment_id_deployment_merge_requests_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_environment_id_deployment_merge_requests_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::BackfillEnvironmentIdDeploymentMergeRequests, schema: 20200312134637 do +RSpec.describe Gitlab::BackgroundMigration::BackfillEnvironmentIdDeploymentMergeRequests, schema: 20200312134637 do let(:environments) { table(:environments) } let(:merge_requests) { table(:merge_requests) } let(:deployments) { table(:deployments) } diff --git a/spec/lib/gitlab/background_migration/backfill_hashed_project_repositories_spec.rb b/spec/lib/gitlab/background_migration/backfill_hashed_project_repositories_spec.rb index f64c3ccc058..79b344ea6fa 100644 --- a/spec/lib/gitlab/background_migration/backfill_hashed_project_repositories_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_hashed_project_repositories_spec.rb @@ -2,6 +2,6 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::BackfillHashedProjectRepositories, schema: 20181130102132 do +RSpec.describe Gitlab::BackgroundMigration::BackfillHashedProjectRepositories do it_behaves_like 'backfill migration for project repositories', :hashed end diff --git a/spec/lib/gitlab/background_migration/backfill_legacy_project_repositories_spec.rb b/spec/lib/gitlab/background_migration/backfill_legacy_project_repositories_spec.rb index 806d044ab40..c4013d002b2 100644 --- a/spec/lib/gitlab/background_migration/backfill_legacy_project_repositories_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_legacy_project_repositories_spec.rb @@ -2,6 +2,6 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::BackfillLegacyProjectRepositories, schema: 20181212171634 do +RSpec.describe Gitlab::BackgroundMigration::BackfillLegacyProjectRepositories do it_behaves_like 'backfill migration for project repositories', :legacy end diff --git a/spec/lib/gitlab/background_migration/backfill_namespace_settings_spec.rb b/spec/lib/gitlab/background_migration/backfill_namespace_settings_spec.rb new file mode 100644 index 00000000000..43e76a2952e --- /dev/null +++ b/spec/lib/gitlab/background_migration/backfill_namespace_settings_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::BackfillNamespaceSettings, schema: 20200703125016 do + let(:namespaces) { table(:namespaces) } + let(:namespace_settings) { table(:namespace_settings) } + let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') } + + subject { described_class.new } + + describe '#perform' do + it 'creates settings for all projects in range' do + namespaces.create!(id: 5, name: 'test1', path: 'test1') + namespaces.create!(id: 7, name: 'test2', path: 'test2') + namespaces.create!(id: 8, name: 'test3', path: 'test3') + + subject.perform(5, 7) + + expect(namespace_settings.all.pluck(:namespace_id)).to contain_exactly(5, 7) + end + end +end diff --git a/spec/lib/gitlab/background_migration/backfill_project_fullpath_in_repo_config_spec.rb b/spec/lib/gitlab/background_migration/backfill_project_fullpath_in_repo_config_spec.rb index 44f5c3380a1..1b2e1ed0c1a 100644 --- a/spec/lib/gitlab/background_migration/backfill_project_fullpath_in_repo_config_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_project_fullpath_in_repo_config_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::BackfillProjectFullpathInRepoConfig, schema: 20181010133639 do +RSpec.describe Gitlab::BackgroundMigration::BackfillProjectFullpathInRepoConfig do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:group) { namespaces.create!(name: 'foo', path: 'foo') } diff --git a/spec/lib/gitlab/background_migration/backfill_project_repositories_spec.rb b/spec/lib/gitlab/background_migration/backfill_project_repositories_spec.rb index cfaef1578a9..8a8edc1af29 100644 --- a/spec/lib/gitlab/background_migration/backfill_project_repositories_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_project_repositories_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' # rubocop:disable RSpec/FactoriesInMigrationSpecs -describe Gitlab::BackgroundMigration::BackfillProjectRepositories do +RSpec.describe Gitlab::BackgroundMigration::BackfillProjectRepositories do let(:group) { create(:group, name: 'foo', path: 'foo') } describe described_class::ShardFinder do diff --git a/spec/lib/gitlab/background_migration/backfill_project_settings_spec.rb b/spec/lib/gitlab/background_migration/backfill_project_settings_spec.rb index a2b4e003d82..4e7a3a33f7e 100644 --- a/spec/lib/gitlab/background_migration/backfill_project_settings_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_project_settings_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::BackfillProjectSettings, schema: 20200114113341 do +RSpec.describe Gitlab::BackgroundMigration::BackfillProjectSettings, schema: 20200114113341 do let(:projects) { table(:projects) } let(:project_settings) { table(:project_settings) } let(:namespace) { table(:namespaces).create(name: 'user', path: 'user') } diff --git a/spec/lib/gitlab/background_migration/backfill_push_rules_id_in_projects_spec.rb b/spec/lib/gitlab/background_migration/backfill_push_rules_id_in_projects_spec.rb index f150ed4bd2e..39b49d008d4 100644 --- a/spec/lib/gitlab/background_migration/backfill_push_rules_id_in_projects_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_push_rules_id_in_projects_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::BackfillPushRulesIdInProjects, :migration, schema: 2020_03_25_162730 do +RSpec.describe Gitlab::BackgroundMigration::BackfillPushRulesIdInProjects, :migration, schema: 2020_03_25_162730 do let(:push_rules) { table(:push_rules) } let(:projects) { table(:projects) } let(:project_settings) { table(:project_settings) } diff --git a/spec/lib/gitlab/background_migration/backfill_snippet_repositories_spec.rb b/spec/lib/gitlab/background_migration/backfill_snippet_repositories_spec.rb index 27ae60eb278..ec2fd3cc4e0 100644 --- a/spec/lib/gitlab/background_migration/backfill_snippet_repositories_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_snippet_repositories_spec.rb @@ -2,14 +2,13 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::BackfillSnippetRepositories, :migration, schema: 2020_04_20_094444 do +RSpec.describe Gitlab::BackgroundMigration::BackfillSnippetRepositories, :migration, schema: 2020_04_20_094444 do let(:gitlab_shell) { Gitlab::Shell.new } let(:users) { table(:users) } let(:snippets) { table(:snippets) } let(:snippet_repositories) { table(:snippet_repositories) } let(:user_state) { 'active' } - let(:ghost) { false } let(:user_type) { nil } let(:user_name) { 'Test' } @@ -20,13 +19,20 @@ describe Gitlab::BackgroundMigration::BackfillSnippetRepositories, :migration, s username: 'test', name: user_name, state: user_state, - ghost: ghost, last_activity_on: 1.minute.ago, user_type: user_type, confirmed_at: 1.day.ago) end - let(:migration_bot) { User.migration_bot } + let!(:migration_bot) do + users.create(id: 100, + email: "noreply+gitlab-migration-bot%s@#{Settings.gitlab.host}", + user_type: HasUserType::USER_TYPES[:migration_bot], + name: 'GitLab Migration Bot', + projects_limit: 10, + username: 'bot') + end + let!(:snippet_with_repo) { snippets.create(id: 1, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) } let!(:snippet_with_empty_repo) { snippets.create(id: 2, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) } let!(:snippet_without_repo) { snippets.create(id: 3, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) } @@ -113,8 +119,7 @@ describe Gitlab::BackgroundMigration::BackfillSnippetRepositories, :migration, s end context 'when user is a ghost' do - let(:ghost) { true } - let(:user_type) { 'ghost' } + let(:user_type) { HasUserType::USER_TYPES[:ghost] } it_behaves_like 'migration_bot user commits files' end @@ -255,7 +260,6 @@ describe Gitlab::BackgroundMigration::BackfillSnippetRepositories, :migration, s username: 'test2', name: 'Test2', state: user_state, - ghost: ghost, last_activity_on: 1.minute.ago, user_type: user_type, confirmed_at: 1.day.ago) diff --git a/spec/lib/gitlab/background_migration/cleanup_concurrent_schema_change_spec.rb b/spec/lib/gitlab/background_migration/cleanup_concurrent_schema_change_spec.rb new file mode 100644 index 00000000000..2931b5e6dd3 --- /dev/null +++ b/spec/lib/gitlab/background_migration/cleanup_concurrent_schema_change_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::CleanupConcurrentSchemaChange do + describe '#perform' do + it 'new column does not exist' do + expect(subject).to receive(:column_exists?).with(:issues, :closed_at_timestamp).and_return(false) + expect(subject).not_to receive(:column_exists?).with(:issues, :closed_at) + expect(subject).not_to receive(:define_model_for) + + expect(subject.perform(:issues, :closed_at, :closed_at_timestamp)).to be_nil + end + + it 'old column does not exist' do + expect(subject).to receive(:column_exists?).with(:issues, :closed_at_timestamp).and_return(true) + expect(subject).to receive(:column_exists?).with(:issues, :closed_at).and_return(false) + expect(subject).not_to receive(:define_model_for) + + expect(subject.perform(:issues, :closed_at, :closed_at_timestamp)).to be_nil + end + + it 'has both old and new columns' do + expect(subject).to receive(:column_exists?).twice.and_return(true) + + expect { subject.perform('issues', :closed_at, :created_at) }.to raise_error(NotImplementedError) + end + end +end diff --git a/spec/lib/gitlab/background_migration/digest_column_spec.rb b/spec/lib/gitlab/background_migration/digest_column_spec.rb deleted file mode 100644 index 0c76ebe9c66..00000000000 --- a/spec/lib/gitlab/background_migration/digest_column_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe Gitlab::BackgroundMigration::DigestColumn, schema: 20180913142237 do - let(:personal_access_tokens) { table(:personal_access_tokens) } - let(:users) { table(:users) } - - subject { described_class.new } - - describe '#perform' do - context 'token is not yet hashed' do - before do - users.create(id: 1, email: 'user@example.com', projects_limit: 10) - personal_access_tokens.create!(id: 1, user_id: 1, name: 'pat-01', token: 'token-01') - end - - it 'saves token digest' do - expect { subject.perform(PersonalAccessToken, :token, :token_digest, 1, 2) }.to( - change { PersonalAccessToken.find(1).token_digest }.from(nil).to(Gitlab::CryptoHelper.sha256('token-01'))) - end - - it 'erases token' do - expect { subject.perform(PersonalAccessToken, :token, :token_digest, 1, 2) }.to( - change { PersonalAccessToken.find(1).read_attribute(:token) }.from('token-01').to(nil)) - end - end - - context 'token is already hashed' do - before do - users.create(id: 1, email: 'user@example.com', projects_limit: 10) - personal_access_tokens.create!(id: 1, user_id: 1, name: 'pat-01', token_digest: 'token-digest-01') - end - - it 'does not change existing token digest' do - expect { subject.perform(PersonalAccessToken, :token, :token_digest, 1, 2) }.not_to( - change { PersonalAccessToken.find(1).token_digest }) - end - - it 'leaves token empty' do - expect { subject.perform(PersonalAccessToken, :token, :token_digest, 1, 2) }.not_to( - change { PersonalAccessToken.find(1).read_attribute(:token) }.from(nil)) - end - end - end -end diff --git a/spec/lib/gitlab/background_migration/encrypt_columns_spec.rb b/spec/lib/gitlab/background_migration/encrypt_columns_spec.rb deleted file mode 100644 index 6d3ccde7df2..00000000000 --- a/spec/lib/gitlab/background_migration/encrypt_columns_spec.rb +++ /dev/null @@ -1,96 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe Gitlab::BackgroundMigration::EncryptColumns, schema: 20180910115836 do - let(:model) { Gitlab::BackgroundMigration::Models::EncryptColumns::WebHook } - let(:web_hooks) { table(:web_hooks) } - - let(:plaintext_attrs) do - { - 'encrypted_token' => nil, - 'encrypted_url' => nil, - 'token' => 'secret', - 'url' => 'http://example.com?access_token=secret' - } - end - - let(:encrypted_attrs) do - { - 'encrypted_token' => be_present, - 'encrypted_url' => be_present, - 'token' => nil, - 'url' => nil - } - end - - describe '#perform' do - it 'encrypts columns for the specified range' do - hooks = web_hooks.create([plaintext_attrs] * 5).sort_by(&:id) - - # Encrypt all but the first and last rows - subject.perform(model, [:token, :url], hooks[1].id, hooks[3].id) - - hooks = web_hooks.where(id: hooks.map(&:id)).order(:id) - - aggregate_failures do - expect(hooks[0]).to have_attributes(plaintext_attrs) - expect(hooks[1]).to have_attributes(encrypted_attrs) - expect(hooks[2]).to have_attributes(encrypted_attrs) - expect(hooks[3]).to have_attributes(encrypted_attrs) - expect(hooks[4]).to have_attributes(plaintext_attrs) - end - end - - it 'acquires an exclusive lock for the update' do - relation = double('relation', each: nil) - - expect(model).to receive(:where) { relation } - expect(relation).to receive(:lock) { relation } - - subject.perform(model, [:token, :url], 1, 1) - end - - it 'skips already-encrypted columns' do - values = { - 'encrypted_token' => 'known encrypted token', - 'encrypted_url' => 'known encrypted url', - 'token' => 'token', - 'url' => 'url' - } - - hook = web_hooks.create(values) - - subject.perform(model, [:token, :url], hook.id, hook.id) - - hook.reload - - expect(hook).to have_attributes(values) - end - - it 'reloads the model column information' do - expect(model).to receive(:reset_column_information).and_call_original - expect(model).to receive(:define_attribute_methods).and_call_original - - subject.perform(model, [:token, :url], 1, 1) - end - - it 'fails if a source column is not present' do - columns = model.columns.reject { |c| c.name == 'url' } - allow(model).to receive(:columns) { columns } - - expect do - subject.perform(model, [:token, :url], 1, 1) - end.to raise_error(/source column: url is missing/) - end - - it 'fails if a destination column is not present' do - columns = model.columns.reject { |c| c.name == 'encrypted_url' } - allow(model).to receive(:columns) { columns } - - expect do - subject.perform(model, [:token, :url], 1, 1) - end.to raise_error(/destination column: encrypted_url is missing/) - end - end -end diff --git a/spec/lib/gitlab/background_migration/encrypt_runners_tokens_spec.rb b/spec/lib/gitlab/background_migration/encrypt_runners_tokens_spec.rb deleted file mode 100644 index 89262788d9b..00000000000 --- a/spec/lib/gitlab/background_migration/encrypt_runners_tokens_spec.rb +++ /dev/null @@ -1,79 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe Gitlab::BackgroundMigration::EncryptRunnersTokens, schema: 20181121111200 do - let(:settings) { table(:application_settings) } - let(:namespaces) { table(:namespaces) } - let(:projects) { table(:projects) } - let(:runners) { table(:ci_runners) } - - context 'when migrating application settings' do - before do - settings.create!(id: 1, runners_registration_token: 'plain-text-token1') - end - - it 'migrates runners registration tokens' do - migrate!(:settings, 1, 1) - - encrypted_token = settings.first.runners_registration_token_encrypted - decrypted_token = ::Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token) - - expect(decrypted_token).to eq 'plain-text-token1' - expect(settings.first.runners_registration_token).to eq 'plain-text-token1' - end - end - - context 'when migrating namespaces' do - before do - namespaces.create!(id: 11, name: 'gitlab', path: 'gitlab-org', runners_token: 'my-token1') - namespaces.create!(id: 12, name: 'gitlab', path: 'gitlab-org', runners_token: 'my-token2') - namespaces.create!(id: 22, name: 'gitlab', path: 'gitlab-org', runners_token: 'my-token3') - end - - it 'migrates runners registration tokens' do - migrate!(:namespace, 11, 22) - - expect(namespaces.all.reload).to all( - have_attributes(runners_token: be_a(String), runners_token_encrypted: be_a(String)) - ) - end - end - - context 'when migrating projects' do - before do - namespaces.create!(id: 11, name: 'gitlab', path: 'gitlab-org') - projects.create!(id: 111, namespace_id: 11, name: 'gitlab', path: 'gitlab-ce', runners_token: 'my-token1') - projects.create!(id: 114, namespace_id: 11, name: 'gitlab', path: 'gitlab-ce', runners_token: 'my-token2') - projects.create!(id: 116, namespace_id: 11, name: 'gitlab', path: 'gitlab-ce', runners_token: 'my-token3') - end - - it 'migrates runners registration tokens' do - migrate!(:project, 111, 116) - - expect(projects.all.reload).to all( - have_attributes(runners_token: be_a(String), runners_token_encrypted: be_a(String)) - ) - end - end - - context 'when migrating runners' do - before do - runners.create!(id: 201, runner_type: 1, token: 'plain-text-token1') - runners.create!(id: 202, runner_type: 1, token: 'plain-text-token2') - runners.create!(id: 203, runner_type: 1, token: 'plain-text-token3') - end - - it 'migrates runners communication tokens' do - migrate!(:runner, 201, 203) - - expect(runners.all.reload).to all( - have_attributes(token: be_a(String), token_encrypted: be_a(String)) - ) - end - end - - def migrate!(model, from, to) - subject.perform(model, from, to) - end -end diff --git a/spec/lib/gitlab/background_migration/fix_cross_project_label_links_spec.rb b/spec/lib/gitlab/background_migration/fix_cross_project_label_links_spec.rb index cc4ce023f04..8e3ace083fc 100644 --- a/spec/lib/gitlab/background_migration/fix_cross_project_label_links_spec.rb +++ b/spec/lib/gitlab/background_migration/fix_cross_project_label_links_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::FixCrossProjectLabelLinks, schema: 20180702120647 do +RSpec.describe Gitlab::BackgroundMigration::FixCrossProjectLabelLinks do let(:namespaces_table) { table(:namespaces) } let(:projects_table) { table(:projects) } let(:issues_table) { table(:issues) } diff --git a/spec/lib/gitlab/background_migration/fix_projects_without_project_feature_spec.rb b/spec/lib/gitlab/background_migration/fix_projects_without_project_feature_spec.rb index 056ddd7adf9..e2175c41513 100644 --- a/spec/lib/gitlab/background_migration/fix_projects_without_project_feature_spec.rb +++ b/spec/lib/gitlab/background_migration/fix_projects_without_project_feature_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::FixProjectsWithoutProjectFeature, schema: 2020_01_27_111840 do +RSpec.describe Gitlab::BackgroundMigration::FixProjectsWithoutProjectFeature, schema: 2020_01_27_111840 do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:project_features) { table(:project_features) } diff --git a/spec/lib/gitlab/background_migration/fix_projects_without_prometheus_service_spec.rb b/spec/lib/gitlab/background_migration/fix_projects_without_prometheus_service_spec.rb index 3c3e37df200..fe2b206ea74 100644 --- a/spec/lib/gitlab/background_migration/fix_projects_without_prometheus_service_spec.rb +++ b/spec/lib/gitlab/background_migration/fix_projects_without_prometheus_service_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::FixProjectsWithoutPrometheusService, :migration, schema: 2020_02_20_115023 do +RSpec.describe Gitlab::BackgroundMigration::FixProjectsWithoutPrometheusService, :migration, schema: 2020_02_20_115023 do def service_params_for(project_id, params = {}) { project_id: project_id, diff --git a/spec/lib/gitlab/background_migration/fix_promoted_epics_discussion_ids_spec.rb b/spec/lib/gitlab/background_migration/fix_promoted_epics_discussion_ids_spec.rb index 141a0af6c29..452fc962c7b 100644 --- a/spec/lib/gitlab/background_migration/fix_promoted_epics_discussion_ids_spec.rb +++ b/spec/lib/gitlab/background_migration/fix_promoted_epics_discussion_ids_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::FixPromotedEpicsDiscussionIds, schema: 20190715193142 do +RSpec.describe Gitlab::BackgroundMigration::FixPromotedEpicsDiscussionIds, schema: 20190715193142 do let(:namespaces) { table(:namespaces) } let(:users) { table(:users) } let(:epics) { table(:epics) } diff --git a/spec/lib/gitlab/background_migration/fix_user_namespace_names_spec.rb b/spec/lib/gitlab/background_migration/fix_user_namespace_names_spec.rb index 52760cdd115..7768411828c 100644 --- a/spec/lib/gitlab/background_migration/fix_user_namespace_names_spec.rb +++ b/spec/lib/gitlab/background_migration/fix_user_namespace_names_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::FixUserNamespaceNames, schema: 20190620112608 do +RSpec.describe Gitlab::BackgroundMigration::FixUserNamespaceNames, schema: 20190620112608 do let(:namespaces) { table(:namespaces) } let(:users) { table(:users) } let(:user) { users.create(name: "The user's full name", projects_limit: 10, username: 'not-null', email: '1') } diff --git a/spec/lib/gitlab/background_migration/fix_user_project_route_names_spec.rb b/spec/lib/gitlab/background_migration/fix_user_project_route_names_spec.rb index 0fb7eea2bd7..4c04043ebd0 100644 --- a/spec/lib/gitlab/background_migration/fix_user_project_route_names_spec.rb +++ b/spec/lib/gitlab/background_migration/fix_user_project_route_names_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::FixUserProjectRouteNames, schema: 20190620112608 do +RSpec.describe Gitlab::BackgroundMigration::FixUserProjectRouteNames, schema: 20190620112608 do let(:namespaces) { table(:namespaces) } let(:users) { table(:users) } let(:routes) { table(:routes) } diff --git a/spec/lib/gitlab/background_migration/legacy_upload_mover_spec.rb b/spec/lib/gitlab/background_migration/legacy_upload_mover_spec.rb index 850ef48d44a..bf793e7c537 100644 --- a/spec/lib/gitlab/background_migration/legacy_upload_mover_spec.rb +++ b/spec/lib/gitlab/background_migration/legacy_upload_mover_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' # rubocop: disable RSpec/FactoriesInMigrationSpecs -describe Gitlab::BackgroundMigration::LegacyUploadMover do +RSpec.describe Gitlab::BackgroundMigration::LegacyUploadMover do let(:test_dir) { FileUploader.options['storage_path'] } let(:filename) { 'image.png' } diff --git a/spec/lib/gitlab/background_migration/legacy_uploads_migrator_spec.rb b/spec/lib/gitlab/background_migration/legacy_uploads_migrator_spec.rb index 85187d039c1..66a1787b2cb 100644 --- a/spec/lib/gitlab/background_migration/legacy_uploads_migrator_spec.rb +++ b/spec/lib/gitlab/background_migration/legacy_uploads_migrator_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' # rubocop: disable RSpec/FactoriesInMigrationSpecs -describe Gitlab::BackgroundMigration::LegacyUploadsMigrator do +RSpec.describe Gitlab::BackgroundMigration::LegacyUploadsMigrator do let(:test_dir) { FileUploader.options['storage_path'] } let!(:hashed_project) { create(:project) } diff --git a/spec/lib/gitlab/background_migration/link_lfs_objects_projects_spec.rb b/spec/lib/gitlab/background_migration/link_lfs_objects_projects_spec.rb index 5700cac2e0f..dda4f5a3a36 100644 --- a/spec/lib/gitlab/background_migration/link_lfs_objects_projects_spec.rb +++ b/spec/lib/gitlab/background_migration/link_lfs_objects_projects_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::LinkLfsObjectsProjects, :migration, schema: 2020_03_10_075115 do +RSpec.describe Gitlab::BackgroundMigration::LinkLfsObjectsProjects, :migration, schema: 2020_03_10_075115 do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:fork_networks) { table(:fork_networks) } diff --git a/spec/lib/gitlab/background_migration/mailers/unconfirm_mailer_spec.rb b/spec/lib/gitlab/background_migration/mailers/unconfirm_mailer_spec.rb new file mode 100644 index 00000000000..f430009989b --- /dev/null +++ b/spec/lib/gitlab/background_migration/mailers/unconfirm_mailer_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::Mailers::UnconfirmMailer do + let(:user) { User.new(id: 1111) } + let(:subject) { described_class.unconfirm_notification_email(user) } + + it 'contains abuse report url' do + expect(subject.body.encoded).to include(Rails.application.routes.url_helpers.new_abuse_report_url(user_id: user.id)) + end +end diff --git a/spec/lib/gitlab/background_migration/merge_request_assignees_migration_progress_check_spec.rb b/spec/lib/gitlab/background_migration/merge_request_assignees_migration_progress_check_spec.rb index 9dd97b58014..a3840e3a22e 100644 --- a/spec/lib/gitlab/background_migration/merge_request_assignees_migration_progress_check_spec.rb +++ b/spec/lib/gitlab/background_migration/merge_request_assignees_migration_progress_check_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::MergeRequestAssigneesMigrationProgressCheck do +RSpec.describe Gitlab::BackgroundMigration::MergeRequestAssigneesMigrationProgressCheck do context 'rescheduling' do context 'when there are ongoing and no dead jobs' do it 'reschedules check' do diff --git a/spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb b/spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb index adf358f5320..65d45ec694f 100644 --- a/spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::MigrateBuildStage, schema: 20180212101928 do +RSpec.describe Gitlab::BackgroundMigration::MigrateBuildStage do + let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:pipelines) { table(:ci_pipelines) } let(:stages) { table(:ci_stages) } @@ -22,7 +23,8 @@ describe Gitlab::BackgroundMigration::MigrateBuildStage, schema: 20180212101928 end before do - projects.create!(id: 123, name: 'gitlab', path: 'gitlab-ce') + namespace = namespaces.create!(name: 'gitlab-org', path: 'gitlab-org') + projects.create!(id: 123, name: 'gitlab', path: 'gitlab-ce', namespace_id: namespace.id) pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a') jobs.create!(id: 1, commit_id: 1, project_id: 123, @@ -53,7 +55,7 @@ describe Gitlab::BackgroundMigration::MigrateBuildStage, schema: 20180212101928 statuses[:pending]] end - it 'recovers from unique constraint violation only twice', :quarantine do + it 'recovers from unique constraint violation only twice', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/28128' do allow(described_class::Migratable::Stage) .to receive(:find_by).and_return(nil) diff --git a/spec/lib/gitlab/background_migration/migrate_fingerprint_sha256_within_keys_spec.rb b/spec/lib/gitlab/background_migration/migrate_fingerprint_sha256_within_keys_spec.rb index 79a8cd926a7..c58b2d609e9 100644 --- a/spec/lib/gitlab/background_migration/migrate_fingerprint_sha256_within_keys_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_fingerprint_sha256_within_keys_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::MigrateFingerprintSha256WithinKeys, schema: 20200106071113 do +RSpec.describe Gitlab::BackgroundMigration::MigrateFingerprintSha256WithinKeys, schema: 20200106071113 do subject(:fingerprint_migrator) { described_class.new } let(:key_table) { table(:keys) } diff --git a/spec/lib/gitlab/background_migration/migrate_issue_trackers_sensitive_data_spec.rb b/spec/lib/gitlab/background_migration/migrate_issue_trackers_sensitive_data_spec.rb index 4411dca3fd9..d53f79c61c1 100644 --- a/spec/lib/gitlab/background_migration/migrate_issue_trackers_sensitive_data_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_issue_trackers_sensitive_data_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'spec_helper' -describe Gitlab::BackgroundMigration::MigrateIssueTrackersSensitiveData, schema: 20200130145430 do +RSpec.describe Gitlab::BackgroundMigration::MigrateIssueTrackersSensitiveData, schema: 20200130145430 do let(:services) { table(:services) } before do diff --git a/spec/lib/gitlab/background_migration/migrate_legacy_artifacts_spec.rb b/spec/lib/gitlab/background_migration/migrate_legacy_artifacts_spec.rb index 5f2a27acd9b..08f2b2a043e 100644 --- a/spec/lib/gitlab/background_migration/migrate_legacy_artifacts_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_legacy_artifacts_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::MigrateLegacyArtifacts, schema: 20180816161409 do +RSpec.describe Gitlab::BackgroundMigration::MigrateLegacyArtifacts do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:pipelines) { table(:ci_pipelines) } diff --git a/spec/lib/gitlab/background_migration/migrate_null_private_profile_to_false_spec.rb b/spec/lib/gitlab/background_migration/migrate_null_private_profile_to_false_spec.rb index ff88d2a5d00..6ff1157cb86 100644 --- a/spec/lib/gitlab/background_migration/migrate_null_private_profile_to_false_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_null_private_profile_to_false_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::MigrateNullPrivateProfileToFalse, schema: 20190620105427 do +RSpec.describe Gitlab::BackgroundMigration::MigrateNullPrivateProfileToFalse, schema: 20190620105427 do let(:users) { table(:users) } it 'correctly migrates nil private_profile to false' do diff --git a/spec/lib/gitlab/background_migration/migrate_pages_metadata_spec.rb b/spec/lib/gitlab/background_migration/migrate_pages_metadata_spec.rb index 10a1d4ee1b9..906a6a747c9 100644 --- a/spec/lib/gitlab/background_migration/migrate_pages_metadata_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_pages_metadata_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::MigratePagesMetadata, schema: 20190919040324 do +RSpec.describe Gitlab::BackgroundMigration::MigratePagesMetadata, schema: 20190919040324 do let(:projects) { table(:projects) } subject(:migrate_pages_metadata) { described_class.new } diff --git a/spec/lib/gitlab/background_migration/migrate_stage_index_spec.rb b/spec/lib/gitlab/background_migration/migrate_stage_index_spec.rb index 437be125cf0..81874ff7982 100644 --- a/spec/lib/gitlab/background_migration/migrate_stage_index_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_stage_index_spec.rb @@ -2,35 +2,33 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::MigrateStageIndex, schema: 20180420080616 do +RSpec.describe Gitlab::BackgroundMigration::MigrateStageIndex do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:pipelines) { table(:ci_pipelines) } let(:stages) { table(:ci_stages) } let(:jobs) { table(:ci_builds) } + let(:namespace) { namespaces.create(name: 'gitlab-org', path: 'gitlab-org') } + let(:project) { projects.create!(namespace_id: namespace.id, name: 'gitlab', path: 'gitlab') } + let(:pipeline) { pipelines.create!(project_id: project.id, ref: 'master', sha: 'adf43c3a') } + let(:stage1) { stages.create(project_id: project.id, pipeline_id: pipeline.id, name: 'build') } + let(:stage2) { stages.create(project_id: project.id, pipeline_id: pipeline.id, name: 'test') } before do - namespaces.create(id: 10, name: 'gitlab-org', path: 'gitlab-org') - projects.create!(id: 11, namespace_id: 10, name: 'gitlab', path: 'gitlab') - pipelines.create!(id: 12, project_id: 11, ref: 'master', sha: 'adf43c3a') - - stages.create(id: 100, project_id: 11, pipeline_id: 12, name: 'build') - stages.create(id: 101, project_id: 11, pipeline_id: 12, name: 'test') - - jobs.create!(id: 121, commit_id: 12, project_id: 11, - stage_idx: 2, stage_id: 100) - jobs.create!(id: 122, commit_id: 12, project_id: 11, - stage_idx: 2, stage_id: 100) - jobs.create!(id: 123, commit_id: 12, project_id: 11, - stage_idx: 10, stage_id: 100) - jobs.create!(id: 124, commit_id: 12, project_id: 11, - stage_idx: 3, stage_id: 101) + jobs.create!(commit_id: pipeline.id, project_id: project.id, + stage_idx: 2, stage_id: stage1.id) + jobs.create!(commit_id: pipeline.id, project_id: project.id, + stage_idx: 2, stage_id: stage1.id) + jobs.create!(commit_id: pipeline.id, project_id: project.id, + stage_idx: 10, stage_id: stage1.id) + jobs.create!(commit_id: pipeline.id, project_id: project.id, + stage_idx: 3, stage_id: stage2.id) end it 'correctly migrates stages indices' do expect(stages.all.pluck(:position)).to all(be_nil) - described_class.new.perform(100, 101) + described_class.new.perform(stage1.id, stage2.id) expect(stages.all.order(:id).pluck(:position)).to eq [2, 3] end diff --git a/spec/lib/gitlab/background_migration/migrate_users_bio_to_user_details_spec.rb b/spec/lib/gitlab/background_migration/migrate_users_bio_to_user_details_spec.rb index 8603eb73bd5..db3cbe7ccdc 100644 --- a/spec/lib/gitlab/background_migration/migrate_users_bio_to_user_details_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_users_bio_to_user_details_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::MigrateUsersBioToUserDetails, :migration, schema: 20200323074147 do +RSpec.describe Gitlab::BackgroundMigration::MigrateUsersBioToUserDetails, :migration, schema: 20200323074147 do let(:users) { table(:users) } let(:user_details) do diff --git a/spec/lib/gitlab/background_migration/populate_canonical_emails_spec.rb b/spec/lib/gitlab/background_migration/populate_canonical_emails_spec.rb index 37ddb8b569d..ee0024e8526 100644 --- a/spec/lib/gitlab/background_migration/populate_canonical_emails_spec.rb +++ b/spec/lib/gitlab/background_migration/populate_canonical_emails_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::PopulateCanonicalEmails, :migration, schema: 20200312053852 do +RSpec.describe Gitlab::BackgroundMigration::PopulateCanonicalEmails, :migration, schema: 20200312053852 do let(:migration) { described_class.new } let_it_be(:users_table) { table(:users) } diff --git a/spec/lib/gitlab/background_migration/populate_cluster_kubernetes_namespace_table_spec.rb b/spec/lib/gitlab/background_migration/populate_cluster_kubernetes_namespace_table_spec.rb index d445858b8e8..73faca54b52 100644 --- a/spec/lib/gitlab/background_migration/populate_cluster_kubernetes_namespace_table_spec.rb +++ b/spec/lib/gitlab/background_migration/populate_cluster_kubernetes_namespace_table_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::PopulateClusterKubernetesNamespaceTable, schema: 20181022173835 do +RSpec.describe Gitlab::BackgroundMigration::PopulateClusterKubernetesNamespaceTable do include MigrationHelpers::ClusterHelpers let(:migration) { described_class.new } diff --git a/spec/lib/gitlab/background_migration/populate_merge_request_assignees_table_spec.rb b/spec/lib/gitlab/background_migration/populate_merge_request_assignees_table_spec.rb index e65b3549de0..1e5773ee16b 100644 --- a/spec/lib/gitlab/background_migration/populate_merge_request_assignees_table_spec.rb +++ b/spec/lib/gitlab/background_migration/populate_merge_request_assignees_table_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::PopulateMergeRequestAssigneesTable, schema: 20190315191339 do +RSpec.describe Gitlab::BackgroundMigration::PopulateMergeRequestAssigneesTable, schema: 20190315191339 do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:users) { table(:users) } diff --git a/spec/lib/gitlab/background_migration/populate_project_snippet_statistics_spec.rb b/spec/lib/gitlab/background_migration/populate_project_snippet_statistics_spec.rb new file mode 100644 index 00000000000..897f5e81372 --- /dev/null +++ b/spec/lib/gitlab/background_migration/populate_project_snippet_statistics_spec.rb @@ -0,0 +1,224 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::PopulateProjectSnippetStatistics do + let(:file_name) { 'file_name.rb' } + let(:content) { 'content' } + let(:snippets) { table(:snippets) } + let(:snippet_repositories) { table(:snippet_repositories) } + let(:users) { table(:users) } + let(:namespaces) { table(:namespaces) } + let(:snippet_statistics) { table(:snippet_statistics) } + let(:project_statistics) { table(:project_statistics) } + let(:projects) { table(:projects) } + let(:namespace_statistics) { table(:namespace_root_storage_statistics) } + let(:routes) { table(:routes) } + let(:repo_size) { 123456 } + let(:expected_repo_size) { repo_size.megabytes } + + let(:user) { users.create!(id: 1, email: 'test@example.com', projects_limit: 100, username: 'test') } + let(:group) { namespaces.create!(id: 10, type: 'Group', name: 'group1', path: 'group1') } + let(:user_namespace) { namespaces.create!(id: 20, name: 'user', path: 'user', owner_id: user.id) } + + let(:project1) { create_project(1, 'test', group) } + let(:project2) { create_project(2, 'test1', user_namespace) } + let(:project3) { create_project(3, 'test2', group) } + + let!(:project_stats1) { create_project_statistics(project1) } + let!(:project_stats2) { create_project_statistics(project2) } + let!(:project_stats3) { create_project_statistics(project3) } + + let(:ids) { snippets.pluck(:id) } + let(:migration) { described_class.new } + + subject do + migration.perform(ids) + + project_stats1.reload if project_stats1.persisted? + project_stats2.reload if project_stats2.persisted? + project_stats3.reload if project_stats3.persisted? + end + + before do + allow_any_instance_of(Repository).to receive(:size).and_return(repo_size) + end + + after do + snippets.all.each { |s| raw_repository(s).remove } + end + + context 'with existing user and group snippets' do + let!(:snippet1) { create_snippet(1, project1) } + let!(:snippet2) { create_snippet(2, project1) } + let!(:snippet3) { create_snippet(3, project2) } + let!(:snippet4) { create_snippet(4, project2) } + let!(:snippet5) { create_snippet(5, project3) } + + before do + create_snippet_statistics(2, 0) + create_snippet_statistics(4, 123) + end + + it 'creates/updates all snippet_statistics' do + expect(snippet_statistics.count).to eq 2 + + subject + + expect(snippet_statistics.count).to eq 5 + + snippet_statistics.all.each do |stat| + expect(stat.repository_size).to eq expected_repo_size + end + end + + it 'updates associated snippet project statistics' do + expect(project_stats1.snippets_size).to be_nil + expect(project_stats2.snippets_size).to be_nil + + subject + + snippets_size = snippet_statistics.where(snippet_id: [snippet1.id, snippet2.id]).sum(:repository_size) + expect(project_stats1.snippets_size).to eq snippets_size + + snippets_size = snippet_statistics.where(snippet_id: [snippet3.id, snippet4.id]).sum(:repository_size) + expect(project_stats2.snippets_size).to eq snippets_size + + snippets_size = snippet_statistics.where(snippet_id: snippet5.id).sum(:repository_size) + expect(project_stats3.snippets_size).to eq snippets_size + end + + it 'forces the project statistics refresh' do + expect(migration).to receive(:update_project_statistics).exactly(3).times + + subject + end + + it 'creates/updates the associated namespace statistics' do + expect(migration).to receive(:update_namespace_statistics).twice.and_call_original + + subject + + expect(namespace_statistics.find_by(namespace_id: group.id).snippets_size).to eq project_stats1.snippets_size + project_stats3.snippets_size + expect(namespace_statistics.find_by(namespace_id: user_namespace.id).snippets_size).to eq project_stats2.snippets_size + end + + context 'when the project statistics does not exists' do + it 'does not raise any error' do + project_stats3.delete + + subject + + expect(namespace_statistics.find_by(namespace_id: group.id).snippets_size).to eq project_stats1.snippets_size + expect(namespace_statistics.find_by(namespace_id: user_namespace.id).snippets_size).to eq project_stats2.snippets_size + end + end + + context 'when an error is raised when updating a project statistics' do + it 'logs the error and continue execution' do + expect(migration).to receive(:update_project_statistics).with(Project.find(project1.id)).and_raise('Error') + expect(migration).to receive(:update_project_statistics).with(Project.find(project2.id)).and_call_original + expect(migration).to receive(:update_project_statistics).with(Project.find(project3.id)).and_call_original + + expect_next_instance_of(Gitlab::BackgroundMigration::Logger) do |instance| + expect(instance).to receive(:error).with(message: /Error updating statistics for project #{project1.id}/).once + end + + subject + + expect(project_stats2.snippets_size).not_to be_nil + expect(project_stats3.snippets_size).not_to be_nil + end + end + + context 'when an error is raised when updating a namespace statistics' do + it 'logs the error and continue execution' do + expect(migration).to receive(:update_namespace_statistics).with(Group.find(group.id)).and_raise('Error') + expect(migration).to receive(:update_namespace_statistics).with(Namespace.find(user_namespace.id)).and_call_original + + expect_next_instance_of(Gitlab::BackgroundMigration::Logger) do |instance| + expect(instance).to receive(:error).with(message: /Error updating statistics for namespace/).once + end + + subject + + expect(namespace_statistics.find_by(namespace_id: user_namespace.id).snippets_size).to eq project_stats2.snippets_size + end + end + end + + context 'when project snippet is in a subgroup' do + let(:subgroup) { namespaces.create!(id: 30, type: 'Group', name: 'subgroup', path: 'subgroup', parent_id: group.id) } + let(:project1) { create_project(1, 'test', subgroup, "#{group.path}/#{subgroup.path}/test") } + let!(:snippet1) { create_snippet(1, project1) } + + it 'updates the root namespace statistics' do + subject + + expect(snippet_statistics.count).to eq 1 + expect(project_stats1.snippets_size).to eq snippet_statistics.first.repository_size + expect(namespace_statistics.find_by(namespace_id: subgroup.id)).to be_nil + expect(namespace_statistics.find_by(namespace_id: group.id).snippets_size).to eq project_stats1.snippets_size + end + end + + context 'when a snippet repository is empty' do + let!(:snippet1) { create_snippet(1, project1, with_repo: false) } + let!(:snippet2) { create_snippet(2, project1) } + + it 'logs error and continues execution' do + expect_next_instance_of(Gitlab::BackgroundMigration::Logger) do |instance| + expect(instance).to receive(:error).with(message: /Invalid snippet repository/).once + end + + subject + + expect(snippet_statistics.find_by(snippet_id: snippet1.id)).to be_nil + expect(project_stats1.snippets_size).to eq snippet_statistics.find(snippet2.id).repository_size + end + end + + def create_snippet(id, project, with_repo: true) + snippets.create!(id: id, type: 'ProjectSnippet', project_id: project.id, author_id: user.id, file_name: file_name, content: content).tap do |snippet| + if with_repo + allow(snippet).to receive(:disk_path).and_return(disk_path(snippet)) + + TestEnv.copy_repo(snippet, + bare_repo: TestEnv.factory_repo_path_bare, + refs: TestEnv::BRANCH_SHA) + + raw_repository(snippet).create_repository + end + end + end + + def create_project(id, name, namespace, path = nil) + projects.create!(id: id, name: name, path: name.downcase.gsub(/\s/, '_'), namespace_id: namespace.id).tap do |project| + path ||= "#{namespace.path}/#{project.path}" + routes.create!(id: id, source_type: 'Project', source_id: project.id, path: path) + end + end + + def create_snippet_statistics(snippet_id, repository_size = 0) + snippet_statistics.create!(snippet_id: snippet_id, repository_size: repository_size) + end + + def create_project_statistics(project, snippets_size = nil) + project_statistics.create!(id: project.id, project_id: project.id, namespace_id: project.namespace_id, snippets_size: snippets_size) + end + + def raw_repository(snippet) + Gitlab::Git::Repository.new('default', + "#{disk_path(snippet)}.git", + Gitlab::GlRepository::SNIPPET.identifier_for_container(snippet), + "@snippets/#{snippet.id}") + end + + def hashed_repository(snippet) + Storage::Hashed.new(snippet, prefix: '@snippets') + end + + def disk_path(snippet) + hashed_repository(snippet).disk_path + end +end diff --git a/spec/lib/gitlab/background_migration/populate_untracked_uploads_dependencies/untracked_file_spec.rb b/spec/lib/gitlab/background_migration/populate_untracked_uploads_dependencies/untracked_file_spec.rb index 0250ebd7759..6a25e8e2784 100644 --- a/spec/lib/gitlab/background_migration/populate_untracked_uploads_dependencies/untracked_file_spec.rb +++ b/spec/lib/gitlab/background_migration/populate_untracked_uploads_dependencies/untracked_file_spec.rb @@ -2,8 +2,7 @@ require 'spec_helper' -# Rollback DB to 10.5 (later than this was originally written for) because it still needs to work. -describe Gitlab::BackgroundMigration::PopulateUntrackedUploadsDependencies::UntrackedFile, schema: 20180208183958 do +RSpec.describe Gitlab::BackgroundMigration::PopulateUntrackedUploadsDependencies::UntrackedFile do include MigrationsHelpers::TrackUntrackedUploadsHelpers let!(:appearances) { table(:appearances) } diff --git a/spec/lib/gitlab/background_migration/populate_untracked_uploads_spec.rb b/spec/lib/gitlab/background_migration/populate_untracked_uploads_spec.rb index 44cec112bfd..787cc54e79a 100644 --- a/spec/lib/gitlab/background_migration/populate_untracked_uploads_spec.rb +++ b/spec/lib/gitlab/background_migration/populate_untracked_uploads_spec.rb @@ -2,8 +2,7 @@ require 'spec_helper' -# Rollback DB to 10.5 (later than this was originally written for) because it still needs to work. -describe Gitlab::BackgroundMigration::PopulateUntrackedUploads, schema: 20180208183958 do +RSpec.describe Gitlab::BackgroundMigration::PopulateUntrackedUploads do include MigrationsHelpers::TrackUntrackedUploadsHelpers subject { described_class.new } diff --git a/spec/lib/gitlab/background_migration/populate_user_highest_roles_table_spec.rb b/spec/lib/gitlab/background_migration/populate_user_highest_roles_table_spec.rb index be661d5b83e..f0b0f77280e 100644 --- a/spec/lib/gitlab/background_migration/populate_user_highest_roles_table_spec.rb +++ b/spec/lib/gitlab/background_migration/populate_user_highest_roles_table_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::PopulateUserHighestRolesTable, schema: 20200311130802 do +RSpec.describe Gitlab::BackgroundMigration::PopulateUserHighestRolesTable, schema: 20200311130802 do let(:members) { table(:members) } let(:users) { table(:users) } let(:user_highest_roles) { table(:user_highest_roles) } diff --git a/spec/lib/gitlab/background_migration/prepare_untracked_uploads_spec.rb b/spec/lib/gitlab/background_migration/prepare_untracked_uploads_spec.rb index 9daf35d0311..9b01407dc8b 100644 --- a/spec/lib/gitlab/background_migration/prepare_untracked_uploads_spec.rb +++ b/spec/lib/gitlab/background_migration/prepare_untracked_uploads_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' # Rollback DB to 10.5 (later than this was originally written for) because it still needs to work. -describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, schema: 20180208183958 do +RSpec.describe Gitlab::BackgroundMigration::PrepareUntrackedUploads do include MigrationsHelpers::TrackUntrackedUploadsHelpers let!(:untracked_files_for_uploads) { table(:untracked_files_for_uploads) } diff --git a/spec/lib/gitlab/background_migration/recalculate_project_authorizations_spec.rb b/spec/lib/gitlab/background_migration/recalculate_project_authorizations_spec.rb index ba87312e2bf..33e1f31d1f1 100644 --- a/spec/lib/gitlab/background_migration/recalculate_project_authorizations_spec.rb +++ b/spec/lib/gitlab/background_migration/recalculate_project_authorizations_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::RecalculateProjectAuthorizations, schema: 20200204113223 do +RSpec.describe Gitlab::BackgroundMigration::RecalculateProjectAuthorizations, schema: 20200204113223 do let(:users_table) { table(:users) } let(:namespaces_table) { table(:namespaces) } let(:projects_table) { table(:projects) } diff --git a/spec/lib/gitlab/background_migration/recalculate_project_authorizations_with_min_max_user_id_spec.rb b/spec/lib/gitlab/background_migration/recalculate_project_authorizations_with_min_max_user_id_spec.rb index edb46efad7c..c1ba1607b89 100644 --- a/spec/lib/gitlab/background_migration/recalculate_project_authorizations_with_min_max_user_id_spec.rb +++ b/spec/lib/gitlab/background_migration/recalculate_project_authorizations_with_min_max_user_id_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::RecalculateProjectAuthorizationsWithMinMaxUserId, schema: 20200204113224 do +RSpec.describe Gitlab::BackgroundMigration::RecalculateProjectAuthorizationsWithMinMaxUserId, schema: 20200204113224 do let(:users_table) { table(:users) } let(:min) { 1 } let(:max) { 5 } diff --git a/spec/lib/gitlab/background_migration/remove_restricted_todos_spec.rb b/spec/lib/gitlab/background_migration/remove_restricted_todos_spec.rb index 3de24f577ab..7019d5d4212 100644 --- a/spec/lib/gitlab/background_migration/remove_restricted_todos_spec.rb +++ b/spec/lib/gitlab/background_migration/remove_restricted_todos_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::RemoveRestrictedTodos, schema: 20180704204006 do +RSpec.describe Gitlab::BackgroundMigration::RemoveRestrictedTodos do + let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:users) { table(:users) } let(:todos) { table(:todos) } @@ -18,8 +19,9 @@ describe Gitlab::BackgroundMigration::RemoveRestrictedTodos, schema: 20180704204 users.create(id: 2, email: 'reporter@example.com', projects_limit: 10) users.create(id: 3, email: 'guest@example.com', projects_limit: 10) - projects.create!(id: 1, name: 'project-1', path: 'project-1', visibility_level: 0, namespace_id: 1) - projects.create!(id: 2, name: 'project-2', path: 'project-2', visibility_level: 0, namespace_id: 1) + namespace = namespaces.create(name: 'gitlab-org', path: 'gitlab-org') + projects.create!(id: 1, name: 'project-1', path: 'project-1', visibility_level: 0, namespace_id: namespace.id) + projects.create!(id: 2, name: 'project-2', path: 'project-2', visibility_level: 0, namespace_id: namespace.id) issues.create(id: 1, project_id: 1) issues.create(id: 2, project_id: 2) @@ -92,7 +94,7 @@ describe Gitlab::BackgroundMigration::RemoveRestrictedTodos, schema: 20180704204 context 'when issues are restricted to project members' do before do - project_features.create(issues_access_level: 10, project_id: 2) + project_features.create(issues_access_level: 10, pages_access_level: 10, project_id: 2) end it 'removes non members issue todos' do @@ -102,7 +104,7 @@ describe Gitlab::BackgroundMigration::RemoveRestrictedTodos, schema: 20180704204 context 'when merge requests are restricted to project members' do before do - project_features.create(merge_requests_access_level: 10, project_id: 2) + project_features.create(merge_requests_access_level: 10, pages_access_level: 10, project_id: 2) end it 'removes non members issue todos' do @@ -112,7 +114,7 @@ describe Gitlab::BackgroundMigration::RemoveRestrictedTodos, schema: 20180704204 context 'when repository and merge requests are restricted to project members' do before do - project_features.create(repository_access_level: 10, merge_requests_access_level: 10, project_id: 2) + project_features.create(repository_access_level: 10, merge_requests_access_level: 10, pages_access_level: 10, project_id: 2) end it 'removes non members commit and merge requests todos' do diff --git a/spec/lib/gitlab/background_migration/reset_merge_status_spec.rb b/spec/lib/gitlab/background_migration/reset_merge_status_spec.rb index e057aea6bb3..43fc0fb3691 100644 --- a/spec/lib/gitlab/background_migration/reset_merge_status_spec.rb +++ b/spec/lib/gitlab/background_migration/reset_merge_status_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::ResetMergeStatus do +RSpec.describe Gitlab::BackgroundMigration::ResetMergeStatus do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:namespace) { namespaces.create(name: 'gitlab', path: 'gitlab-org') } diff --git a/spec/lib/gitlab/background_migration/schedule_calculate_wiki_sizes_spec.rb b/spec/lib/gitlab/background_migration/schedule_calculate_wiki_sizes_spec.rb deleted file mode 100644 index 387e3343ede..00000000000 --- a/spec/lib/gitlab/background_migration/schedule_calculate_wiki_sizes_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' -require Rails.root.join('db', 'post_migrate', '20190527194900_schedule_calculate_wiki_sizes.rb') - -describe ScheduleCalculateWikiSizes do - let(:migration_class) { Gitlab::BackgroundMigration::CalculateWikiSizes } - let(:migration_name) { migration_class.to_s.demodulize } - - let(:namespaces) { table(:namespaces) } - let(:projects) { table(:projects) } - let(:project_statistics) { table(:project_statistics) } - - context 'when missing wiki sizes exist' do - before do - namespaces.create!(id: 1, name: 'wiki-migration', path: 'wiki-migration') - projects.create!(id: 1, name: 'wiki-project-1', path: 'wiki-project-1', namespace_id: 1) - projects.create!(id: 2, name: 'wiki-project-2', path: 'wiki-project-2', namespace_id: 1) - projects.create!(id: 3, name: 'wiki-project-3', path: 'wiki-project-3', namespace_id: 1) - project_statistics.create!(id: 1, project_id: 1, namespace_id: 1, wiki_size: 1000) - project_statistics.create!(id: 2, project_id: 2, namespace_id: 1, wiki_size: nil) - project_statistics.create!(id: 3, project_id: 3, namespace_id: 1, wiki_size: nil) - end - - it 'schedules a background migration' do - Sidekiq::Testing.fake! do - Timecop.freeze do - migrate! - - expect(migration_name).to be_scheduled_delayed_migration(5.minutes, 2, 3) - expect(BackgroundMigrationWorker.jobs.size).to eq 1 - end - end - end - - it 'calculates missing wiki sizes', :sidekiq_might_not_need_inline do - expect(project_statistics.find_by(id: 2).wiki_size).to be_nil - expect(project_statistics.find_by(id: 3).wiki_size).to be_nil - - migrate! - - expect(project_statistics.find_by(id: 2).wiki_size).not_to be_nil - expect(project_statistics.find_by(id: 3).wiki_size).not_to be_nil - end - end - - context 'when missing wiki sizes do not exist' do - before do - namespaces.create!(id: 1, name: 'wiki-migration', path: 'wiki-migration') - projects.create!(id: 1, name: 'wiki-project-1', path: 'wiki-project-1', namespace_id: 1) - project_statistics.create!(id: 1, project_id: 1, namespace_id: 1, wiki_size: 1000) - end - - it 'does not schedule a background migration' do - Sidekiq::Testing.fake! do - Timecop.freeze do - migrate! - - expect(BackgroundMigrationWorker.jobs.size).to eq 0 - end - end - end - end -end diff --git a/spec/lib/gitlab/background_migration/set_confidential_note_events_on_services_spec.rb b/spec/lib/gitlab/background_migration/set_confidential_note_events_on_services_spec.rb index 5ce4a322e51..364edf3ed2a 100644 --- a/spec/lib/gitlab/background_migration/set_confidential_note_events_on_services_spec.rb +++ b/spec/lib/gitlab/background_migration/set_confidential_note_events_on_services_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::SetConfidentialNoteEventsOnServices, schema: 20180122154930 do +RSpec.describe Gitlab::BackgroundMigration::SetConfidentialNoteEventsOnServices do let(:services) { table(:services) } describe '#perform' do diff --git a/spec/lib/gitlab/background_migration/set_confidential_note_events_on_webhooks_spec.rb b/spec/lib/gitlab/background_migration/set_confidential_note_events_on_webhooks_spec.rb index 08f1f543f5d..28b06ac3ba3 100644 --- a/spec/lib/gitlab/background_migration/set_confidential_note_events_on_webhooks_spec.rb +++ b/spec/lib/gitlab/background_migration/set_confidential_note_events_on_webhooks_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::SetConfidentialNoteEventsOnWebhooks, schema: 20180104131052 do +RSpec.describe Gitlab::BackgroundMigration::SetConfidentialNoteEventsOnWebhooks do let(:web_hooks) { table(:web_hooks) } describe '#perform' do diff --git a/spec/lib/gitlab/background_migration/update_existing_subgroup_to_match_visibility_level_of_parent_spec.rb b/spec/lib/gitlab/background_migration/update_existing_subgroup_to_match_visibility_level_of_parent_spec.rb index 70397ae1e30..6c0a1d3a5b0 100644 --- a/spec/lib/gitlab/background_migration/update_existing_subgroup_to_match_visibility_level_of_parent_spec.rb +++ b/spec/lib/gitlab/background_migration/update_existing_subgroup_to_match_visibility_level_of_parent_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BackgroundMigration::UpdateExistingSubgroupToMatchVisibilityLevelOfParent, schema: 2020_01_10_121314 do +RSpec.describe Gitlab::BackgroundMigration::UpdateExistingSubgroupToMatchVisibilityLevelOfParent, schema: 2020_01_10_121314 do include MigrationHelpers::NamespacesHelpers context 'private visibility level' do diff --git a/spec/lib/gitlab/background_migration/user_mentions/create_resource_user_mention_spec.rb b/spec/lib/gitlab/background_migration/user_mentions/create_resource_user_mention_spec.rb index d4f52a11ce7..08a4bbe38ac 100644 --- a/spec/lib/gitlab/background_migration/user_mentions/create_resource_user_mention_spec.rb +++ b/spec/lib/gitlab/background_migration/user_mentions/create_resource_user_mention_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' require './db/post_migrate/20200128134110_migrate_commit_notes_mentions_to_db' require './db/post_migrate/20200211155539_migrate_merge_request_mentions_to_db' -describe Gitlab::BackgroundMigration::UserMentions::CreateResourceUserMention, schema: 20200211155539 do +RSpec.describe Gitlab::BackgroundMigration::UserMentions::CreateResourceUserMention, schema: 20200211155539 do include MigrationsHelpers context 'when migrating data' do diff --git a/spec/lib/gitlab/background_migration/wrongfully_confirmed_email_unconfirmer_spec.rb b/spec/lib/gitlab/background_migration/wrongfully_confirmed_email_unconfirmer_spec.rb new file mode 100644 index 00000000000..c9928edb8e8 --- /dev/null +++ b/spec/lib/gitlab/background_migration/wrongfully_confirmed_email_unconfirmer_spec.rb @@ -0,0 +1,123 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::WrongfullyConfirmedEmailUnconfirmer, schema: 20200615111857 do + let(:users) { table(:users) } + let(:emails) { table(:emails) } + let(:confirmed_at_2_days_ago) { 2.days.ago } + let(:confirmed_at_3_days_ago) { 3.days.ago } + let(:one_year_ago) { 1.year.ago } + + let!(:user_needs_migration_1) { users.create!(name: 'user1', email: 'test1@test.com', state: 'active', projects_limit: 1, confirmed_at: confirmed_at_2_days_ago, confirmation_sent_at: one_year_ago) } + let!(:user_needs_migration_2) { users.create!(name: 'user2', email: 'test2@test.com', unconfirmed_email: 'unconfirmed@test.com', state: 'active', projects_limit: 1, confirmed_at: confirmed_at_3_days_ago, confirmation_sent_at: one_year_ago) } + let!(:user_does_not_need_migration) { users.create!(name: 'user3', email: 'test3@test.com', state: 'active', projects_limit: 1) } + let!(:inactive_user) { users.create!(name: 'user4', email: 'test4@test.com', state: 'blocked', projects_limit: 1, confirmed_at: confirmed_at_3_days_ago, confirmation_sent_at: one_year_ago) } + let!(:alert_bot_user) { users.create!(name: 'user5', email: 'test5@test.com', state: 'active', user_type: 2, projects_limit: 1, confirmed_at: confirmed_at_3_days_ago, confirmation_sent_at: one_year_ago) } + + let!(:bad_email_1) { emails.create!(user_id: user_needs_migration_1.id, email: 'other1@test.com', confirmed_at: confirmed_at_2_days_ago, confirmation_sent_at: one_year_ago) } + let!(:bad_email_2) { emails.create!(user_id: user_needs_migration_2.id, email: 'other2@test.com', confirmed_at: confirmed_at_3_days_ago, confirmation_sent_at: one_year_ago) } + let!(:bad_email_3_inactive_user) { emails.create!(user_id: inactive_user.id, email: 'other-inactive@test.com', confirmed_at: confirmed_at_3_days_ago, confirmation_sent_at: one_year_ago) } + let!(:bad_email_4_bot_user) { emails.create!(user_id: alert_bot_user.id, email: 'other-bot@test.com', confirmed_at: confirmed_at_3_days_ago, confirmation_sent_at: one_year_ago) } + + let!(:good_email_1) { emails.create!(user_id: user_needs_migration_2.id, email: 'other3@test.com', confirmed_at: confirmed_at_2_days_ago, confirmation_sent_at: one_year_ago) } + let!(:good_email_2) { emails.create!(user_id: user_needs_migration_2.id, email: 'other4@test.com', confirmed_at: nil) } + let!(:good_email_3) { emails.create!(user_id: user_does_not_need_migration.id, email: 'other5@test.com', confirmed_at: confirmed_at_2_days_ago, confirmation_sent_at: one_year_ago) } + + subject do + email_ids = [bad_email_1, bad_email_2, good_email_1, good_email_2, good_email_3].map(&:id) + + described_class.new.perform(email_ids.min, email_ids.max) + end + + it 'does not change irrelevant email records' do + subject + + expect(good_email_1.reload.confirmed_at).to be_within(1.second).of(confirmed_at_2_days_ago) + expect(good_email_2.reload.confirmed_at).to be_nil + expect(good_email_3.reload.confirmed_at).to be_within(1.second).of(confirmed_at_2_days_ago) + + expect(bad_email_3_inactive_user.reload.confirmed_at).to be_within(1.second).of(confirmed_at_3_days_ago) + expect(bad_email_4_bot_user.reload.confirmed_at).to be_within(1.second).of(confirmed_at_3_days_ago) + + expect(good_email_1.reload.confirmation_sent_at).to be_within(1.second).of(one_year_ago) + expect(good_email_2.reload.confirmation_sent_at).to be_nil + expect(good_email_3.reload.confirmation_sent_at).to be_within(1.second).of(one_year_ago) + + expect(bad_email_3_inactive_user.reload.confirmation_sent_at).to be_within(1.second).of(one_year_ago) + expect(bad_email_4_bot_user.reload.confirmation_sent_at).to be_within(1.second).of(one_year_ago) + end + + it 'clears the `unconfirmed_email` field' do + subject + + user_needs_migration_2.reload + expect(user_needs_migration_2.unconfirmed_email).to be_nil + end + + it 'does not change irrelevant user records' do + subject + + expect(user_does_not_need_migration.reload.confirmed_at).to be_nil + expect(inactive_user.reload.confirmed_at).to be_within(1.second).of(confirmed_at_3_days_ago) + expect(alert_bot_user.reload.confirmed_at).to be_within(1.second).of(confirmed_at_3_days_ago) + + expect(user_does_not_need_migration.reload.confirmation_sent_at).to be_nil + expect(inactive_user.reload.confirmation_sent_at).to be_within(1.second).of(one_year_ago) + expect(alert_bot_user.reload.confirmation_sent_at).to be_within(1.second).of(one_year_ago) + end + + it 'updates confirmation_sent_at column' do + subject + + expect(user_needs_migration_1.reload.confirmation_sent_at).to be_within(1.minute).of(Time.now) + expect(user_needs_migration_2.reload.confirmation_sent_at).to be_within(1.minute).of(Time.now) + + expect(bad_email_1.reload.confirmation_sent_at).to be_within(1.minute).of(Time.now) + expect(bad_email_2.reload.confirmation_sent_at).to be_within(1.minute).of(Time.now) + end + + it 'unconfirms bad email records' do + subject + + expect(bad_email_1.reload.confirmed_at).to be_nil + expect(bad_email_2.reload.confirmed_at).to be_nil + + expect(bad_email_1.reload.confirmation_token).not_to be_nil + expect(bad_email_2.reload.confirmation_token).not_to be_nil + end + + it 'unconfirms user records' do + subject + + expect(user_needs_migration_1.reload.confirmed_at).to be_nil + expect(user_needs_migration_2.reload.confirmed_at).to be_nil + + expect(user_needs_migration_1.reload.confirmation_token).not_to be_nil + expect(user_needs_migration_2.reload.confirmation_token).not_to be_nil + end + + context 'enqueued jobs' do + let(:user_1) { User.find(user_needs_migration_1.id) } + let(:user_2) { User.find(user_needs_migration_2.id) } + + let(:email_1) { Email.find(bad_email_1.id) } + let(:email_2) { Email.find(bad_email_2.id) } + + it 'enqueues the email confirmation and the unconfirm notification mailer jobs' do + allow(DeviseMailer).to receive(:confirmation_instructions).and_call_original + allow(Gitlab::BackgroundMigration::Mailers::UnconfirmMailer).to receive(:unconfirm_notification_email).and_call_original + + subject + + expect(DeviseMailer).to have_received(:confirmation_instructions).with(email_1, email_1.confirmation_token) + expect(DeviseMailer).to have_received(:confirmation_instructions).with(email_2, email_2.confirmation_token) + + expect(Gitlab::BackgroundMigration::Mailers::UnconfirmMailer).to have_received(:unconfirm_notification_email).with(user_1) + expect(DeviseMailer).to have_received(:confirmation_instructions).with(user_1, user_1.confirmation_token) + + expect(Gitlab::BackgroundMigration::Mailers::UnconfirmMailer).to have_received(:unconfirm_notification_email).with(user_2) + expect(DeviseMailer).to have_received(:confirmation_instructions).with(user_2, user_2.confirmation_token) + end + end +end |