# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Stripe::CallbacksController do
  let(:enterprise) { create(:distributor_enterprise) }

  context "#index" do
    let(:params) { { id: enterprise.permalink } }
    let(:connector) { double(:connector) }

    before do
      allow(controller).to receive(:spree_current_user) { enterprise.owner }
      allow(Stripe::AccountConnector).to receive(:new) { connector }
    end

    context "when the connector.create_account raises a StripeError" do
      before do
        allow(connector).to receive(:create_account).and_raise Stripe::StripeError, "some error"
      end

      it "returns a 500 error" do
        spree_get :index, params
        expect(response).to have_http_status :internal_server_error
      end
    end

    context "when the connector.create_account raises an AccessDenied error" do
      before do
        allow(connector).to receive(:create_account).and_raise CanCan::AccessDenied, "some error"
      end

      it "redirects to unauthorized" do
        spree_get :index, params
        expect(response).to redirect_to unauthorized_path
      end
    end

    context "when the connector fails in creating a new stripe account record" do
      before { allow(connector).to receive(:create_account) { false } }

      context "when the user cancelled the connection" do
        before { allow(connector).to receive(:connection_cancelled_by_user?) { true } }

        it "renders a failure message" do
          allow(connector).to receive(:enterprise) { enterprise }
          spree_get :index, params
          expect(flash[:notice]).to eq 'Connection to Stripe has been cancelled'
          expect(response).to redirect_to edit_admin_enterprise_path(enterprise,
                                                                     anchor: 'payment_methods')
        end
      end

      context "when some other error caused the failure" do
        before { allow(connector).to receive(:connection_cancelled_by_user?) { false } }

        it "renders a failure message" do
          allow(connector).to receive(:enterprise) { enterprise }
          spree_get :index, params
          expect(flash[:error]).to eq 'Sorry, the connection of your Stripe account failed'
          expect(response).to redirect_to edit_admin_enterprise_path(enterprise,
                                                                     anchor: 'payment_methods')
        end
      end
    end

    context "when the connector succeeds in creating a new stripe account record" do
      before { allow(connector).to receive(:create_account) { true } }

      it "redirects to the enterprise edit path" do
        allow(connector).to receive(:enterprise) { enterprise }
        spree_get :index, params
        expect(flash[:success]).to eq 'Stripe account connected successfully'
        expect(response).to redirect_to edit_admin_enterprise_path(enterprise,
                                                                   anchor: 'payment_methods')
      end
    end
  end
end
