shared_draft_start_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::SharedDraftStart, current_user_id: 1, type: :model do
  4. subject(:shared_draft_start) { create(:ticket_shared_draft_start, :with_inline_image) }
  5. it { is_expected.to belong_to :group }
  6. it { is_expected.to validate_presence_of :name }
  7. it { expect(shared_draft_start.content).to be_a(Hash) }
  8. describe '#clear_group_id' do
  9. it 'removes group ID from content' do
  10. shared_draft_start.content['group_id'] = 'test group id'
  11. shared_draft_start.save!
  12. expect(shared_draft_start.content).not_to have_key('group_id')
  13. end
  14. end
  15. describe '#trigger_subscriptions' do
  16. it 'triggers subscription when draft is created' do
  17. allow(Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup)
  18. .to receive(:trigger)
  19. shared_draft_start
  20. expect(Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup).to have_received(:trigger)
  21. .with(be_nil,
  22. arguments: include(
  23. group_id: Gql::ZammadSchema.id_from_object(shared_draft_start.group)
  24. ))
  25. end
  26. it 'triggers subscription when draft is updated' do
  27. shared_draft_start
  28. allow(Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup)
  29. .to receive(:trigger)
  30. shared_draft_start.touch
  31. expect(Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup).to have_received(:trigger)
  32. .with(be_nil,
  33. arguments: include(
  34. group_id: Gql::ZammadSchema.id_from_object(shared_draft_start.group)
  35. ))
  36. end
  37. it 'triggers subscription twice when draft group is changed', aggregate_failures: true do
  38. shared_draft_start
  39. allow(Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup)
  40. .to receive(:trigger)
  41. old_group = shared_draft_start.group
  42. shared_draft_start.update! group: create(:group)
  43. expect(Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup).to have_received(:trigger)
  44. .with(be_nil,
  45. arguments: include(
  46. group_id: Gql::ZammadSchema.id_from_object(shared_draft_start.group)
  47. ))
  48. expect(Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup).to have_received(:trigger)
  49. .with(be_nil,
  50. arguments: include(
  51. group_id: Gql::ZammadSchema.id_from_object(old_group)
  52. ))
  53. end
  54. it 'triggers subscription when draft is destroyed' do
  55. shared_draft_start
  56. allow(Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup)
  57. .to receive(:trigger)
  58. shared_draft_start.destroy
  59. expect(Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup).to have_received(:trigger)
  60. .with(be_nil,
  61. arguments: include(
  62. group_id: Gql::ZammadSchema.id_from_object(shared_draft_start.group)
  63. ))
  64. end
  65. end
  66. describe '#body' do
  67. let(:test) { Faker::Lorem.sentence }
  68. it 'reads value from content' do
  69. shared_draft_start.content[:body] = test
  70. expect(shared_draft_start.body).to eq(test)
  71. end
  72. it 'sets value to content' do
  73. shared_draft_start.body = test
  74. expect(shared_draft_start.content).to include(body: test, title: '123')
  75. end
  76. end
  77. describe '#body_with_base64' do
  78. it 'returns inline images in base64' do
  79. expect(shared_draft_start.body_with_base64).to start_with('text and <img src="data:image/jpeg;base64,')
  80. end
  81. end
  82. describe '#content_with_base64' do
  83. it 'returns inline images in base64' do
  84. expect(shared_draft_start.content_with_base64)
  85. .to include(body: start_with('text and <img src="data:image/jpeg;base64,'))
  86. end
  87. end
  88. describe '#content_with_body_urls' do
  89. it 'returns inline images with URLs' do
  90. expect(shared_draft_start.content_with_body_urls)
  91. .to include(body: start_with('text and <img src="/api/v1/attachments'))
  92. end
  93. end
  94. describe '#content_with_form_id_body_urls' do
  95. let(:new_form_id) { '123' }
  96. it 'returns inline images referencing copied assets' do
  97. shared_draft_start.clone_attachments('UploadCache', new_form_id)
  98. new_attachments = Store.list(object: 'UploadCache', o_id: new_form_id)
  99. expect(shared_draft_start.content_with_form_id_body_urls(new_form_id)).to include(
  100. body: start_with("text and <img src=\"/api/v1/attachments/#{new_attachments.last.id}")
  101. )
  102. end
  103. end
  104. end