Testing: Migrate Channel specs to channel-driver spec file

This commit is contained in:
Ryan Lue 2020-02-25 16:44:32 +08:00 committed by Thorsten Eckel
parent a6ee25e15e
commit 71dad38129
10 changed files with 5440 additions and 6747 deletions

View file

@ -39,7 +39,6 @@ FactoryBot.define do
}, },
sync: { sync: {
webhook_id: '', webhook_id: '',
track_retweets: true,
mentions: { mentions: {
group_id: Group.first.id group_id: Group.first.id
}, },

View file

@ -772,4 +772,101 @@ RSpec.describe Channel::Driver::Twitter do
include_examples 'for #send' include_examples 'for #send'
end end
end end
describe '#fetch', :use_vcr do
describe 'Twitter API authentication' do
let(:consumer_credentials) do
{
consumer_key: external_credential.credentials[:consumer_key],
consumer_secret: external_credential.credentials[:consumer_secret],
}
end
let(:oauth_credentials) do
{
access_token: channel.options[:auth][:oauth_token],
access_token_secret: channel.options[:auth][:oauth_token_secret],
}
end
it 'uses consumer key/secret stored on ExternalCredential' do
expect(Twitter::REST::Client)
.to receive(:new).with(hash_including(consumer_credentials))
.and_call_original
channel.fetch
end
it 'uses OAuth token/secret stored on #options hash' do
expect(Twitter::REST::Client)
.to receive(:new).with(hash_including(oauth_credentials))
.and_call_original
channel.fetch
end
end
describe 'Twitter API activity' do
# travel back in time when VCR was recorded
before { travel_to '2020-02-06 13:37 +0100' }
it 'sets successful status attributes' do
expect { channel.fetch(true) }
.to change { channel.reload.attributes }
.to hash_including(
'status_in' => 'ok',
'last_log_in' => '',
'status_out' => nil,
'last_log_out' => nil
)
end
it 'adds tickets based on .options[:sync][:search] parameters' do
expect { channel.fetch(true) }
.to change(Ticket, :count).by(8)
expect(Ticket.last.attributes).to include(
'title' => "Come and join our team to bring Zammad even further forward! It's gonna be ama...",
'preferences' => { 'channel_id' => channel.id,
'channel_screen_name' => channel.options[:user][:screen_name] },
'customer_id' => User.find_by(firstname: 'Mr.Generation', lastname: '').id
)
end
it 'skips tweets more than 15 days older than channel itself'
context 'and "track_retweets" option' do
subject(:channel) { create(:twitter_channel, custom_options: { sync: { track_retweets: true } }) }
it 'adds tickets based on .options[:sync][:search] parameters' do
expect { channel.fetch(true) }
.to change(Ticket, :count).by(21)
expect(Ticket.last.attributes).to include(
'title' => 'RT @BarackObama: Kobe was a legend on the court and just getting started in what...',
'preferences' => { 'channel_id' => channel.id,
'channel_screen_name' => channel.options[:user][:screen_name] },
'customer_id' => User.find_by(firstname: 'Zammad', lastname: 'Ali').id
)
end
end
context 'and legacy "import_older_tweets" option' do
subject(:channel) { create(:twitter_channel, :legacy) }
it 'adds tickets based on .options[:sync][:search] parameters' do
expect { channel.fetch(true) }
.to change(Ticket, :count).by(21)
expect(Ticket.last.attributes).to include(
'title' => 'Wir haben unsere DMs deaktiviert. ' \
'Leider können wir dank der neuen Twitter API k...',
'preferences' => { 'channel_id' => channel.id,
'channel_screen_name' => channel.options[:user][:screen_name] },
'customer_id' => User.find_by(firstname: 'Ccc', lastname: 'Event Logistics').id
)
end
end
end
end
end end

View file

@ -1,78 +0,0 @@
require 'rails_helper'
RSpec.describe Channel do
describe '#fetch', use_vcr: :with_oauth_headers do
context 'for Twitter driver' do
context 'with valid token' do
subject(:twitter_channel) { create(:twitter_channel) }
# travel back in time when VCR was recorded
before { travel_to '2020-02-06 13:37 +0100' }
it 'returns true' do
expect(twitter_channel.fetch(true)).to be(true)
end
it 'sets successful status attributes' do
expect { twitter_channel.fetch(true) }
.to change { twitter_channel.reload.attributes }
.to hash_including(
'status_in' => 'ok',
'last_log_in' => '',
'status_out' => nil,
'last_log_out' => nil
)
end
it 'adds tickets based on config parameters (mention/DM/search)' do
expect { twitter_channel.fetch(true) }
.to change(Ticket, :count).by(21)
expect(Ticket.last.attributes).to include(
'title' => 'RT @BarackObama: Kobe was a legend on the court and just getting started in what...',
'preferences' => { 'channel_id' => twitter_channel.id,
'channel_screen_name' => twitter_channel.options[:user][:screen_name] },
'customer_id' => User.find_by(firstname: 'Zammad', lastname: 'Ali').id
)
end
context 'and legacy "import_older_tweets" option' do
subject(:twitter_channel) { create(:twitter_channel, :legacy) }
it 'adds tickets based on config parameters (mention/DM/search)' do
expect { twitter_channel.fetch(true) }
.to change(Ticket, :count).by(26)
expect(Ticket.last.attributes).to include(
'title' => 'Wir haben unsere DMs deaktiviert. ' \
'Leider können wir dank der neuen Twitter API k...',
'preferences' => { 'channel_id' => twitter_channel.id,
'channel_screen_name' => twitter_channel.options[:user][:screen_name] },
'customer_id' => User.find_by(firstname: 'Ccc', lastname: 'Event Logistics').id
)
end
end
end
context 'with invalid token' do
subject(:twitter_channel) { create(:twitter_channel, :invalid) }
it 'returns false' do
expect(twitter_channel.fetch(true)).to be(false)
end
it 'sets error/nil status attributes' do
expect { twitter_channel.fetch(true) }
.to change { twitter_channel.reload.attributes }
.to hash_including(
'status_in' => 'error',
'last_log_in' => "Can't use Channel::Driver::Twitter: " \
'#<Twitter::Error::Unauthorized: Invalid or expired token.>',
'status_out' => nil,
'last_log_out' => nil
)
end
end
end
end
end

View file

@ -1,51 +0,0 @@
---
http_interactions:
- request:
method: get
uri: https://api.twitter.com/1.1/search/tweets.json?count=100&q=zammad&result_type=mixed
body:
encoding: UTF-8
string: ''
headers:
User-Agent:
- TwitterRubyGem/6.2.0
Authorization:
- OAuth oauth_consumer_key="q7K8GEkhyCHs9jHLtkmD9Kod4", oauth_nonce="b5b77e1667355db2efc64e178b8a0aaa",
oauth_signature="tybPhlz3I5fMRF5%2BE12Pwx3U5XM%3D", oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1543796201", oauth_token="7783712304-H9s75r2d532diPmJYK6JrvUWxu9gTDZ6ocjfToL", oauth_version="1.0"
Connection:
- close
Host:
- api.twitter.com
response:
status:
code: 401
message: Unauthorized
headers:
Connection:
- close
Content-Length:
- '62'
Content-Type:
- application/json; charset=utf-8
Date:
- Mon, 03 Dec 2018 00:16:41 GMT
Server:
- tsa_o
Set-Cookie:
- guest_id=v1%3A154379620109191613; Expires=Wed, 02 Dec 2020 00:16:41 GMT; Path=/;
Domain=.twitter.com
- personalization_id="v1_i2UDOt8QXhYvnNAv90Q8jA=="; Expires=Wed, 02 Dec 2020
00:16:41 GMT; Path=/; Domain=.twitter.com
Strict-Transport-Security:
- max-age=631138519
X-Connection-Hash:
- 8af740bd8d5f98022086657c7172b7ee
X-Response-Time:
- '114'
body:
encoding: UTF-8
string: '{"errors":[{"code":89,"message":"Invalid or expired token."}]}'
http_version:
recorded_at: Mon, 03 Dec 2018 00:16:41 GMT
recorded_with: VCR 4.0.0

View file

@ -1,51 +0,0 @@
---
http_interactions:
- request:
method: get
uri: https://api.twitter.com/1.1/search/tweets.json?count=100&q=zammad&result_type=mixed
body:
encoding: UTF-8
string: ''
headers:
User-Agent:
- TwitterRubyGem/6.2.0
Authorization:
- OAuth oauth_consumer_key="q7K8GEkhyCHs9jHLtkmD9Kod4", oauth_nonce="b5b77e1667355db2efc64e178b8a0aaa",
oauth_signature="tybPhlz3I5fMRF5%2BE12Pwx3U5XM%3D", oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1543796201", oauth_token="7783712304-H9s75r2d532diPmJYK6JrvUWxu9gTDZ6ocjfToL", oauth_version="1.0"
Connection:
- close
Host:
- api.twitter.com
response:
status:
code: 401
message: Unauthorized
headers:
Connection:
- close
Content-Length:
- '62'
Content-Type:
- application/json; charset=utf-8
Date:
- Mon, 03 Dec 2018 00:16:41 GMT
Server:
- tsa_o
Set-Cookie:
- guest_id=v1%3A154379620109191613; Expires=Wed, 02 Dec 2020 00:16:41 GMT; Path=/;
Domain=.twitter.com
- personalization_id="v1_i2UDOt8QXhYvnNAv90Q8jA=="; Expires=Wed, 02 Dec 2020
00:16:41 GMT; Path=/; Domain=.twitter.com
Strict-Transport-Security:
- max-age=631138519
X-Connection-Hash:
- 8af740bd8d5f98022086657c7172b7ee
X-Response-Time:
- '114'
body:
encoding: UTF-8
string: '{"errors":[{"code":89,"message":"Invalid or expired token."}]}'
http_version:
recorded_at: Mon, 03 Dec 2018 00:16:41 GMT
recorded_with: VCR 4.0.0

File diff suppressed because one or more lines are too long