delete_spec.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Ticket::SharedDraft::Start::Delete, :aggregate_failures, type: :graphql do
  4. let(:group) { create(:group) }
  5. let(:shared_draft) { create(:ticket_shared_draft_start, group:) }
  6. let(:variables) { { sharedDraftId: gql.id(shared_draft) } }
  7. let(:query) do
  8. <<~QUERY
  9. mutation ticketSharedDraftStartDelete($sharedDraftId: ID!) {
  10. ticketSharedDraftStartDelete(sharedDraftId: $sharedDraftId) {
  11. success
  12. errors {
  13. message
  14. field
  15. }
  16. }
  17. }
  18. QUERY
  19. end
  20. context 'with an agent', authenticated_as: :agent do
  21. let(:access) { :create }
  22. let(:agent) do
  23. create(:agent)
  24. .tap { |elem| elem.user_groups.create!(group:, access:) if access }
  25. end
  26. context 'when agent has access to the draft group' do
  27. it 'deletes the shared draft' do
  28. expect { gql.execute(query, variables:) }
  29. .to change { Ticket::SharedDraftStart.exists? shared_draft.id }
  30. .to false
  31. end
  32. it 'returns success' do
  33. gql.execute(query, variables:)
  34. expect(gql.result.data).to include('success' => true)
  35. end
  36. end
  37. context 'when agent has insufficient access to the given group' do
  38. let(:access) { 'read' }
  39. it 'does not delete the shared draft' do
  40. expect { gql.execute(query, variables:) }
  41. .not_to change { Ticket::SharedDraftStart.exists? shared_draft.id }
  42. .from true
  43. end
  44. it 'raises an error' do
  45. gql.execute(query, variables:)
  46. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  47. end
  48. end
  49. context 'when agent has no access to the given group' do
  50. let(:access) { false }
  51. it 'does not delete the shared draft' do
  52. expect { gql.execute(query, variables:) }
  53. .not_to change { Ticket::SharedDraftStart.exists? shared_draft.id }
  54. .from true
  55. end
  56. it 'raises an error' do
  57. gql.execute(query, variables:)
  58. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  59. end
  60. end
  61. end
  62. it_behaves_like 'graphql responds with error if unauthenticated' do
  63. before do
  64. gql.execute(query, variables:)
  65. end
  66. end
  67. end