diff options
Diffstat (limited to 'spec/models/project_services/microsoft_teams_service_spec.rb')
-rw-r--r-- | spec/models/project_services/microsoft_teams_service_spec.rb | 97 |
1 files changed, 74 insertions, 23 deletions
diff --git a/spec/models/project_services/microsoft_teams_service_spec.rb b/spec/models/project_services/microsoft_teams_service_spec.rb index 73c20359091..275244fa5fd 100644 --- a/spec/models/project_services/microsoft_teams_service_spec.rb +++ b/spec/models/project_services/microsoft_teams_service_spec.rb @@ -226,9 +226,10 @@ describe MicrosoftTeamsService do ) end - shared_examples 'call Microsoft Teams API' do + shared_examples 'call Microsoft Teams API' do |branches_to_be_notified: nil| before do WebMock.stub_request(:post, webhook_url) + chat_service.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified end it 'calls Microsoft Teams API for pipeline events' do @@ -245,6 +246,18 @@ describe MicrosoftTeamsService do end end + shared_examples 'does not call Microsoft Teams API' do |branches_to_be_notified: nil| + before do + chat_service.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified + end + it 'does not call Microsoft Teams API for pipeline events' do + data = Gitlab::DataBuilder::Pipeline.build(pipeline) + result = chat_service.execute(data) + + expect(result).to be_falsy + end + end + context 'with failed pipeline' do let(:status) { 'failed' } @@ -272,35 +285,73 @@ describe MicrosoftTeamsService do end end - context 'only notify for the default branch' do - context 'when enabled' do - let(:pipeline) do - create(:ci_pipeline, project: project, status: 'failed', ref: 'not-the-default-branch') - end + context 'with default branch' do + let(:pipeline) do + create(:ci_pipeline, project: project, status: 'failed', sha: project.commit.sha, ref: project.default_branch) + end - before do - chat_service.notify_only_default_branch = true - end + context 'only notify for the default branch' do + it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "default" + end - it 'does not call the Microsoft Teams API for pipeline events' do - data = Gitlab::DataBuilder::Pipeline.build(pipeline) - result = chat_service.execute(data) + context 'notify for only protected branches' do + it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "protected" + end - expect(result).to be_falsy - end + context 'notify for only default and protected branches' do + it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "default_and_protected" end - context 'when disabled' do - let(:pipeline) do - create(:ci_pipeline, :failed, project: project, - sha: project.commit.sha, ref: 'not-the-default-branch') - end + context 'notify for all branches' do + it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "all" + end + end - before do - chat_service.notify_only_default_branch = false - end + context 'with protected branch' do + before do + create(:protected_branch, project: project, name: 'a-protected-branch') + end - it_behaves_like 'call Microsoft Teams API' + let(:pipeline) do + create(:ci_pipeline, project: project, status: 'failed', sha: project.commit.sha, ref: 'a-protected-branch') + end + + context 'only notify for the default branch' do + it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "default" + end + + context 'notify for only protected branches' do + it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "protected" + end + + context 'notify for only default and protected branches' do + it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "default_and_protected" + end + + context 'notify for all branches' do + it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "all" + end + end + + context 'with neither protected nor default branch' do + let(:pipeline) do + create(:ci_pipeline, project: project, status: 'failed', sha: project.commit.sha, ref: 'a-random-branch') + end + + context 'only notify for the default branch' do + it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "default" + end + + context 'notify for only protected branches' do + it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "protected" + end + + context 'notify for only default and protected branches' do + it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "default_and_protected" + end + + context 'notify for all branches' do + it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "all" end end end |