Add tests for SlackProvider

This commit is contained in:
David Taylor 2017-06-30 16:39:19 +01:00
parent 0074f90f14
commit b824773a8a
3 changed files with 140 additions and 4 deletions

View File

@ -1,3 +1,5 @@
require_relative "slack_message_formatter.rb"
module DiscourseChat::Provider::SlackProvider
PROVIDER_NAME = "slack".freeze
@ -31,7 +33,7 @@ module DiscourseChat::Provider::SlackProvider
message = {
channel: channel,
username: SiteSetting.title,
username: SiteSetting.title || "Discourse",
icon_url: icon_url,
attachments: []
}
@ -57,7 +59,7 @@ module DiscourseChat::Provider::SlackProvider
message
end
def self.send_via_api(message)
def self.send_via_api(post, channel, message)
http = Net::HTTP.new("slack.com", 443)
http.use_ssl = true
@ -90,14 +92,16 @@ module DiscourseChat::Provider::SlackProvider
response = http.request(Net::HTTP::Post.new(uri))
DiscourseChat.pstore_set("slack_topic_#{post.topic.id}_#{channel}", JSON.parse(response.body) )
response
end
def self.send_via_webhook(message)
http = Net::HTTP.new("hooks.slack.com", 443)
http.use_ssl = true
req = Net::HTTP::Post.new(URI(SiteSetting.slack_outbound_webhook_url), 'Content-Type' =>'application/json')
req = Net::HTTP::Post.new(URI(SiteSetting.chat_slack_outbound_webhook_url), 'Content-Type' =>'application/json')
req.body = message.to_json
response = http.request(req)
response
end
def self.trigger_notification(post, channel)
@ -106,7 +110,7 @@ module DiscourseChat::Provider::SlackProvider
if SiteSetting.chat_slack_access_token.empty?
self.send_via_webhook(message)
else
self.send_via_api(message)
self.send_via_api(post, channel, message)
end
end

View File

@ -0,0 +1,29 @@
require 'rails_helper'
RSpec.describe DiscourseChat::Provider::SlackProvider::SlackMessageFormatter do
describe '.format' do
context 'links' do
it 'should return the right message' do
expect(described_class.format("<a href='http://somepath.com'>test</a>"))
.to eq('<http://somepath.com|test>')
end
describe 'when text contains a link with an incomplete URL' do
it 'should return the right message' do
expect(described_class.format("test <a href='//localhost:3000/some/path'></a>"))
.to eq("test <http://localhost:3000/some/path|>")
SiteSetting.force_https = true
expect(described_class.format("test <a href='//localhost:3000/some/path'></a>"))
.to eq("test <https://localhost:3000/some/path|>")
end
end
it "should not raise an error with unparseable urls" do
expect(described_class.format("<a>test</a>")).to eq("<test.localhost|test>")
end
end
end
end

View File

@ -0,0 +1,103 @@
require 'rails_helper'
RSpec.describe DiscourseChat::Provider::SlackProvider do
let(:post) { Fabricate(:post) }
describe '.excerpt' do
describe 'when post contains emoijs' do
before do
post.update!(raw: ':slight_smile: This is a test')
end
it 'should return the right excerpt' do
expect(described_class.excerpt(post)).to eq('🙂 This is a test')
end
end
describe 'when post contains onebox' do
it 'should return the right excerpt' do
post.update!(cooked: <<~COOKED
<aside class=\"onebox whitelistedgeneric\">
<header class=\"source\">
<a href=\"http://somesource.com\">
meta.discourse.org
</a>
</header>
<article class=\"onebox-body\">
<img src=\"http://somesource.com\" width=\"\" height=\"\" class=\"thumbnail\">
<h3>
<a href=\"http://somesource.com\">
Some text
</a>
</h3>
<p>
some text
</p>
</article>
<div class=\"onebox-metadata\">\n \n \n</div>
<div style=\"clear: both\"></div>
</aside>
COOKED
)
expect(described_class.excerpt(post))
.to eq('<http://somesource.com|meta.discourse.org>')
end
end
end
describe '.trigger_notifications' do
before do
SiteSetting.chat_slack_outbound_webhook_url = "https://hooks.slack.com/services/abcde"
SiteSetting.chat_slack_enabled = true
end
before do
@stub1 = stub_request(:post, SiteSetting.chat_slack_outbound_webhook_url).to_return(body: "success")
end
it 'sends a webhook request' do
expect(@stub1).to have_been_requested.times(0)
described_class.trigger_notification(post, '#general')
expect(@stub1).to have_been_requested.once
end
describe 'with api token' do
before do
SiteSetting.chat_slack_access_token = "magic"
@stub2 = stub_request(:post, %r{https://slack.com/api/chat.postMessage}).to_return(body: "{\"success\":true, \"ts\": \"#{Time.now.to_i}.012345\", \"message\": {\"attachments\": [], \"username\":\"blah\", \"text\":\"blah2\"} }", headers: {'Content-Type' => 'application/json'})
@stub3 = stub_request(:post, %r{https://slack.com/api/chat.update}).to_return(body: '{"success":true, "ts": "some_message_id"}', headers: {'Content-Type' => 'application/json'})
end
it 'sends an api request' do
expect(@stub2).to have_been_requested.times(0)
described_class.trigger_notification(post, '#general')
expect(@stub1).to have_been_requested.times(0)
expect(@stub2).to have_been_requested.once
end
it 'correctly merges replies' do
second_post = Fabricate(:post, topic: post.topic, post_number:2)
expect(@stub2).to have_been_requested.times(0)
expect(@stub3).to have_been_requested.times(0)
described_class.trigger_notification(post, '#general')
described_class.trigger_notification(second_post, '#general')
expect(@stub1).to have_been_requested.times(0)
expect(@stub2).to have_been_requested.once # Initial creation of message
expect(@stub3).to have_been_requested.once # Requests to update the existing message
end
end
end
end