discourse-chat-integration/spec/lib/discourse_chat/provider/slack/slack_provider_spec.rb

104 lines
3.4 KiB
Ruby

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_integration_slack_outbound_webhook_url = "https://hooks.slack.com/services/abcde"
SiteSetting.chat_integration_slack_enabled = true
end
before do
@stub1 = stub_request(:post, SiteSetting.chat_integration_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_integration_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