create_spec.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::Ticket::SharedDraft::Start::Create do
  4. let(:group) { create(:group) }
  5. let(:content) { { content: Faker::Lorem.unique.sentence } }
  6. let(:name) { Faker::Lorem.unique.sentence }
  7. let(:form_id) { 123 }
  8. let(:user) do
  9. create(:agent)
  10. .tap { |elem| elem.user_groups.create!(group:, access: :create) }
  11. end
  12. context 'when user has access to the draft group' do
  13. it 'returns new object' do
  14. draft = described_class
  15. .new(user, form_id, name:, content:, group:)
  16. .execute
  17. expect(draft).to have_attributes(name:, content:, group:)
  18. end
  19. it 'copies attachments from the given form' do
  20. create(:store, o_id: form_id)
  21. draft = described_class
  22. .new(user, form_id, name:, content:, group:)
  23. .execute
  24. expect(Store.list(object: draft.class.name, o_id: draft.id))
  25. .to contain_exactly(have_attributes(filename: 'test.txt'))
  26. end
  27. it 'copies inline attachment and keeps it inline' do
  28. content = attributes_for(:ticket_shared_draft_start, :with_inline_image)[:content]
  29. draft = described_class
  30. .new(user, form_id, name:, content:, group:)
  31. .execute
  32. expect(Store.list(object: draft.class.name, o_id: draft.id))
  33. .to contain_exactly(have_attributes(
  34. filename: 'image1.jpeg',
  35. preferences: include('Content-Disposition' => 'inline')
  36. ))
  37. end
  38. end
  39. context 'when user has insufficient access to the draft group' do
  40. before do
  41. allow(user).to receive(:group_access?).with(group.id, :create).and_return(false)
  42. end
  43. it 'raises an error' do
  44. expect do
  45. described_class
  46. .new(user, form_id, name:, content:, group:)
  47. .execute
  48. end
  49. .to raise_error(Pundit::NotAuthorizedError)
  50. end
  51. end
  52. end