diff options
Diffstat (limited to 'spec/support/shared_examples/graphql')
3 files changed, 77 insertions, 19 deletions
diff --git a/spec/support/shared_examples/graphql/mutation_shared_examples.rb b/spec/support/shared_examples/graphql/mutation_shared_examples.rb index 51d52cbb901..dc590e23ace 100644 --- a/spec/support/shared_examples/graphql/mutation_shared_examples.rb +++ b/spec/support/shared_examples/graphql/mutation_shared_examples.rb @@ -8,7 +8,7 @@ # There must be a method or let called `mutation` defined that executes # the mutation. RSpec.shared_examples 'a mutation that returns top-level errors' do |errors: []| - let(:match_errors) { eq(errors) } + let(:match_errors) { match_array(errors) } it do post_graphql_mutation(mutation, current_user: current_user) 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 index 34c58f524cd..05fee45427a 100644 --- 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 @@ -1,12 +1,34 @@ # 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 + let_it_be(:other_user_author) { create(:user) } + + def issue_attributes(issue) + issue.attributes.except( + # Description and title can be updated by authors and assignees of the issues + 'description', + 'title', + # Those fields are calculated or expected to be modified during the mutations + 'author_id', + 'updated_at', + 'updated_by_id', + 'last_edited_at', + 'last_edited_by_id', + 'lock_version', + # There were spec failures due to nano-second comparisons + # this property isn't changed by any mutation so we don't have to verify it + 'created_at' + ) end - shared_examples_for 'when the user does not have access to the resource' do |raise_for_assigned| + let(:expected) { issue_attributes(issue) } + + shared_examples_for 'when the user does not have access to the resource' do |raise_for_assigned_and_author| + before do + issue.assignees = [] + issue.update!(author: other_user_author) + end + it 'raises an error' do expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) end @@ -17,21 +39,25 @@ RSpec.shared_examples 'permission level for issue mutation is correctly verified end it 'does not modify issue' do - if raises_for_all_errors || raise_for_assigned + if raises_for_all_errors || raise_for_assigned_and_author expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) else - expect(subject[:issue]).to eq issue + expect(issue_attributes(subject[:issue])).to eq expected end end end context 'even if author of the issue' do before do - issue.author = user + issue.update!(author: user) end - it 'raises an error' do - expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + it 'does not modify issue' do + if raises_for_all_errors || raise_for_assigned_and_author + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + else + expect(issue_attributes(subject[:issue])).to eq expected + end 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 index 1ddbad1cea7..b0ac742079a 100644 --- 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 @@ -1,13 +1,39 @@ # 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 + let(:other_user_author) { create(:user) } + + def mr_attributes(mr) + mr.attributes.except( + # Authors and assignees can edit title, description, target branch and draft status + 'title', + 'description', + 'target_branch', + 'draft', + # Those fields are calculated or expected to be modified during the mutations + 'author_id', + 'latest_merge_request_diff_id', + 'last_edited_at', + 'last_edited_by_id', + 'lock_version', + 'updated_at', + 'updated_by_id', + 'merge_status', + # There were spec failures due to nano-second comparisons + # this property isn't changed by any mutation so we don't have to verify it + 'created_at' + ) end - shared_examples_for 'when the user does not have access to the resource' do |raise_for_assigned| + let(:expected) { mr_attributes(merge_request) } + + shared_examples_for 'when the user does not have access to the resource' do |raise_for_assigned_and_author| + before do + merge_request.assignees = [] + merge_request.reviewers = [] + merge_request.update!(author: other_user_author) + end + it 'raises an error' do expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) end @@ -18,12 +44,12 @@ RSpec.shared_examples 'permission level for merge request mutation is correctly end it 'does not modify merge request' do - if raise_for_assigned + if raise_for_assigned_and_author 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 + expect(mr_attributes(subject[:merge_request])).to eq expected end end end @@ -40,11 +66,17 @@ RSpec.shared_examples 'permission level for merge request mutation is correctly context 'even if author of the merge request' do before do - merge_request.author = user + merge_request.update!(author: user) end it 'raises an error' do - expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + if raise_for_assigned_and_author + 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(mr_attributes(subject[:merge_request])).to eq expected + end end end end |