From 7be5d1ceaf8aeed82584e3b28d48221bbf86a7e6 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 23 Aug 2021 00:11:14 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- .../replace_external_wiki_triggers_spec.rb | 132 +++++++++++++++++++++ spec/models/project_spec.rb | 38 +++--- 2 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 spec/migrations/replace_external_wiki_triggers_spec.rb (limited to 'spec') diff --git a/spec/migrations/replace_external_wiki_triggers_spec.rb b/spec/migrations/replace_external_wiki_triggers_spec.rb new file mode 100644 index 00000000000..392ef76c5ba --- /dev/null +++ b/spec/migrations/replace_external_wiki_triggers_spec.rb @@ -0,0 +1,132 @@ +# frozen_string_literal: true + +require 'spec_helper' + +require_migration! + +RSpec.describe ReplaceExternalWikiTriggers do + let(:migration) { described_class.new } + let(:namespaces) { table(:namespaces) } + let(:projects) { table(:projects) } + let(:integrations) { table(:integrations) } + + before do + @namespace = namespaces.create!(name: 'foo', path: 'foo') + @project = projects.create!(namespace_id: @namespace.id) + end + + def create_external_wiki_integration(**attrs) + attrs.merge!(type_info) + + integrations.create!(**attrs) + end + + def has_external_wiki + !!@project.reload.has_external_wiki + end + + shared_examples 'external wiki triggers' do + describe 'INSERT trigger' do + it 'sets `has_external_wiki` to true when active external wiki integration is inserted' do + expect do + create_external_wiki_integration(active: true, project_id: @project.id) + end.to change { has_external_wiki }.to(true) + end + + it 'does not set `has_external_wiki` to true when integration is for a different project' do + different_project = projects.create!(namespace_id: @namespace.id) + + expect do + create_external_wiki_integration(active: true, project_id: different_project.id) + end.not_to change { has_external_wiki } + end + + it 'does not set `has_external_wiki` to true when inactive external wiki integration is inserted' do + expect do + create_external_wiki_integration(active: false, project_id: @project.id) + end.not_to change { has_external_wiki } + end + + it 'does not set `has_external_wiki` to true when active other service is inserted' do + expect do + integrations.create!(type_new: 'Integrations::MyService', type: 'MyService', active: true, project_id: @project.id) + end.not_to change { has_external_wiki } + end + end + + describe 'UPDATE trigger' do + it 'sets `has_external_wiki` to true when `ExternalWikiService` is made active' do + service = create_external_wiki_integration(active: false, project_id: @project.id) + + expect do + service.update!(active: true) + end.to change { has_external_wiki }.to(true) + end + + it 'sets `has_external_wiki` to false when integration is made inactive' do + service = create_external_wiki_integration(active: true, project_id: @project.id) + + expect do + service.update!(active: false) + end.to change { has_external_wiki }.to(false) + end + + it 'does not change `has_external_wiki` when integration is for a different project' do + different_project = projects.create!(namespace_id: @namespace.id) + service = create_external_wiki_integration(active: false, project_id: different_project.id) + + expect do + service.update!(active: true) + end.not_to change { has_external_wiki } + end + end + + describe 'DELETE trigger' do + it 'sets `has_external_wiki` to false when integration is deleted' do + service = create_external_wiki_integration(active: true, project_id: @project.id) + + expect do + service.delete + end.to change { has_external_wiki }.to(false) + end + + it 'does not change `has_external_wiki` when integration is for a different project' do + different_project = projects.create!(namespace_id: @namespace.id) + service = create_external_wiki_integration(active: true, project_id: different_project.id) + + expect do + service.delete + end.not_to change { has_external_wiki } + end + end + end + + describe '#up' do + before do + migrate! + end + + context 'when integrations are created with the new STI value' do + let(:type_info) { { type_new: 'Integrations::ExternalWiki' } } + + it_behaves_like 'external wiki triggers' + end + + context 'when integrations are created with the old STI value' do + let(:type_info) { { type: 'ExternalWikiService' } } + + it_behaves_like 'external wiki triggers' + end + end + + describe '#down' do + before do + migration.up + migration.down + end + + let(:type_info) { { type: 'ExternalWikiService' } } + + it_behaves_like 'external wiki triggers' + end +end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index d8f3a63d221..501f36c5107 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -1257,19 +1257,19 @@ RSpec.describe Project, factory_default: :keep do end it 'returns an active external wiki' do - create(:service, project: project, type: 'ExternalWikiService', active: true) + create(:external_wiki_integration, project: project, active: true) is_expected.to be_kind_of(Integrations::ExternalWiki) end it 'does not return an inactive external wiki' do - create(:service, project: project, type: 'ExternalWikiService', active: false) + create(:external_wiki_integration, project: project, active: false) is_expected.to eq(nil) end it 'sets Project#has_external_wiki when it is nil' do - create(:service, project: project, type: 'ExternalWikiService', active: true) + create(:external_wiki_integration, project: project, active: true) project.update_column(:has_external_wiki, nil) expect { subject }.to change { project.has_external_wiki }.from(nil).to(true) @@ -1279,36 +1279,40 @@ RSpec.describe Project, factory_default: :keep do describe '#has_external_wiki' do let_it_be(:project) { create(:project) } - def subject + def has_external_wiki project.reload.has_external_wiki end - specify { is_expected.to eq(false) } + specify { expect(has_external_wiki).to eq(false) } - context 'when there is an active external wiki service' do - let!(:service) do - create(:service, project: project, type: 'ExternalWikiService', active: true) + context 'when there is an active external wiki integration' do + let(:active) { true } + + let!(:integration) do + create(:external_wiki_integration, project: project, active: active) end - specify { is_expected.to eq(true) } + specify { expect(has_external_wiki).to eq(true) } it 'becomes false if the external wiki service is destroyed' do expect do - Integration.find(service.id).delete - end.to change { subject }.to(false) + Integration.find(integration.id).delete + end.to change { has_external_wiki }.to(false) end it 'becomes false if the external wiki service becomes inactive' do expect do - service.update_column(:active, false) - end.to change { subject }.to(false) + integration.update_column(:active, false) + end.to change { has_external_wiki }.to(false) end - end - it 'is false when external wiki service is not active' do - create(:service, project: project, type: 'ExternalWikiService', active: false) + context 'when created as inactive' do + let(:active) { false } - is_expected.to eq(false) + it 'is false' do + expect(has_external_wiki).to eq(false) + end + end end end -- cgit v1.2.3