<commit_before>require 'rails_helper'

RSpec.describe RewardPolicy do
  subject{ RewardPolicy }

  let(:policy){ RewardPolicy.new(user, reward) }
  let(:user){ nil }
  let(:reward){ create(:reward) }

  shared_examples_for "create permissions" do
    it "should deny access if user is nil" do
      is_expected.not_to permit(nil, reward)
    end

    it "should deny access if user is not project owner" do
      is_expected.not_to permit(User.new, reward)
    end

    it "should permit access if user is project owner" do
      new_user = reward.project.user
      is_expected.to permit(new_user, reward)
    end

    it "should permit access if user is admin" do
      admin = build(:user, admin: true)
      is_expected.to permit(admin, reward)
    end
  end

  shared_examples_for "destroy permissions" do
    it_should_behave_like "create permissions"

    it "should deny access if reward has one contribution waiting for confirmation" do
      create(:contribution, project: reward.project, reward: reward, state: 'waiting_confirmation')
      is_expected.not_to permit(reward.project.user, reward)
    end

    it "should deny access if reward has one confirmed contribution" do
      create(:contribution, project: reward.project, reward: reward, state: 'confirmed')
      is_expected.not_to permit(reward.project.user, reward)
    end
  end


  permissions :new? do
    it_should_behave_like "create permissions"
  end

  permissions :create? do
    it_should_behave_like "create permissions"
  end

  permissions :edit? do
    it_should_behave_like "create permissions"
  end

  permissions :update? do
    it_should_behave_like "create permissions"
  end

  permissions :sort? do
    it_should_behave_like "create permissions"
  end

  permissions :destroy? do
    it_should_behave_like "destroy permissions"
  end

  describe "#permitted_for?" do
    let(:user){ reward.project.user }
    subject{ policy.permitted_for?(field, :update) }

    ['waiting_confirmation', 'confirmed'].each do |state|
      context "when we have one contribution in state #{state}" do
        before do
          create(:contribution, project: reward.project, reward: reward, state: 'waiting_confirmation')
        end

        context "and want to update minimum_value" do
          let(:field){ :minimum_value }
          it{ is_expected.to eq(false) }
        end

        context "and want to update description" do
          let(:field){ :description }
          it{ is_expected.to eq(true) }
        end

        context "and want to update maximum_contributions" do
          let(:field){ :maximum_contributions }
          it{ is_expected.to eq(true) }
        end

        context "and want to update deliver_at" do
          let(:field){ :deliver_at}
          it{ is_expected.to eq(true) }<commit_msg>Fixes reward policy spec to work with payments<commit_after>require 'rails_helper'

RSpec.describe RewardPolicy do
  subject{ RewardPolicy }

  let(:policy){ RewardPolicy.new(user, reward) }
  let(:user){ nil }
  let(:reward){ create(:reward) }

  shared_examples_for "create permissions" do
    it "should deny access if user is nil" do
      is_expected.not_to permit(nil, reward)
    end

    it "should deny access if user is not project owner" do
      is_expected.not_to permit(User.new, reward)
    end

    it "should permit access if user is project owner" do
      new_user = reward.project.user
      is_expected.to permit(new_user, reward)
    end

    it "should permit access if user is admin" do
      admin = build(:user, admin: true)
      is_expected.to permit(admin, reward)
    end
  end

  shared_examples_for "destroy permissions" do
    it_should_behave_like "create permissions"

    it "should deny access if reward has one contribution waiting for confirmation" do
      create(:pending_contribution, project: reward.project, reward: reward)
      is_expected.not_to permit(reward.project.user, reward)
    end

    it "should deny access if reward has one confirmed contribution" do
      create(:confirmed_contribution, project: reward.project, reward: reward)
      is_expected.not_to permit(reward.project.user, reward)
    end
  end


  permissions :new? do
    it_should_behave_like "create permissions"
  end

  permissions :create? do
    it_should_behave_like "create permissions"
  end

  permissions :edit? do
    it_should_behave_like "create permissions"
  end

  permissions :update? do
    it_should_behave_like "create permissions"
  end

  permissions :sort? do
    it_should_behave_like "create permissions"
  end

  permissions :destroy? do
    it_should_behave_like "destroy permissions"
  end

  describe "#permitted_for?" do
    let(:user){ reward.project.user }
    subject{ policy.permitted_for?(field, :update) }

    [:pending_contribution, :confirmed_contribution].each do |state|
      context "when we have one contribution in state #{state}" do
        before do
          create(state, project: reward.project, reward: reward)
        end

        context "and want to update minimum_value" do
          let(:field){ :minimum_value }
          it{ is_expected.to eq(false) }
        end

        context "and want to update description" do
          let(:field){ :description }
          it{ is_expected.to eq(true) }
        end

        context "and want to update maximum_contributions" do
          let(:field){ :maximum_contributions }
          it{ is_expected.to eq(true) }
        end

        context "and want to update deliver_at" do
          let(:field){ :deliver_at}
          it{ is_expected.to eq(true) }
{'fraction_non_alphanumeric': 0.08043711931207452, 'fraction_numerical': 0.0, 'mean_word_length': 3.1051470588235293, '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': '5306880', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1600, 'n_words': 519}