2017-06-29 12:01:04 -04:00
|
|
|
require 'rails_helper'
|
2017-07-13 11:09:34 -04:00
|
|
|
require_relative '../dummy_provider'
|
2017-06-29 12:01:04 -04:00
|
|
|
|
|
|
|
RSpec.describe DiscourseChat::Rule do
|
2017-07-13 11:09:34 -04:00
|
|
|
include_context "dummy provider"
|
2017-07-12 13:28:45 -04:00
|
|
|
|
2017-07-03 12:38:13 -04:00
|
|
|
let(:tag1){Fabricate(:tag)}
|
|
|
|
let(:tag2){Fabricate(:tag)}
|
|
|
|
|
2017-07-13 15:50:45 -04:00
|
|
|
let(:channel){DiscourseChat::Channel.create(provider:'dummy')}
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2017-07-12 13:28:45 -04:00
|
|
|
describe '.alloc_key' do
|
2017-06-29 12:01:04 -04:00
|
|
|
it 'should return sequential numbers' do
|
2017-07-13 11:09:34 -04:00
|
|
|
expect( DiscourseChat::Rule.create(channel: channel).key ).to eq("rule:1")
|
|
|
|
expect( DiscourseChat::Rule.create(channel: channel).key ).to eq("rule:2")
|
|
|
|
expect( DiscourseChat::Rule.create(channel: channel).key ).to eq("rule:3")
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-13 11:09:34 -04:00
|
|
|
it 'should convert between channel and channel_id successfully' do
|
|
|
|
rule = DiscourseChat::Rule.create(channel: channel)
|
|
|
|
expect( rule.channel_id ).to eq(channel.id)
|
|
|
|
expect( rule.channel.id ).to eq(channel.id)
|
|
|
|
end
|
|
|
|
|
2017-06-29 12:01:04 -04:00
|
|
|
it 'should save and load successfully' do
|
|
|
|
expect(DiscourseChat::Rule.all.length).to eq(0)
|
|
|
|
|
2017-07-12 13:28:45 -04:00
|
|
|
rule = DiscourseChat::Rule.create({
|
2017-07-13 11:09:34 -04:00
|
|
|
channel: channel,
|
2017-07-03 12:38:13 -04:00
|
|
|
category_id: 1,
|
|
|
|
tags: [tag1.name, tag2.name],
|
2017-06-29 12:01:04 -04:00
|
|
|
filter: 'watch'
|
2017-07-12 13:28:45 -04:00
|
|
|
})
|
2017-06-29 12:01:04 -04:00
|
|
|
|
|
|
|
expect(DiscourseChat::Rule.all.length).to eq(1)
|
|
|
|
|
|
|
|
loadedRule = DiscourseChat::Rule.find(rule.id)
|
|
|
|
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(loadedRule.channel.id).to eq(channel.id)
|
2017-07-03 12:38:13 -04:00
|
|
|
expect(loadedRule.category_id).to eq(1)
|
|
|
|
expect(loadedRule.tags).to contain_exactly(tag1.name,tag2.name)
|
2017-06-29 12:01:04 -04:00
|
|
|
expect(loadedRule.filter).to eq('watch')
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'general operations' do
|
|
|
|
before do
|
2017-07-12 13:28:45 -04:00
|
|
|
rule = DiscourseChat::Rule.create({
|
2017-07-13 11:09:34 -04:00
|
|
|
channel: channel,
|
2017-07-03 12:38:13 -04:00
|
|
|
category_id: 1,
|
|
|
|
tags: [tag1.name, tag2.name]
|
2017-07-12 13:28:45 -04:00
|
|
|
})
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be modified' do
|
|
|
|
rule = DiscourseChat::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
|
|
|
|
|
|
|
rule = DiscourseChat::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
|
|
|
|
|
|
|
|
it 'can be deleted' do
|
2017-07-13 11:09:34 -04:00
|
|
|
DiscourseChat::Rule.new(channel:channel).save!
|
2017-06-29 12:01:04 -04:00
|
|
|
expect(DiscourseChat::Rule.all.length).to eq(2)
|
|
|
|
|
|
|
|
rule = DiscourseChat::Rule.all.first
|
|
|
|
rule.destroy
|
|
|
|
|
|
|
|
expect(DiscourseChat::Rule.all.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can delete all' do
|
2017-07-13 11:09:34 -04:00
|
|
|
DiscourseChat::Rule.create({channel:channel})
|
|
|
|
DiscourseChat::Rule.create({channel:channel})
|
|
|
|
DiscourseChat::Rule.create({channel:channel})
|
|
|
|
DiscourseChat::Rule.create({channel:channel})
|
2017-07-03 12:38:13 -04:00
|
|
|
|
2017-06-29 12:01:04 -04:00
|
|
|
expect(DiscourseChat::Rule.all.length).to eq(5)
|
|
|
|
|
|
|
|
DiscourseChat::Rule.destroy_all
|
|
|
|
|
|
|
|
expect(DiscourseChat::Rule.all.length).to eq(0)
|
|
|
|
end
|
|
|
|
|
2017-07-05 17:38:53 -04:00
|
|
|
it 'can be filtered by channel' do
|
2017-07-13 15:50:45 -04:00
|
|
|
channel2 = DiscourseChat::Channel.create(provider:'dummy')
|
|
|
|
channel3 = DiscourseChat::Channel.create(provider:'dummy')
|
2017-07-05 17:38:53 -04:00
|
|
|
|
2017-07-13 11:09:34 -04:00
|
|
|
rule2 = DiscourseChat::Rule.create(channel:channel)
|
|
|
|
rule3 = DiscourseChat::Rule.create(channel:channel)
|
|
|
|
rule4 = DiscourseChat::Rule.create(channel:channel2)
|
|
|
|
rule5 = DiscourseChat::Rule.create(channel:channel3)
|
|
|
|
|
2017-07-05 17:38:53 -04:00
|
|
|
expect(DiscourseChat::Rule.all.length).to eq(5)
|
|
|
|
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(DiscourseChat::Rule.with_channel(channel).length).to eq(3)
|
|
|
|
expect(DiscourseChat::Rule.with_channel(channel2).length).to eq(1)
|
2017-07-05 17:38:53 -04:00
|
|
|
end
|
|
|
|
|
2017-06-29 12:01:04 -04:00
|
|
|
it 'can be filtered by category' do
|
2017-07-13 11:09:34 -04:00
|
|
|
rule2 = DiscourseChat::Rule.create(channel:channel, category_id: 1)
|
|
|
|
rule3 = DiscourseChat::Rule.create(channel:channel, category_id: nil)
|
2017-06-29 12:01:04 -04:00
|
|
|
|
|
|
|
expect(DiscourseChat::Rule.all.length).to eq(3)
|
|
|
|
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(DiscourseChat::Rule.with_category_id(1).length).to eq(2)
|
|
|
|
expect(DiscourseChat::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
|
|
|
|
|
|
|
it 'can be sorted by precedence' do
|
|
|
|
rule2 = DiscourseChat::Rule.create(channel:channel, filter:'mute')
|
|
|
|
rule3 = DiscourseChat::Rule.create(channel:channel, filter:'follow')
|
|
|
|
rule4 = DiscourseChat::Rule.create(channel:channel, filter:'mute')
|
|
|
|
|
|
|
|
expect(DiscourseChat::Rule.all.length).to eq(4)
|
|
|
|
|
|
|
|
expect(DiscourseChat::Rule.all.order_by_precedence.map(&:filter)).to eq(["mute", "mute", "watch", "follow"])
|
|
|
|
end
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
|
|
|
|
2017-07-03 12:38:13 -04:00
|
|
|
describe 'validations' do
|
|
|
|
|
|
|
|
let(:rule) do
|
2017-07-12 13:28:45 -04:00
|
|
|
DiscourseChat::Rule.create({
|
2017-07-03 12:38:13 -04:00
|
|
|
filter: 'watch',
|
2017-07-13 11:09:34 -04:00
|
|
|
channel: channel,
|
2017-07-03 12:38:13 -04:00
|
|
|
category_id: 1,
|
2017-07-12 13:28:45 -04:00
|
|
|
})
|
2017-07-03 12:38:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates channel correctly' do
|
|
|
|
expect(rule.valid?).to eq(true)
|
2017-07-13 11:09:34 -04: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
|
|
|
|
|
|
|
|
it 'validates category correctly' do
|
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.category_id = 99
|
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates filter correctly' do
|
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.filter = 'follow'
|
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.filter = 'mute'
|
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.filter = ''
|
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
rule.filter = 'somerandomstring'
|
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates tags correctly' do
|
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.tags = []
|
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.tags = [tag1.name]
|
|
|
|
expect(rule.valid?).to eq(true)
|
|
|
|
rule.tags = [tag1.name, 'blah']
|
|
|
|
expect(rule.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't allow save when invalid" do
|
|
|
|
expect(rule.valid?).to eq(true)
|
2017-07-13 11:09:34 -04: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-06-29 12:01:04 -04:00
|
|
|
end
|