diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
commit | 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c (patch) | |
tree | 4dcc884cf6d81db44adae4aa99f8ec1233a41f55 /spec/requests/api/graphql/current_user/groups_query_spec.rb | |
parent | 744144d28e3e7fddc117924fef88de5d9674fe4c (diff) |
Add latest changes from gitlab-org/gitlab@14-3-stable-eev14.3.0-rc42
Diffstat (limited to 'spec/requests/api/graphql/current_user/groups_query_spec.rb')
-rw-r--r-- | spec/requests/api/graphql/current_user/groups_query_spec.rb | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/spec/requests/api/graphql/current_user/groups_query_spec.rb b/spec/requests/api/graphql/current_user/groups_query_spec.rb new file mode 100644 index 00000000000..39f323b21a3 --- /dev/null +++ b/spec/requests/api/graphql/current_user/groups_query_spec.rb @@ -0,0 +1,112 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'Query current user groups' do + include GraphqlHelpers + + let_it_be(:user) { create(:user) } + let_it_be(:guest_group) { create(:group, name: 'public guest', path: 'public-guest') } + let_it_be(:private_maintainer_group) { create(:group, :private, name: 'b private maintainer', path: 'b-private-maintainer') } + let_it_be(:public_developer_group) { create(:group, :private, project_creation_level: nil, name: 'c public developer', path: 'c-public-developer') } + let_it_be(:public_maintainer_group) { create(:group, :private, name: 'a public maintainer', path: 'a-public-maintainer') } + + let(:group_arguments) { {} } + let(:current_user) { user } + + let(:fields) do + <<~GRAPHQL + nodes { id path fullPath name } + GRAPHQL + end + + let(:query) do + graphql_query_for('currentUser', {}, query_graphql_field('groups', group_arguments, fields)) + end + + before_all do + guest_group.add_guest(user) + private_maintainer_group.add_maintainer(user) + public_developer_group.add_developer(user) + public_maintainer_group.add_maintainer(user) + end + + subject { graphql_data.dig('currentUser', 'groups', 'nodes') } + + before do + post_graphql(query, current_user: current_user) + end + + it_behaves_like 'a working graphql query' + + it 'avoids N+1 queries', :request_store do + control = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: current_user) } + + new_group = create(:group, :private) + new_group.add_maintainer(current_user) + + expect { post_graphql(query, current_user: current_user) }.not_to exceed_query_limit(control) + end + + it 'returns all groups where the user is a direct member' do + is_expected.to match( + expected_group_hash( + public_maintainer_group, + private_maintainer_group, + public_developer_group, + guest_group + ) + ) + end + + context 'when permission_scope is CREATE_PROJECTS' do + let(:group_arguments) { { permission_scope: :CREATE_PROJECTS } } + + specify do + is_expected.to match( + expected_group_hash( + public_maintainer_group, + private_maintainer_group, + public_developer_group + ) + ) + end + + context 'when search is provided' do + let(:group_arguments) { { permission_scope: :CREATE_PROJECTS, search: 'maintainer' } } + + specify do + is_expected.to match( + expected_group_hash( + public_maintainer_group, + private_maintainer_group + ) + ) + end + end + end + + context 'when search is provided' do + let(:group_arguments) { { search: 'maintainer' } } + + specify do + is_expected.to match( + expected_group_hash( + public_maintainer_group, + private_maintainer_group + ) + ) + end + end + + def expected_group_hash(*groups) + groups.map do |group| + { + 'id' => group.to_global_id.to_s, + 'name' => group.name, + 'path' => group.path, + 'fullPath' => group.full_path + } + end + end +end |