require "rails_helper"

RSpec.describe UserPolicy do
  let(:user) { build(:user, admin: false) }
  let(:user_context) { build(:user_context, user: user, account: user.primary_account) }

  subject { described_class }

  permissions :create? do
    context "when user is admin" do
      let(:user) { create(:user, :admin) }

      it { is_expected.to permit(user_context, :create) }
    end

    context "when user is NOT admin" do
      let(:user) { create(:user, :associate) }

      it { is_expected.not_to permit(user_context, :create) }
    end
  end

  permissions :update? do
    context "when user is admin" do
      let(:user) { create(:user, :admin) }

      it { is_expected.to permit(user_context, :update) }
    end

    context "when user is NOT admin" do
      let(:user) { create(:user) }

      it { is_expected.not_to permit(user_context, :update) }
    end
  end

  permissions :destroy? do
    context "when user is admin" do
      let(:user) { create(:user, :admin) }

      it { is_expected.to permit(user_context, :destroy) }
    end

    context "when user is NOT admin" do
      let(:user) { create(:user) }

      it { is_expected.not_to permit(user_context, :destroy) }
    end
  end

  permissions ".scope" do
    subject { Pundit.policy_scope!(user_context, User) }

    context "as a normal user" do
      it { is_expected.to be_empty }
    end

    context "as an admin" do
      let(:user) { create(:user, :admin) }

      it { is_expected.not_to be_empty }
    end
  end
end
