2018-12-13 09:06:44 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe TwitterSync do
|
Refactoring: Prep refactoring of Twitter webhook processing logic
This commit rescopes test coverage added in 9678b1857
from TwitterSync#process_webhook to Channel::Driver::Twitter#process.
When Zammad processes an incoming webhook event from Twitter, it enters:
* ChannelsTwitterController#webhook_incoming, which thinly wraps
* Channel::Driver::Twitter#process, which thinly wraps
* TwitterSync#process_webhook
The core logic for webhook processing is contained in this last method,
so that's the method that test coverage was written around.
After examining the classes more carefully,
it seems that this logic doesn't quite belong in the TwitterSync class,
which is principally for outbound communication to the Twitter API.
Rather, it is more analogous to Channel::EmailParser#process
(which takes an incoming email and converts it into the appropriate
Zammad assets; i.e., users, tickets, and articles),
and so will be migrated upstream into Channel::Driver::Twitter#process.
This refactoring will be undertaken in subsequent commits.
2020-01-06 06:36:29 +00:00
|
|
|
subject(:twitter_sync) { described_class.new(channel.options[:auth]) }
|
2019-12-18 06:39:45 +00:00
|
|
|
|
|
|
|
let(:channel) { create(:twitter_channel) }
|
|
|
|
|
2018-12-13 09:06:44 +00:00
|
|
|
describe '.preferences_cleanup' do
|
2019-12-11 02:32:45 +00:00
|
|
|
shared_examples 'for normalizing input' do
|
|
|
|
it 'is converted (from bare hash)' do
|
|
|
|
expect(described_class.preferences_cleanup(raw_preferences)).to include(clean_preferences)
|
2018-12-13 09:06:44 +00:00
|
|
|
end
|
|
|
|
|
2019-12-11 02:32:45 +00:00
|
|
|
it 'is converted (from article.preferences hash)' do
|
|
|
|
expect(described_class.preferences_cleanup(twitter: raw_preferences)).to match({ twitter: hash_including(clean_preferences) })
|
2018-12-13 09:06:44 +00:00
|
|
|
end
|
|
|
|
end
|
2019-12-11 02:32:45 +00:00
|
|
|
|
|
|
|
describe ':geo key' do
|
|
|
|
context 'when absent' do
|
|
|
|
let(:raw_preferences) { {} }
|
|
|
|
let(:clean_preferences) { { geo: {} } }
|
|
|
|
|
|
|
|
include_examples 'for normalizing input'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when instance_of(Twitter::NullOjbect)' do
|
|
|
|
let(:raw_preferences) { { geo: Twitter::NullObject.new } }
|
|
|
|
let(:clean_preferences) { { geo: {} } }
|
|
|
|
|
|
|
|
include_examples 'for normalizing input'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when instance_of(Twitter::Geo.new)' do
|
|
|
|
let(:raw_preferences) { { geo: Twitter::Geo.new(coordinates: [1, 1]) } }
|
|
|
|
let(:clean_preferences) { { geo: { coordinates: [1, 1] } } }
|
|
|
|
|
|
|
|
include_examples 'for normalizing input'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ':place key' do
|
|
|
|
context 'when absent' do
|
|
|
|
let(:raw_preferences) { {} }
|
|
|
|
let(:clean_preferences) { { place: {} } }
|
|
|
|
|
|
|
|
include_examples 'for normalizing input'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when instance_of(Twitter::NullOjbect)' do
|
|
|
|
let(:raw_preferences) { { place: Twitter::NullObject.new } }
|
|
|
|
let(:clean_preferences) { { place: {} } }
|
|
|
|
|
|
|
|
include_examples 'for normalizing input'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when instance_of(Twitter::Place.new)' do
|
|
|
|
let(:raw_preferences) { { place: Twitter::Place.new({ country: 'da', name: 'do', woeid: 1, id: 1 }) } }
|
|
|
|
let(:clean_preferences) { { place: { country: 'da', name: 'do', woeid: 1, id: 1 } } }
|
|
|
|
|
|
|
|
include_examples 'for normalizing input'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ':mention_ids key' do
|
|
|
|
let(:raw_preferences) { { mention_ids: [1_234_567_890] } }
|
|
|
|
let(:clean_preferences) { { mention_ids: [1_234_567_890] } }
|
|
|
|
|
|
|
|
include_examples 'for normalizing input'
|
|
|
|
end
|
2018-12-13 09:06:44 +00:00
|
|
|
end
|
|
|
|
end
|