Skip to content

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

24.5 KB
Raw Blame
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Account, type: :model do
  include ActiveJob::TestHelper

  describe 'associations' do
    it { is_expected.to have_many(:sla_policies).dependent(:destroy_async) }
    it { is_expected.to have_many(:applied_slas).dependent(:destroy_async) }
    it { is_expected.to have_many(:custom_roles).dependent(:destroy_async) }
  end

  describe '#selected_feature_flags=' do
    it 'keeps advanced assignment enabled when assignment v2 is selected for a business account' do
      account = build(:account, custom_attributes: { 'plan_name' => 'Business' })

      account.selected_feature_flags = [:feature_assignment_v2]

      expect(account).to be_feature_assignment_v2
      expect(account).to be_feature_advanced_assignment
    end

    it 'disables advanced assignment when assignment v2 is not selected' do
      account = build(:account, custom_attributes: { 'plan_name' => 'Business' })
      account.enable_features(:assignment_v2, :advanced_assignment)

      account.selected_feature_flags = []

      expect(account).not_to be_feature_assignment_v2
      expect(account).not_to be_feature_advanced_assignment
    end

    it 'uses Shopify plan features when preserving advanced assignment' do
      create(
        :installation_config,
        name: 'CHATWOOT_SHOPIFY_PLANS',
        value: [
          {
            'name' => 'Shopify Pro',
            'handle' => 'shopify-pro',
            'features' => %w[advanced_assignment],
            'limits' => { 'agents' => 10, 'inboxes' => 20 }
          }
        ],
        locked: true
      )
      account = build(
        :account,
        internal_attributes: { 'billing_provider' => 'shopify' },
        custom_attributes: { 'plan_name' => 'Shopify Pro' }
      )

      account.selected_feature_flags = [:feature_assignment_v2]

      expect(account).to be_feature_assignment_v2
      expect(account).to be_feature_advanced_assignment
    end
  end

  describe '#auto_translate_enabled?' do
    it 'needs both the auto translate flag and Captain tasks' do
      account = create(:account, settings: { auto_translate: true })
      account.enable_features('auto_translate')
      account.disable_features('captain_tasks')

      expect(account).not_to be_auto_translate_enabled
      account.enable_features('captain_tasks')
      expect(account).to be_auto_translate_enabled
    end

    it 'stays off until the account switches it on' do
      account = create(:account)
      account.enable_features('auto_translate', 'captain_tasks')

      expect(account).not_to be_auto_translate_enabled
    end
  end

  describe '#api_and_webhooks_enabled?' do
    let(:account) { create(:account) }

    it 'is always enabled for self-hosted enterprise accounts' do
      allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(false)
      account.disable_features!('api_and_webhooks')

      expect(account.api_and_webhooks_enabled?).to be true
    end

    it 'uses the account feature flag on Chatwoot Cloud' do
      allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
      account.disable_features!('api_and_webhooks')

      expect(account.api_and_webhooks_enabled?).to be false

      account.enable_features!('api_and_webhooks')

      expect(account.api_and_webhooks_enabled?).to be true
    end
  end

  describe 'billing identity' do
    it 'defaults existing accounts to Stripe and Chatwoot signup' do
      account = create(:account)

      expect(account.billing_provider).to eq('stripe')
      expect(account.signup_source).to eq('chatwoot')
    end

    it 'stores Shopify identity at account creation' do
      account = create(
        :account,
        internal_attributes: {
          'billing_provider' => 'shopify',
          'signup_source' => 'shopify'
        }
      )

      expect(account.billing_provider).to eq('shopify')
      expect(account.signup_source).to eq('shopify')
    end

    it 'rejects unsupported billing providers and signup sources' do
      account = build(:account)
      account.billing_provider = 'unsupported'
      account.signup_source = 'unsupported'

      expect(account).not_to be_valid
      expect(account.errors[:billing_provider]).to be_present
      expect(account.errors[:signup_source]).to be_present
    end

    it 'does not allow billing identity to change after account creation' do
      account = create(:account)

      account.billing_provider = 'shopify'
      account.signup_source = 'shopify'

      expect(account.save).to be(false)
      expect(account.errors[:billing_provider]).to include('cannot be changed after account creation')
      expect(account.errors[:signup_source]).to include('cannot be changed after account creation')
      expect(account.reload.billing_provider).to eq('stripe')
      expect(account.signup_source).to eq('chatwoot')
    end

    it 'does not allow billing identity to change through symbol-keyed internal attributes' do
      account = create(:account)

      account.internal_attributes = account.internal_attributes.merge(billing_provider: 'shopify', signup_source: 'shopify')

      expect(account.save).to be(false)
      expect(account.errors[:billing_provider]).to include('cannot be changed after account creation')
      expect(account.errors[:signup_source]).to include('cannot be changed after account creation')
      expect(account.reload.billing_provider).to eq('stripe')
      expect(account.signup_source).to eq('chatwoot')
    end
  end

  describe 'sla_policies' do
    let!(:account) { create(:account) }
    let!(:sla_policy) { create(:sla_policy, account: account) }

    it 'returns associated sla policies' do
      expect(account.sla_policies).to eq([sla_policy])
    end

    it 'deletes associated sla policies' do
      perform_enqueued_jobs do
        account.destroy!
      end
      expect { sla_policy.reload }.to raise_error(ActiveRecord::RecordNotFound)
    end
  end

  context 'with usage_limits' do
    let(:captain_limits) do
      {
        :startups => { :documents => 100, :responses => 100 },
        :business => { :documents => 200, :responses => 300 },
        :enterprise => { :documents => 300, :responses => 500 }
      }.with_indifferent_access
    end
    let(:account) { create(:account, { custom_attributes: { plan_name: 'startups' } }) }
    let(:assistant) { create(:captain_assistant, account: account) }

    before do
      allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
      create(:installation_config, name: 'ACCOUNT_AGENTS_LIMIT', value: 20)
    end

    describe 'when captain limits are configured' do
      before do
        create_list(:captain_document, 3, account: account, assistant: assistant, status: :available)
        create(:installation_config, name: 'CAPTAIN_CLOUD_PLAN_LIMITS', value: captain_limits.to_json)
      end

      ## Document
      it 'updates document count accurately' do
        account.update_document_usage
        expect(account.custom_attributes['captain_documents_usage']).to eq(3)
      end

      it 'handles zero documents' do
        account.captain_documents.destroy_all
        account.update_document_usage
        expect(account.custom_attributes['captain_documents_usage']).to eq(0)
      end

      it 'reflects document limits' do
        document_limits = account.usage_limits[:captain][:documents]

        expect(document_limits[:consumed]).to eq 3
        expect(document_limits[:current_available]).to eq captain_limits[:startups][:documents] - 3
      end

      ## Responses
      it 'incrementing responses updates usage_limits' do
        account.increment_response_usage

        responses_limits = account.usage_limits[:captain][:responses]

        expect(account.custom_attributes['captain_responses_usage']).to eq 1
        expect(responses_limits[:consumed]).to eq 1
        expect(responses_limits[:current_available]).to eq captain_limits[:startups][:responses] - 1
      end

      it 'reseting responses limits updates usage_limits' do
        account.custom_attributes['captain_responses_usage'] = 30
        account.save!

        responses_limits = account.usage_limits[:captain][:responses]

        expect(responses_limits[:consumed]).to eq 30
        expect(responses_limits[:current_available]).to eq captain_limits[:startups][:responses] - 30

        account.reset_response_usage
        responses_limits = account.usage_limits[:captain][:responses]

        expect(account.custom_attributes['captain_responses_usage']).to eq 0
        expect(responses_limits[:consumed]).to eq 0
        expect(responses_limits[:current_available]).to eq captain_limits[:startups][:responses]
      end

      it 'returns monthly limit accurately' do
        %w[startups business enterprise].each do |plan|
          account.custom_attributes = { 'plan_name': plan }
          account.save!
          expect(account.captain_monthly_limit).to eq captain_limits[plan]
        end
      end

      it 'current_available is never out of bounds' do
        account.custom_attributes['captain_responses_usage'] = 3000
        account.save!

        responses_limits = account.usage_limits[:captain][:responses]
        expect(responses_limits[:consumed]).to eq 3000
        expect(responses_limits[:current_available]).to eq 0

        account.custom_attributes['captain_responses_usage'] = -100
        account.save!

        responses_limits = account.usage_limits[:captain][:responses]
        expect(responses_limits[:consumed]).to eq 0
        expect(responses_limits[:current_available]).to eq captain_limits[:startups][:responses]
      end
    end

    describe 'when captain limits are not configured' do
      it 'returns default values' do
        account.custom_attributes = { 'plan_name': 'unknown' }
        expect(account.captain_monthly_limit).to eq(
          { documents: ChatwootApp.max_limit, responses: ChatwootApp.max_limit }.with_indifferent_access
        )
      end
    end

    describe 'when limits are configured for an account' do
      before do
        create(:installation_config, name: 'CAPTAIN_CLOUD_PLAN_LIMITS', value: captain_limits.to_json)
        account.update(limits: { captain_documents: 5555, captain_responses: 9999 })
      end

      it 'returns limits based on custom attributes' do
        usage_limits = account.usage_limits
        expect(usage_limits[:captain][:documents][:total_count]).to eq(5555)
        expect(usage_limits[:captain][:responses][:total_count]).to eq(9999)
      end
    end

    describe 'audit logs' do
      it 'returns audit logs' do
        # checking whether associated_audits method is present
        expect(account.associated_audits.present?).to be false
      end

      it 'creates audit logs when account is updated' do
        account.update(name: 'New Name')
        expect(Audited::Audit.where(auditable_type: 'Account', action: 'update').count).to eq 1
      end
    end

    it 'returns max limits from global config when enterprise version' do
      expect(account.usage_limits[:agents]).to eq(20)
    end

    it 'returns max limits from account when enterprise version' do
      account.update(limits: { agents: 10 })
      expect(account.usage_limits[:agents]).to eq(10)
    end

    it 'returns limits based on subscription' do
      account.update(limits: { agents: 10 }, custom_attributes: { subscribed_quantity: 5 })
      expect(account.usage_limits[:agents]).to eq(5)
    end

    it 'returns max limits from global config if account limit is absent' do
      account.update(limits: { agents: '' })
      expect(account.usage_limits[:agents]).to eq(20)
    end

    it 'returns max limits from app limit if account limit and installation config is absent' do
      account.update(limits: { agents: '' })
      InstallationConfig.where(name: 'ACCOUNT_AGENTS_LIMIT').update(value: '')

      expect(account.usage_limits[:agents]).to eq(ChatwootApp.max_limit)
    end
  end

  describe 'Captain response usage alerts' do
    let(:mailer) { double }
    let(:message_delivery) { instance_double(ActionMailer::MessageDelivery, deliver_later: true) }
    let(:account) do
      create(
        :account,
        limits: { 'captain_responses' => 300 },
        custom_attributes: { 'captain_responses_usage' => 239 }
      )
    end

    before do
      allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
      allow(ActiveRecord).to receive(:after_all_transactions_commit).and_yield
      account.enable_features!('captain_integration')
      allow(AdministratorNotifications::CaptainNotificationMailer).to receive(:with).with(account: account).and_return(mailer)
      allow(mailer).to receive(:response_usage_alert).and_return(message_delivery)
    end

    it 'queues one alert after usage exceeds 80 percent of the persisted account limit' do
      account.increment_response_usage
      expect(message_delivery).not_to have_received(:deliver_later)

      account.increment_response_usage
      3.times { account.increment_response_usage }

      expect(mailer).to have_received(:response_usage_alert).with(consumed: 241, remaining: 59).once
      expect(message_delivery).to have_received(:deliver_later).once
      expect(account.reload.custom_attributes).to include(
        'captain_responses_usage' => 244,
        'captain_responses_usage_alerted' => true
      )
    end

    it 'does not queue an alert when Captain is disabled' do
      account.disable_features!('captain_integration')
      account.update!(custom_attributes: account.custom_attributes.merge('captain_responses_usage' => 240))

      account.increment_response_usage

      expect(message_delivery).not_to have_received(:deliver_later)
    end

    it 'does not count or queue response usage outside Chatwoot Cloud' do
      allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(false)

      account.increment_response_usage

      expect(account.reload.custom_attributes['captain_responses_usage']).to eq(239)
      expect(message_delivery).not_to have_received(:deliver_later)
    end

    it 'does not queue an alert for a zero response limit' do
      account.update!(limits: { 'captain_responses' => 0 })

      account.increment_response_usage

      expect(message_delivery).not_to have_received(:deliver_later)
    end

    it 'waits for the surrounding transaction to commit before queueing the alert' do
      after_commit = nil
      allow(ActiveRecord).to receive(:after_all_transactions_commit) { |&block| after_commit = block }
      account.update!(custom_attributes: account.custom_attributes.merge('captain_responses_usage' => 240))

      account.increment_response_usage

      expect(message_delivery).not_to have_received(:deliver_later)
      after_commit.call
      expect(message_delivery).to have_received(:deliver_later).once
    end

    it 'keeps the response successful when the alert cannot be queued' do
      account.update!(custom_attributes: account.custom_attributes.merge('captain_responses_usage' => 240))
      allow(message_delivery).to receive(:deliver_later).and_raise(StandardError, 'queue unavailable')
      allow(Rails.logger).to receive(:error)

      expect { account.increment_response_usage }.not_to raise_error
      expect(account.reload.custom_attributes).to include(
        'captain_responses_usage' => 241,
        'captain_responses_usage_alerted' => true
      )

      account.increment_response_usage

      expect(mailer).to have_received(:response_usage_alert).once
      expect(Rails.logger).to have_received(:error).with(
        "Captain response usage alert enqueue failed for account #{account.id}: StandardError - queue unavailable"
      )
    end

    it 'rearms the alert and preserves other custom attributes when usage resets' do
      account.update!(custom_attributes: account.custom_attributes.merge(
        'captain_responses_usage_alerted' => true,
        'unrelated_key' => 'preserved'
      ))

      account.reset_response_usage

      expect(account.reload.custom_attributes).to include(
        'captain_responses_usage' => 0,
        'captain_responses_usage_alerted' => false,
        'unrelated_key' => 'preserved'
      )
    end
  end

  context 'with Shopify plan entitlements' do
    let(:account) do
      create(
        :account,
        internal_attributes: { 'billing_provider' => 'shopify' },
        custom_attributes: {
          'plan_name' => 'Shopify Basic',
          'subscribed_quantity' => 100,
          'captain_documents_usage' => 5,
          'captain_responses_usage' => 10
        }
      )
    end
    let(:shopify_plan) do
      {
        'name' => 'Shopify Basic',
        'handle' => 'shopify-basic',
        'features' => %w[help_center campaigns],
        'limits' => {
          'agents' => 5,
          'inboxes' => 10,
          'emails' => 500,
          'captain_documents' => 25,
          'captain_responses' => 100
        }
      }
    end

    before do
      create(:installation_config, name: 'CHATWOOT_SHOPIFY_PLANS', value: [shopify_plan], locked: true)
    end

    it 'uses fixed Shopify limits instead of Stripe subscription quantity or global limits' do
      usage_limits = account.usage_limits

      expect(usage_limits[:agents]).to eq(5)
      expect(usage_limits[:inboxes]).to eq(10)
      expect(usage_limits.dig(:captain, :documents)).to eq(
        total_count: 25,
        current_available: 20,
        consumed: 5
      )
      expect(usage_limits.dig(:captain, :responses)).to eq(
        total_count: 100,
        current_available: 90,
        consumed: 10
      )
      expect(account.email_rate_limit).to eq(500)
    end

    it 'reads subscribed features from the Shopify plan catalog' do
      expect(account.subscribed_features).to eq(%w[help_center campaigns])
      expect(account.email_transcript_enabled?).to be(true)
    end

    it 'fails closed when the stored Shopify plan is unknown' do
      account.update!(custom_attributes: { 'plan_name' => 'Unknown' })

      expect(account.usage_limits[:agents]).to eq(0)
      expect(account.usage_limits[:inboxes]).to eq(0)
      expect(account.subscribed_features).to eq([])
      expect(account.email_transcript_enabled?).to be(false)
    end
  end

  describe 'subscribed_features' do
    let(:account) { create(:account) }
    let(:plan_features) do
      {
        'hacker' => %w[feature1 feature2],
        'startups' => %w[feature1 feature2 feature3 feature4]
      }
    end

    before do
      InstallationConfig.where(name: 'CHATWOOT_CLOUD_PLAN_FEATURES').first_or_create(value: plan_features)
    end

    context 'when plan_name is hacker' do
      it 'returns the features for the hacker plan' do
        account.custom_attributes = { 'plan_name': 'hacker' }
        account.save!

        expect(account.subscribed_features).to eq(%w[feature1 feature2])
      end
    end

    context 'when plan_name is startups' do
      it 'returns the features for the startups plan' do
        account.custom_attributes = { 'plan_name': 'startups' }
        account.save!

        expect(account.subscribed_features).to eq(%w[feature1 feature2 feature3 feature4])
      end
    end

    context 'when plan_features is blank' do
      it 'returns an empty array' do
        account.custom_attributes = {}
        account.save!

        expect(account.subscribed_features).to be_nil
      end
    end
  end

  describe 'default features' do
    before do
      InstallationConfig.find_or_initialize_by(name: 'ACCOUNT_LEVEL_FEATURE_DEFAULTS').update!(
        value: Featurable::FEATURE_LIST,
        locked: true
      )
    end

    it 'enables Captain V2 for new paid self-hosted accounts' do
      allow(ChatwootApp).to receive(:self_hosted_paid?).and_return(true)

      account = create(:account)

      expect(account).to be_feature_enabled('captain_integration')
      expect(account.captain_preferences[:models]['assistant']).to eq('gpt-5.2')
      expect(account.captain_models).to be_nil
    end
  end

  describe 'captain document sync cadence' do
    let(:account) { create(:account) }

    it 'has no cadence when installation config is missing' do
      account.update!(custom_attributes: { plan_name: 'business' })
      expect(account.captain_document_sync_interval).to be_nil
    end

    it 'uses configured plan intervals from installation config' do
      intervals = {
        business: 48,
        enterprise: 24
      }
      create(:installation_config, name: 'CAPTAIN_DOCUMENT_AUTO_SYNC_INTERVALS', value: intervals.to_json)
      account.update!(custom_attributes: { plan_name: 'business' })

      expect(account.captain_document_sync_interval).to eq(2.days)
    end

    it 'normalizes configured plan name casing' do
      create(:installation_config, name: 'CAPTAIN_DOCUMENT_AUTO_SYNC_INTERVALS', value: { business: 24 }.to_json)
      account.update!(custom_attributes: { plan_name: 'Business' })

      expect(account.captain_document_sync_interval).to eq(1.day)
    end

    it 'uses the enterprise cadence for paid self-hosted installs without a plan_name' do
      allow(ChatwootApp).to receive(:self_hosted_paid?).and_return(true)
      create(:installation_config, name: 'CAPTAIN_DOCUMENT_AUTO_SYNC_INTERVALS', value: { enterprise: 6 }.to_json)
      account.update!(custom_attributes: {})

      expect(account.captain_document_sync_interval).to eq(6.hours)
    end

    it 'allows installation config to disable a plan cadence' do
      create(:installation_config, name: 'CAPTAIN_DOCUMENT_AUTO_SYNC_INTERVALS', value: { business: nil }.to_json)
      account.update!(custom_attributes: { plan_name: 'business' })

      expect(account.captain_document_sync_interval).to be_nil
    end

    it 'has no cadence when installation config is invalid' do
      create(:installation_config, name: 'CAPTAIN_DOCUMENT_AUTO_SYNC_INTERVALS', value: 'invalid-json')
      account.update!(custom_attributes: { plan_name: 'business' })

      expect(account.captain_document_sync_interval).to be_nil
    end

    it 'treats invalid plan interval values as disabled' do
      intervals = {
        business: false,
        enterprise: { hours: 6 },
        startups: '168'
      }
      create(:installation_config, name: 'CAPTAIN_DOCUMENT_AUTO_SYNC_INTERVALS', value: intervals.to_json)

      account.update!(custom_attributes: { plan_name: 'business' })
      expect(account.captain_document_sync_interval).to be_nil

      account.update!(custom_attributes: { plan_name: 'enterprise' })
      expect(account.captain_document_sync_interval).to be_nil

      account.update!(custom_attributes: { plan_name: 'startups' })
      expect(account.captain_document_sync_interval).to be_nil
    end
  end

  describe 'account deletion' do
    let(:account) { create(:account) }
    let(:admin) { create(:user, account: account, role: :administrator) }

    describe '#mark_for_deletion' do
      it 'sets the marked_for_deletion_at and marked_for_deletion_reason attributes' do
        expect do
          account.mark_for_deletion('inactivity')
        end.to change { account.reload.custom_attributes['marked_for_deletion_at'] }.from(nil).to(be_present)
           .and change { account.reload.custom_attributes['marked_for_deletion_reason'] }.from(nil).to('inactivity')
      end

      it 'sends a user-initiated deletion email when reason is manual_deletion' do
        mailer = double
        expect(AdministratorNotifications::AccountNotificationMailer).to receive(:with).with(account: account).and_return(mailer)
        expect(mailer).to receive(:account_deletion_user_initiated).with(account, 'manual_deletion').and_return(mailer)
        expect(mailer).to receive(:deliver_later)

        account.mark_for_deletion('manual_deletion')
      end

      it 'sends a system-initiated deletion email when reason is not manual_deletion' do
        mailer = double
        expect(AdministratorNotifications::AccountNotificationMailer).to receive(:with).with(account: account).and_return(mailer)
        expect(mailer).to receive(:account_deletion_for_inactivity).with(account, 'inactivity').and_return(mailer)
        expect(mailer).to receive(:deliver_later)

        account.mark_for_deletion('inactivity')
      end

      it 'returns true when successful' do
        expect(account.mark_for_deletion).to be_truthy
      end
    end

    describe '#unmark_for_deletion' do
      before do
        account.update!(
          custom_attributes: {
            'marked_for_deletion_at' => 7.days.from_now.iso8601,
            'marked_for_deletion_reason' => 'test_reason'
          }
        )
      end

      it 'removes the marked_for_deletion_at and marked_for_deletion_reason attributes' do
        expect do
          account.unmark_for_deletion
        end.to change { account.reload.custom_attributes['marked_for_deletion_at'] }.from(be_present).to(nil)
           .and change { account.reload.custom_attributes['marked_for_deletion_reason'] }.from('test_reason').to(nil)
      end

      it 'returns true when successful' do
        expect(account.unmark_for_deletion).to be_truthy
      end
    end
  end
end