project_feature_spec.rb 4.13 KB
Newer Older
Felipe's avatar
Felipe committed
1 2 3
require 'spec_helper'

describe ProjectFeature do
4
  let(:project) { create(:project) }
Felipe's avatar
Felipe committed
5 6
  let(:user) { create(:user) }

7 8 9 10 11 12 13 14 15 16 17 18
  describe '.quoted_access_level_column' do
    it 'returns the table name and quoted column name for a feature' do
      expected = if Gitlab::Database.postgresql?
                   '"project_features"."issues_access_level"'
                 else
                   '`project_features`.`issues_access_level`'
                 end

      expect(described_class.quoted_access_level_column(:issues)).to eq(expected)
    end
  end

Felipe's avatar
Felipe committed
19
  describe '#feature_available?' do
20
    let(:features) { %w(issues wiki builds merge_requests snippets repository) }
Felipe's avatar
Felipe committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

    context 'when features are disabled' do
      it "returns false" do
        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::DISABLED)
          expect(project.feature_available?(:issues, user)).to eq(false)
        end
      end
    end

    context 'when features are enabled only for team members' do
      it "returns false when user is not a team member" do
        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
          expect(project.feature_available?(:issues, user)).to eq(false)
        end
      end

      it "returns true when user is a team member" do
40
        project.add_developer(user)
Felipe's avatar
Felipe committed
41 42 43 44 45 46 47 48 49

        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
          expect(project.feature_available?(:issues, user)).to eq(true)
        end
      end

      it "returns true when user is a member of project group" do
        group = create(:group)
50
        project = create(:project, namespace: group)
Felipe's avatar
Felipe committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        group.add_developer(user)

        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
          expect(project.feature_available?(:issues, user)).to eq(true)
        end
      end

      it "returns true if user is an admin" do
        user.update_attribute(:admin, true)

        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
          expect(project.feature_available?(:issues, user)).to eq(true)
        end
      end
    end

    context 'when feature is enabled for everyone' do
      it "returns true" do
        features.each do |feature|
          expect(project.feature_available?(:issues, user)).to eq(true)
        end
      end
    end
  end

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  context 'repository related features' do
    before do
      project.project_feature.update_attributes(
        merge_requests_access_level: ProjectFeature::DISABLED,
        builds_access_level: ProjectFeature::DISABLED,
        repository_access_level: ProjectFeature::PRIVATE
      )
    end

    it "does not allow repository related features have higher level" do
      features = %w(builds merge_requests)
      project_feature = project.project_feature

      features.each do |feature|
        field = "#{feature}_access_level".to_sym
        project_feature.update_attribute(field, ProjectFeature::ENABLED)
        expect(project_feature.valid?).to be_falsy
      end
    end
  end

Felipe's avatar
Felipe committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  describe '#*_enabled?' do
    let(:features) { %w(wiki builds merge_requests) }

    it "returns false when feature is disabled" do
      features.each do |feature|
        project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::DISABLED)
        expect(project.public_send("#{feature}_enabled?")).to eq(false)
      end
    end

    it "returns true when feature is enabled only for team members" do
      features.each do |feature|
        project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
        expect(project.public_send("#{feature}_enabled?")).to eq(true)
      end
    end

    it "returns true when feature is enabled for everyone" do
      features.each do |feature|
        expect(project.public_send("#{feature}_enabled?")).to eq(true)
      end
    end
  end
end