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-07-13 11:09:34 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
RSpec.describe DiscourseChatIntegration::Channel do
|
2022-10-03 15:36:52 -04:00
|
|
|
include_context "with dummy provider"
|
|
|
|
include_context "with validated dummy provider"
|
2017-07-13 11:09:34 -04:00
|
|
|
|
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::Channel.all.length).to eq(0)
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
chan = DiscourseChatIntegration::Channel.create(provider: "dummy")
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Channel.all.length).to eq(1)
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
loadedChan = DiscourseChatIntegration::Channel.find(chan.id)
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(loadedChan.provider).to eq("dummy")
|
2017-07-13 11:09:34 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should edit successfully" do
|
2021-07-13 15:36:16 -04:00
|
|
|
channel = DiscourseChatIntegration::Channel.create!(provider: "dummy2", data: { val: "hello" })
|
2017-07-29 14:29:19 -04:00
|
|
|
expect(channel.valid?).to eq(true)
|
|
|
|
channel.save!
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can be filtered by provider" do
|
|
|
|
channel1 = DiscourseChatIntegration::Channel.create!(provider: "dummy")
|
|
|
|
channel2 = DiscourseChatIntegration::Channel.create!(provider: "dummy2", data: { val: "blah" })
|
|
|
|
channel3 = DiscourseChatIntegration::Channel.create!(provider: "dummy2", data: { val: "blah2" })
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Channel.all.length).to eq(3)
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(DiscourseChatIntegration::Channel.with_provider("dummy2").length).to eq(2)
|
|
|
|
expect(DiscourseChatIntegration::Channel.with_provider("dummy").length).to eq(1)
|
2017-07-13 11:09:34 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can be filtered by data value" do
|
|
|
|
channel2 = DiscourseChatIntegration::Channel.create!(provider: "dummy2", data: { val: "foo" })
|
|
|
|
channel3 = DiscourseChatIntegration::Channel.create!(provider: "dummy2", data: { val: "blah" })
|
2017-07-13 17:50:14 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Channel.all.length).to eq(2)
|
2017-07-13 17:50:14 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
for_provider = DiscourseChatIntegration::Channel.with_provider("dummy2")
|
2017-07-13 17:50:14 -04:00
|
|
|
expect(for_provider.length).to eq(2)
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(
|
|
|
|
DiscourseChatIntegration::Channel
|
|
|
|
.with_provider("dummy2")
|
|
|
|
.with_data_value("val", "blah")
|
|
|
|
.length,
|
|
|
|
).to eq(1)
|
2017-07-13 17:50:14 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "can find its own rules" do
|
|
|
|
channel = DiscourseChatIntegration::Channel.create(provider: "dummy")
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(channel.rules.size).to eq(0)
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Rule.create(channel: channel)
|
|
|
|
DiscourseChatIntegration::Rule.create(channel: channel)
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(channel.rules.size).to eq(2)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "destroys its rules on destroy" do
|
|
|
|
channel = DiscourseChatIntegration::Channel.create(provider: "dummy")
|
2017-07-18 11:42:05 -04:00
|
|
|
expect(channel.rules.size).to eq(0)
|
2021-07-13 15:36:16 -04:00
|
|
|
rule1 = DiscourseChatIntegration::Rule.create(channel: channel)
|
|
|
|
rule2 = DiscourseChatIntegration::Rule.create(channel: channel)
|
2017-07-18 11:42:05 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.with_channel(channel).exists?).to eq(true)
|
2017-07-18 11:42:05 -04:00
|
|
|
channel.destroy()
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Rule.with_channel(channel).exists?).to eq(false)
|
2017-07-18 11:42:05 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "validations" do
|
|
|
|
it "validates provider correctly" do
|
2021-07-13 15:36:16 -04:00
|
|
|
channel = DiscourseChatIntegration::Channel.create!(provider: "dummy")
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(channel.valid?).to eq(true)
|
2022-12-29 07:31:05 -05:00
|
|
|
channel.provider = "somerandomprovider"
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(channel.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "succeeds with valid data" do
|
2021-07-13 15:36:16 -04:00
|
|
|
channel2 = DiscourseChatIntegration::Channel.new(provider: "dummy2", data: { val: "hello" })
|
2017-07-13 15:50:45 -04:00
|
|
|
expect(channel2.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "disallows invalid data" do
|
|
|
|
channel2 = DiscourseChatIntegration::Channel.new(provider: "dummy2", data: { val: " " })
|
2017-07-13 15:50:45 -04:00
|
|
|
expect(channel2.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "disallows unknown keys" do
|
|
|
|
channel2 =
|
|
|
|
DiscourseChatIntegration::Channel.new(
|
|
|
|
provider: "dummy2",
|
|
|
|
data: {
|
|
|
|
val: "hello",
|
|
|
|
unknown: "world",
|
|
|
|
},
|
|
|
|
)
|
2017-07-13 15:50:45 -04:00
|
|
|
expect(channel2.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "requires all keys" do
|
2021-07-13 15:36:16 -04:00
|
|
|
channel2 = DiscourseChatIntegration::Channel.new(provider: "dummy2", data: {})
|
2017-07-13 15:50:45 -04:00
|
|
|
expect(channel2.valid?).to eq(false)
|
2017-07-13 11:09:34 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "disallows duplicate channels" do
|
|
|
|
channel1 =
|
|
|
|
DiscourseChatIntegration::Channel.create(provider: "dummy2", data: { val: "hello" })
|
2021-07-13 15:36:16 -04:00
|
|
|
channel2 = DiscourseChatIntegration::Channel.new(provider: "dummy2", data: { val: "hello" })
|
2017-07-28 10:47:46 -04:00
|
|
|
expect(channel2.valid?).to eq(false)
|
|
|
|
channel2.data[:val] = "hello2"
|
|
|
|
expect(channel2.valid?).to eq(true)
|
|
|
|
end
|
2017-07-13 11:09:34 -04:00
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
end
|