debian-mirror-gitlab/spec/helpers/groups/group_members_helper_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

192 lines
6.8 KiB
Ruby
Raw Normal View History

2019-09-04 21:01:54 +05:30
# frozen_string_literal: true
require "spec_helper"
2020-07-28 23:09:34 +05:30
RSpec.describe Groups::GroupMembersHelper do
2020-11-24 15:15:51 +05:30
include MembersPresentation
2021-01-03 14:25:43 +05:30
let_it_be(:current_user) { create(:user) }
let_it_be(:group) { create(:group) }
before do
2021-10-27 15:23:28 +05:30
allow(helper).to receive(:can?).with(current_user, :export_group_memberships, group).and_return(false)
2021-01-03 14:25:43 +05:30
allow(helper).to receive(:can?).with(current_user, :owner_access, group).and_return(true)
allow(helper).to receive(:current_user).and_return(current_user)
end
2019-09-04 21:01:54 +05:30
2021-01-03 14:25:43 +05:30
describe '.group_member_select_options' do
2019-09-04 21:01:54 +05:30
before do
helper.instance_variable_set(:@group, group)
end
it 'returns an options hash' do
expect(helper.group_member_select_options).to include(multiple: true, scope: :all, email_user: true)
end
end
2020-11-24 15:15:51 +05:30
2021-10-27 15:23:28 +05:30
describe '#group_members_app_data' do
2021-09-04 01:27:46 +05:30
include_context 'group_group_link'
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
let(:members) { create_list(:group_member, 2, group: shared_group, created_by: current_user) }
let(:invited) { create_list(:group_member, 2, :invited, group: shared_group, created_by: current_user) }
let!(:access_requests) { create_list(:group_member, 2, :access_request, group: shared_group, created_by: current_user) }
let(:members_collection) { members }
subject do
2021-10-27 15:23:28 +05:30
helper.group_members_app_data(
shared_group,
members: present_members(members_collection),
invited: present_members(invited),
2022-06-21 17:19:12 +05:30
access_requests: present_members(access_requests),
include_relations: [:inherited, :direct],
search: nil
2021-09-04 01:27:46 +05:30
)
end
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
shared_examples 'members.json' do |member_type|
2021-06-08 01:23:25 +05:30
it 'returns `members` property that matches json schema' do
2021-10-27 15:23:28 +05:30
expect(subject[member_type.to_sym][:members].to_json).to match_schema('members')
2021-09-04 01:27:46 +05:30
end
it 'sets `member_path` property' do
2021-10-27 15:23:28 +05:30
expect(subject[member_type.to_sym][:member_path]).to eq('/groups/foo-bar/-/group_members/:id')
2020-11-24 15:15:51 +05:30
end
end
2021-06-08 01:23:25 +05:30
before do
2021-10-27 15:23:28 +05:30
allow(helper).to receive(:can?).with(current_user, :export_group_memberships, shared_group).and_return(true)
2021-09-04 01:27:46 +05:30
allow(helper).to receive(:group_group_member_path).with(shared_group, ':id').and_return('/groups/foo-bar/-/group_members/:id')
allow(helper).to receive(:group_group_link_path).with(shared_group, ':id').and_return('/groups/foo-bar/-/group_links/:id')
allow(helper).to receive(:can?).with(current_user, :admin_group_member, shared_group).and_return(true)
2021-06-08 01:23:25 +05:30
end
it 'returns expected json' do
expected = {
2021-09-04 01:27:46 +05:30
source_id: shared_group.id,
2021-06-08 01:23:25 +05:30
can_manage_members: true
2021-10-27 15:23:28 +05:30
}
2020-11-24 15:15:51 +05:30
2021-06-08 01:23:25 +05:30
expect(subject).to include(expected)
end
2021-09-04 01:27:46 +05:30
context 'group members' do
it_behaves_like 'members.json', 'user'
2021-01-03 14:25:43 +05:30
context 'with user status set' do
let(:user) { create(:user) }
let!(:status) { create(:user_status, user: user) }
2021-09-04 01:27:46 +05:30
let(:members) { [create(:group_member, group: shared_group, user: user, created_by: current_user)] }
2021-01-03 14:25:43 +05:30
2021-09-04 01:27:46 +05:30
it_behaves_like 'members.json', 'user'
2021-01-03 14:25:43 +05:30
end
2020-11-24 15:15:51 +05:30
end
2021-09-04 01:27:46 +05:30
context 'invited group members' do
it_behaves_like 'members.json', 'invite'
end
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
context 'access requests' do
it_behaves_like 'members.json', 'access_request'
2020-11-24 15:15:51 +05:30
end
2021-09-04 01:27:46 +05:30
context 'group links' do
it 'sets `group.members` property that matches json schema' do
2021-10-27 15:23:28 +05:30
expect(subject[:group][:members].to_json).to match_schema('group_link/group_group_links')
2021-09-04 01:27:46 +05:30
end
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
it 'sets `member_path` property' do
2021-10-27 15:23:28 +05:30
expect(subject[:group][:member_path]).to eq('/groups/foo-bar/-/group_links/:id')
2021-09-04 01:27:46 +05:30
end
2022-06-21 17:19:12 +05:30
context 'inherited' do
let_it_be(:sub_shared_group) { create(:group, parent: shared_group) }
let_it_be(:sub_shared_with_group) { create(:group) }
let_it_be(:sub_group_group_link) { create(:group_group_link, shared_group: sub_shared_group, shared_with_group: sub_shared_with_group) }
let_it_be(:subject_group) { sub_shared_group }
before do
allow(helper).to receive(:group_group_member_path).with(sub_shared_group, ':id').and_return('/groups/foo-bar/-/group_members/:id')
allow(helper).to receive(:group_group_link_path).with(sub_shared_group, ':id').and_return('/groups/foo-bar/-/group_links/:id')
allow(helper).to receive(:can?).with(current_user, :admin_group_member, sub_shared_group).and_return(true)
allow(helper).to receive(:can?).with(current_user, :export_group_memberships, sub_shared_group).and_return(true)
end
subject do
helper.group_members_app_data(
sub_shared_group,
members: present_members(members_collection),
invited: present_members(invited),
access_requests: present_members(access_requests),
include_relations: include_relations,
search: nil
)
end
using RSpec::Parameterized::TableSyntax
where(:include_relations, :result) do
[:inherited, :direct] | lazy { [group_group_link, sub_group_group_link].map(&:id) }
[:inherited] | lazy { [group_group_link].map(&:id) }
[:direct] | lazy { [sub_group_group_link].map(&:id) }
end
with_them do
it 'returns correct group links' do
expect(subject[:group][:members].map { |link| link[:id] }).to match_array(result)
end
end
context 'when group_member_inherited_group disabled' do
before do
stub_feature_flags(group_member_inherited_group: false)
end
where(:include_relations, :result) do
[:inherited, :direct] | lazy { [sub_group_group_link.id] }
[:inherited] | lazy { [sub_group_group_link.id] }
[:direct] | lazy { [sub_group_group_link.id] }
end
with_them do
it 'always returns direct member links' do
expect(subject[:group][:members].map { |link| link[:id] }).to match_array(result)
end
end
end
end
2020-11-24 15:15:51 +05:30
end
2021-01-03 14:25:43 +05:30
2021-06-08 01:23:25 +05:30
context 'when pagination is not available' do
it 'sets `pagination` attribute to expected json' do
expected = {
current_page: nil,
per_page: nil,
total_items: 2,
param_name: nil,
params: {}
}.as_json
2021-10-27 15:23:28 +05:30
expect(subject[:access_request][:pagination].as_json).to include(expected)
2021-06-08 01:23:25 +05:30
end
2021-01-03 14:25:43 +05:30
end
2021-06-08 01:23:25 +05:30
context 'when pagination is available' do
2021-09-04 01:27:46 +05:30
let(:members_collection) { Kaminari.paginate_array(members).page(1).per(1) }
2021-06-08 01:23:25 +05:30
it 'sets `pagination` attribute to expected json' do
expected = {
current_page: 1,
per_page: 1,
total_items: 2,
param_name: :page,
2021-09-04 01:27:46 +05:30
params: { invited_members_page: nil, search_invited: nil }
2021-06-08 01:23:25 +05:30
}.as_json
2021-10-27 15:23:28 +05:30
expect(subject[:user][:pagination].as_json).to include(expected)
2021-06-08 01:23:25 +05:30
end
2021-01-03 14:25:43 +05:30
end
end
2019-09-04 21:01:54 +05:30
end