2020-06-12 17:42:47 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Api::V1::Accounts::Integrations::Slacks', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
2020-06-22 07:49:26 +00:00
|
|
|
let!(:hook) { create(:integrations_hook, account: account) }
|
2020-06-12 17:42:47 +00:00
|
|
|
|
|
|
|
describe 'POST /api/v1/accounts/{account.id}/integrations/slack' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/integrations/slack", params: {}
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'creates hook' do
|
|
|
|
hook_builder = Integrations::Slack::HookBuilder.new(account: account, code: SecureRandom.hex)
|
2020-06-22 07:49:26 +00:00
|
|
|
expect(hook_builder).to receive(:fetch_access_token).and_return(SecureRandom.hex)
|
2020-06-12 17:42:47 +00:00
|
|
|
expect(Integrations::Slack::HookBuilder).to receive(:new).and_return(hook_builder)
|
|
|
|
|
2020-06-22 07:49:26 +00:00
|
|
|
channel_builder = Integrations::Slack::ChannelBuilder.new(hook: hook, channel: 'channel')
|
|
|
|
expect(channel_builder).to receive(:perform)
|
|
|
|
expect(Integrations::Slack::ChannelBuilder).to receive(:new).and_return(channel_builder)
|
|
|
|
|
2020-06-12 17:42:47 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/integrations/slack",
|
|
|
|
params: { code: SecureRandom.hex },
|
|
|
|
headers: agent.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
2020-06-22 07:49:26 +00:00
|
|
|
expect(json_response['id']).to eql('slack')
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-22 07:49:26 +00:00
|
|
|
describe 'PUT /api/v1/accounts/{account.id}/integrations/slack/' do
|
2020-06-12 17:42:47 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-06-22 07:49:26 +00:00
|
|
|
put "/api/v1/accounts/#{account.id}/integrations/slack/", params: {}
|
2020-06-12 17:42:47 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'updates hook' do
|
|
|
|
channel_builder = Integrations::Slack::ChannelBuilder.new(hook: hook, channel: 'channel')
|
2020-06-22 07:49:26 +00:00
|
|
|
expect(channel_builder).to receive(:perform)
|
2020-06-12 17:42:47 +00:00
|
|
|
|
|
|
|
expect(Integrations::Slack::ChannelBuilder).to receive(:new).and_return(channel_builder)
|
|
|
|
|
2020-06-22 07:49:26 +00:00
|
|
|
put "/api/v1/accounts/#{account.id}/integrations/slack",
|
2020-06-12 17:42:47 +00:00
|
|
|
params: { channel: SecureRandom.hex },
|
|
|
|
headers: agent.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
2020-06-22 07:49:26 +00:00
|
|
|
expect(json_response['app_id']).to eql('slack')
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-22 07:49:26 +00:00
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/integrations/slack' do
|
2020-06-12 17:42:47 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-06-22 07:49:26 +00:00
|
|
|
delete "/api/v1/accounts/#{account.id}/integrations/slack", params: {}
|
2020-06-12 17:42:47 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'deletes hook' do
|
2020-06-22 07:49:26 +00:00
|
|
|
delete "/api/v1/accounts/#{account.id}/integrations/slack",
|
2020-06-12 17:42:47 +00:00
|
|
|
headers: agent.create_new_auth_token
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(Integrations::Hook.find_by(id: hook.id)).to be nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|