summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/graphql
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/shared_examples/graphql')
-rw-r--r--spec/support/shared_examples/graphql/label_fields.rb4
-rw-r--r--spec/support/shared_examples/graphql/mutations/boards_create_shared_examples.rb2
-rw-r--r--spec/support/shared_examples/graphql/mutations/create_todo_shared_examples.rb26
-rw-r--r--spec/support/shared_examples/graphql/mutations/issues/permission_check_shared_examples.rb52
-rw-r--r--spec/support/shared_examples/graphql/mutations/merge_requests/permission_check_shared_examples.rb73
5 files changed, 153 insertions, 4 deletions
diff --git a/spec/support/shared_examples/graphql/label_fields.rb b/spec/support/shared_examples/graphql/label_fields.rb
index b1bfb395bc6..caf5dae409a 100644
--- a/spec/support/shared_examples/graphql/label_fields.rb
+++ b/spec/support/shared_examples/graphql/label_fields.rb
@@ -106,13 +106,11 @@ RSpec.shared_examples 'querying a GraphQL type with labels' do
end
it 'batches queries for labels by title' do
- pending('See: https://gitlab.com/gitlab-org/gitlab/-/issues/217767')
-
multi_selection = query_for(label_b, label_c)
single_selection = query_for(label_d)
expect { run_query(multi_selection) }
- .to issue_same_number_of_queries_as { run_query(single_selection) }
+ .to issue_same_number_of_queries_as { run_query(single_selection) }.ignoring_cached_queries
end
end
diff --git a/spec/support/shared_examples/graphql/mutations/boards_create_shared_examples.rb b/spec/support/shared_examples/graphql/mutations/boards_create_shared_examples.rb
index ec64519cd9c..9c0b398a5c1 100644
--- a/spec/support/shared_examples/graphql/mutations/boards_create_shared_examples.rb
+++ b/spec/support/shared_examples/graphql/mutations/boards_create_shared_examples.rb
@@ -65,7 +65,7 @@ RSpec.shared_examples 'boards create mutation' do
let(:params) { { name: name } }
it_behaves_like 'a mutation that returns top-level errors',
- errors: ['group_path or project_path arguments are required']
+ errors: ['Exactly one of group_path or project_path arguments is required']
it 'does not create the board' do
expect { subject }.not_to change { Board.count }
diff --git a/spec/support/shared_examples/graphql/mutations/create_todo_shared_examples.rb b/spec/support/shared_examples/graphql/mutations/create_todo_shared_examples.rb
new file mode 100644
index 00000000000..fbef8be9e88
--- /dev/null
+++ b/spec/support/shared_examples/graphql/mutations/create_todo_shared_examples.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+RSpec.shared_examples 'create todo mutation' do
+ let_it_be(:current_user) { create(:user) }
+
+ let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }, field: nil) }
+
+ context 'when user does not have permission to create todo' do
+ it 'raises error' do
+ expect { mutation.resolve(target_id: global_id_of(target)) }
+ .to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ end
+ end
+
+ context 'when user has permission to create todo' do
+ it 'creates a todo' do
+ target.resource_parent.add_reporter(current_user)
+
+ result = mutation.resolve(target_id: global_id_of(target))
+
+ expect(result[:todo]).to be_valid
+ expect(result[:todo].target).to eq(target)
+ expect(result[:todo].state).to eq('pending')
+ end
+ end
+end
diff --git a/spec/support/shared_examples/graphql/mutations/issues/permission_check_shared_examples.rb b/spec/support/shared_examples/graphql/mutations/issues/permission_check_shared_examples.rb
new file mode 100644
index 00000000000..34c58f524cd
--- /dev/null
+++ b/spec/support/shared_examples/graphql/mutations/issues/permission_check_shared_examples.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+RSpec.shared_examples 'permission level for issue mutation is correctly verified' do |raises_for_all_errors = false|
+ before do
+ issue.assignees = []
+ issue.author = user
+ end
+
+ shared_examples_for 'when the user does not have access to the resource' do |raise_for_assigned|
+ it 'raises an error' do
+ expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ end
+
+ context 'even if assigned to the issue' do
+ before do
+ issue.assignees.push(user)
+ end
+
+ it 'does not modify issue' do
+ if raises_for_all_errors || raise_for_assigned
+ expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ else
+ expect(subject[:issue]).to eq issue
+ end
+ end
+ end
+
+ context 'even if author of the issue' do
+ before do
+ issue.author = user
+ end
+
+ it 'raises an error' do
+ expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ end
+ end
+ end
+
+ context 'when the user is not a project member' do
+ it_behaves_like 'when the user does not have access to the resource', true
+ end
+
+ context 'when the user is a project member' do
+ context 'with guest role' do
+ before do
+ issue.project.add_guest(user)
+ end
+
+ it_behaves_like 'when the user does not have access to the resource', false
+ end
+ end
+end
diff --git a/spec/support/shared_examples/graphql/mutations/merge_requests/permission_check_shared_examples.rb b/spec/support/shared_examples/graphql/mutations/merge_requests/permission_check_shared_examples.rb
new file mode 100644
index 00000000000..1ddbad1cea7
--- /dev/null
+++ b/spec/support/shared_examples/graphql/mutations/merge_requests/permission_check_shared_examples.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+RSpec.shared_examples 'permission level for merge request mutation is correctly verified' do
+ before do
+ merge_request.assignees = []
+ merge_request.reviewers = []
+ merge_request.author = nil
+ end
+
+ shared_examples_for 'when the user does not have access to the resource' do |raise_for_assigned|
+ it 'raises an error' do
+ expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ end
+
+ context 'even if assigned to the merge request' do
+ before do
+ merge_request.assignees.push(user)
+ end
+
+ it 'does not modify merge request' do
+ if raise_for_assigned
+ expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ else
+ # In some cases we simply do nothing instead of raising
+ # https://gitlab.com/gitlab-org/gitlab/-/issues/196241
+ expect(subject[:merge_request]).to eq merge_request
+ end
+ end
+ end
+
+ context 'even if reviewer of the merge request' do
+ before do
+ merge_request.reviewers.push(user)
+ end
+
+ it 'raises an error' do
+ expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ end
+ end
+
+ context 'even if author of the merge request' do
+ before do
+ merge_request.author = user
+ end
+
+ it 'raises an error' do
+ expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ end
+ end
+ end
+
+ context 'when the user is not a project member' do
+ it_behaves_like 'when the user does not have access to the resource', true
+ end
+
+ context 'when the user is a project member' do
+ context 'with guest role' do
+ before do
+ merge_request.project.add_guest(user)
+ end
+
+ it_behaves_like 'when the user does not have access to the resource', true
+ end
+
+ context 'with reporter role' do
+ before do
+ merge_request.project.add_reporter(user)
+ end
+
+ it_behaves_like 'when the user does not have access to the resource', false
+ end
+ end
+end