summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMark Chao <mchao@gitlab.com>2019-09-16 17:50:41 +0800
committerMark Chao <mchao@gitlab.com>2019-10-01 17:16:07 +1300
commitf132561154314b6fc9050fc7f3d0baf34c13c44b (patch)
tree3467e7d8566929485749a57e32223d2031d86a06 /spec
parent5a54a8d9be626ef1a708f3ffbc80b39209c07351 (diff)
downloadgitlab-ce-f132561154314b6fc9050fc7f3d0baf34c13c44b.tar.gz
EE port: Fix private feature Elasticsearch leak
Add spec to test different combinations. Accept string for required_minimum_access_level Allow more flexible project membership query
Diffstat (limited to 'spec')
-rw-r--r--spec/models/project_feature_spec.rb24
-rw-r--r--spec/models/project_spec.rb30
-rw-r--r--spec/support/helpers/project_helpers.rb25
-rw-r--r--spec/support/shared_contexts/policies/project_policy_table_shared_context.rb118
4 files changed, 197 insertions, 0 deletions
diff --git a/spec/models/project_feature_spec.rb b/spec/models/project_feature_spec.rb
index e6505bb4a51..5b448c343a0 100644
--- a/spec/models/project_feature_spec.rb
+++ b/spec/models/project_feature_spec.rb
@@ -228,4 +228,28 @@ describe ProjectFeature do
end
end
end
+
+ describe '.required_minimum_access_level' do
+ it 'handles reporter level' do
+ expect(described_class.required_minimum_access_level(:merge_requests)).to eq(Gitlab::Access::REPORTER)
+ end
+
+ it 'handles guest level' do
+ expect(described_class.required_minimum_access_level(:issues)).to eq(Gitlab::Access::GUEST)
+ end
+
+ it 'accepts ActiveModel' do
+ expect(described_class.required_minimum_access_level(MergeRequest)).to eq(Gitlab::Access::REPORTER)
+ end
+
+ it 'accepts string' do
+ expect(described_class.required_minimum_access_level('merge_requests')).to eq(Gitlab::Access::REPORTER)
+ end
+
+ it 'raises error if feature is invalid' do
+ expect do
+ described_class.required_minimum_access_level(:foos)
+ end.to raise_error
+ end
+ end
end
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 67f64822184..7fe48e66def 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -3462,6 +3462,36 @@ describe Project do
end
end
+ describe '.filter_by_feature_visibility' do
+ include_context 'ProjectPolicyTable context'
+ include ProjectHelpers
+ using RSpec::Parameterized::TableSyntax
+
+ set(:group) { create(:group) }
+ let!(:project) { create(:project, project_level, namespace: group ) }
+ let(:user) { create_user_from_membership(project, membership) }
+
+ context 'reporter level access' do
+ let(:feature) { MergeRequest }
+
+ where(:project_level, :feature_access_level, :membership, :expected_count) do
+ permission_table_for_reporter_feature_access
+ end
+
+ with_them do
+ it "respects visibility" do
+ update_feature_access_level(project, feature_access_level)
+
+ expected_objects = expected_count == 1 ? [project] : []
+
+ expect(
+ described_class.filter_by_feature_visibility(feature, user)
+ ).to eq(expected_objects)
+ end
+ end
+ end
+ end
+
describe '#pages_available?' do
let(:project) { create(:project, group: group) }
diff --git a/spec/support/helpers/project_helpers.rb b/spec/support/helpers/project_helpers.rb
new file mode 100644
index 00000000000..61056b47aed
--- /dev/null
+++ b/spec/support/helpers/project_helpers.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module ProjectHelpers
+ # @params target [Project] membership target
+ # @params membership [Symbol] accepts the membership levels :guest, :reporter...
+ # and phony levels :non_member and :anonymous
+ def create_user_from_membership(target, membership)
+ case membership
+ when :anonymous
+ nil
+ when :non_member
+ create(:user, name: membership)
+ else
+ create(:user, name: membership).tap { |u| target.add_user(u, membership) }
+ end
+ end
+
+ def update_feature_access_level(project, access_level)
+ project.update!(
+ repository_access_level: access_level,
+ merge_requests_access_level: access_level,
+ builds_access_level: access_level
+ )
+ end
+end
diff --git a/spec/support/shared_contexts/policies/project_policy_table_shared_context.rb b/spec/support/shared_contexts/policies/project_policy_table_shared_context.rb
new file mode 100644
index 00000000000..e666b346b8b
--- /dev/null
+++ b/spec/support/shared_contexts/policies/project_policy_table_shared_context.rb
@@ -0,0 +1,118 @@
+# frozen_string_literal: true
+
+RSpec.shared_context 'ProjectPolicyTable context' do
+ using RSpec::Parameterized::TableSyntax
+
+ # rubocop:disable Metrics/AbcSize
+ def permission_table_for_reporter_feature_access
+ :public | :enabled | :reporter | 1
+ :public | :enabled | :guest | 1
+ :public | :enabled | :non_member | 1
+ :public | :enabled | :anonymous | 1
+
+ :public | :private | :reporter | 1
+ :public | :private | :guest | 0
+ :public | :private | :non_member | 0
+ :public | :private | :anonymous | 0
+
+ :public | :disabled | :reporter | 0
+ :public | :disabled | :guest | 0
+ :public | :disabled | :non_member | 0
+ :public | :disabled | :anonymous | 0
+
+ :internal | :enabled | :reporter | 1
+ :internal | :enabled | :guest | 1
+ :internal | :enabled | :non_member | 1
+ :internal | :enabled | :anonymous | 0
+
+ :internal | :private | :reporter | 1
+ :internal | :private | :guest | 0
+ :internal | :private | :non_member | 0
+ :internal | :private | :anonymous | 0
+
+ :internal | :disabled | :reporter | 0
+ :internal | :disabled | :guest | 0
+ :internal | :disabled | :non_member | 0
+ :internal | :disabled | :anonymous | 0
+
+ :private | :enabled | :reporter | 1
+ :private | :enabled | :guest | 1
+ :private | :enabled | :non_member | 0
+ :private | :enabled | :anonymous | 0
+
+ :private | :private | :reporter | 1
+ :private | :private | :guest | 0
+ :private | :private | :non_member | 0
+ :private | :private | :anonymous | 0
+
+ :private | :disabled | :reporter | 0
+ :private | :disabled | :guest | 0
+ :private | :disabled | :non_member | 0
+ :private | :disabled | :anonymous | 0
+ end
+
+ def permission_table_for_guest_feature_access
+ :public | :enabled | :reporter | 1
+ :public | :enabled | :guest | 1
+ :public | :enabled | :non_member | 1
+ :public | :enabled | :anonymous | 1
+
+ :public | :private | :reporter | 1
+ :public | :private | :guest | 1
+ :public | :private | :non_member | 0
+ :public | :private | :anonymous | 0
+
+ :public | :disabled | :reporter | 0
+ :public | :disabled | :guest | 0
+ :public | :disabled | :non_member | 0
+ :public | :disabled | :anonymous | 0
+
+ :internal | :enabled | :reporter | 1
+ :internal | :enabled | :guest | 1
+ :internal | :enabled | :non_member | 1
+ :internal | :enabled | :anonymous | 0
+
+ :internal | :private | :reporter | 1
+ :internal | :private | :guest | 1
+ :internal | :private | :non_member | 0
+ :internal | :private | :anonymous | 0
+
+ :internal | :disabled | :reporter | 0
+ :internal | :disabled | :guest | 0
+ :internal | :disabled | :non_member | 0
+ :internal | :disabled | :anonymous | 0
+
+ :private | :enabled | :reporter | 1
+ :private | :enabled | :guest | 1
+ :private | :enabled | :non_member | 0
+ :private | :enabled | :anonymous | 0
+
+ :private | :private | :reporter | 1
+ :private | :private | :guest | 1
+ :private | :private | :non_member | 0
+ :private | :private | :anonymous | 0
+
+ :private | :disabled | :reporter | 0
+ :private | :disabled | :guest | 0
+ :private | :disabled | :non_member | 0
+ :private | :disabled | :anonymous | 0
+ end
+
+ def permission_table_for_project_access
+ :public | :reporter | 1
+ :public | :guest | 1
+ :public | :non_member | 1
+ :public | :anonymous | 1
+
+ :internal | :reporter | 1
+ :internal | :guest | 1
+ :internal | :non_member | 1
+ :internal | :anonymous | 0
+
+ :private | :reporter | 1
+ :private | :guest | 1
+ :private | :non_member | 0
+ :private | :anonymous | 0
+ end
+ # rubocop:enable Metrics/AbcSize
+end