<commit_before>require 'rails_helper'

describe Reservation::ReservationPolicy do
  describe "permissions" do
    include_context "policy objs"
    let(:resource) { create(:resource) }
    let(:reservation) { build(:reservation, reserver: user, resource: resource) }

    shared_examples_for "allow all active users" do
      it "grants access to active users" do
        expect(subject).to permit(user, reservation)
      end

      it "denies access to inactive users" do
        expect(subject).not_to permit(inactive_user, reservation)
      end
    end

    shared_examples_for "allow reserver and admins" do
      it "grants access to reserver" do
        expect(subject).to permit(user, reservation)
      end

      it "grants access to admins" do
        expect(subject).to permit(admin, reservation)
      end

      it "denies access to other regular users" do
        expect(subject).not_to permit(other_user, reservation)
      end
    end

    permissions :choose_reserver? do
      it "allows admins only" do
        expect(subject).not_to permit(user, reservation)
        expect(subject).to permit(admin, reservation)
      end
    end

    context "non-meal reservation" do
      permissions :index?, :show?, :new?, :create? do
        it_behaves_like "allow all active users"
      end

      permissions :edit?, :update? do
        it_behaves_like "allow reserver and admins"
      end

      permissions :destroy? do
        context "future reservation" do
          let(:reservation) { build(:reservation, reserver: user, starts_at: 1.day.from_now) }
          it_behaves_like "allow reserver and admins"
        end

        context "just created reservation" do
          let(:reservation) { build(:reservation, reserver: user, starts_at: 1.day.ago,
            created_at: 50.minutes.ago) }
          it_behaves_like "allow reserver and admins"
        end

        context "old past reservation" do
          let(:reservation) { build(:reservation, reserver: user, starts_at: 1.day.ago,
            created_at: 1.week.ago) }

          it "denies access to non-admins" do
            expect(subject).not_to permit(user, reservation)
            expect(subject).not_to permit(other_user, reservation)
          end

          it "grants access to admins" do
            expect(subject).to permit(admin, reservation)
          end
        end
      end
    end

    context "meal reservation" do
      let(:reservation) { build(:reservation, reserver: user, resource: resource, kind: "_meal") }

      permissions :index?, :show? do
        it_behaves_like "allow all active users"
      end

      permissions :new?, :create?, :edit?, :update?, :destroy? do
        it "denies access to all" do
          expect(subject).not_to permit(user, reservation)
          expect(subject).not_to permit(other_user, reservation)
          expect(subject).not_to permit(admin, reservation)
        end
      end

    end
  end

  describe "scope" do
    let!(:reservation1) { create(:reservation) }
    let!(:reservation2) { create(:reservation) }
    let(:user) { User.new }

    it "returns all reservations" do
      permitted = Reservation::ReservationPolicy::Scope.new(user, Reservation::Reservation.all).resolve
      expect(permitted).to contain_exactly(reservation1, reservation2)
    end
  end

  describe "permitted_attributes" do
    let(:reservation) { build(:reservation, reserver: user) }
    subject { Reservation::ReservationPolicy.new(user, reservation).permitted_attributes }

    context "regular user" do
      let(:user) { User.new }

      it "should allow appropriate attribs" do
        expect(subject).to contain_exactly(*%i(name kind resource_id
          sponsor_id starts_at ends_at guidelines_ok))
      end
    end

    context "admin" do
      let(:user) { build(:user, admin: true) }

      it "should allow appropriate attribs" do
        expect(subject).to contain_exactly(*%i(name kind reserver_id resource_id
          sponsor_id starts_at ends_at guidelines_ok))
      end
    end
  end
end<commit_msg>Fixed failing spec<commit_after>require 'rails_helper'

describe Reservation::ReservationPolicy do
  describe "permissions" do
    include_context "policy objs"
    let(:resource) { create(:resource) }
    let(:reservation) { build(:reservation, reserver: user, resource: resource) }

    shared_examples_for "allow all active users" do
      it "grants access to active users" do
        expect(subject).to permit(user, reservation)
      end

      it "denies access to inactive users" do
        expect(subject).not_to permit(inactive_user, reservation)
      end
    end

    shared_examples_for "allow reserver and admins" do
      it "grants access to reserver" do
        expect(subject).to permit(user, reservation)
      end

      it "grants access to admins" do
        expect(subject).to permit(admin, reservation)
      end

      it "denies access to other regular users" do
        expect(subject).not_to permit(other_user, reservation)
      end
    end

    permissions :choose_reserver? do
      it "allows admins only" do
        expect(subject).not_to permit(user, reservation)
        expect(subject).to permit(admin, reservation)
      end
    end

    context "non-meal reservation" do
      permissions :index?, :show?, :new?, :create? do
        it_behaves_like "allow all active users"
      end

      permissions :edit?, :update? do
        it_behaves_like "allow reserver and admins"
      end

      permissions :destroy? do
        context "future reservation" do
          let(:reservation) { build(:reservation, reserver: user, starts_at: 1.day.from_now) }
          it_behaves_like "allow reserver and admins"
        end

        context "just created reservation" do
          let(:reservation) { build(:reservation, reserver: user, starts_at: 1.day.ago,
            created_at: 50.minutes.ago) }
          it_behaves_like "allow reserver and admins"
        end

        context "old past reservation" do
          let(:reservation) { build(:reservation, reserver: user, starts_at: 1.day.ago,
            created_at: 1.week.ago) }

          it "denies access to non-admins" do
            expect(subject).not_to permit(user, reservation)
            expect(subject).not_to permit(other_user, reservation)
          end

          it "grants access to admins" do
            expect(subject).to permit(admin, reservation)
          end
        end
      end
    end

    context "meal reservation" do
      let(:reservation) { build(:reservation, reserver: user, resource: resource, kind: "_meal") }

      permissions :index?, :show? do
        it_behaves_like "allow all active users"
      end

      permissions :new?, :create?, :edit?, :update?, :destroy? do
        it "denies access to all" do
          expect(subject).not_to permit(user, reservation)
          expect(subject).not_to permit(other_user, reservation)
          expect(subject).not_to permit(admin, reservation)
        end
      end

    end
  end

  describe "scope" do
    let!(:reservation1) { create(:reservation) }
    let!(:reservation2) { create(:reservation) }
    let(:user) { User.new }

    it "returns all reservations" do
      permitted = Reservation::ReservationPolicy::Scope.new(user, Reservation::Reservation.all).resolve
      expect(permitted).to contain_exactly(reservation1, reservation2)
    end
  end

  describe "permitted_attributes" do
    let(:reservation) { build(:reservation, reserver: user) }
    subject { Reservation::ReservationPolicy.new(user, reservation).permitted_attributes }

    context "regular user" do
      let(:user) { User.new }

      it "should allow appropriate attribs" do
        expect(subject).to contain_exactly(*%i(name kind resource_id
          sponsor_id starts_at ends_at guidelines_ok note))
      end
    end

    context "admin" do
      let(:user) { build(:user, admin: true) }

      it "should allow appropriate attribs" do
        expect(subject).to contain_exactly(*%i(name kind reserver_id resource_id
          sponsor_id starts_at ends_at guidelines_ok note))
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0906292328530969, 'fraction_numerical': 0.0024627508927471987, 'mean_word_length': 2.7724105898745934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4327863', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2282, 'n_words': 671}