Refactoring: Clean up ticket/article_spec.rb

This commit is contained in:
Ryan Lue 2019-02-05 17:59:13 +08:00 committed by Martin Edenhofer
parent c02d7f2159
commit 768a350160

View file

@ -6,88 +6,114 @@ RSpec.describe Ticket::Article, type: :model do
it_behaves_like 'ApplicationModel' it_behaves_like 'ApplicationModel'
it_behaves_like 'CanBeImported' it_behaves_like 'CanBeImported'
describe '.create' do describe 'Callbacks, Observers, & Async Transactions' do
it 'handles NULL byte in subject or body' do describe 'NULL byte handling (via ChecksAttributeValuesAndLength concern):' do
expect(create(:ticket_article, subject: "com test 1\u0000", body: "some\u0000message 123")) it 'removes them from subject on creation, if necessary (postgres doesnt like them)' do
.to be_persisted expect(create(:ticket_article, subject: "com test 1\u0000"))
.to be_persisted
end
it 'removes them from body on creation, if necessary (postgres doesnt like them)' do
expect(create(:ticket_article, body: "some\u0000message 123"))
.to be_persisted
end
end end
end
describe 'hooks on creation' do describe 'Auto-setting of outgoing Twitter article attributes (via bj jobs):' do
context 'of outgoing article' do subject!(:twitter_article) { create(:twitter_article, sender_name: 'Agent') }
context 'over Twitter' do let(:channel) { Channel.find(twitter_article.ticket.preferences[:channel_id]) }
subject!(:twitter_article) { create(:twitter_article, sender_name: 'Agent') }
let(:channel) { Channel.find(twitter_article.ticket.preferences[:channel_id]) }
describe 'background job actions' do let(:run_bg_jobs) do
let(:run_bg_jobs) do lambda do
lambda do VCR.use_cassette(cassette, match_requests_on: %i[method uri oauth_headers]) do
VCR.use_cassette(cassette, match_requests_on: %i[method uri oauth_headers]) do Scheduler.worker(true)
Scheduler.worker(true) end
end end
end end
let(:cassette) { 'models/channel/driver/twitter/article_to_tweet' }
it 'sets #from to senders Twitter handle' do
expect(&run_bg_jobs)
.to change { twitter_article.reload.from }
.to('@example')
end
it 'sets #to to recipients Twitter handle' do
expect(&run_bg_jobs)
.to change { twitter_article.reload.to }
.to('') # Tweet in VCR cassette is addressed to no one
end
it 'sets #message_id to tweet ID (https://twitter.com/statuses/<id>)' do
expect(&run_bg_jobs)
.to change { twitter_article.reload.message_id }
.to('1069382411899817990')
end
it 'sets #preferences with tweet metadata' do
expect(&run_bg_jobs)
.to change { twitter_article.reload.preferences }
.to(hash_including('twitter', 'links'))
expect(twitter_article.preferences[:links].first)
.to include(
'name' => 'on Twitter',
'target' => '_blank',
'url' => "https://twitter.com/statuses/#{twitter_article.message_id}"
)
end
it 'does not change #cc' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.cc }
end
it 'does not change #subject' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.subject }
end
it 'does not change #content_type' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.content_type }
end
it 'does not change #body' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.body }
end
it 'does not change #sender' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.sender }
end
it 'does not change #type' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.type }
end
it 'sets appropriate status attributes on the tickets channel' do
expect(&run_bg_jobs)
.to change { channel.reload.attributes }
.to hash_including(
'status_in' => nil,
'last_log_in' => nil,
'status_out' => 'ok',
'last_log_out' => ''
)
end
context 'when the original channel (specified in ticket.preferences) was deleted' do
context 'but a new one with the same screen_name exists' do
let(:cassette) { 'models/channel/driver/twitter/article_to_tweet_channel_replace' }
let(:new_channel) { create(:twitter_channel) }
before do
channel.destroy
expect(new_channel.options[:user][:screen_name])
.to eq(channel.options[:user][:screen_name])
end end
let(:cassette) { 'models/channel/driver/twitter/article_to_tweet' } it 'sets appropriate status attributes on the new channel' do
it 'sets #from attribute to senders Twitter handle' do
expect(&run_bg_jobs) expect(&run_bg_jobs)
.to change { twitter_article.reload.from } .to change { new_channel.reload.attributes }
.to('@example')
end
it 'sets #to attribute to recipients Twitter handle' do
expect(&run_bg_jobs)
.to change { twitter_article.reload.to }
.to('') # Tweet in VCR cassette is addressed to no one
end
it 'sets #message_id attribute to tweet ID (https://twitter.com/statuses/<id>)' do
expect(&run_bg_jobs)
.to change { twitter_article.reload.message_id }
.to('1069382411899817990')
end
it 'sets #preferences hash with tweet metadata' do
expect(&run_bg_jobs)
.to change { twitter_article.reload.preferences }
.to(hash_including('twitter', 'links'))
expect(twitter_article.preferences[:links].first)
.to include(
'name' => 'on Twitter',
'target' => '_blank',
'url' => "https://twitter.com/statuses/#{twitter_article.message_id}"
)
end
it 'does not change #cc attribute' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.cc }
end
it 'does not change #subject attribute' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.subject }
end
it 'does not change #content_type attribute' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.content_type }
end
it 'does not change #body attribute' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.body }
end
it 'does not change #sender association' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.sender }
end
it 'does not change #type association' do
expect(&run_bg_jobs).not_to change { twitter_article.reload.type }
end
it 'sets appropriate status attributes on the tickets channel' do
expect(&run_bg_jobs)
.to change { channel.reload.attributes }
.to hash_including( .to hash_including(
'status_in' => nil, 'status_in' => nil,
'last_log_in' => nil, 'last_log_in' => nil,
@ -95,31 +121,6 @@ RSpec.describe Ticket::Article, type: :model do
'last_log_out' => '' 'last_log_out' => ''
) )
end end
context 'when the original channel (specified in ticket.preferences) was deleted' do
context 'but a new one with the same screen_name exists' do
let(:cassette) { 'models/channel/driver/twitter/article_to_tweet_channel_replace' }
let(:new_channel) { create(:twitter_channel) }
before do
channel.destroy
expect(new_channel.options[:user][:screen_name])
.to eq(channel.options[:user][:screen_name])
end
it 'sets appropriate status attributes on the new channel' do
expect(&run_bg_jobs)
.to change { new_channel.reload.attributes }
.to hash_including(
'status_in' => nil,
'last_log_in' => nil,
'status_out' => 'ok',
'last_log_out' => ''
)
end
end
end
end end
end end
end end