create_spec.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::Ticket::Create, current_user_id: -> { user.id } do
  4. subject(:service) { described_class.new(current_user: user) }
  5. let(:user) { create(:agent, groups: [group]) }
  6. let(:group) { create(:group) }
  7. let(:customer) { create(:customer) }
  8. describe '#execute' do
  9. let(:sample_title) { Faker::Lorem.sentence }
  10. let(:ticket_data) do
  11. {
  12. title: sample_title,
  13. group: group,
  14. customer: customer
  15. }
  16. end
  17. it 'creates a ticket with given metadata' do
  18. ticket = service.execute(ticket_data:)
  19. expect(ticket)
  20. .to have_attributes(
  21. title: sample_title,
  22. group: group,
  23. customer: customer
  24. )
  25. end
  26. it 'creates a ticket with customer email address' do
  27. test_email = Faker::Internet.unique.email
  28. ticket_data[:customer] = test_email
  29. ticket = service.execute(ticket_data:)
  30. expect(ticket.customer).to have_attributes(
  31. email: test_email,
  32. role_ids: Role.signup_role_ids
  33. )
  34. end
  35. it 'fails to create ticket without access' do
  36. allow_any_instance_of(TicketPolicy)
  37. .to receive(:create?).and_return(false)
  38. expect { service.execute(ticket_data:) }
  39. .to raise_error(Pundit::NotAuthorizedError)
  40. end
  41. it 'adds article when present' do
  42. sample_body = Faker::Lorem.sentence
  43. ticket_data[:article] = {
  44. body: sample_body
  45. }
  46. ticket = service.execute(ticket_data:)
  47. expect(ticket.articles.first)
  48. .to have_attributes(
  49. body: sample_body
  50. )
  51. end
  52. context 'when email article should be created, but to is not present' do
  53. let(:customer) { create(:customer, email: '') }
  54. let(:ticket_data) do
  55. {
  56. title: sample_title,
  57. group: group,
  58. customer: customer,
  59. article: {
  60. body: Faker::Lorem.sentence,
  61. type: 'email',
  62. to: nil,
  63. }
  64. }
  65. end
  66. it 'raises an error' do
  67. expect { service.execute(ticket_data:) }.to raise_error(Exceptions::InvalidAttribute, 'Sending an email without a valid recipient is not possible.')
  68. end
  69. end
  70. context 'when email article should be created, but to is not a valid email' do
  71. let(:customer) { create(:customer) }
  72. let(:ticket_data) do
  73. {
  74. title: sample_title,
  75. group: group,
  76. customer: customer,
  77. article: {
  78. body: Faker::Lorem.sentence,
  79. type: 'email',
  80. to: 'invalid-email',
  81. }
  82. }
  83. end
  84. it 'raises an error' do
  85. expect { service.execute(ticket_data:) }.to raise_error(Exceptions::InvalidAttribute, 'Sending an email without a valid recipient is not possible.')
  86. end
  87. end
  88. it 'adds tags when present' do
  89. sample_tags = [Faker::Lorem.word]
  90. ticket_data[:tags] = sample_tags
  91. ticket = service.execute(ticket_data:)
  92. expect(ticket.tag_list)
  93. .to eq sample_tags
  94. end
  95. context 'when adding links' do
  96. let!(:other_ticket) { create(:ticket, customer: customer) }
  97. let(:links) do
  98. [
  99. { link_object: other_ticket, link_type: 'child' },
  100. { link_object: other_ticket, link_type: 'normal' },
  101. ]
  102. end
  103. it 'adds links correctly' do
  104. ticket_data[:links] = links
  105. ticket = service.execute(ticket_data:)
  106. expect(Link.list(link_object: 'Ticket', link_object_value: ticket.id)).to contain_exactly(
  107. { 'link_object' => 'Ticket', 'link_object_value' => other_ticket.id, 'link_type' => 'parent' },
  108. { 'link_object' => 'Ticket', 'link_object_value' => other_ticket.id, 'link_type' => 'normal' },
  109. )
  110. end
  111. end
  112. context 'when tag creation is disabled' do
  113. before do
  114. Setting.set('tag_new', false)
  115. end
  116. it 'does not adds tags when present' do
  117. sample_tags = [Faker::Lorem.word]
  118. ticket_data[:tags] = sample_tags
  119. ticket = service.execute(ticket_data:)
  120. expect(ticket.tag_list).to be_empty
  121. end
  122. end
  123. describe 'shared draft handling' do
  124. let(:shared_draft) { create(:ticket_shared_draft_start, group:) }
  125. before { ticket_data[:shared_draft] = shared_draft }
  126. it 'destroys given shared draft' do
  127. service.execute(ticket_data:)
  128. expect(Ticket::SharedDraftStart).not_to exist(shared_draft.id)
  129. end
  130. it 'raises error if shared drafts are disabled on that group' do
  131. group.update! shared_drafts: false
  132. expect { service.execute(ticket_data:) }
  133. .to raise_error(Exceptions::UnprocessableEntity)
  134. end
  135. it 'raises error if shared draft group does not match ticket group' do
  136. shared_draft.update! group: create(:group)
  137. group.update! shared_drafts: false
  138. expect { service.execute(ticket_data:) }
  139. .to raise_error(Exceptions::UnprocessableEntity)
  140. end
  141. end
  142. end
  143. end