2021-01-06 04:10:31 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Projects::ProjectMembersHelper do
|
2021-01-22 07:08:52 -05:00
|
|
|
include MembersPresentation
|
|
|
|
|
2021-01-06 04:10:31 -05:00
|
|
|
let_it_be(:current_user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
|
|
|
|
let(:allow_admin_project) { nil }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:current_user).and_return(current_user)
|
|
|
|
allow(helper).to receive(:can?).with(current_user, :admin_project_member, project).and_return(allow_admin_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'when `current_user` does not have `admin_project_member` permissions' do
|
|
|
|
let(:allow_admin_project) { false }
|
|
|
|
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can_manage_project_members?' do
|
|
|
|
subject { helper.can_manage_project_members?(project) }
|
|
|
|
|
|
|
|
context 'when `current_user` has `admin_project_member` permissions' do
|
|
|
|
let(:allow_admin_project) { true }
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'when `current_user` does not have `admin_project_member` permissions'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#show_groups?' do
|
|
|
|
subject { helper.show_groups?(project.project_group_links) }
|
|
|
|
|
|
|
|
context 'when group links exist' do
|
|
|
|
let!(:project_group_link) { create(:project_group_link, project: project) }
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when `search_groups` param is set' do
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:params).and_return({ search_groups: 'foo' })
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when `search_groups` param is not set and group links do not exist' do
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#show_invited_members?' do
|
|
|
|
subject { helper.show_invited_members?(project, project.project_members.invite) }
|
|
|
|
|
|
|
|
context 'when `current_user` has `admin_project_member` permissions' do
|
|
|
|
let(:allow_admin_project) { true }
|
|
|
|
|
|
|
|
context 'when invited members exist' do
|
|
|
|
let!(:invite) { create(:project_member, :invited, project: project) }
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when invited members do not exist' do
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'when `current_user` does not have `admin_project_member` permissions'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#show_access_requests?' do
|
|
|
|
subject { helper.show_access_requests?(project, project.requesters) }
|
|
|
|
|
|
|
|
context 'when `current_user` has `admin_project_member` permissions' do
|
|
|
|
let(:allow_admin_project) { true }
|
|
|
|
|
|
|
|
context 'when access requests exist' do
|
|
|
|
let!(:access_request) { create(:project_member, :access_request, project: project) }
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when access requests do not exist' do
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'when `current_user` does not have `admin_project_member` permissions'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#groups_tab_active?' do
|
|
|
|
subject { helper.groups_tab_active? }
|
|
|
|
|
|
|
|
context 'when `search_groups` param is set' do
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:params).and_return({ search_groups: 'foo' })
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when `search_groups` param is not set' do
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#current_user_is_group_owner?' do
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
|
|
|
|
subject { helper.current_user_is_group_owner?(project2) }
|
|
|
|
|
|
|
|
describe "when current user is the owner of the project's parent group" do
|
|
|
|
let(:project2) { create(:project, namespace: group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_owner(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when current user is not the owner of the project's parent group" do
|
|
|
|
let_it_be(:user) { create(:user) }
|
2021-04-19 14:09:09 -04:00
|
|
|
|
2021-01-06 04:10:31 -05:00
|
|
|
let(:project2) { create(:project, namespace: group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when project does not have a parent group" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project2) { create(:project, namespace: user.namespace) }
|
|
|
|
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
|
|
|
end
|
2021-01-22 07:08:52 -05:00
|
|
|
|
|
|
|
describe 'project members' do
|
2021-05-19 20:10:36 -04:00
|
|
|
let_it_be(:members) { create_list(:project_member, 2, project: project) }
|
|
|
|
let_it_be(:group_links) { create_list(:project_group_link, 1, project: project) }
|
|
|
|
let_it_be(:invited) { create_list(:project_member, 2, :invited, project: project) }
|
|
|
|
let_it_be(:access_requests) { create_list(:project_member, 2, :access_request, project: project) }
|
2021-01-22 07:08:52 -05:00
|
|
|
|
2021-05-19 20:10:36 -04:00
|
|
|
let(:members_collection) { members }
|
2021-01-22 07:08:52 -05:00
|
|
|
|
2021-05-19 20:10:36 -04:00
|
|
|
describe '#project_members_app_data_json' do
|
2021-01-22 07:08:52 -05:00
|
|
|
let(:allow_admin_project) { true }
|
2021-05-11 14:10:36 -04:00
|
|
|
|
2021-05-19 20:10:36 -04:00
|
|
|
subject do
|
|
|
|
Gitlab::Json.parse(
|
|
|
|
helper.project_members_app_data_json(
|
|
|
|
project,
|
|
|
|
members: present_members(members_collection),
|
|
|
|
group_links: group_links,
|
|
|
|
invited: present_members(invited),
|
|
|
|
access_requests: present_members(access_requests)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2021-01-22 07:08:52 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:project_project_member_path).with(project, ':id').and_return('/foo-bar/-/project_members/:id')
|
|
|
|
end
|
|
|
|
|
2021-05-11 14:10:36 -04:00
|
|
|
it 'returns expected json' do
|
|
|
|
expected = {
|
2021-01-22 07:08:52 -05:00
|
|
|
source_id: project.id,
|
2021-05-11 14:10:36 -04:00
|
|
|
can_manage_members: true
|
|
|
|
}.as_json
|
|
|
|
|
|
|
|
expect(subject).to include(expected)
|
|
|
|
end
|
|
|
|
|
2021-05-19 20:10:36 -04:00
|
|
|
it 'sets `members` property that matches json schema' do
|
|
|
|
expect(subject['user']['members'].to_json).to match_schema('members')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets `member_path` property' do
|
|
|
|
expect(subject['user']['member_path']).to eq('/foo-bar/-/project_members/:id')
|
2021-01-22 07:08:52 -05:00
|
|
|
end
|
2021-04-22 08:09:49 -04:00
|
|
|
|
|
|
|
context 'when pagination is not available' do
|
|
|
|
it 'sets `pagination` attribute to expected json' do
|
2021-05-11 14:10:36 -04:00
|
|
|
expected = {
|
2021-04-22 08:09:49 -04:00
|
|
|
current_page: nil,
|
|
|
|
per_page: nil,
|
|
|
|
total_items: 2,
|
|
|
|
param_name: nil,
|
|
|
|
params: {}
|
2021-05-11 14:10:36 -04:00
|
|
|
}.as_json
|
|
|
|
|
2021-05-19 20:10:36 -04:00
|
|
|
expect(subject['invite']['pagination']).to include(expected)
|
2021-04-22 08:09:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pagination is available' do
|
2021-05-19 20:10:36 -04:00
|
|
|
let(:members_collection) { Kaminari.paginate_array(members).page(1).per(1) }
|
2021-04-22 08:09:49 -04:00
|
|
|
|
|
|
|
it 'sets `pagination` attribute to expected json' do
|
2021-05-11 14:10:36 -04:00
|
|
|
expected = {
|
2021-04-22 08:09:49 -04:00
|
|
|
current_page: 1,
|
|
|
|
per_page: 1,
|
|
|
|
total_items: 2,
|
|
|
|
param_name: :page,
|
|
|
|
params: { search_groups: nil }
|
2021-05-11 14:10:36 -04:00
|
|
|
}.as_json
|
|
|
|
|
2021-05-19 20:10:36 -04:00
|
|
|
expect(subject['user']['pagination']).to match(expected)
|
2021-04-22 08:09:49 -04:00
|
|
|
end
|
|
|
|
end
|
2021-01-22 07:08:52 -05:00
|
|
|
end
|
|
|
|
end
|
2021-01-06 04:10:31 -05:00
|
|
|
end
|