update_spec.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::Ticket::SharedDraft::Start::Update do
  4. let(:group) { create(:group) }
  5. let(:content) { { content: Faker::Lorem.unique.sentence } }
  6. let(:form_id) { 123 }
  7. let(:shared_draft) { create(:ticket_shared_draft_start, group:, name: draft_name) }
  8. let(:draft_name) { 'initial draft name' }
  9. let(:user) do
  10. create(:agent)
  11. .tap { |elem| elem.user_groups.create!(group:, access: :create) }
  12. end
  13. context 'when user has access to the draft group' do
  14. it 'returns new object' do
  15. described_class
  16. .new(user, shared_draft, form_id, content:, group:)
  17. .execute
  18. expect(shared_draft.reload).to have_attributes(name: draft_name, content:, group:)
  19. end
  20. # name can be changed via REST api, but GraphQL mutation does not support it
  21. it 'changes name if given' do
  22. described_class
  23. .new(user, shared_draft, form_id, content:, group:, name: 'new name')
  24. .execute
  25. expect(shared_draft.reload).to have_attributes(name: 'new name')
  26. end
  27. it 'copies attachments from the given form' do
  28. create(:store, o_id: form_id)
  29. described_class
  30. .new(user, shared_draft, form_id, content:, group:)
  31. .execute
  32. expect(Store.list(object: shared_draft.class.name, o_id: shared_draft.id))
  33. .to contain_exactly(have_attributes(filename: 'test.txt'))
  34. end
  35. end
  36. context 'when user has insufficient access to the target draft group' do
  37. let(:new_group) { create(:group) }
  38. before do
  39. allow(user).to receive(:group_access?).with(new_group.id, :create).and_return(false)
  40. end
  41. it 'raises an error' do
  42. expect do
  43. described_class
  44. .new(user, shared_draft, form_id, content:, group: new_group)
  45. .execute
  46. end
  47. .to raise_error(Pundit::NotAuthorizedError)
  48. end
  49. end
  50. end