debian-mirror-gitlab/spec/models/integrations/microsoft_teams_spec.rb

354 lines
11 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2021-09-04 01:27:46 +05:30
RSpec.describe Integrations::MicrosoftTeams do
2021-09-30 23:02:18 +05:30
let(:chat_integration) { described_class.new }
2017-08-17 22:00:37 +05:30
let(:webhook_url) { 'https://example.gitlab.com/' }
describe 'Validations' do
2021-09-30 23:02:18 +05:30
context 'when integration is active' do
2017-09-10 17:25:29 +05:30
before do
subject.active = true
end
2017-08-17 22:00:37 +05:30
it { is_expected.to validate_presence_of(:webhook) }
2021-09-30 23:02:18 +05:30
it_behaves_like 'issue tracker integration URL attribute', :webhook
2017-08-17 22:00:37 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when integration is inactive' do
2017-09-10 17:25:29 +05:30
before do
subject.active = false
end
2017-08-17 22:00:37 +05:30
it { is_expected.not_to validate_presence_of(:webhook) }
end
end
2019-07-31 22:56:46 +05:30
describe '.supported_events' do
it 'does not support deployment_events' do
expect(described_class.supported_events).not_to include('deployment')
end
end
2017-08-17 22:00:37 +05:30
describe "#execute" do
2020-03-13 15:44:24 +05:30
let(:user) { create(:user) }
let_it_be(:project) { create(:project, :repository, :wiki_repo) }
2017-08-17 22:00:37 +05:30
before do
2021-09-30 23:02:18 +05:30
allow(chat_integration).to receive_messages(
2017-08-17 22:00:37 +05:30
project: project,
project_id: project.id,
webhook: webhook_url
)
WebMock.stub_request(:post, webhook_url)
end
context 'with push events' do
let(:push_sample_data) do
Gitlab::DataBuilder::Push.build_sample(project, user)
end
it "calls Microsoft Teams API for push events" do
2021-09-30 23:02:18 +05:30
chat_integration.execute(push_sample_data)
2017-08-17 22:00:37 +05:30
expect(WebMock).to have_requested(:post, webhook_url).once
end
it 'specifies the webhook when it is configured' do
2021-09-30 23:02:18 +05:30
integration = double(:microsoft_teams_integration).as_null_object
expect(::MicrosoftTeams::Notifier).to receive(:new).with(webhook_url).and_return(integration)
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
chat_integration.execute(push_sample_data)
2017-08-17 22:00:37 +05:30
end
end
context 'with issue events' do
let(:opts) { { title: 'Awesome issue', description: 'please fix' } }
let(:issues_sample_data) do
2021-09-30 23:02:18 +05:30
service = Issues::CreateService.new(project: project, current_user: user, params: opts, spam_params: nil)
2017-08-17 22:00:37 +05:30
issue = service.execute
service.hook_data(issue, 'open')
end
it "calls Microsoft Teams API" do
2021-09-30 23:02:18 +05:30
chat_integration.execute(issues_sample_data)
2017-08-17 22:00:37 +05:30
expect(WebMock).to have_requested(:post, webhook_url).once
end
end
context 'with merge events' do
let(:opts) do
{
title: 'Awesome merge_request',
description: 'please fix',
source_branch: 'feature',
target_branch: 'master'
}
end
let(:merge_sample_data) do
2021-06-08 01:23:25 +05:30
service = MergeRequests::CreateService.new(project: project, current_user: user, params: opts)
2017-08-17 22:00:37 +05:30
merge_request = service.execute
service.hook_data(merge_request, 'open')
end
2018-03-17 18:26:18 +05:30
before do
project.add_developer(user)
end
2017-08-17 22:00:37 +05:30
it "calls Microsoft Teams API" do
2021-09-30 23:02:18 +05:30
chat_integration.execute(merge_sample_data)
2017-08-17 22:00:37 +05:30
expect(WebMock).to have_requested(:post, webhook_url).once
end
end
context 'with wiki page events' do
let(:opts) do
{
title: "Awesome wiki_page",
content: "Some text describing some thing or another",
format: "md",
message: "user created page: Awesome wiki_page"
}
end
2020-10-24 23:57:45 +05:30
2020-05-24 23:13:21 +05:30
let(:wiki_page) { create(:wiki_page, wiki: project.wiki, **opts) }
2018-03-17 18:26:18 +05:30
let(:wiki_page_sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, 'create') }
2017-08-17 22:00:37 +05:30
it "calls Microsoft Teams API" do
2021-09-30 23:02:18 +05:30
chat_integration.execute(wiki_page_sample_data)
2017-08-17 22:00:37 +05:30
expect(WebMock).to have_requested(:post, webhook_url).once
end
end
end
describe "Note events" do
let(:user) { create(:user) }
let(:project) { create(:project, :repository, creator: user) }
before do
2021-09-30 23:02:18 +05:30
allow(chat_integration).to receive_messages(
2017-08-17 22:00:37 +05:30
project: project,
project_id: project.id,
webhook: webhook_url
)
WebMock.stub_request(:post, webhook_url)
end
context 'when commit comment event executed' do
let(:commit_note) do
create(:note_on_commit, author: user,
project: project,
commit_id: project.repository.commit.id,
note: 'a comment on a commit')
end
it "calls Microsoft Teams API for commit comment events" do
data = Gitlab::DataBuilder::Note.build(commit_note, user)
2021-09-30 23:02:18 +05:30
chat_integration.execute(data)
2017-08-17 22:00:37 +05:30
expect(WebMock).to have_requested(:post, webhook_url).once
end
end
context 'when merge request comment event executed' do
let(:merge_request_note) do
create(:note_on_merge_request, project: project,
note: "merge request note")
end
it "calls Microsoft Teams API for merge request comment events" do
data = Gitlab::DataBuilder::Note.build(merge_request_note, user)
2021-09-30 23:02:18 +05:30
chat_integration.execute(data)
2017-08-17 22:00:37 +05:30
expect(WebMock).to have_requested(:post, webhook_url).once
end
end
context 'when issue comment event executed' do
let(:issue_note) do
create(:note_on_issue, project: project, note: "issue note")
end
it "calls Microsoft Teams API for issue comment events" do
data = Gitlab::DataBuilder::Note.build(issue_note, user)
2021-09-30 23:02:18 +05:30
chat_integration.execute(data)
2017-08-17 22:00:37 +05:30
expect(WebMock).to have_requested(:post, webhook_url).once
end
end
context 'when snippet comment event executed' do
let(:snippet_note) do
create(:note_on_project_snippet, project: project,
note: "snippet note")
end
it "calls Microsoft Teams API for snippet comment events" do
data = Gitlab::DataBuilder::Note.build(snippet_note, user)
2021-09-30 23:02:18 +05:30
chat_integration.execute(data)
2017-08-17 22:00:37 +05:30
expect(WebMock).to have_requested(:post, webhook_url).once
end
end
end
describe 'Pipeline events' do
let(:user) { create(:user) }
let(:project) { create(:project, :repository) }
let(:pipeline) do
create(:ci_pipeline,
project: project, status: status,
sha: project.commit.sha, ref: project.default_branch)
end
before do
2021-09-30 23:02:18 +05:30
allow(chat_integration).to receive_messages(
2017-08-17 22:00:37 +05:30
project: project,
webhook: webhook_url
)
end
2019-12-04 20:38:33 +05:30
shared_examples 'call Microsoft Teams API' do |branches_to_be_notified: nil|
2017-08-17 22:00:37 +05:30
before do
WebMock.stub_request(:post, webhook_url)
2021-09-30 23:02:18 +05:30
chat_integration.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
2017-08-17 22:00:37 +05:30
end
it 'calls Microsoft Teams API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
2018-11-08 19:23:39 +05:30
data[:markdown] = true
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
chat_integration.execute(data)
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
message = Integrations::ChatMessage::PipelineMessage.new(data)
2018-11-08 19:23:39 +05:30
expect(WebMock).to have_requested(:post, webhook_url)
.with(body: hash_including({ summary: message.summary }))
.once
2017-08-17 22:00:37 +05:30
end
end
2019-12-04 20:38:33 +05:30
shared_examples 'does not call Microsoft Teams API' do |branches_to_be_notified: nil|
before do
2021-09-30 23:02:18 +05:30
chat_integration.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
2019-12-04 20:38:33 +05:30
end
it 'does not call Microsoft Teams API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
2021-09-30 23:02:18 +05:30
result = chat_integration.execute(data)
2019-12-04 20:38:33 +05:30
expect(result).to be_falsy
end
end
2017-08-17 22:00:37 +05:30
context 'with failed pipeline' do
let(:status) { 'failed' }
it_behaves_like 'call Microsoft Teams API'
end
context 'with succeeded pipeline' do
let(:status) { 'success' }
context 'with default to notify_only_broken_pipelines' do
it 'does not call Microsoft Teams API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
2021-09-30 23:02:18 +05:30
result = chat_integration.execute(data)
2017-08-17 22:00:37 +05:30
expect(result).to be_falsy
end
end
context 'with setting notify_only_broken_pipelines to false' do
before do
2021-09-30 23:02:18 +05:30
chat_integration.notify_only_broken_pipelines = false
2017-08-17 22:00:37 +05:30
end
it_behaves_like 'call Microsoft Teams API'
end
end
2019-12-04 20:38:33 +05:30
context 'with default branch' do
let(:pipeline) do
create(:ci_pipeline, project: project, status: 'failed', sha: project.commit.sha, ref: project.default_branch)
end
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
context 'only notify for the default branch' do
it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "default"
end
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
context 'notify for only protected branches' do
it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "protected"
end
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
context 'notify for only default and protected branches' do
it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "default_and_protected"
2017-08-17 22:00:37 +05:30
end
2019-09-04 21:01:54 +05:30
2019-12-04 20:38:33 +05:30
context 'notify for all branches' do
it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "all"
end
end
2019-09-04 21:01:54 +05:30
2019-12-04 20:38:33 +05:30
context 'with protected branch' do
before do
create(:protected_branch, project: project, name: 'a-protected-branch')
end
2019-09-04 21:01:54 +05:30
2019-12-04 20:38:33 +05:30
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"
2019-09-04 21:01:54 +05:30
end
2017-08-17 22:00:37 +05:30
end
end
end