require "rails_helper"

describe DirectoryPolicy do
  subject { described_class }

  let(:user_context) { build(:user_context, user: user, account: user.primary_account) }

  context "for an associate" do
    let(:user) { create(:user, :associate, admin: false) }

    permissions :create? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :show? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :update? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :destroy? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :new_file? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :download_file? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :destroy_file? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :can_view_permissions_settings? do
      it { is_expected.not_to permit(user_context, subject) }
    end

    permissions ".scope" do
      let!(:project) { create(:project_with_directories, name: "Project", members: user, account: user.primary_account) }

      subject { Pundit.policy_scope!(user_context, project.directories) }
      
      it "returns all directories for associate" do
        names = subject.map(&:name)

        expect(names).to include("Shared")
        expect(names).not_to include("Financial Documents")
        expect(names).not_to include("Salaries")
      end
    end
  end

  context "for a project manager" do
    let(:user) { create(:user, :manager, admin: false) }

    permissions :create? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :show? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :update? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :destroy? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :new_file? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :download_file? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :destroy_file? do
      it { is_expected.to permit(user_context, subject) }
    end
    permissions :can_view_permissions_settings? do
      it { is_expected.to permit(user_context, subject) }
    end

    permissions ".scope" do
      let!(:project) { create(:project_with_directories, name: "Project", members: user, account: user.primary_account) }

      subject { Pundit.policy_scope!(user_context, project.directories) }
      
      it "returns all directories for manager" do
        names = subject.map(&:name)

        expect(names).to include("Shared")
        expect(names).to include("Financial Documents")
        expect(names).not_to include("Salaries")
      end
    end
  end
  
  context "for account managers" do
    permissions ".scope" do
      let(:user) { create(:user, :account_manager) }
      let!(:project) { create(:project_with_directories, name: "Project", account: user.primary_account) }

      subject { Pundit.policy_scope!(user_context, project.directories) }
      
      it "returns all directories for manager" do
        names = subject.map(&:name)

        expect(names).to include("Shared")
        expect(names).to include("Financial Documents")
        expect(names).to include("Salaries")
      end
    end
  end
end
