2017-07-13 11:09:34 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
require_relative '../dummy_provider'
|
|
|
|
|
|
|
|
RSpec.describe DiscourseChat::Channel do
|
|
|
|
include_context "dummy provider"
|
2017-07-13 15:50:45 -04:00
|
|
|
include_context "validated dummy provider"
|
2017-07-13 11:09:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
it 'should save and load successfully' do
|
|
|
|
expect(DiscourseChat::Channel.all.length).to eq(0)
|
|
|
|
|
|
|
|
chan = DiscourseChat::Channel.create({
|
|
|
|
provider:"dummy",
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(DiscourseChat::Channel.all.length).to eq(1)
|
|
|
|
|
|
|
|
loadedChan = DiscourseChat::Channel.find(chan.id)
|
|
|
|
|
|
|
|
expect(loadedChan.provider).to eq('dummy')
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be filtered by provider' do
|
2017-07-13 15:50:45 -04:00
|
|
|
channel1 = DiscourseChat::Channel.create!(provider:'dummy')
|
|
|
|
channel2 = DiscourseChat::Channel.create!(provider:'dummy2', data:{val:"blah"})
|
|
|
|
channel3 = DiscourseChat::Channel.create!(provider:'dummy2', data:{val:"blah"})
|
2017-07-13 11:09:34 -04:00
|
|
|
|
|
|
|
expect(DiscourseChat::Channel.all.length).to eq(3)
|
|
|
|
|
2017-07-13 15:50:45 -04:00
|
|
|
expect(DiscourseChat::Channel.with_provider('dummy2').length).to eq(2)
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(DiscourseChat::Channel.with_provider('dummy').length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can find its own rules' do
|
2017-07-13 15:50:45 -04:00
|
|
|
channel = DiscourseChat::Channel.create({provider:'dummy'})
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(channel.rules.size).to eq(0)
|
|
|
|
DiscourseChat::Rule.create(channel: channel)
|
|
|
|
DiscourseChat::Rule.create(channel: channel)
|
|
|
|
expect(channel.rules.size).to eq(2)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
2017-07-13 15:50:45 -04:00
|
|
|
let(:channel) { }
|
2017-07-13 11:09:34 -04:00
|
|
|
|
|
|
|
it 'validates provider correctly' do
|
2017-07-13 15:50:45 -04:00
|
|
|
channel = DiscourseChat::Channel.create!(provider:"dummy")
|
2017-07-13 11:09:34 -04:00
|
|
|
expect(channel.valid?).to eq(true)
|
|
|
|
channel.provider = 'somerandomprovider'
|
|
|
|
expect(channel.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2017-07-13 15:50:45 -04:00
|
|
|
it 'succeeds with valid data' do
|
|
|
|
channel2 = DiscourseChat::Channel.new(provider:"dummy2", data:{val:"hello"})
|
|
|
|
expect(channel2.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disallows invalid data' do
|
|
|
|
channel2 = DiscourseChat::Channel.new(provider:"dummy2", data:{val:''})
|
|
|
|
expect(channel2.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disallows unknown keys' do
|
|
|
|
channel2 = DiscourseChat::Channel.new(provider:"dummy2", data:{val:"hello", unknown:"world"})
|
|
|
|
expect(channel2.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'requires all keys' do
|
|
|
|
channel2 = DiscourseChat::Channel.new(provider:"dummy2", data:{})
|
|
|
|
expect(channel2.valid?).to eq(false)
|
2017-07-13 11:09:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|