2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-05 12:46:32 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Projects::ParticipantsService do
|
2020-12-10 10:10:12 -05:00
|
|
|
describe '#execute' do
|
2020-12-11 04:09:48 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project, :public) }
|
|
|
|
let_it_be(:noteable) { create(:issue, project: project) }
|
|
|
|
|
|
|
|
before_all do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
2019-03-22 11:51:14 -04:00
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
def run_service
|
|
|
|
described_class.new(project, user).execute(noteable)
|
|
|
|
end
|
2019-03-22 11:51:14 -04:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
context 'N+1 checks' do
|
|
|
|
before do
|
|
|
|
run_service # warmup, runs table cache queries and create queries
|
|
|
|
BatchLoader::Executor.clear_current
|
|
|
|
end
|
2019-03-22 11:51:14 -04:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
it 'avoids N+1 UserDetail queries' do
|
|
|
|
project.add_developer(create(:user))
|
2020-12-10 10:10:12 -05:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
control_count = ActiveRecord::QueryRecorder.new { run_service.to_a }.count
|
2020-12-10 10:10:12 -05:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
BatchLoader::Executor.clear_current
|
2020-12-10 10:10:12 -05:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
project.add_developer(create(:user, status: build(:user_status, availability: :busy)))
|
2020-12-10 10:10:12 -05:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
expect { run_service.to_a }.not_to exceed_query_limit(control_count)
|
|
|
|
end
|
2019-03-22 11:51:14 -04:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
it 'avoids N+1 groups queries' do
|
|
|
|
group_1 = create(:group)
|
|
|
|
group_1.add_owner(user)
|
2019-03-22 11:51:14 -04:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
control_count = ActiveRecord::QueryRecorder.new { run_service }.count
|
2020-12-10 10:10:12 -05:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
BatchLoader::Executor.clear_current
|
2020-12-10 10:10:12 -05:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
group_2 = create(:group)
|
|
|
|
group_2.add_owner(user)
|
|
|
|
|
|
|
|
expect { run_service }.not_to exceed_query_limit(control_count)
|
|
|
|
end
|
|
|
|
end
|
2019-03-22 11:51:14 -04:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
it 'does not return duplicate author' do
|
|
|
|
participants = run_service
|
2019-03-22 11:51:14 -04:00
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
expect(participants.count { |p| p[:username] == noteable.author.username }).to eq 1
|
2019-03-22 11:51:14 -04:00
|
|
|
end
|
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
describe 'group items' do
|
|
|
|
subject(:group_items) { run_service.select { |hash| hash[:type].eql?('Group') } }
|
2017-01-05 12:46:32 -05:00
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
describe 'group user counts' do
|
|
|
|
let(:group_1) { create(:group) }
|
|
|
|
let(:group_2) { create(:group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group_1.add_owner(user)
|
|
|
|
group_1.add_owner(create(:user))
|
2017-01-05 12:46:32 -05:00
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
group_2.add_owner(user)
|
|
|
|
create(:group_member, :access_request, group: group_2, user: create(:user))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns correct user counts for groups' do
|
|
|
|
expect(group_items).to contain_exactly(
|
|
|
|
a_hash_including(name: group_1.full_name, count: 2),
|
|
|
|
a_hash_including(name: group_2.full_name, count: 1)
|
|
|
|
)
|
|
|
|
end
|
2017-01-05 12:46:32 -05:00
|
|
|
end
|
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
describe 'avatar_url' do
|
|
|
|
let(:group) { create(:group, avatar: fixture_file_upload('spec/fixtures/dk.png')) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an url for the avatar' do
|
|
|
|
expect(group_items.size).to eq 1
|
|
|
|
expect(group_items.first[:avatar_url]).to eq("/uploads/-/system/group/avatar/#{group.id}/dk.png")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an url for the avatar with relative url' do
|
|
|
|
stub_config_setting(relative_url_root: '/gitlab')
|
|
|
|
stub_config_setting(url: Settings.send(:build_gitlab_url))
|
2017-01-05 12:46:32 -05:00
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
expect(group_items.size).to eq 1
|
|
|
|
expect(group_items.first[:avatar_url]).to eq("/gitlab/uploads/-/system/group/avatar/#{group.id}/dk.png")
|
|
|
|
end
|
2017-01-05 12:46:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-04 13:17:06 -04:00
|
|
|
|
|
|
|
describe '#project_members' do
|
|
|
|
subject(:usernames) { service.project_members.map { |member| member[:username] } }
|
|
|
|
|
|
|
|
context 'when there is a project in group namespace' do
|
2020-01-10 04:07:49 -05:00
|
|
|
let_it_be(:public_group) { create(:group, :public) }
|
|
|
|
let_it_be(:public_project) { create(:project, :public, namespace: public_group)}
|
2019-07-04 13:17:06 -04:00
|
|
|
|
2020-01-10 04:07:49 -05:00
|
|
|
let_it_be(:public_group_owner) { create(:user) }
|
2019-07-04 13:17:06 -04:00
|
|
|
|
|
|
|
let(:service) { described_class.new(public_project, create(:user)) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
public_group.add_owner(public_group_owner)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns members of a group' do
|
|
|
|
expect(usernames).to include(public_group_owner.username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a private group and a public project' do
|
2020-01-10 04:07:49 -05:00
|
|
|
let_it_be(:public_group) { create(:group, :public) }
|
|
|
|
let_it_be(:private_group) { create(:group, :private, :nested) }
|
|
|
|
let_it_be(:public_project) { create(:project, :public, namespace: public_group)}
|
2019-07-04 13:17:06 -04:00
|
|
|
|
2020-01-10 04:07:49 -05:00
|
|
|
let_it_be(:project_issue) { create(:issue, project: public_project)}
|
2019-07-04 13:17:06 -04:00
|
|
|
|
2020-01-10 04:07:49 -05:00
|
|
|
let_it_be(:public_group_owner) { create(:user) }
|
|
|
|
let_it_be(:private_group_member) { create(:user) }
|
|
|
|
let_it_be(:public_project_maintainer) { create(:user) }
|
|
|
|
let_it_be(:private_group_owner) { create(:user) }
|
2019-07-04 13:17:06 -04:00
|
|
|
|
2020-01-10 04:07:49 -05:00
|
|
|
let_it_be(:group_ancestor_owner) { create(:user) }
|
2019-07-04 13:17:06 -04:00
|
|
|
|
2020-04-01 11:07:45 -04:00
|
|
|
before_all do
|
2019-07-04 13:17:06 -04:00
|
|
|
public_group.add_owner public_group_owner
|
|
|
|
private_group.add_developer private_group_member
|
|
|
|
public_project.add_maintainer public_project_maintainer
|
|
|
|
|
|
|
|
private_group.add_owner private_group_owner
|
|
|
|
private_group.parent.add_owner group_ancestor_owner
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the private group is invited to the public project' do
|
2020-04-01 11:07:45 -04:00
|
|
|
before_all do
|
2019-07-04 13:17:06 -04:00
|
|
|
create(:project_group_link, group: private_group, project: public_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user who is outside the public project and the private group is signed in' do
|
|
|
|
let(:service) { described_class.new(public_project, create(:user)) }
|
|
|
|
|
|
|
|
it 'does not return the private group' do
|
|
|
|
expect(usernames).not_to include(private_group.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return private group members' do
|
|
|
|
expect(usernames).not_to include(private_group_member.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the project maintainer' do
|
|
|
|
expect(usernames).to include(public_project_maintainer.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns project members from an invited public group' do
|
|
|
|
invited_public_group = create(:group, :public)
|
|
|
|
invited_public_group.add_owner create(:user)
|
|
|
|
|
|
|
|
create(:project_group_link, group: invited_public_group, project: public_project)
|
|
|
|
|
|
|
|
expect(usernames).to include(invited_public_group.users.first.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return ancestors of the private group' do
|
|
|
|
expect(usernames).not_to include(group_ancestor_owner.username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when private group owner is signed in' do
|
|
|
|
let(:service) { described_class.new(public_project, private_group_owner) }
|
|
|
|
|
|
|
|
it 'returns private group members' do
|
|
|
|
expect(usernames).to include(private_group_member.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns ancestors of the the private group' do
|
|
|
|
expect(usernames).to include(group_ancestor_owner.username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the namespace owner of the public project is signed in' do
|
|
|
|
let(:service) { described_class.new(public_project, public_group_owner) }
|
|
|
|
|
|
|
|
it 'returns private group members' do
|
|
|
|
expect(usernames).to include(private_group_member.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return members of the ancestral groups of the private group' do
|
|
|
|
expect(usernames).to include(group_ancestor_owner.username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-01-05 12:46:32 -05:00
|
|
|
end
|