debian-mirror-gitlab/spec/requests/api/graphql/group/group_members_spec.rb

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

257 lines
7.5 KiB
Ruby
Raw Permalink Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe 'getting group members information', feature_category: :subgroups do
2020-11-24 15:15:51 +05:30
include GraphqlHelpers
2021-02-22 17:27:13 +05:30
let_it_be(:parent_group) { create(:group, :public) }
2020-11-24 15:15:51 +05:30
let_it_be(:user) { create(:user) }
2022-08-13 15:12:31 +05:30
let_it_be(:user_1) { create(:user, username: 'user', name: 'Same Name') }
let_it_be(:user_2) { create(:user, username: 'test', name: 'Same Name') }
2020-11-24 15:15:51 +05:30
2021-02-22 17:27:13 +05:30
before_all do
[user_1, user_2].each { |user| parent_group.add_guest(user) }
2020-11-24 15:15:51 +05:30
end
2021-09-04 01:27:46 +05:30
context 'when a member is invited only via email' do
before do
create(:group_member, :invited, source: parent_group)
end
it 'returns null in the user field' do
fetch_members(group: parent_group, args: { relations: [:DIRECT] })
expect(graphql_errors).to be_nil
expect(graphql_data_at(:group, :group_members, :edges, :node)).to contain_exactly(
2022-07-16 23:28:13 +05:30
{ 'user' => a_graphql_entity_for(user_1) },
{ 'user' => a_graphql_entity_for(user_2) },
2021-09-04 01:27:46 +05:30
'user' => nil
)
end
end
2020-11-24 15:15:51 +05:30
context 'when the request is correct' do
it_behaves_like 'a working graphql query' do
2021-03-08 18:12:59 +05:30
before do
2021-02-22 17:27:13 +05:30
fetch_members
2020-11-24 15:15:51 +05:30
end
end
it 'returns group members successfully' do
2021-02-22 17:27:13 +05:30
fetch_members
2020-11-24 15:15:51 +05:30
expect(graphql_errors).to be_nil
2021-02-22 17:27:13 +05:30
expect_array_response(user_1, user_2)
2020-11-24 15:15:51 +05:30
end
2022-08-13 15:12:31 +05:30
describe 'search argument' do
it 'returns members that match the search query' do
fetch_members(args: { search: 'test' })
2020-11-24 15:15:51 +05:30
2022-08-13 15:12:31 +05:30
expect(graphql_errors).to be_nil
expect_array_response(user_2)
end
context 'when paginating' do
it 'returns correct results' do
fetch_members(args: { search: 'Same Name', first: 1 })
expect_array_response(user_1)
next_cursor = graphql_data_at(:group, :groupMembers, :pageInfo, :endCursor)
fetch_members(args: { search: 'Same Name', first: 1, after: next_cursor })
expect_array_response(user_2)
end
end
2020-11-24 15:15:51 +05:30
end
end
2022-05-07 20:08:51 +05:30
context "when requesting member's notification email" do
context 'when current_user is admin' do
let_it_be(:admin_user) { create(:user, :admin) }
it 'returns notification email' do
fetch_members_notification_email(current_user: admin_user)
notification_emails = graphql_data_at(:group, :group_members, :edges, :node, :notification_email)
expect(notification_emails).to all be_present
expect(graphql_errors).to be_nil
end
end
context 'when current_user is not admin' do
it 'returns an error' do
fetch_members_notification_email
expect(graphql_errors.first)
.to include('path' => ['group', 'groupMembers', 'edges', 0, 'node', 'notificationEmail'],
'message' => a_string_including("you don't have permission to perform this action"))
end
end
end
2022-07-16 23:28:13 +05:30
context 'by access levels' do
before do
parent_group.add_owner(user_1)
parent_group.add_maintainer(user_2)
end
subject(:by_access_levels) { fetch_members(group: parent_group, args: { access_levels: access_levels }) }
context 'by owner' do
let(:access_levels) { :OWNER }
it 'returns owner' do
by_access_levels
expect(graphql_errors).to be_nil
expect_array_response(user_1)
end
end
context 'by maintainer' do
let(:access_levels) { :MAINTAINER }
it 'returns maintainer' do
by_access_levels
expect(graphql_errors).to be_nil
expect_array_response(user_2)
end
end
context 'by owner and maintainer' do
let(:access_levels) { [:OWNER, :MAINTAINER] }
it 'returns owner and maintainer' do
by_access_levels
expect(graphql_errors).to be_nil
expect_array_response(user_1, user_2)
end
end
end
2021-02-22 17:27:13 +05:30
context 'member relations' do
let_it_be(:child_group) { create(:group, :public, parent: parent_group) }
let_it_be(:grandchild_group) { create(:group, :public, parent: child_group) }
2022-03-02 08:16:31 +05:30
let_it_be(:invited_group) { create(:group, :public) }
2021-02-22 17:27:13 +05:30
let_it_be(:child_user) { create(:user) }
let_it_be(:grandchild_user) { create(:user) }
2022-03-02 08:16:31 +05:30
let_it_be(:invited_user) { create(:user) }
let_it_be(:group_link) { create(:group_group_link, shared_group: child_group, shared_with_group: invited_group) }
2021-02-22 17:27:13 +05:30
before_all do
child_group.add_guest(child_user)
grandchild_group.add_guest(grandchild_user)
2022-03-02 08:16:31 +05:30
invited_group.add_guest(invited_user)
2021-02-22 17:27:13 +05:30
end
it 'returns direct members' do
fetch_members(group: child_group, args: { relations: [:DIRECT] })
expect(graphql_errors).to be_nil
expect_array_response(child_user)
end
2022-10-11 01:57:18 +05:30
it 'returns invited members and inherited members of a shared group' do
2022-03-02 08:16:31 +05:30
fetch_members(group: child_group, args: { relations: [:DIRECT, :INHERITED, :SHARED_FROM_GROUPS] })
expect(graphql_errors).to be_nil
expect_array_response(invited_user, user_1, user_2, child_user)
end
2022-10-11 01:57:18 +05:30
it 'returns invited members and inherited members of an ancestor of a shared group' do
fetch_members(group: grandchild_group, args: { relations: [:DIRECT, :INHERITED, :SHARED_FROM_GROUPS] })
expect(graphql_errors).to be_nil
expect_array_response(grandchild_user, invited_user, user_1, user_2, child_user)
end
2021-02-22 17:27:13 +05:30
it 'returns direct and inherited members' do
fetch_members(group: child_group, args: { relations: [:DIRECT, :INHERITED] })
expect(graphql_errors).to be_nil
expect_array_response(child_user, user_1, user_2)
end
it 'returns direct, inherited, and descendant members' do
fetch_members(group: child_group, args: { relations: [:DIRECT, :INHERITED, :DESCENDANTS] })
expect(graphql_errors).to be_nil
expect_array_response(child_user, user_1, user_2, grandchild_user)
end
it 'returns an error for an invalid member relation' do
fetch_members(group: child_group, args: { relations: [:OBLIQUE] })
expect(graphql_errors.first)
.to include('path' => %w[query group groupMembers relations],
'message' => a_string_including('invalid value ([OBLIQUE])'))
end
end
context 'when unauthenticated' do
2021-03-08 18:12:59 +05:30
it 'returns visible members' do
2021-02-22 17:27:13 +05:30
fetch_members(current_user: nil)
2021-03-08 18:12:59 +05:30
expect_array_response(user_1, user_2)
2021-02-22 17:27:13 +05:30
end
end
def fetch_members(group: parent_group, current_user: user, args: {})
post_graphql(members_query(group.full_path, args), current_user: current_user)
2020-11-24 15:15:51 +05:30
end
2022-05-07 20:08:51 +05:30
def fetch_members_notification_email(group: parent_group, current_user: user)
post_graphql(member_notification_email_query(group.full_path), current_user: current_user)
end
2021-02-22 17:27:13 +05:30
def members_query(group_path, args = {})
2020-11-24 15:15:51 +05:30
members_node = <<~NODE
edges {
node {
user {
id
}
}
}
2022-08-13 15:12:31 +05:30
pageInfo {
endCursor
}
2020-11-24 15:15:51 +05:30
NODE
graphql_query_for("group",
2021-02-22 17:27:13 +05:30
{ full_path: group_path },
2020-11-24 15:15:51 +05:30
[query_graphql_field("groupMembers", args, members_node)]
)
end
2022-05-07 20:08:51 +05:30
def member_notification_email_query(group_path)
members_node = <<~NODE
edges {
node {
user {
id
}
notificationEmail
}
}
NODE
graphql_query_for("group",
{ full_path: group_path },
[query_graphql_field("groupMembers", {}, members_node)]
)
end
2020-11-24 15:15:51 +05:30
def expect_array_response(*items)
expect(response).to have_gitlab_http_status(:success)
2022-07-16 23:28:13 +05:30
members = graphql_data_at(:group, :group_members, :edges, :node, :user)
2021-03-08 18:12:59 +05:30
2022-07-16 23:28:13 +05:30
expect(members).to match_array(items.map { |u| a_graphql_entity_for(u) })
2020-11-24 15:15:51 +05:30
end
end