2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
require "rails_helper"
|
|
|
|
require_relative "../dummy_provider"
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
RSpec.describe DiscourseChatIntegration::Rule do
|
2022-10-03 15:36:52 -04:00
|
|
|
include_context "with dummy provider"
|
2017-07-12 13:28:45 -04:00
|
|
|
|
2017-08-01 15:53:39 -04:00
|
|
|
let(:tag1) { Fabricate(:tag) }
|
|
|
|
let(:tag2) { Fabricate(:tag) }
|
2017-07-03 12:38:13 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
let(:channel) { DiscourseChatIntegration::Channel.create(provider: "dummy") }
|
2017-08-01 15:53:39 -04:00
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe ".alloc_key" do
|
|
|
|
it "should return sequential numbers" do
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.create(channel: channel).key).to eq("rule:1")
|
|
|
|
expect(DiscourseChatIntegration::Rule.create(channel: channel).key).to eq("rule:2")
|
|
|
|
expect(DiscourseChatIntegration::Rule.create(channel: channel).key).to eq("rule:3")
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should convert between channel and channel_id successfully" do
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.create(channel: channel)
|
2017-08-01 15:53:39 -04:00
|
|
|
expect(rule.channel_id).to eq(channel.id)
|
|
|
|
expect(rule.channel.id).to eq(channel.id)
|
2017-07-13 11:09:34 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should save and load successfully" do
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(0)
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
rule =
|
|
|
|
DiscourseChatIntegration::Rule.create(
|
|
|
|
channel: channel,
|
|
|
|
category_id: category.id,
|
|
|
|
tags: [tag1.name, tag2.name],
|
|
|
|
filter: "watch",
|
|
|
|
)
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(1)
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
loadedRule = DiscourseChatIntegration::Rule.find(rule.id)
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(loadedRule.channel.id).to eq(channel.id)
|
2017-07-28 15:27:49 -04:00
|
|
|
expect(loadedRule.category_id).to eq(category.id)
|
2017-08-01 15:53:39 -04:00
|
|
|
expect(loadedRule.tags).to contain_exactly(tag1.name, tag2.name)
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(loadedRule.filter).to eq("watch")
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "general operations" do
|
2017-06-29 12:01:04 -04:00
|
|
|
before do
|
2022-12-29 07:31:05 -05:00
|
|
|
rule =
|
|
|
|
DiscourseChatIntegration::Rule.create(
|
|
|
|
channel: channel,
|
|
|
|
category_id: category.id,
|
|
|
|
tags: [tag1.name, tag2.name],
|
|
|
|
)
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can be modified" do
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.all.first
|
2017-07-13 11:09:34 -04:00
|
|
|
rule.tags = [tag1.name]
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2017-07-03 12:38:13 -04:00
|
|
|
rule.save!
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.all.first
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(rule.tags).to contain_exactly(tag1.name)
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can be deleted" do
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Rule.new(channel: channel).save!
|
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(2)
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.all.first
|
2017-06-29 12:01:04 -04:00
|
|
|
rule.destroy
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(1)
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can delete all" do
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Rule.create(channel: channel)
|
|
|
|
DiscourseChatIntegration::Rule.create(channel: channel)
|
|
|
|
DiscourseChatIntegration::Rule.create(channel: channel)
|
|
|
|
DiscourseChatIntegration::Rule.create(channel: channel)
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(5)
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Rule.destroy_all
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(0)
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can be filtered by channel" do
|
|
|
|
channel2 = DiscourseChatIntegration::Channel.create(provider: "dummy")
|
|
|
|
channel3 = DiscourseChatIntegration::Channel.create(provider: "dummy")
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
rule2 = DiscourseChatIntegration::Rule.create(channel: channel)
|
|
|
|
rule3 = DiscourseChatIntegration::Rule.create(channel: channel)
|
|
|
|
rule4 = DiscourseChatIntegration::Rule.create(channel: channel2)
|
|
|
|
rule5 = DiscourseChatIntegration::Rule.create(channel: channel3)
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(5)
|
2017-07-05 17:38:53 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.with_channel(channel).length).to eq(3)
|
|
|
|
expect(DiscourseChatIntegration::Rule.with_channel(channel2).length).to eq(1)
|
2017-07-05 17:38:53 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can be filtered by category" do
|
2021-07-13 15:36:16 -04:00
|
|
|
rule2 = DiscourseChatIntegration::Rule.create(channel: channel, category_id: category.id)
|
|
|
|
rule3 = DiscourseChatIntegration::Rule.create(channel: channel, category_id: nil)
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(3)
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.with_category_id(category.id).length).to eq(2)
|
|
|
|
expect(DiscourseChatIntegration::Rule.with_category_id(nil).length).to eq(1)
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
2017-07-18 16:36:07 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can be filtered by group" do
|
2017-07-28 15:27:49 -04:00
|
|
|
group1 = Fabricate(:group)
|
|
|
|
group2 = Fabricate(:group)
|
2022-12-29 07:31:05 -05:00
|
|
|
rule2 =
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: channel,
|
|
|
|
type: "group_message",
|
|
|
|
group_id: group1.id,
|
|
|
|
)
|
|
|
|
rule3 =
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: channel,
|
|
|
|
type: "group_message",
|
|
|
|
group_id: group2.id,
|
|
|
|
)
|
2017-07-28 15:27:49 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(3)
|
2017-07-28 15:27:49 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.with_category_id(category.id).length).to eq(1)
|
|
|
|
expect(DiscourseChatIntegration::Rule.with_group_ids([group1.id, group2.id]).length).to eq(2)
|
|
|
|
expect(DiscourseChatIntegration::Rule.with_group_ids([group1.id]).length).to eq(1)
|
|
|
|
expect(DiscourseChatIntegration::Rule.with_group_ids([group2.id]).length).to eq(1)
|
2017-07-28 15:27:49 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can be filtered by type" do
|
2017-08-01 12:11:34 -04:00
|
|
|
group1 = Fabricate(:group)
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
rule2 =
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: channel,
|
|
|
|
type: "group_message",
|
|
|
|
group_id: group1.id,
|
|
|
|
)
|
|
|
|
rule3 =
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: channel,
|
|
|
|
type: "group_mention",
|
|
|
|
group_id: group1.id,
|
|
|
|
)
|
2017-08-01 12:11:34 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(3)
|
2017-08-01 12:11:34 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(DiscourseChatIntegration::Rule.with_type("group_message").length).to eq(1)
|
|
|
|
expect(DiscourseChatIntegration::Rule.with_type("group_mention").length).to eq(1)
|
|
|
|
expect(DiscourseChatIntegration::Rule.with_type("normal").length).to eq(1)
|
2017-08-01 12:11:34 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can be sorted by precedence" do
|
|
|
|
rule2 = DiscourseChatIntegration::Rule.create(channel: channel, filter: "mute")
|
|
|
|
rule3 = DiscourseChatIntegration::Rule.create(channel: channel, filter: "follow")
|
|
|
|
rule4 = DiscourseChatIntegration::Rule.create(channel: channel, filter: "thread")
|
|
|
|
rule5 = DiscourseChatIntegration::Rule.create(channel: channel, filter: "mute")
|
2017-07-18 16:36:07 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.length).to eq(5)
|
2017-07-18 16:36:07 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(DiscourseChatIntegration::Rule.all.order_by_precedence.map(&:filter)).to eq(
|
|
|
|
%w[mute mute thread watch follow],
|
|
|
|
)
|
2017-07-18 16:36:07 -04:00
|
|
|
end
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "validations" do
|
2017-07-03 12:38:13 -04:00
|
|
|
let(:rule) do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create(
|
|
|
|
filter: "watch",
|
|
|
|
channel: channel,
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
2017-07-03 12:38:13 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "validates channel correctly" do
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(true)
|
2022-12-29 07:31:05 -05:00
|
|
|
rule.channel_id = "blahblahblah"
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(false)
|
2017-07-13 11:09:34 -04:00
|
|
|
rule.channel_id = -1
|
2017-07-03 19:14:01 -04:00
|
|
|
expect(rule.valid?).to eq(false)
|
2017-07-03 12:38:13 -04:00
|
|
|
end
|
|
|
|
|
2017-07-28 15:27:49 -04:00
|
|
|
it "doesn't allow both category and group to be set" do
|
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.group_id = group.id
|
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
rule.category_id = nil
|
2017-08-01 10:20:00 -04:00
|
|
|
rule.type = "group_message"
|
2017-07-28 15:27:49 -04:00
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "validates group correctly" do
|
2017-07-28 15:27:49 -04:00
|
|
|
rule.category_id = nil
|
|
|
|
rule.group_id = group.id
|
2017-08-01 10:20:00 -04:00
|
|
|
rule.type = "group_message"
|
2017-07-28 15:27:49 -04:00
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.group_id = -99
|
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "validates category correctly" do
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(true)
|
2017-07-28 15:27:49 -04:00
|
|
|
rule.category_id = -99
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "validates filter correctly" do
|
2020-06-15 11:45:25 -04:00
|
|
|
expect(rule.valid?).to eq(true)
|
2022-12-29 07:31:05 -05:00
|
|
|
rule.filter = "thread"
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(true)
|
2022-12-29 07:31:05 -05:00
|
|
|
rule.filter = "follow"
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(true)
|
2022-12-29 07:31:05 -05:00
|
|
|
rule.filter = "mute"
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(true)
|
2022-12-29 07:31:05 -05:00
|
|
|
rule.filter = ""
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(false)
|
2022-12-29 07:31:05 -05:00
|
|
|
rule.filter = "somerandomstring"
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "validates tags correctly" do
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.tags = []
|
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.tags = [tag1.name]
|
|
|
|
expect(rule.valid?).to eq(true)
|
2022-12-29 07:31:05 -05:00
|
|
|
rule.tags = [tag1.name, "blah"]
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't allow save when invalid" do
|
|
|
|
expect(rule.valid?).to eq(true)
|
2022-12-29 07:31:05 -05:00
|
|
|
rule.filter = "somerandomfilter"
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
expect(rule.save).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
end
|