Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

6.8 KB
Raw Blame
require 'rails_helper'

RSpec.describe 'Stripe Integration API', type: :request do
  let(:account) { create(:account) }
  let(:admin) { create(:user, account: account, role: :administrator) }
  let(:agent) { create(:user, account: account, role: :agent) }
  let!(:hook) { create(:integrations_hook, app_id: 'stripe', account: account, reference_id: 'acct_sandbox', access_token: 'private-credentials') }
  let(:path) { "/api/v1/accounts/#{account.id}/integrations/stripe" }
  let(:conversation) { create(:conversation, account: account) }
  let(:summary) { instance_double(Integrations::Stripe::CustomerSummary, perform: { customers: [] }) }

  before do
    account.enable_features!('stripe_integration')
    allow(Integrations::Stripe::Oauth).to receive(:configured?).and_return(true)
  end

  it 'returns account details to administrators without credentials' do
    get path, headers: admin.create_new_auth_token
    expect(response).to have_http_status(:ok)
    expect(response.parsed_body).to include('account_id' => 'acct_sandbox', 'mode' => 'sandbox')
    expect(response.body).not_to include('private-credentials', 'access_token', 'refresh_token')
  end

  it 'rejects unauthenticated access' do
    get path
    expect(response).to have_http_status(:unauthorized)
  end

  it 'returns the authorization date rather than hook creation or token update dates' do
    connected_at = 2.days.ago.iso8601
    hook.update!(settings: { 'connected_at' => connected_at })
    get path, headers: admin.create_new_auth_token
    expect(response.parsed_body['connected_at']).to eq(connected_at)
  end

  it 'reports the stored live environment without exposing credentials' do
    hook.update!(settings: { 'livemode' => true })
    get path, headers: admin.create_new_auth_token
    expect(response.parsed_body).to include('mode' => 'live')
    expect(response.body).not_to include('private-credentials', 'access_token', 'refresh_token')
  end

  it 'prevents agents from reading installation details or connecting' do
    headers = agent.create_new_auth_token
    get path, headers: headers
    expect(response).to have_http_status(:unauthorized)
    post "#{path}/auth", headers: headers
    expect(response).to have_http_status(:unauthorized)
  end

  it 'starts OAuth for the current account administrator' do
    url = "https://marketplace.stripe.com/test?state=#{'b' * 64}"
    expect(Integrations::Stripe::Oauth).to receive(:authorize_url).with(account: account, user: admin).and_return(url)
    post "#{path}/auth", headers: admin.create_new_auth_token
    expect(response.parsed_body).to eq('url' => url)
  end

  it 'allows administrators to disconnect only their own hook' do
    other = create(:integrations_hook, app_id: 'stripe')
    delete path, headers: admin.create_new_auth_token
    expect(response).to have_http_status(:no_content)
    expect(Integrations::Hook.exists?(hook.id)).to be false
    expect(Integrations::Hook.exists?(other.id)).to be true
  end

  it 'prevents agents from disconnecting' do
    delete path, headers: agent.create_new_auth_token
    expect(response).to have_http_status(:unauthorized)
    expect(hook.reload).to be_present
  end

  it 'exposes administrator cleanup without enabling billing when the feature is disabled' do
    account.disable_features!('stripe_integration')
    get "/api/v1/accounts/#{account.id}/integrations/apps", headers: admin.create_new_auth_token
    stripe = response.parsed_body.fetch('payload').find { |app| app['id'] == 'stripe' }
    expect(stripe).to include('cleanup_only' => true, 'enabled' => false)
    get "#{path}/customer", params: { conversation_id: conversation.display_id }, headers: admin.create_new_auth_token
    expect(response).to have_http_status(:not_found)
    delete path, headers: agent.create_new_auth_token
    expect(response).to have_http_status(:unauthorized)
    delete path, headers: admin.create_new_auth_token
    expect(response).to have_http_status(:no_content)
    expect(Integrations::Hook.exists?(hook.id)).to be false
  end

  it 'uses the authorized conversation contact rather than a supplied email' do
    expect(Integrations::Stripe::CustomerSummary).to receive(:new)
      .with(connection: instance_of(Integrations::Stripe::Connection), contact: conversation.contact).and_return(summary)
    get "#{path}/customer", params: { conversation_id: conversation.display_id, email: '[email protected]' }, headers: admin.create_new_auth_token
    expect(response).to have_http_status(:ok)
  end

  it 'denies agents without access to the conversation inbox' do
    expect(Integrations::Stripe::CustomerSummary).not_to receive(:new)
    get "#{path}/customer", params: { conversation_id: conversation.display_id }, headers: agent.create_new_auth_token
    expect(response).to have_http_status(:unauthorized)
  end

  it 'allows agents to read billing details for an accessible conversation' do
    create(:inbox_member, inbox: conversation.inbox, user: agent)
    allow(Integrations::Stripe::CustomerSummary).to receive(:new).and_return(summary)
    get "#{path}/customer", params: { conversation_id: conversation.display_id }, headers: agent.create_new_auth_token
    expect(response).to have_http_status(:ok)
  end

  it 'does not look up a conversation in another account' do
    other = create(:conversation)
    get "#{path}/customer", params: { conversation_id: other.display_id }, headers: admin.create_new_auth_token
    expect(response).to have_http_status(:not_found)
  end

  it 'returns a safe error on provider failure' do
    allow(Integrations::Stripe::CustomerSummary).to receive(:new).and_return(summary)
    error = Stripe::InvalidRequestError.new(
      'sensitive provider details', 'query',
      http_status: 400,
      http_headers: { 'request-id' => 'req_stripe_failure' }, code: 'parameter_invalid'
    )
    allow(summary).to receive(:perform).and_raise(error)
    allow(Rails.logger).to receive(:warn)
    get "#{path}/customer", params: { conversation_id: conversation.display_id }, headers: admin.create_new_auth_token
    expect(response).to have_http_status(:unprocessable_entity)
    expect(response.parsed_body).to eq('error' => 'stripe_unavailable')
    expect(Rails.logger).to have_received(:warn).with({
      error: 'Stripe::InvalidRequestError', code: 'parameter_invalid', status: 400, request_id: 'req_stripe_failure'
    }.to_json)
  end

  it 'allows cleanup but not authorization when installation settings are missing' do
    allow(Integrations::Stripe::Oauth).to receive(:configured?).and_return(false)
    get path, headers: admin.create_new_auth_token
    expect(response).to have_http_status(:ok)
    post "#{path}/auth", headers: admin.create_new_auth_token
    expect(response).to have_http_status(:not_found)
    delete path, headers: admin.create_new_auth_token
    expect(response).to have_http_status(:no_content)
    expect(Integrations::Hook.exists?(hook.id)).to be false
  end
end