<commit_before>module SharedModelSpecs
  shared_examples_for "exportable" do
    it "Model#export returns all records with extra attributes added" do
      pending
      # User/assignee for the second record has no first/last name.
      exported.size.should == 2
      exported[0].user_id_full_name.should == "#{exported[0].user.first_name} #{exported[0].user.last_name}"
      exported[1].user_id_full_name.should == "#{exported[1].user.email}"

      if exported[0].respond_to?(:assigned_to)
        if exported[0].assigned_to?
          exported[0].assigned_to_full_name.should == "#{exported[0].assignee.first_name} #{exported[0].assignee.last_name}"
        else
          exported[0].assigned_to_full_name.should == ''
        end
        if exported[1].assigned_to?
          exported[1].assigned_to_full_name.should == "#{exported[1].assignee.email}"
        else
          exported[1].assigned_to_full_name.should == ''
        end
      end

      if exported[0].respond_to?(:completed_by)
        if exported[0].completed_by?
          exported[0].completed_by_full_name.should == "#{exported[0].completor.first_name} #{exported[0].completor.last_name}"
        else
          exported[0].completed_by_full_name.should == ''
        end
        if exported[1].completed_by?
          exported[1].completed_by_full_name.should == "#{exported[1].completor.email}"
        else
          exported[1].completed_by_full_name.should == ''
        end
      end
    end
  end

  require "cancan/matchers"

  shared_examples_for Ability do |klass|

    subject { ability }
    let(:ability){ Ability.new(user) }
    let(:user){ FactoryGirl.create(:user) }
    let(:factory){ klass.model_name.underscore }

    context "create" do
      it{ should be_able_to(:create, klass) }
    end

    context "when public access" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Public') }

      it{ should be_able_to(:manage, asset) }
    end

    context "when private access owner" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Private', :user_id => user.id) }

      it{ should be_able_to(:manage, asset) }
    end

    context "when private access not owner" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Private') }

      it{ should_not be_able_to(:manage, asset) }
    end

    context "when shared access with permission" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Shared', :permissions => [permission]) }
      let(:permission){ Permission.new(:user => user) }

      it{ should be_able_to(:manage, asset) }
    end

    context "when shared access with no permission" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Shared', :permissions => [permission]) }
      let(:permission){ Permission.new(:user => FactoryGirl.create(:user)) }

      it{ should_not be_able_to(:manage, asset) }
    end

    context "when shared access with group permission" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Shared', :permissions => [permission]) }
      let(:permission){ Permission.new(:group => group) }
      let(:group){ FactoryGirl.create(:group, :users => [user]) }

      it{ should be_able_to(:manage, asset) }
    end

    context "when shared access with no group permission" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Shared', :permissions => [permission]) }
      let(:permission){ Permission.new(:group => group) }
      let(:group){ FactoryGirl.create(:group) }

      it{ should_not be_able_to(:manage, asset) }
    end
  end
end<commit_msg>Added tests to ensure administrator can access Private and Shared entities.<commit_after>module SharedModelSpecs
  shared_examples_for "exportable" do
    it "Model#export returns all records with extra attributes added" do
      pending
      # User/assignee for the second record has no first/last name.
      exported.size.should == 2
      exported[0].user_id_full_name.should == "#{exported[0].user.first_name} #{exported[0].user.last_name}"
      exported[1].user_id_full_name.should == "#{exported[1].user.email}"

      if exported[0].respond_to?(:assigned_to)
        if exported[0].assigned_to?
          exported[0].assigned_to_full_name.should == "#{exported[0].assignee.first_name} #{exported[0].assignee.last_name}"
        else
          exported[0].assigned_to_full_name.should == ''
        end
        if exported[1].assigned_to?
          exported[1].assigned_to_full_name.should == "#{exported[1].assignee.email}"
        else
          exported[1].assigned_to_full_name.should == ''
        end
      end

      if exported[0].respond_to?(:completed_by)
        if exported[0].completed_by?
          exported[0].completed_by_full_name.should == "#{exported[0].completor.first_name} #{exported[0].completor.last_name}"
        else
          exported[0].completed_by_full_name.should == ''
        end
        if exported[1].completed_by?
          exported[1].completed_by_full_name.should == "#{exported[1].completor.email}"
        else
          exported[1].completed_by_full_name.should == ''
        end
      end
    end
  end

  require "cancan/matchers"

  shared_examples_for Ability do |klass|

    subject { ability }
    let(:ability){ Ability.new(user) }
    let(:user){ FactoryGirl.create(:user) }
    let(:factory){ klass.model_name.underscore }

    context "create" do
      it{ should be_able_to(:create, klass) }
    end

    context "when public access" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Public') }

      it{ should be_able_to(:manage, asset) }
    end

    context "when private access owner" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Private', :user_id => user.id) }

      it{ should be_able_to(:manage, asset) }
    end
    
    context "when private access administrator" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Private') }
      let(:user) { FactoryGirl.create(:user, :admin => true) }

      it{ should be_able_to(:manage, asset) }
    end

    context "when private access not owner" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Private') }

      it{ should_not be_able_to(:manage, asset) }
    end

    context "when shared access with permission" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Shared', :permissions => [permission]) }
      let(:permission){ Permission.new(:user => user) }

      it{ should be_able_to(:manage, asset) }
    end

    context "when shared access with no permission" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Shared', :permissions => [permission]) }
      let(:permission){ Permission.new(:user => FactoryGirl.create(:user)) }

      it{ should_not be_able_to(:manage, asset) }
    end
    
    context "when shared access with no permission but administrator" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Shared', :permissions => [permission]) }
      let(:permission){ Permission.new(:user => FactoryGirl.create(:user)) }
      let(:user) { FactoryGirl.create(:user, :admin => true) }

      it{ should be_able_to(:manage, asset) }
    end

    context "when shared access with group permission" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Shared', :permissions => [permission]) }
      let(:permission){ Permission.new(:group => group) }
      let(:group){ FactoryGirl.create(:group, :users => [user]) }

      it{ should be_able_to(:manage, asset) }
    end

    context "when shared access with no group permission" do
      let(:asset){ FactoryGirl.create(factory, :access => 'Shared', :permissions => [permission]) }
      let(:permission){ Permission.new(:group => group) }
      let(:group){ FactoryGirl.create(:group) }

      it{ should_not be_able_to(:manage, asset) }
    end
    
  end
end
{'fraction_non_alphanumeric': 0.13551940566158577, 'fraction_numerical': 0.006660689125144102, 'mean_word_length': 3.273672687465791, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '6053247', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2343, 'n_words': 526}