From 141faaacf9119ce5d765efe73c6509030ba078cd Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Fri, 25 Nov 2016 17:36:37 -0200 Subject: Mattermost Notifications Service --- .../chat_message/build_message_spec.rb | 57 +++++++++ .../chat_message/issue_message_spec.rb | 67 +++++++++++ .../chat_message/merge_message_spec.rb | 51 ++++++++ .../chat_message/note_message_spec.rb | 130 +++++++++++++++++++++ .../chat_message/pipeline_message_spec.rb | 67 +++++++++++ .../chat_message/push_message_spec.rb | 88 ++++++++++++++ .../chat_message/wiki_page_message_spec.rb | 73 ++++++++++++ 7 files changed, 533 insertions(+) create mode 100644 spec/models/project_services/chat_message/build_message_spec.rb create mode 100644 spec/models/project_services/chat_message/issue_message_spec.rb create mode 100644 spec/models/project_services/chat_message/merge_message_spec.rb create mode 100644 spec/models/project_services/chat_message/note_message_spec.rb create mode 100644 spec/models/project_services/chat_message/pipeline_message_spec.rb create mode 100644 spec/models/project_services/chat_message/push_message_spec.rb create mode 100644 spec/models/project_services/chat_message/wiki_page_message_spec.rb (limited to 'spec/models/project_services/chat_message') diff --git a/spec/models/project_services/chat_message/build_message_spec.rb b/spec/models/project_services/chat_message/build_message_spec.rb new file mode 100644 index 00000000000..b71d153f814 --- /dev/null +++ b/spec/models/project_services/chat_message/build_message_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +describe ChatMessage::BuildMessage do + subject { described_class.new(args) } + + let(:args) do + { + sha: '97de212e80737a608d939f648d959671fb0a0142', + ref: 'develop', + tag: false, + + project_name: 'project_name', + project_url: 'example.gitlab.com', + + commit: { + status: status, + author_name: 'hacker', + duration: duration, + }, + } + end + + let(:message) { build_message } + + context 'build succeeded' do + let(:status) { 'success' } + let(:color) { 'good' } + let(:duration) { 10 } + let(:message) { build_message('passed') } + + it 'returns a message with information about succeeded build' do + expect(subject.pretext).to be_empty + expect(subject.fallback).to eq(message) + expect(subject.attachments).to eq([text: message, color: color]) + end + end + + context 'build failed' do + let(:status) { 'failed' } + let(:color) { 'danger' } + let(:duration) { 10 } + + it 'returns a message with information about failed build' do + expect(subject.pretext).to be_empty + expect(subject.fallback).to eq(message) + expect(subject.attachments).to eq([text: message, color: color]) + end + end + + def build_message(status_text = status) + ":" \ + " Commit " \ + " of branch" \ + " by hacker #{status_text} in #{duration} #{'second'.pluralize(duration)}" + end +end diff --git a/spec/models/project_services/chat_message/issue_message_spec.rb b/spec/models/project_services/chat_message/issue_message_spec.rb new file mode 100644 index 00000000000..ebe0ead4408 --- /dev/null +++ b/spec/models/project_services/chat_message/issue_message_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe ChatMessage::IssueMessage, models: true do + subject { described_class.new(args) } + + let(:args) do + { + user: { + name: 'Test User', + username: 'test.user' + }, + project_name: 'project_name', + project_url: 'somewhere.com', + + object_attributes: { + title: 'Issue title', + id: 10, + iid: 100, + assignee_id: 1, + url: 'url', + action: 'open', + state: 'opened', + description: 'issue description' + } + } + end + + let(:color) { '#C95823' } + + context '#initialize' do + before do + args[:object_attributes][:description] = nil + end + + it 'returns a non-null description' do + expect(subject.description).to eq('') + end + end + + context 'open' do + it 'returns a message regarding opening of issues' do + expect(subject.pretext).to eq( + '] Issue opened by test.user') + expect(subject.attachments).to eq([ + { + title: "#100 Issue title", + title_link: "url", + text: "issue description", + color: color, + } + ]) + end + end + + context 'close' do + before do + args[:object_attributes][:action] = 'close' + args[:object_attributes][:state] = 'closed' + end + + it 'returns a message regarding closing of issues' do + expect(subject.pretext). to eq( + '] Issue closed by test.user') + expect(subject.attachments).to be_empty + end + end +end diff --git a/spec/models/project_services/chat_message/merge_message_spec.rb b/spec/models/project_services/chat_message/merge_message_spec.rb new file mode 100644 index 00000000000..07c414c6ca4 --- /dev/null +++ b/spec/models/project_services/chat_message/merge_message_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +describe ChatMessage::MergeMessage, models: true do + subject { described_class.new(args) } + + let(:args) do + { + user: { + name: 'Test User', + username: 'test.user' + }, + project_name: 'project_name', + project_url: 'somewhere.com', + + object_attributes: { + title: "Issue title\nSecond line", + id: 10, + iid: 100, + assignee_id: 1, + url: 'url', + state: 'opened', + description: 'issue description', + source_branch: 'source_branch', + target_branch: 'target_branch', + } + } + end + + let(:color) { '#345' } + + context 'open' do + it 'returns a message regarding opening of merge requests' do + expect(subject.pretext).to eq( + 'test.user opened '\ + 'in : *Issue title*') + expect(subject.attachments).to be_empty + end + end + + context 'close' do + before do + args[:object_attributes][:state] = 'closed' + end + it 'returns a message regarding closing of merge requests' do + expect(subject.pretext).to eq( + 'test.user closed '\ + 'in : *Issue title*') + expect(subject.attachments).to be_empty + end + end +end diff --git a/spec/models/project_services/chat_message/note_message_spec.rb b/spec/models/project_services/chat_message/note_message_spec.rb new file mode 100644 index 00000000000..31936da40a2 --- /dev/null +++ b/spec/models/project_services/chat_message/note_message_spec.rb @@ -0,0 +1,130 @@ +require 'spec_helper' + +describe ChatMessage::NoteMessage, models: true do + let(:color) { '#345' } + + before do + @args = { + user: { + name: 'Test User', + username: 'test.user', + avatar_url: 'http://fakeavatar' + }, + project_name: 'project_name', + project_url: 'somewhere.com', + repository: { + name: 'project_name', + url: 'somewhere.com', + }, + object_attributes: { + id: 10, + note: 'comment on a commit', + url: 'url', + noteable_type: 'Commit' + } + } + end + + context 'commit notes' do + before do + @args[:object_attributes][:note] = 'comment on a commit' + @args[:object_attributes][:noteable_type] = 'Commit' + @args[:commit] = { + id: '5f163b2b95e6f53cbd428f5f0b103702a52b9a23', + message: "Added a commit message\ndetails\n123\n" + } + end + + it 'returns a message regarding notes on commits' do + message = described_class.new(@args) + expect(message.pretext).to eq("test.user in : " \ + "*Added a commit message*") + expected_attachments = [ + { + text: "comment on a commit", + color: color, + } + ] + expect(message.attachments).to eq(expected_attachments) + end + end + + context 'merge request notes' do + before do + @args[:object_attributes][:note] = 'comment on a merge request' + @args[:object_attributes][:noteable_type] = 'MergeRequest' + @args[:merge_request] = { + id: 1, + iid: 30, + title: "merge request title\ndetails\n" + } + end + + it 'returns a message regarding notes on a merge request' do + message = described_class.new(@args) + expect(message.pretext).to eq("test.user in : " \ + "*merge request title*") + expected_attachments = [ + { + text: "comment on a merge request", + color: color, + } + ] + expect(message.attachments).to eq(expected_attachments) + end + end + + context 'issue notes' do + before do + @args[:object_attributes][:note] = 'comment on an issue' + @args[:object_attributes][:noteable_type] = 'Issue' + @args[:issue] = { + id: 1, + iid: 20, + title: "issue title\ndetails\n" + } + end + + it 'returns a message regarding notes on an issue' do + message = described_class.new(@args) + expect(message.pretext).to eq( + "test.user in : " \ + "*issue title*") + expected_attachments = [ + { + text: "comment on an issue", + color: color, + } + ] + expect(message.attachments).to eq(expected_attachments) + end + end + + context 'project snippet notes' do + before do + @args[:object_attributes][:note] = 'comment on a snippet' + @args[:object_attributes][:noteable_type] = 'Snippet' + @args[:snippet] = { + id: 5, + title: "snippet title\ndetails\n" + } + end + + it 'returns a message regarding notes on a project snippet' do + message = described_class.new(@args) + expect(message.pretext).to eq("test.user in : " \ + "*snippet title*") + expected_attachments = [ + { + text: "comment on a snippet", + color: color, + } + ] + expect(message.attachments).to eq(expected_attachments) + end + end +end diff --git a/spec/models/project_services/chat_message/pipeline_message_spec.rb b/spec/models/project_services/chat_message/pipeline_message_spec.rb new file mode 100644 index 00000000000..eca71db07b6 --- /dev/null +++ b/spec/models/project_services/chat_message/pipeline_message_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe ChatMessage::PipelineMessage do + subject { described_class.new(args) } + let(:user) { { name: 'hacker' } } + + let(:args) do + { + object_attributes: { + id: 123, + sha: '97de212e80737a608d939f648d959671fb0a0142', + tag: false, + ref: 'develop', + status: status, + duration: duration + }, + project: { path_with_namespace: 'project_name', + web_url: 'example.gitlab.com' }, + user: user + } + end + + let(:message) { build_message } + + context 'pipeline succeeded' do + let(:status) { 'success' } + let(:color) { 'good' } + let(:duration) { 10 } + let(:message) { build_message('passed') } + + it 'returns a message with information about succeeded build' do + verify_message + end + end + + context 'pipeline failed' do + let(:status) { 'failed' } + let(:color) { 'danger' } + let(:duration) { 10 } + + it 'returns a message with information about failed build' do + verify_message + end + + context 'when triggered by API therefore lacking user' do + let(:user) { nil } + let(:message) { build_message(status, 'API') } + + it 'returns a message stating it is by API' do + verify_message + end + end + end + + def verify_message + expect(subject.pretext).to be_empty + expect(subject.fallback).to eq(message) + expect(subject.attachments).to eq([text: message, color: color]) + end + + def build_message(status_text = status, name = user[:name]) + ":" \ + " Pipeline " \ + " of branch" \ + " by #{name} #{status_text} in #{duration} #{'second'.pluralize(duration)}" + end +end diff --git a/spec/models/project_services/chat_message/push_message_spec.rb b/spec/models/project_services/chat_message/push_message_spec.rb new file mode 100644 index 00000000000..b781c4505db --- /dev/null +++ b/spec/models/project_services/chat_message/push_message_spec.rb @@ -0,0 +1,88 @@ +require 'spec_helper' + +describe ChatMessage::PushMessage, models: true do + subject { described_class.new(args) } + + let(:args) do + { + after: 'after', + before: 'before', + project_name: 'project_name', + ref: 'refs/heads/master', + user_name: 'test.user', + project_url: 'url' + } + end + + let(:color) { '#345' } + + context 'push' do + before do + args[:commits] = [ + { message: 'message1', url: 'url1', id: 'abcdefghijkl', author: { name: 'author1' } }, + { message: 'message2', url: 'url2', id: '123456789012', author: { name: 'author2' } }, + ] + end + + it 'returns a message regarding pushes' do + expect(subject.pretext).to eq( + 'test.user pushed to branch of '\ + ' ()' + ) + expect(subject.attachments).to eq([ + { + text: ": message1 - author1\n"\ + ": message2 - author2", + color: color, + } + ]) + end + end + + context 'tag push' do + let(:args) do + { + after: 'after', + before: Gitlab::Git::BLANK_SHA, + project_name: 'project_name', + ref: 'refs/tags/new_tag', + user_name: 'test.user', + project_url: 'url' + } + end + + it 'returns a message regarding pushes' do + expect(subject.pretext).to eq('test.user pushed new tag ' \ + ' to ' \ + '') + expect(subject.attachments).to be_empty + end + end + + context 'new branch' do + before do + args[:before] = Gitlab::Git::BLANK_SHA + end + + it 'returns a message regarding a new branch' do + expect(subject.pretext).to eq( + 'test.user pushed new branch to '\ + '' + ) + expect(subject.attachments).to be_empty + end + end + + context 'removed branch' do + before do + args[:after] = Gitlab::Git::BLANK_SHA + end + + it 'returns a message regarding a removed branch' do + expect(subject.pretext).to eq( + 'test.user removed branch master from ' + ) + expect(subject.attachments).to be_empty + end + end +end diff --git a/spec/models/project_services/chat_message/wiki_page_message_spec.rb b/spec/models/project_services/chat_message/wiki_page_message_spec.rb new file mode 100644 index 00000000000..94c04dc0865 --- /dev/null +++ b/spec/models/project_services/chat_message/wiki_page_message_spec.rb @@ -0,0 +1,73 @@ +require 'spec_helper' + +describe ChatMessage::WikiPageMessage, models: true do + subject { described_class.new(args) } + + let(:args) do + { + user: { + name: 'Test User', + username: 'test.user' + }, + project_name: 'project_name', + project_url: 'somewhere.com', + object_attributes: { + title: 'Wiki page title', + url: 'url', + content: 'Wiki page description' + } + } + end + + describe '#pretext' do + context 'when :action == "create"' do + before { args[:object_attributes][:action] = 'create' } + + it 'returns a message that a new wiki page was created' do + expect(subject.pretext).to eq( + 'test.user created in : '\ + '*Wiki page title*') + end + end + + context 'when :action == "update"' do + before { args[:object_attributes][:action] = 'update' } + + it 'returns a message that a wiki page was updated' do + expect(subject.pretext).to eq( + 'test.user edited in : '\ + '*Wiki page title*') + end + end + end + + describe '#attachments' do + let(:color) { '#345' } + + context 'when :action == "create"' do + before { args[:object_attributes][:action] = 'create' } + + it 'returns the attachment for a new wiki page' do + expect(subject.attachments).to eq([ + { + text: "Wiki page description", + color: color, + } + ]) + end + end + + context 'when :action == "update"' do + before { args[:object_attributes][:action] = 'update' } + + it 'returns the attachment for an updated wiki page' do + expect(subject.attachments).to eq([ + { + text: "Wiki page description", + color: color, + } + ]) + end + end + end +end -- cgit v1.2.3