2019-10-30 05:19:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
2021-07-31 15:49:42 +00:00
|
|
|
require Rails.root.join 'spec/models/concerns/out_of_offisable_shared.rb'
|
2019-10-30 05:19:23 +00:00
|
|
|
|
|
|
|
RSpec.describe Inbox do
|
|
|
|
describe 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:account_id) }
|
2022-06-30 05:29:37 +00:00
|
|
|
it { is_expected.to validate_presence_of(:name) }
|
2019-10-30 05:19:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to belong_to(:account) }
|
|
|
|
|
|
|
|
it { is_expected.to belong_to(:channel) }
|
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
it { is_expected.to have_many(:contact_inboxes).dependent(:destroy_async) }
|
2019-10-30 05:19:23 +00:00
|
|
|
|
|
|
|
it { is_expected.to have_many(:contacts).through(:contact_inboxes) }
|
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
it { is_expected.to have_many(:inbox_members).dependent(:destroy_async) }
|
2019-10-30 05:19:23 +00:00
|
|
|
|
|
|
|
it { is_expected.to have_many(:members).through(:inbox_members).source(:user) }
|
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
it { is_expected.to have_many(:conversations).dependent(:destroy_async) }
|
2019-10-30 05:19:23 +00:00
|
|
|
|
|
|
|
it { is_expected.to have_many(:messages).through(:conversations) }
|
2020-03-05 20:13:12 +00:00
|
|
|
|
|
|
|
it { is_expected.to have_one(:agent_bot_inbox) }
|
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
it { is_expected.to have_many(:webhooks).dependent(:destroy_async) }
|
2020-03-18 11:23:35 +00:00
|
|
|
|
2022-02-28 12:46:12 +00:00
|
|
|
it { is_expected.to have_many(:reporting_events) }
|
2020-06-12 17:42:47 +00:00
|
|
|
|
|
|
|
it { is_expected.to have_many(:hooks) }
|
2019-10-30 05:19:23 +00:00
|
|
|
end
|
|
|
|
|
2020-10-31 18:44:33 +00:00
|
|
|
describe 'concerns' do
|
|
|
|
it_behaves_like 'out_of_offisable'
|
|
|
|
end
|
|
|
|
|
2019-10-30 05:19:23 +00:00
|
|
|
describe '#add_member' do
|
|
|
|
let(:inbox) { FactoryBot.create(:inbox) }
|
|
|
|
let(:user) { FactoryBot.create(:user) }
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(inbox.inbox_members.size).to eq(0)
|
|
|
|
|
|
|
|
inbox.add_member(user.id)
|
|
|
|
expect(inbox.reload.inbox_members.size).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#remove_member' do
|
|
|
|
let(:inbox) { FactoryBot.create(:inbox) }
|
|
|
|
let(:user) { FactoryBot.create(:user) }
|
|
|
|
|
|
|
|
before { inbox.add_member(user.id) }
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(inbox.inbox_members.size).to eq(1)
|
|
|
|
|
|
|
|
inbox.remove_member(user.id)
|
|
|
|
expect(inbox.reload.inbox_members.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#facebook?' do
|
|
|
|
let(:inbox) do
|
|
|
|
FactoryBot.build(:inbox, channel: channel_val)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the channel type is Channel::FacebookPage' do
|
|
|
|
let(:channel_val) { Channel::FacebookPage.new }
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(inbox.facebook?).to eq(true)
|
2020-09-04 13:43:47 +00:00
|
|
|
expect(inbox.inbox_type).to eq('Facebook')
|
2019-10-30 05:19:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the channel type is not Channel::FacebookPage' do
|
|
|
|
let(:channel_val) { Channel::WebWidget.new }
|
|
|
|
|
2020-09-04 13:43:47 +00:00
|
|
|
it do
|
|
|
|
expect(inbox.facebook?).to eq(false)
|
|
|
|
expect(inbox.inbox_type).to eq('Website')
|
|
|
|
end
|
2019-10-30 05:19:23 +00:00
|
|
|
end
|
|
|
|
end
|
2021-08-27 14:26:45 +00:00
|
|
|
|
|
|
|
describe '#web_widget?' do
|
|
|
|
let(:inbox) do
|
|
|
|
FactoryBot.build(:inbox, channel: channel_val)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the channel type is Channel::WebWidget' do
|
|
|
|
let(:channel_val) { Channel::WebWidget.new }
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(inbox.web_widget?).to eq(true)
|
|
|
|
expect(inbox.inbox_type).to eq('Website')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the channel is not Channel::WebWidget' do
|
|
|
|
let(:channel_val) { Channel::Api.new }
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(inbox.web_widget?).to eq(false)
|
|
|
|
expect(inbox.inbox_type).to eq('API')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#api?' do
|
|
|
|
let(:inbox) do
|
|
|
|
FactoryBot.build(:inbox, channel: channel_val)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the channel type is Channel::Api' do
|
|
|
|
let(:channel_val) { Channel::Api.new }
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(inbox.api?).to eq(true)
|
|
|
|
expect(inbox.inbox_type).to eq('API')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the channel is not Channel::Api' do
|
|
|
|
let(:channel_val) { Channel::FacebookPage.new }
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(inbox.api?).to eq(false)
|
|
|
|
expect(inbox.inbox_type).to eq('Facebook')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-06-30 05:29:37 +00:00
|
|
|
|
|
|
|
describe '#validations' do
|
|
|
|
let(:inbox) { FactoryBot.create(:inbox) }
|
|
|
|
|
|
|
|
context 'when validating inbox name' do
|
|
|
|
it 'does not allow any special character at the end' do
|
|
|
|
inbox.name = 'this is my inbox name-'
|
|
|
|
expect(inbox).not_to be_valid
|
|
|
|
expect(inbox.errors.full_messages).to eq(
|
|
|
|
['Name should not start or end with symbols, and it should not have < > / \\ @ characters.']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow any special character at the start' do
|
|
|
|
inbox.name = '-this is my inbox name'
|
|
|
|
expect(inbox).not_to be_valid
|
|
|
|
expect(inbox.errors.full_messages).to eq(
|
|
|
|
['Name should not start or end with symbols, and it should not have < > / \\ @ characters.']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow chacters like /\@<> in the entire string' do
|
|
|
|
inbox.name = 'inbox@name'
|
|
|
|
expect(inbox).not_to be_valid
|
|
|
|
expect(inbox.errors.full_messages).to eq(
|
|
|
|
['Name should not start or end with symbols, and it should not have < > / \\ @ characters.']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not empty string' do
|
|
|
|
inbox.name = ''
|
|
|
|
expect(inbox).not_to be_valid
|
|
|
|
expect(inbox.errors.full_messages[0]).to eq(
|
|
|
|
"Name can't be blank"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does allow special characters except /\@<> in between' do
|
|
|
|
inbox.name = 'inbox-name'
|
|
|
|
expect(inbox).to be_valid
|
|
|
|
|
|
|
|
inbox.name = 'inbox_name.and_1'
|
|
|
|
expect(inbox).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when special characters allowed for some channel' do
|
|
|
|
let!(:tw_channel_val) { FactoryBot.create(:channel_twitter_profile) }
|
|
|
|
let(:inbox) { create(:inbox, channel: tw_channel_val) }
|
|
|
|
|
|
|
|
it 'does allow special chacters like /\@<> for Facebook Channel' do
|
|
|
|
inbox.name = 'inbox@name'
|
|
|
|
expect(inbox).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-30 05:19:23 +00:00
|
|
|
end
|