summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/user_access_snippet_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-27 15:09:24 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-27 15:09:24 +0000
commitf8d15ca65390475e356b06dedc51e10ccd179f86 (patch)
treeef916d4e8e11c9e00d809e5cdcf63814e86d6e89 /spec/lib/gitlab/user_access_snippet_spec.rb
parent3ab4feda4dce9c9f0672375ae27c2f7c2ba6f4ad (diff)
downloadgitlab-ce-f8d15ca65390475e356b06dedc51e10ccd179f86.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/user_access_snippet_spec.rb')
-rw-r--r--spec/lib/gitlab/user_access_snippet_spec.rb95
1 files changed, 95 insertions, 0 deletions
diff --git a/spec/lib/gitlab/user_access_snippet_spec.rb b/spec/lib/gitlab/user_access_snippet_spec.rb
new file mode 100644
index 00000000000..57e52e2e93d
--- /dev/null
+++ b/spec/lib/gitlab/user_access_snippet_spec.rb
@@ -0,0 +1,95 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::UserAccessSnippet do
+ subject(:access) { described_class.new(user, snippet: snippet) }
+
+ let_it_be(:project) { create(:project, :private) }
+ let_it_be(:snippet) { create(:project_snippet, :private, project: project) }
+ let(:user) { create(:user) }
+
+ describe '#can_do_action?' do
+ before do
+ allow(Ability).to receive(:allowed?).and_call_original
+ allow(Ability).to receive(:allowed?).with(user, :ability, snippet).and_return(:foo)
+ end
+
+ context 'when can access_git' do
+ it 'calls Ability#allowed? and returns its result' do
+ expect(access.can_do_action?(:ability)).to eq(:foo)
+ end
+ end
+
+ context 'when can not access_git' do
+ it 'disallows access' do
+ expect(Ability).to receive(:allowed?).with(user, :access_git, :global).and_return(false)
+
+ expect(access.can_do_action?(:ability)).to eq(false)
+ end
+ end
+
+ context 'when user is nil' do
+ let(:user) { nil }
+
+ it 'disallows access' do
+ expect(access.can_do_action?(:ability)).to eq(false)
+ end
+ end
+ end
+
+ describe '#can_push_to_branch?' do
+ include ProjectHelpers
+
+ [:anonymous, :non_member, :guest, :reporter, :maintainer, :admin, :author].each do |membership|
+ context membership.to_s do
+ let(:user) do
+ membership == :author ? snippet.author : create_user_from_membership(project, membership)
+ end
+
+ context 'when can access_git' do
+ it 'respects accessibility' do
+ expected_result = Ability.allowed?(user, :update_snippet, snippet)
+
+ expect(access.can_push_to_branch?('random_branch')).to eq(expected_result)
+ end
+ end
+
+ context 'when can not access_git' do
+ it 'disallows access' do
+ expect(Ability).to receive(:allowed?).with(user, :access_git, :global).and_return(false) if user
+
+ expect(access.can_push_to_branch?('random_branch')).to eq(false)
+ end
+ end
+ end
+ end
+
+ context 'when snippet is nil' do
+ let(:user) { create_user_from_membership(project, :admin) }
+ let(:snippet) { nil }
+
+ it 'disallows access' do
+ expect(access.can_push_to_branch?('random_branch')).to eq(false)
+ end
+ end
+ end
+
+ describe '#can_create_tag?' do
+ it 'returns false' do
+ expect(access.can_create_tag?('random_tag')).to be_falsey
+ end
+ end
+
+ describe '#can_delete_branch?' do
+ it 'returns false' do
+ expect(access.can_delete_branch?('random_branch')).to be_falsey
+ end
+ end
+
+ describe '#can_merge_to_branch?' do
+ it 'returns false' do
+ expect(access.can_merge_to_branch?('random_branch')).to be_falsey
+ end
+ end
+end