2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
require "rails_helper"
|
2017-10-03 04:33:20 -04:00
|
|
|
|
|
|
|
RSpec.describe Jobs::DiscourseChatMigrateFromSlackOfficial do
|
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "site settings" do
|
2017-10-04 03:03:23 -04:00
|
|
|
before do
|
|
|
|
PluginStoreRow.create!(
|
2022-12-29 07:31:05 -05:00
|
|
|
plugin_name: "discourse-slack-official",
|
2017-10-04 03:03:23 -04:00
|
|
|
key: "category_#{category.id}",
|
|
|
|
type_name: "JSON",
|
2022-12-29 07:31:05 -05:00
|
|
|
value: "[{\"channel\":\"#slack-channel\",\"filter\":\"mute\"}]",
|
2017-10-04 03:03:23 -04:00
|
|
|
)
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
SiteSetting.create!(value: "t", data_type: 5, name: "slack_enabled")
|
|
|
|
SiteSetting.create!(value: "token", data_type: 1, name: "slack_access_token")
|
|
|
|
SiteSetting.create!(value: "token2", data_type: 1, name: "slack_incoming_webhook_token")
|
|
|
|
SiteSetting.create!(value: 300, data_type: 3, name: "slack_discourse_excerpt_length")
|
|
|
|
SiteSetting.create!(
|
|
|
|
value: "https://hooks.slack.com/services/something",
|
|
|
|
data_type: 1,
|
|
|
|
name: "slack_outbound_webhook_url",
|
|
|
|
)
|
|
|
|
SiteSetting.create!(value: "http://outbound2.com", data_type: 1, name: "slack_icon_url")
|
|
|
|
SiteSetting.create!(value: 100, data_type: 3, name: "post_to_slack_window_secs")
|
|
|
|
SiteSetting.create!(value: User.last.username, data_type: 1, name: "slack_discourse_username")
|
2017-10-04 03:03:23 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should migrate the site settings correctly" do
|
2017-10-04 03:03:23 -04:00
|
|
|
described_class.new.execute_onceoff({})
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(SiteSetting.find_by(name: "slack_enabled").value).to eq("f")
|
|
|
|
expect(SiteSetting.chat_integration_slack_access_token).to eq("token")
|
|
|
|
expect(SiteSetting.chat_integration_slack_incoming_webhook_token).to eq("token2")
|
2017-10-04 03:03:23 -04:00
|
|
|
expect(SiteSetting.chat_integration_slack_excerpt_length).to eq(300)
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(SiteSetting.chat_integration_slack_outbound_webhook_url).to eq(
|
|
|
|
"https://hooks.slack.com/services/something",
|
|
|
|
)
|
2017-10-04 03:03:23 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(SiteSetting.chat_integration_slack_icon_url).to eq("http://outbound2.com")
|
2017-10-04 03:03:23 -04:00
|
|
|
|
|
|
|
expect(SiteSetting.chat_integration_delay_seconds).to eq(100)
|
|
|
|
expect(SiteSetting.chat_integration_discourse_username).to eq(User.last.username)
|
|
|
|
expect(SiteSetting.chat_integration_slack_enabled).to eq(true)
|
|
|
|
expect(SiteSetting.chat_integration_enabled).to eq(true)
|
|
|
|
end
|
2017-10-10 03:12:10 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "when slack_discourse_username is not valid" do
|
|
|
|
before { SiteSetting.find_by(name: "slack_discourse_username").update!(value: "someguy") }
|
2017-10-10 03:12:10 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should default to the system user" do
|
2017-10-10 03:12:10 -04:00
|
|
|
described_class.new.execute_onceoff({})
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(SiteSetting.chat_integration_discourse_username).to eq(
|
|
|
|
Discourse.system_user.username,
|
|
|
|
)
|
2017-10-10 03:12:10 -04:00
|
|
|
end
|
|
|
|
end
|
2017-10-04 03:03:23 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "when a uncategorized filter is present" do
|
2017-10-03 04:33:20 -04:00
|
|
|
before do
|
|
|
|
PluginStoreRow.create!(
|
2022-12-29 07:31:05 -05:00
|
|
|
plugin_name: "discourse-slack-official",
|
2017-10-03 04:33:20 -04:00
|
|
|
key: "category_*",
|
|
|
|
type_name: "JSON",
|
2022-12-29 07:31:05 -05:00
|
|
|
value:
|
|
|
|
"[{\"channel\":\"#channel1\",\"filter\":\"watch\"},{\"channel\":\"channel2\",\"filter\":\"follow\"},{\"channel\":\"#channel1\",\"filter\":\"mute\"}]",
|
2017-10-03 04:33:20 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should create the right channels and rules" do
|
2017-10-03 04:33:20 -04:00
|
|
|
described_class.new.execute_onceoff({})
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Channel.count).to eq(2)
|
|
|
|
expect(DiscourseChatIntegration::Rule.count).to eq(2)
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
channel = DiscourseChatIntegration::Channel.first
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(channel.value["provider"]).to eq("slack")
|
|
|
|
expect(channel.value["data"]["identifier"]).to eq("#channel1")
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.first
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(rule.value["channel_id"]).to eq(channel.id)
|
|
|
|
expect(rule.value["filter"]).to eq("mute")
|
|
|
|
expect(rule.value["category_id"]).to eq(nil)
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
channel = DiscourseChatIntegration::Channel.last
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(channel.value["provider"]).to eq("slack")
|
|
|
|
expect(channel.value["data"]["identifier"]).to eq("#channel2")
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.last
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(rule.value["channel_id"]).to eq(channel.id)
|
|
|
|
expect(rule.value["filter"]).to eq("follow")
|
|
|
|
expect(rule.value["category_id"]).to eq(nil)
|
2017-10-03 04:33:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "when filter contains an invalid tag" do
|
2017-10-03 04:33:20 -04:00
|
|
|
let(:tag) { Fabricate(:tag) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
PluginStoreRow.create!(
|
2022-12-29 07:31:05 -05:00
|
|
|
plugin_name: "discourse-slack-official",
|
2017-10-03 04:33:20 -04:00
|
|
|
key: "category_#{category.id}",
|
|
|
|
type_name: "JSON",
|
2022-12-29 07:31:05 -05:00
|
|
|
value:
|
|
|
|
"[{\"channel\":\"#slack-channel\",\"filter\":\"mute\",\"tags\":[\"#{tag.name}\",\"random-tag\"]}]",
|
2017-10-03 04:33:20 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should discard invalid tags" do
|
2017-10-03 04:33:20 -04:00
|
|
|
described_class.new.execute_onceoff({})
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.first
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(rule.value["tags"]).to eq([tag.name])
|
2017-10-03 04:33:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "when a category filter is present" do
|
2017-10-03 04:33:20 -04:00
|
|
|
before do
|
|
|
|
PluginStoreRow.create!(
|
2022-12-29 07:31:05 -05:00
|
|
|
plugin_name: "discourse-slack-official",
|
2017-10-03 04:33:20 -04:00
|
|
|
key: "category_#{category.id}",
|
|
|
|
type_name: "JSON",
|
2022-12-29 07:31:05 -05:00
|
|
|
value: "[{\"channel\":\"#slack-channel\",\"filter\":\"mute\"}]",
|
2017-10-03 04:33:20 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should migrate the settings correctly" do
|
2017-10-03 04:33:20 -04:00
|
|
|
described_class.new.execute_onceoff({})
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
channel = DiscourseChatIntegration::Channel.first
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(channel.value["provider"]).to eq("slack")
|
|
|
|
expect(channel.value["data"]["identifier"]).to eq("#slack-channel")
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.first
|
2017-10-03 04:33:20 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(rule.value["channel_id"]).to eq(channel.id)
|
|
|
|
expect(rule.value["filter"]).to eq("mute")
|
|
|
|
expect(rule.value["category_id"]).to eq(category.id)
|
2017-10-03 04:33:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "when a category has been deleted" do
|
2017-10-03 04:33:20 -04:00
|
|
|
before do
|
|
|
|
PluginStoreRow.create!(
|
2022-12-29 07:31:05 -05:00
|
|
|
plugin_name: "discourse-slack-official",
|
|
|
|
key: "category_9999",
|
2017-10-03 04:33:20 -04:00
|
|
|
type_name: "JSON",
|
2022-12-29 07:31:05 -05:00
|
|
|
value: "[{\"channel\":\"#slack-channel\",\"filter\":\"mute\"}]",
|
2017-10-03 04:33:20 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should not migrate the settings" do
|
2017-10-03 04:33:20 -04:00
|
|
|
described_class.new.execute_onceoff({})
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Channel.count).to eq(0)
|
|
|
|
expect(DiscourseChatIntegration::Rule.count).to eq(0)
|
2017-10-03 04:33:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|