2016-05-26 11:38:28 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe Ability do
|
2017-03-03 01:43:05 +00:00
|
|
|
context 'using a nil subject' do
|
2017-04-06 21:09:58 +00:00
|
|
|
it 'has no permissions' do
|
2017-07-25 17:09:00 +00:00
|
|
|
expect(described_class.policy_for(nil, nil)).to be_banned
|
2017-03-03 01:43:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-20 04:52:31 +00:00
|
|
|
describe '.can_edit_note?' do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2017-01-26 22:44:58 +00:00
|
|
|
let(:note) { create(:note_on_issue, project: project) }
|
2016-07-20 04:52:31 +00:00
|
|
|
|
|
|
|
context 'using an anonymous user' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(described_class.can_edit_note?(nil, note)).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'using a system note' do
|
|
|
|
it 'returns false' do
|
|
|
|
system_note = create(:note, system: true)
|
|
|
|
user = create(:user)
|
|
|
|
|
|
|
|
expect(described_class.can_edit_note?(user, system_note)).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'using users with different access levels' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'returns true for the author' do
|
|
|
|
expect(described_class.can_edit_note?(note.author, note)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for a guest user' do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_guest(user)
|
2016-07-20 04:52:31 +00:00
|
|
|
|
|
|
|
expect(described_class.can_edit_note?(user, note)).to be_falsy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for a developer' do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_developer(user)
|
2016-07-20 04:52:31 +00:00
|
|
|
|
|
|
|
expect(described_class.can_edit_note?(user, note)).to be_falsy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true for a master' do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_master(user)
|
2016-07-20 04:52:31 +00:00
|
|
|
|
|
|
|
expect(described_class.can_edit_note?(user, note)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true for a group owner' do
|
|
|
|
group = create(:group)
|
|
|
|
project.project_group_links.create(
|
|
|
|
group: group,
|
|
|
|
group_access: Gitlab::Access::MASTER)
|
|
|
|
group.add_owner(user)
|
|
|
|
|
|
|
|
expect(described_class.can_edit_note?(user, note)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-26 11:38:28 +00:00
|
|
|
describe '.users_that_can_read_project' do
|
|
|
|
context 'using a public project' do
|
|
|
|
it 'returns all the users' do
|
2017-08-02 19:55:11 +00:00
|
|
|
project = create(:project, :public)
|
2016-05-26 11:38:28 +00:00
|
|
|
user = build(:user)
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project([user], project))
|
|
|
|
.to eq([user])
|
2016-05-26 11:38:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'using an internal project' do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :internal) }
|
2016-05-26 11:38:28 +00:00
|
|
|
|
|
|
|
it 'returns users that are administrators' do
|
|
|
|
user = build(:user, admin: true)
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project([user], project))
|
|
|
|
.to eq([user])
|
2016-05-26 11:38:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns internal users while skipping external users' do
|
|
|
|
user1 = build(:user)
|
|
|
|
user2 = build(:user, external: true)
|
|
|
|
users = [user1, user2]
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project(users, project))
|
|
|
|
.to eq([user1])
|
2016-05-26 11:38:28 +00:00
|
|
|
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-07-24 10:35:54 +00:00
|
|
|
expect(project).to receive(:owner).at_least(:once).and_return(user1)
|
2016-05-26 11:38:28 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project(users, project))
|
|
|
|
.to eq([user1])
|
2016-05-26 11:38:28 +00:00
|
|
|
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-07-24 10:35:54 +00:00
|
|
|
expect(project.team).to receive(:members).at_least(:once).and_return([user1])
|
2016-05-26 11:38:28 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project(users, project))
|
|
|
|
.to eq([user1])
|
2016-05-26 11:38:28 +00:00
|
|
|
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-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project(users, project))
|
|
|
|
.to eq([])
|
2016-05-26 11:38:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'using a private project' do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :private) }
|
2016-05-26 11:38:28 +00:00
|
|
|
|
|
|
|
it 'returns users that are administrators' do
|
|
|
|
user = build(:user, admin: true)
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project([user], project))
|
|
|
|
.to eq([user])
|
2016-05-26 11:38:28 +00:00
|
|
|
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-07-24 10:35:54 +00:00
|
|
|
expect(project).to receive(:owner).at_least(:once).and_return(user1)
|
2016-05-26 11:38:28 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project(users, project))
|
|
|
|
.to eq([user1])
|
2016-05-26 11:38:28 +00:00
|
|
|
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-07-24 10:35:54 +00:00
|
|
|
expect(project.team).to receive(:members).at_least(:once).and_return([user1])
|
2016-05-26 11:38:28 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project(users, project))
|
|
|
|
.to eq([user1])
|
2016-05-26 11:38:28 +00:00
|
|
|
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-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project(users, project))
|
|
|
|
.to eq([])
|
2016-05-26 11:38:28 +00:00
|
|
|
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-06-21 13:48:12 +00:00
|
|
|
expect(described_class.users_that_can_read_project(users, project))
|
|
|
|
.to eq([])
|
2016-05-26 11:38:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-20 18:13:02 +00:00
|
|
|
|
2017-01-05 13:36:06 +00:00
|
|
|
describe '.users_that_can_read_personal_snippet' do
|
2017-01-20 10:28:40 +00:00
|
|
|
def users_for_snippet(snippet)
|
|
|
|
described_class.users_that_can_read_personal_snippet(users, snippet)
|
|
|
|
end
|
|
|
|
|
2017-01-05 13:36:06 +00:00
|
|
|
let(:users) { create_list(:user, 3) }
|
|
|
|
let(:author) { users[0] }
|
|
|
|
|
2017-01-20 10:28:40 +00:00
|
|
|
it 'private snippet is readable only by its author' do
|
|
|
|
snippet = create(:personal_snippet, :private, author: author)
|
2017-01-05 13:36:06 +00:00
|
|
|
|
2017-01-20 10:28:40 +00:00
|
|
|
expect(users_for_snippet(snippet)).to match_array([author])
|
2017-01-05 13:36:06 +00:00
|
|
|
end
|
|
|
|
|
2017-01-20 10:28:40 +00:00
|
|
|
it 'internal snippet is readable by all registered users' do
|
|
|
|
snippet = create(:personal_snippet, :public, author: author)
|
2017-01-05 13:36:06 +00:00
|
|
|
|
2017-01-20 10:28:40 +00:00
|
|
|
expect(users_for_snippet(snippet)).to match_array(users)
|
2017-01-05 13:36:06 +00:00
|
|
|
end
|
|
|
|
|
2017-01-20 10:28:40 +00:00
|
|
|
it 'public snippet is readable by all users' do
|
|
|
|
snippet = create(:personal_snippet, :public, author: author)
|
2017-01-05 13:36:06 +00:00
|
|
|
|
2017-01-20 10:28:40 +00:00
|
|
|
expect(users_for_snippet(snippet)).to match_array(users)
|
2017-01-05 13:36:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-20 18:13:02 +00:00
|
|
|
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-06-21 13:48:12 +00:00
|
|
|
expect(described_class.issues_readable_by_user([issue], user))
|
|
|
|
.to eq([issue])
|
2016-07-20 18:13:02 +00:00
|
|
|
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-06-21 13:48:12 +00:00
|
|
|
expect(described_class.issues_readable_by_user([issue], user))
|
|
|
|
.to eq([issue])
|
2016-07-20 18:13:02 +00:00
|
|
|
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-06-21 13:48:12 +00:00
|
|
|
issues = described_class
|
|
|
|
.issues_readable_by_user([hidden_issue, visible_issue])
|
2016-07-20 18:13:02 +00:00
|
|
|
|
|
|
|
expect(issues).to eq([visible_issue])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-26 20:20:00 +00:00
|
|
|
|
|
|
|
describe '.project_disabled_features_rules' do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :wiki_disabled) }
|
2016-08-26 20:20:00 +00:00
|
|
|
|
2017-04-06 21:09:58 +00:00
|
|
|
subject { described_class.policy_for(project.owner, project) }
|
2016-08-26 20:20:00 +00:00
|
|
|
|
|
|
|
context 'wiki named abilities' do
|
|
|
|
it 'disables wiki abilities if the project has no wiki' do
|
2016-08-01 22:31:21 +00:00
|
|
|
expect(project).to receive(:has_external_wiki?).and_return(false)
|
2017-04-06 21:09:58 +00:00
|
|
|
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-08-26 20:20:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-26 11:38:28 +00:00
|
|
|
end
|