project_authorizations_spec.rb 2.17 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe Gitlab::ProjectAuthorizations do
  let(:group) { create(:group) }
5 6 7
  let!(:owned_project) { create(:project) }
  let!(:other_project) { create(:project) }
  let!(:group_project) { create(:project, namespace: group) }
8 9 10 11 12 13 14 15 16 17

  let(:user) { owned_project.namespace.owner }

  def map_access_levels(rows)
    rows.each_with_object({}) do |row, hash|
      hash[row.project_id] = row.access_level
    end
  end

  before do
18
    other_project.add_reporter(user)
19 20 21 22
    group.add_developer(user)
  end

  let(:authorizations) do
23
    klass = if Group.supports_nested_objects?
24 25 26 27 28 29 30 31 32 33 34 35 36
              Gitlab::ProjectAuthorizations::WithNestedGroups
            else
              Gitlab::ProjectAuthorizations::WithoutNestedGroups
            end

    klass.new(user).calculate
  end

  it 'returns the correct number of authorizations' do
    expect(authorizations.length).to eq(3)
  end

  it 'includes the correct projects' do
37 38
    expect(authorizations.pluck(:project_id))
      .to include(owned_project.id, other_project.id, group_project.id)
39 40 41 42 43
  end

  it 'includes the correct access levels' do
    mapping = map_access_levels(authorizations)

44
    expect(mapping[owned_project.id]).to eq(Gitlab::Access::MAINTAINER)
45 46 47 48
    expect(mapping[other_project.id]).to eq(Gitlab::Access::REPORTER)
    expect(mapping[group_project.id]).to eq(Gitlab::Access::DEVELOPER)
  end

49
  if Group.supports_nested_objects?
50 51
    context 'with nested groups' do
      let!(:nested_group) { create(:group, parent: group) }
52
      let!(:nested_project) { create(:project, namespace: nested_group) }
53 54 55 56 57 58 59 60 61 62 63 64

      it 'includes nested groups' do
        expect(authorizations.pluck(:project_id)).to include(nested_project.id)
      end

      it 'inherits access levels when the user is not a member of a nested group' do
        mapping = map_access_levels(authorizations)

        expect(mapping[nested_project.id]).to eq(Gitlab::Access::DEVELOPER)
      end

      it 'uses the greatest access level when a user is a member of a nested group' do
65
        nested_group.add_maintainer(user)
66 67 68

        mapping = map_access_levels(authorizations)

69
        expect(mapping[nested_project.id]).to eq(Gitlab::Access::MAINTAINER)
70 71 72 73
      end
    end
  end
end