debian-mirror-gitlab/spec/models/ability_spec.rb

309 lines
10 KiB
Ruby
Raw Normal View History

require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Ability do
2017-08-17 22:00:37 +05:30
context 'using a nil subject' do
2017-09-10 17:25:29 +05:30
it 'has no permissions' do
expect(described_class.policy_for(nil, nil)).to be_banned
2017-08-17 22:00:37 +05:30
end
end
describe '.users_that_can_read_project' do
context 'using a public project' do
it 'returns all the users' do
2017-09-10 17:25:29 +05:30
project = create(:project, :public)
user = build(:user)
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project([user], project))
.to eq([user])
end
end
context 'using an internal project' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :internal) }
it 'returns users that are administrators' do
user = build(:user, admin: true)
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project([user], project))
.to eq([user])
end
it 'returns internal users while skipping external users' do
user1 = build(:user)
user2 = build(:user, external: true)
users = [user1, user2]
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project(users, project))
.to eq([user1])
end
it 'returns external users if they are the project owner' do
user1 = build(:user, external: true)
user2 = build(:user, external: true)
users = [user1, user2]
2017-09-10 17:25:29 +05:30
expect(project).to receive(:owner).at_least(:once).and_return(user1)
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project(users, project))
.to eq([user1])
end
it 'returns external users if they are project members' do
user1 = build(:user, external: true)
user2 = build(:user, external: true)
users = [user1, user2]
2017-09-10 17:25:29 +05:30
expect(project.team).to receive(:members).at_least(:once).and_return([user1])
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project(users, project))
.to eq([user1])
end
it 'returns an empty Array if all users are external users without access' do
user1 = build(:user, external: true)
user2 = build(:user, external: true)
users = [user1, user2]
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project(users, project))
.to eq([])
end
end
context 'using a private project' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :private) }
it 'returns users that are administrators' do
user = build(:user, admin: true)
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project([user], project))
.to eq([user])
end
it 'returns external users if they are the project owner' do
user1 = build(:user, external: true)
user2 = build(:user, external: true)
users = [user1, user2]
2017-09-10 17:25:29 +05:30
expect(project).to receive(:owner).at_least(:once).and_return(user1)
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project(users, project))
.to eq([user1])
end
it 'returns external users if they are project members' do
user1 = build(:user, external: true)
user2 = build(:user, external: true)
users = [user1, user2]
2017-09-10 17:25:29 +05:30
expect(project.team).to receive(:members).at_least(:once).and_return([user1])
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project(users, project))
.to eq([user1])
end
it 'returns an empty Array if all users are internal users without access' do
user1 = build(:user)
user2 = build(:user)
users = [user1, user2]
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project(users, project))
.to eq([])
end
it 'returns an empty Array if all users are external users without access' do
user1 = build(:user, external: true)
user2 = build(:user, external: true)
users = [user1, user2]
2017-09-10 17:25:29 +05:30
expect(described_class.users_that_can_read_project(users, project))
.to eq([])
end
end
end
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
describe '.users_that_can_read_personal_snippet' do
def users_for_snippet(snippet)
described_class.users_that_can_read_personal_snippet(users, snippet)
end
let(:users) { create_list(:user, 3) }
let(:author) { users[0] }
it 'private snippet is readable only by its author' do
snippet = create(:personal_snippet, :private, author: author)
expect(users_for_snippet(snippet)).to match_array([author])
end
it 'internal snippet is readable by all registered users' do
snippet = create(:personal_snippet, :public, author: author)
expect(users_for_snippet(snippet)).to match_array(users)
end
it 'public snippet is readable by all users' do
snippet = create(:personal_snippet, :public, author: author)
expect(users_for_snippet(snippet)).to match_array(users)
end
end
2018-03-27 19:54:05 +05:30
describe '.merge_requests_readable_by_user' do
context 'with an admin' do
it 'returns all merge requests' do
user = build(:user, admin: true)
merge_request = build(:merge_request)
expect(described_class.merge_requests_readable_by_user([merge_request], user))
.to eq([merge_request])
end
end
context 'without a user' do
it 'returns merge_requests that are publicly visible' do
hidden_merge_request = build(:merge_request)
visible_merge_request = build(:merge_request, source_project: build(:project, :public))
merge_requests = described_class
.merge_requests_readable_by_user([hidden_merge_request, visible_merge_request])
expect(merge_requests).to eq([visible_merge_request])
end
end
context 'with a user' do
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:merge_request) { create(:merge_request, source_project: project) }
let(:cross_project_merge_request) do
create(:merge_request, source_project: create(:project, :public))
end
let(:other_merge_request) { create(:merge_request) }
let(:all_merge_requests) do
[merge_request, cross_project_merge_request, other_merge_request]
end
subject(:readable_merge_requests) do
described_class.merge_requests_readable_by_user(all_merge_requests, user)
end
before do
project.add_developer(user)
end
it 'returns projects visible to the user' do
expect(readable_merge_requests).to contain_exactly(merge_request, cross_project_merge_request)
end
context 'when a user cannot read cross project and a filter is passed' do
before do
allow(described_class).to receive(:allowed?).and_call_original
expect(described_class).to receive(:allowed?).with(user, :read_cross_project) { false }
end
subject(:readable_merge_requests) do
read_cross_project_filter = -> (merge_requests) do
merge_requests.select { |mr| mr.source_project == project }
end
described_class.merge_requests_readable_by_user(
all_merge_requests, user,
filters: { read_cross_project: read_cross_project_filter }
)
end
it 'returns only MRs of the specified project without checking access on others' do
expect(described_class).not_to receive(:allowed?).with(user, :read_merge_request, cross_project_merge_request)
expect(readable_merge_requests).to contain_exactly(merge_request)
end
end
end
end
2016-09-13 17:45:13 +05:30
describe '.issues_readable_by_user' do
context 'with an admin user' do
it 'returns all given issues' do
user = build(:user, admin: true)
issue = build(:issue)
2017-09-10 17:25:29 +05:30
expect(described_class.issues_readable_by_user([issue], user))
.to eq([issue])
2016-09-13 17:45:13 +05:30
end
end
context 'with a regular user' do
it 'returns the issues readable by the user' do
user = build(:user)
issue = build(:issue)
expect(issue).to receive(:readable_by?).with(user).and_return(true)
2017-09-10 17:25:29 +05:30
expect(described_class.issues_readable_by_user([issue], user))
.to eq([issue])
2016-09-13 17:45:13 +05:30
end
it 'returns an empty Array when no issues are readable' do
user = build(:user)
issue = build(:issue)
expect(issue).to receive(:readable_by?).with(user).and_return(false)
expect(described_class.issues_readable_by_user([issue], user)).to eq([])
end
end
context 'without a regular user' do
it 'returns issues that are publicly visible' do
hidden_issue = build(:issue)
visible_issue = build(:issue)
expect(hidden_issue).to receive(:publicly_visible?).and_return(false)
expect(visible_issue).to receive(:publicly_visible?).and_return(true)
2017-09-10 17:25:29 +05:30
issues = described_class
.issues_readable_by_user([hidden_issue, visible_issue])
2016-09-13 17:45:13 +05:30
expect(issues).to eq([visible_issue])
end
end
2018-03-27 19:54:05 +05:30
context 'when the user cannot read cross project' do
let(:user) { create(:user) }
let(:issue) { create(:issue) }
let(:other_project_issue) { create(:issue) }
let(:project) { issue.project }
before do
project.add_developer(user)
allow(described_class).to receive(:allowed?).and_call_original
allow(described_class).to receive(:allowed?).with(user, :read_cross_project, any_args) { false }
end
it 'excludes issues from other projects whithout checking separatly when passing a scope' do
expect(described_class).not_to receive(:allowed?).with(user, :read_issue, other_project_issue)
filters = { read_cross_project: -> (issues) { issues.where(project: project) } }
result = described_class.issues_readable_by_user(Issue.all, user, filters: filters)
expect(result).to contain_exactly(issue)
end
end
2016-09-13 17:45:13 +05:30
end
2016-09-29 09:46:39 +05:30
describe '.project_disabled_features_rules' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :wiki_disabled) }
2016-09-29 09:46:39 +05:30
2017-09-10 17:25:29 +05:30
subject { described_class.policy_for(project.owner, project) }
2016-09-29 09:46:39 +05:30
context 'wiki named abilities' do
it 'disables wiki abilities if the project has no wiki' do
2017-09-10 17:25:29 +05:30
expect(subject).not_to be_allowed(:read_wiki)
expect(subject).not_to be_allowed(:create_wiki)
expect(subject).not_to be_allowed(:update_wiki)
expect(subject).not_to be_allowed(:admin_wiki)
2016-09-29 09:46:39 +05:30
end
end
end
end