2020-02-11 08:57:38 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Twitter::CallbacksController', type: :request do
|
|
|
|
let(:twitter_client) { instance_double(::Twitty::Facade) }
|
|
|
|
let(:twitter_response) { instance_double(::Twitty::Response, status: '200', body: { message: 'Valid' }) }
|
|
|
|
let(:raw_response) do
|
|
|
|
object_double('raw_response', body: 'oauth_token=1&oauth_token_secret=1&user_id=100&screen_name=chatwoot')
|
|
|
|
end
|
|
|
|
let(:account) { create(:account) }
|
2020-05-14 17:21:07 +00:00
|
|
|
let(:webhook_service) { double }
|
2020-02-11 08:57:38 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(::Twitty::Facade).to receive(:new).and_return(twitter_client)
|
|
|
|
allow(::Redis::Alfred).to receive(:get).and_return(account.id)
|
|
|
|
allow(::Redis::Alfred).to receive(:delete).and_return('OK')
|
|
|
|
allow(twitter_client).to receive(:access_token).and_return(twitter_response)
|
|
|
|
allow(twitter_response).to receive(:raw_response).and_return(raw_response)
|
2020-05-14 17:21:07 +00:00
|
|
|
allow(::Twitter::WebhookSubscribeService).to receive(:new).and_return(webhook_service)
|
2020-02-11 08:57:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /twitter/callback' do
|
2020-05-14 17:21:07 +00:00
|
|
|
it 'creates inboxes if subscription is successful' do
|
|
|
|
allow(webhook_service).to receive(:perform).and_return true
|
2020-02-11 08:57:38 +00:00
|
|
|
get twitter_callback_url
|
|
|
|
account.reload
|
2020-03-28 06:21:42 +00:00
|
|
|
expect(response).to redirect_to app_twitter_inbox_agents_url(account_id: account.id, inbox_id: account.inboxes.last.id)
|
2020-02-11 08:57:38 +00:00
|
|
|
expect(account.inboxes.count).to be 1
|
|
|
|
expect(account.twitter_profiles.last.inbox.name).to eq 'chatwoot'
|
|
|
|
expect(account.twitter_profiles.last.profile_id).to eq '100'
|
|
|
|
end
|
2020-05-14 17:21:07 +00:00
|
|
|
|
|
|
|
it 'does not create inbox if subscription fails' do
|
|
|
|
allow(webhook_service).to receive(:perform).and_raise StandardError
|
|
|
|
get twitter_callback_url
|
|
|
|
account.reload
|
|
|
|
expect(response).to redirect_to app_new_twitter_inbox_url(account_id: account.id)
|
|
|
|
expect(account.inboxes.count).to be 0
|
|
|
|
end
|
2020-02-11 08:57:38 +00:00
|
|
|
end
|
|
|
|
end
|