diff options
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/ci/job_artifact_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/ci/pipeline_spec.rb | 119 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 46 | ||||
-rw-r--r-- | spec/models/project_services/confluence_service_spec.rb | 90 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 41 |
5 files changed, 258 insertions, 50 deletions
diff --git a/spec/models/ci/job_artifact_spec.rb b/spec/models/ci/job_artifact_spec.rb index 21a1b3fda75..41693b2a084 100644 --- a/spec/models/ci/job_artifact_spec.rb +++ b/spec/models/ci/job_artifact_spec.rb @@ -174,18 +174,6 @@ RSpec.describe Ci::JobArtifact do end end - describe '.for_ref' do - let(:first_pipeline) { create(:ci_pipeline, ref: 'first_ref') } - let(:second_pipeline) { create(:ci_pipeline, ref: 'second_ref', project: first_pipeline.project) } - let!(:first_artifact) { create(:ci_job_artifact, job: create(:ci_build, pipeline: first_pipeline)) } - let!(:second_artifact) { create(:ci_job_artifact, job: create(:ci_build, pipeline: second_pipeline)) } - - it 'returns job artifacts for a given pipeline ref' do - expect(described_class.for_ref(first_pipeline.ref, first_pipeline.project.id)).to eq([first_artifact]) - expect(described_class.for_ref(second_pipeline.ref, first_pipeline.project.id)).to eq([second_artifact]) - end - end - describe '.for_job_name' do it 'returns job artifacts for a given job name' do first_job = create(:ci_build, name: 'first') diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 3a5df241504..e6a92c4d8d5 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -219,6 +219,50 @@ RSpec.describe Ci::Pipeline, :mailer do end end + describe '.outside_pipeline_family' do + subject(:outside_pipeline_family) { described_class.outside_pipeline_family(upstream_pipeline) } + + let(:upstream_pipeline) { create(:ci_pipeline, project: project) } + let(:child_pipeline) { create(:ci_pipeline, project: project) } + + let!(:other_pipeline) { create(:ci_pipeline, project: project) } + + before do + create(:ci_sources_pipeline, + source_job: create(:ci_build, pipeline: upstream_pipeline), + source_project: project, + pipeline: child_pipeline, + project: project) + end + + it 'only returns pipelines outside pipeline family' do + expect(outside_pipeline_family).to contain_exactly(other_pipeline) + end + end + + describe '.before_pipeline' do + subject(:before_pipeline) { described_class.before_pipeline(child_pipeline) } + + let!(:older_other_pipeline) { create(:ci_pipeline, project: project) } + + let!(:upstream_pipeline) { create(:ci_pipeline, project: project) } + let!(:child_pipeline) { create(:ci_pipeline, project: project) } + + let!(:other_pipeline) { create(:ci_pipeline, project: project) } + + before do + create(:ci_sources_pipeline, + source_job: create(:ci_build, pipeline: upstream_pipeline), + source_project: project, + pipeline: child_pipeline, + project: project) + end + + it 'only returns older pipelines outside pipeline family' do + expect(before_pipeline).to contain_exactly(older_other_pipeline) + end + end + describe '#merge_request?' do let(:pipeline) { create(:ci_pipeline, merge_request: merge_request) } let(:merge_request) { create(:merge_request) } @@ -2635,6 +2679,55 @@ RSpec.describe Ci::Pipeline, :mailer do end end + describe '#same_family_pipeline_ids' do + subject(:same_family_pipeline_ids) { pipeline.same_family_pipeline_ids } + + context 'when pipeline is not child nor parent' do + it 'returns just the pipeline id' do + expect(same_family_pipeline_ids).to contain_exactly(pipeline) + end + end + + context 'when pipeline is child' do + let(:parent) { create(:ci_pipeline, project: pipeline.project) } + let(:sibling) { create(:ci_pipeline, project: pipeline.project) } + + before do + create(:ci_sources_pipeline, + source_job: create(:ci_build, pipeline: parent), + source_project: parent.project, + pipeline: pipeline, + project: pipeline.project) + + create(:ci_sources_pipeline, + source_job: create(:ci_build, pipeline: parent), + source_project: parent.project, + pipeline: sibling, + project: sibling.project) + end + + it 'returns parent sibling and self ids' do + expect(same_family_pipeline_ids).to contain_exactly(parent, pipeline, sibling) + end + end + + context 'when pipeline is parent' do + let(:child) { create(:ci_pipeline, project: pipeline.project) } + + before do + create(:ci_sources_pipeline, + source_job: create(:ci_build, pipeline: pipeline), + source_project: pipeline.project, + pipeline: child, + project: child.project) + end + + it 'returns self and child ids' do + expect(same_family_pipeline_ids).to contain_exactly(pipeline, child) + end + end + end + describe '#stuck?' do before do create(:ci_build, :pending, pipeline: pipeline) @@ -3179,6 +3272,32 @@ RSpec.describe Ci::Pipeline, :mailer do end end end + + context 'when transitioning to success' do + context 'when feature is enabled' do + before do + stub_feature_flags(keep_latest_artifacts_for_ref: true) + end + + it 'calls the PipelineSuccessUnlockArtifactsWorker' do + expect(Ci::PipelineSuccessUnlockArtifactsWorker).to receive(:perform_async).with(pipeline.id) + + pipeline.succeed! + end + end + + context 'when feature is disabled' do + before do + stub_feature_flags(keep_latest_artifacts_for_ref: false) + end + + it 'does not call the PipelineSuccessUnlockArtifactsWorker' do + expect(Ci::PipelineSuccessUnlockArtifactsWorker).not_to receive(:perform_async) + + pipeline.succeed! + end + end + end end describe '#default_branch?' do diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index 3c739771b19..9863a4051f4 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -1248,51 +1248,21 @@ RSpec.describe MergeRequest do let(:merge_request) { subject } let(:repository) { merge_request.source_project.repository } - context 'when memoize_source_branch_merge_request feature is enabled' do - before do - stub_feature_flags(memoize_source_branch_merge_request: true) - end - - context 'when the source project is set' do - it 'memoizes the value and returns the result' do - expect(repository).to receive(:branch_exists?).once.with(merge_request.source_branch).and_return(true) - - 2.times { expect(merge_request.source_branch_exists?).to eq(true) } - end - end + context 'when the source project is set' do + it 'memoizes the value and returns the result' do + expect(repository).to receive(:branch_exists?).once.with(merge_request.source_branch).and_return(true) - context 'when the source project is not set' do - before do - merge_request.source_project = nil - end - - it 'returns false' do - expect(merge_request.source_branch_exists?).to eq(false) - end + 2.times { expect(merge_request.source_branch_exists?).to eq(true) } end end - context 'when memoize_source_branch_merge_request feature is disabled' do + context 'when the source project is not set' do before do - stub_feature_flags(memoize_source_branch_merge_request: false) + merge_request.source_project = nil end - context 'when the source project is set' do - it 'does not memoize the value and returns the result' do - expect(repository).to receive(:branch_exists?).twice.with(merge_request.source_branch).and_return(true) - - 2.times { expect(merge_request.source_branch_exists?).to eq(true) } - end - end - - context 'when the source project is not set' do - before do - merge_request.source_project = nil - end - - it 'returns false' do - expect(merge_request.source_branch_exists?).to eq(false) - end + it 'returns false' do + expect(merge_request.source_branch_exists?).to eq(false) end end end diff --git a/spec/models/project_services/confluence_service_spec.rb b/spec/models/project_services/confluence_service_spec.rb new file mode 100644 index 00000000000..5d153b17070 --- /dev/null +++ b/spec/models/project_services/confluence_service_spec.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ConfluenceService do + describe 'Associations' do + it { is_expected.to belong_to :project } + it { is_expected.to have_one :service_hook } + end + + describe 'Validations' do + before do + subject.active = active + end + + context 'when service is active' do + let(:active) { true } + + it { is_expected.not_to allow_value('https://example.com').for(:confluence_url) } + it { is_expected.not_to allow_value('example.com').for(:confluence_url) } + it { is_expected.not_to allow_value('foo').for(:confluence_url) } + it { is_expected.not_to allow_value('ftp://example.atlassian.net/wiki').for(:confluence_url) } + it { is_expected.not_to allow_value('https://example.atlassian.net').for(:confluence_url) } + it { is_expected.not_to allow_value('https://.atlassian.net/wiki').for(:confluence_url) } + it { is_expected.not_to allow_value('https://example.atlassian.net/wikifoo').for(:confluence_url) } + it { is_expected.not_to allow_value('').for(:confluence_url) } + it { is_expected.not_to allow_value(nil).for(:confluence_url) } + it { is_expected.not_to allow_value('😊').for(:confluence_url) } + it { is_expected.to allow_value('https://example.atlassian.net/wiki').for(:confluence_url) } + it { is_expected.to allow_value('http://example.atlassian.net/wiki').for(:confluence_url) } + it { is_expected.to allow_value('https://example.atlassian.net/wiki/').for(:confluence_url) } + it { is_expected.to allow_value('http://example.atlassian.net/wiki/').for(:confluence_url) } + it { is_expected.to allow_value('https://example.atlassian.net/wiki/foo').for(:confluence_url) } + + it { is_expected.to validate_presence_of(:confluence_url) } + end + + context 'when service is inactive' do + let(:active) { false } + + it { is_expected.not_to validate_presence_of(:confluence_url) } + it { is_expected.to allow_value('foo').for(:confluence_url) } + end + end + + describe '#detailed_description' do + it 'can correctly return a link to the project wiki when active' do + project = create(:project) + subject.project = project + subject.active = true + + expect(subject.detailed_description).to include(Gitlab::Routing.url_helpers.project_wikis_url(project)) + end + + context 'when the project wiki is not enabled' do + it 'returns nil when both active or inactive', :aggregate_failures do + project = create(:project, :wiki_disabled) + subject.project = project + + [true, false].each do |active| + subject.active = active + + expect(subject.detailed_description).to be_nil + end + end + end + end + + describe 'Caching has_confluence on project_settings' do + let(:project) { create(:project) } + + subject { project.project_setting.has_confluence? } + + it 'sets the property to true when service is active' do + create(:confluence_service, project: project, active: true) + + is_expected.to be(true) + end + + it 'sets the property to false when service is not active' do + create(:confluence_service, project: project, active: false) + + is_expected.to be(false) + end + + it 'creates a project_setting record if one was not already created' do + expect { create(:confluence_service) }.to change { ProjectSetting.count }.by(1) + end + end +end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 2fede93bfa5..e0da7e15240 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -63,6 +63,7 @@ RSpec.describe Project do it { is_expected.to have_one(:bugzilla_service) } it { is_expected.to have_one(:gitlab_issue_tracker_service) } it { is_expected.to have_one(:external_wiki_service) } + it { is_expected.to have_one(:confluence_service) } it { is_expected.to have_one(:project_feature) } it { is_expected.to have_one(:project_repository) } it { is_expected.to have_one(:container_expiration_policy) } @@ -1041,6 +1042,32 @@ RSpec.describe Project do end end + describe '#has_confluence?' do + let_it_be(:project) { build_stubbed(:project) } + + it 'returns false when project_setting.has_confluence property is false' do + project.project_setting.has_confluence = false + + expect(project.has_confluence?).to be(false) + end + + context 'when project_setting.has_confluence property is true' do + before do + project.project_setting.has_confluence = true + end + + it 'returns true' do + expect(project.has_confluence?).to be(true) + end + + it 'returns false when confluence integration feature flag is disabled' do + stub_feature_flags(ConfluenceService::FEATURE_FLAG => false) + + expect(project.has_confluence?).to be(false) + end + end + end + describe '#external_wiki' do let(:project) { create(:project) } @@ -5385,6 +5412,20 @@ RSpec.describe Project do expect(services.count).to eq(2) expect(services.map(&:title)).to eq(['JetBrains TeamCity CI', 'Pushover']) end + + describe 'interaction with the confluence integration feature flag' do + it 'contains a ConfluenceService when feature flag is enabled' do + stub_feature_flags(ConfluenceService::FEATURE_FLAG => true) + + expect(subject.find_or_initialize_services).to include(ConfluenceService) + end + + it 'does not contain a ConfluenceService when the confluence integration feature flag is disabled' do + stub_feature_flags(ConfluenceService::FEATURE_FLAG => false) + + expect(subject.find_or_initialize_services).not_to include(ConfluenceService) + end + end end describe '#find_or_initialize_service' do |