diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-19 10:33:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-19 10:33:21 +0300 |
commit | 36a59d088eca61b834191dacea009677a96c052f (patch) | |
tree | e4f33972dab5d8ef79e3944a9f403035fceea43f /spec/graphql/mutations/timelogs/delete_spec.rb | |
parent | a1761f15ec2cae7c7f7bbda39a75494add0dfd6f (diff) |
Add latest changes from gitlab-org/gitlab@15-0-stable-eev15.0.0-rc42
Diffstat (limited to 'spec/graphql/mutations/timelogs/delete_spec.rb')
-rw-r--r-- | spec/graphql/mutations/timelogs/delete_spec.rb | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/spec/graphql/mutations/timelogs/delete_spec.rb b/spec/graphql/mutations/timelogs/delete_spec.rb new file mode 100644 index 00000000000..f4a258e0f78 --- /dev/null +++ b/spec/graphql/mutations/timelogs/delete_spec.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::Timelogs::Delete do + include GraphqlHelpers + + let_it_be(:author) { create(:user) } + let_it_be(:maintainer) { create(:user) } + let_it_be(:administrator) { create(:user, :admin) } + let_it_be(:project) { create(:project, :public) } + let_it_be(:issue) { create(:issue, project: project) } + let_it_be_with_reload(:timelog) { create(:timelog, user: author, issue: issue, time_spent: 1800) } + + let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }, field: nil) } + let(:timelog_id) { global_id_of(timelog) } + let(:mutation_arguments) { { id: timelog_id } } + + describe '#resolve' do + subject(:resolve) do + mutation.resolve(**mutation_arguments) + end + + context 'when the timelog id is not valid' do + let(:current_user) { author } + let(:timelog_id) { global_id_of(model_name: 'Timelog', id: non_existing_record_id) } + + it 'raises Gitlab::Graphql::Errors::ResourceNotAvailable' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + + context 'when the current user is not the timelog\'s author, not a maintainer and not an admin' do + let(:current_user) { create(:user) } + + it 'raises Gitlab::Graphql::Errors::ResourceNotAvailable' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + + context 'when the current user is the timelog\'s author' do + let(:current_user) { author } + + it 'deletes the timelog' do + expect { subject }.to change { Timelog.count }.by(-1) + end + + it 'returns the deleted timelog' do + expect(subject[:timelog]).to eq(timelog) + end + + it 'returns no errors' do + expect(subject[:errors]).to be_empty + end + end + + context 'when the current user is not the timelog\'s author but a maintainer of the project' do + let(:current_user) { maintainer } + + before do + project.add_maintainer(maintainer) + end + + it 'deletes the timelog' do + expect { subject }.to change { Timelog.count }.by(-1) + end + + it 'returns the deleted timelog' do + expect(subject[:timelog]).to eq(timelog) + end + + it 'returns no errors' do + expect(subject[:errors]).to be_empty + end + end + + context 'when the current user is not the timelog\'s author, not a maintainer but an admin', :enable_admin_mode do + let(:current_user) { administrator } + + it 'deletes the timelog' do + expect { subject }.to change { Timelog.count }.by(-1) + end + + it 'returns the deleted timelog' do + expect(subject[:timelog]).to eq(timelog) + end + + it 'returns no errors' do + expect(subject[:errors]).to be_empty + end + end + end +end |