2021-12-27 09:53:59 -08:00
# frozen_string_literal: true
2022-06-21 11:04:06 -07:00
require " rails_helper "
2021-12-27 09:53:59 -08:00
describe ShareWorker do
let ( :user ) { FactoryBot . create ( :user ) }
let ( :answer ) { FactoryBot . create ( :answer , user : user ) }
2022-06-21 11:04:06 -07:00
let! ( :service ) do
Services :: Twitter . create! ( type : " Services::Twitter " ,
user : user )
end
2021-12-27 09:53:59 -08:00
2021-12-27 18:03:59 -08:00
before do
stub_const ( " APP_CONFIG " , {
2022-06-21 11:04:06 -07:00
" hostname " = > " example.com " ,
" anonymous_name " = > " Anonymous " ,
" https " = > true ,
" items_per_page " = > 5 ,
" sharing " = > {
" twitter " = > {
" consumer_key " = > " "
}
}
} )
2021-12-27 18:03:59 -08:00
end
2021-12-27 09:53:59 -08:00
describe " # perform " do
2022-06-21 11:02:49 -07:00
before do
2022-06-21 11:04:06 -07:00
allow ( Sidekiq . logger ) . to receive ( :info )
2022-06-21 11:02:49 -07:00
end
subject do
2021-12-27 14:02:32 -08:00
Sidekiq :: Testing . fake! do
2022-06-21 11:04:06 -07:00
ShareWorker . perform_async ( user . id , answer . id , " twitter " )
2021-12-27 14:02:32 -08:00
end
2022-06-21 11:02:49 -07:00
end
2021-12-27 09:53:59 -08:00
2022-06-21 11:04:06 -07:00
context " when answer doesn't exist " do
it " prevents the job from retrying and logs a message " do
2021-12-27 09:53:59 -08:00
answer . destroy!
2021-12-27 14:02:32 -08:00
expect { subject } . to change ( ShareWorker . jobs , :size ) . by ( 1 )
expect { ShareWorker . drain } . to change ( ShareWorker . jobs , :size ) . by ( - 1 )
2022-06-21 11:02:49 -07:00
expect ( Sidekiq . logger ) . to have_received ( :info ) . with ( " Tried to post answer # #{ answer . id } for user # #{ user . id } to Twitter but the user/answer/service did not exist (likely deleted), will not retry. " )
2021-12-27 14:02:32 -08:00
end
end
2022-06-21 11:04:06 -07:00
context " when answer exists " do
it " handles Twitter::Error::DuplicateStatus " do
2021-12-27 18:59:45 -08:00
allow_any_instance_of ( Services :: Twitter ) . to receive ( :post ) . with ( answer ) . and_raise ( Twitter :: Error :: DuplicateStatus )
subject
ShareWorker . drain
2022-06-21 11:02:49 -07:00
expect ( Sidekiq . logger ) . to have_received ( :info ) . with ( " Tried to post answer # #{ answer . id } from user # #{ user . id } to Twitter but the status was already posted. " )
2021-12-27 18:59:45 -08:00
end
2022-06-21 11:04:06 -07:00
it " handles Twitter::Error::Unauthorized " do
2021-12-27 18:59:45 -08:00
allow_any_instance_of ( Services :: Twitter ) . to receive ( :post ) . with ( answer ) . and_raise ( Twitter :: Error :: Unauthorized )
subject
ShareWorker . drain
2023-01-02 00:36:28 -08:00
expect ( Sidekiq . logger ) . to have_received ( :info ) . with ( " Tried to post answer # #{ answer . id } from user # #{ user . id } to Twitter but the token has expired or been revoked. " )
2021-12-27 18:59:45 -08:00
end
2023-01-01 14:46:10 -08:00
it " revokes the service connection when Twitter::Error::Unauthorized is raised " do
allow_any_instance_of ( Services :: Twitter ) . to receive ( :post ) . with ( answer ) . and_raise ( Twitter :: Error :: Unauthorized )
subject
expect { ShareWorker . drain } . to change { Services :: Twitter . count } . by ( - 1 )
end
it " sends the user a notification when Twitter::Error::Unauthorized is raised " do
allow_any_instance_of ( Services :: Twitter ) . to receive ( :post ) . with ( answer ) . and_raise ( Twitter :: Error :: Unauthorized )
subject
expect { ShareWorker . drain } . to change { Notification :: ServiceTokenExpired . count } . by ( 1 )
end
2022-12-27 08:53:02 -08:00
it " handles Twitter::Error::Forbidden " do
allow_any_instance_of ( Services :: Twitter ) . to receive ( :post ) . with ( answer ) . and_raise ( Twitter :: Error :: Forbidden )
subject
ShareWorker . drain
expect ( Sidekiq . logger ) . to have_received ( :info ) . with ( " Tried to post answer # #{ answer . id } from user # #{ user . id } to Twitter but the account is suspended. " )
end
2022-06-21 11:04:06 -07:00
it " retries on unhandled exceptions " do
2021-12-27 14:02:32 -08:00
expect { subject } . to change ( ShareWorker . jobs , :size ) . by ( 1 )
2021-12-27 18:03:59 -08:00
expect { ShareWorker . drain } . to raise_error ( Twitter :: Error :: BadRequest )
2022-06-21 11:02:49 -07:00
expect ( Sidekiq . logger ) . to have_received ( :info )
2021-12-27 09:53:59 -08:00
end
end
end
end