summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-04-20 08:12:48 +0000
committerRémy Coutable <remy@rymai.me>2016-04-20 08:12:48 +0000
commitd7127890546c317bd3469f18b9fb5e3a81554d48 (patch)
tree43e56856035dd219f4683c1f95573da180f1acda /spec
parentca3fafb904c8293e978a3d7c8c5cafdec4031558 (diff)
parentedd07e2363297d4e296505a8e82eb76b2a009f10 (diff)
downloadgitlab-ce-d7127890546c317bd3469f18b9fb5e3a81554d48.tar.gz
Merge branch 'slack_wiki_notifications' into 'master'
add slack notifications for wiki pages ## What does this MR do? Lets the Slack service be configured to send notifications when wiki pages are created or edited. ## Are there points in the code the reviewer needs to double check? I'm just starting to get familiar with the Gitlab codebase and I was unsure on how to get the wiki page url to pass it to the slack message, on whether or not I needed to refactor the create/update methods for wiki pages from the controller to a service (but seemed necessary to test it better), and if I needed to add a column to the web hooks table or if the services table would have been enough. Please let me know if I should change anything and I will improve the MR, thanks for checking :) ## Why was this MR needed? Related to #563 and fixes #4233. See merge request !2998
Diffstat (limited to 'spec')
-rw-r--r--spec/factories/project_wikis.rb7
-rw-r--r--spec/factories/wiki_pages.rb9
-rw-r--r--spec/lib/gitlab/url_builder_spec.rb9
-rw-r--r--spec/models/project_services/slack_service/wiki_page_message_spec.rb74
-rw-r--r--spec/models/project_services/slack_service_spec.rb17
5 files changed, 116 insertions, 0 deletions
diff --git a/spec/factories/project_wikis.rb b/spec/factories/project_wikis.rb
new file mode 100644
index 00000000000..a3403fd76ae
--- /dev/null
+++ b/spec/factories/project_wikis.rb
@@ -0,0 +1,7 @@
+FactoryGirl.define do
+ factory :project_wiki do
+ project factory: :empty_project
+ user factory: :user
+ initialize_with { new(project, user) }
+ end
+end
diff --git a/spec/factories/wiki_pages.rb b/spec/factories/wiki_pages.rb
new file mode 100644
index 00000000000..938ccf2306b
--- /dev/null
+++ b/spec/factories/wiki_pages.rb
@@ -0,0 +1,9 @@
+require 'ostruct'
+
+FactoryGirl.define do
+ factory :wiki_page do
+ page = OpenStruct.new(url_path: 'some-name')
+ association :wiki, factory: :project_wiki, strategy: :build
+ initialize_with { new(wiki, page, true) }
+ end
+end
diff --git a/spec/lib/gitlab/url_builder_spec.rb b/spec/lib/gitlab/url_builder_spec.rb
index 6ffc0d6e658..bf11472407a 100644
--- a/spec/lib/gitlab/url_builder_spec.rb
+++ b/spec/lib/gitlab/url_builder_spec.rb
@@ -106,5 +106,14 @@ describe Gitlab::UrlBuilder, lib: true do
end
end
end
+
+ context 'when passing a WikiPage' do
+ it 'returns a proper URL' do
+ wiki_page = build(:wiki_page)
+ url = described_class.build(wiki_page)
+
+ expect(url).to eq "#{Gitlab.config.gitlab.url}#{wiki_page.wiki.wiki_base_path}/#{wiki_page.slug}"
+ end
+ end
end
end
diff --git a/spec/models/project_services/slack_service/wiki_page_message_spec.rb b/spec/models/project_services/slack_service/wiki_page_message_spec.rb
new file mode 100644
index 00000000000..6ecab645b49
--- /dev/null
+++ b/spec/models/project_services/slack_service/wiki_page_message_spec.rb
@@ -0,0 +1,74 @@
+require 'spec_helper'
+
+describe SlackService::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 <url|wiki page> in <somewhere.com|project_name>: '\
+ '*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 <url|wiki page> in <somewhere.com|project_name>: '\
+ '*Wiki page title*')
+ end
+ end
+ end
+
+ describe '#attachments' do
+ let(:color) { '#345' }
+
+ context 'when :action == "create"' do
+ before { args[:object_attributes][:action] = 'create' }
+
+
+ it '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 '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
diff --git a/spec/models/project_services/slack_service_spec.rb b/spec/models/project_services/slack_service_spec.rb
index a9e0afad90f..478d59be08b 100644
--- a/spec/models/project_services/slack_service_spec.rb
+++ b/spec/models/project_services/slack_service_spec.rb
@@ -75,6 +75,17 @@ describe SlackService, models: true do
@merge_request = merge_service.execute
@merge_sample_data = merge_service.hook_data(@merge_request,
'open')
+
+ opts = {
+ title: "Awesome wiki_page",
+ content: "Some text describing some thing or another",
+ format: "md",
+ message: "user created page: Awesome wiki_page"
+ }
+
+ wiki_page_service = WikiPages::CreateService.new(project, user, opts)
+ @wiki_page = wiki_page_service.execute
+ @wiki_page_sample_data = wiki_page_service.hook_data(@wiki_page, 'create')
end
it "should call Slack API for push events" do
@@ -95,6 +106,12 @@ describe SlackService, models: true do
expect(WebMock).to have_requested(:post, webhook_url).once
end
+ it "should call Slack API for wiki page events" do
+ slack.execute(@wiki_page_sample_data)
+
+ expect(WebMock).to have_requested(:post, webhook_url).once
+ end
+
it 'should use the username as an option for slack when configured' do
allow(slack).to receive(:username).and_return(username)
expect(Slack::Notifier).to receive(:new).