create_spec.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. describe 'issue trackers handling' do
  143. let(:gitlab_link) { 'https://git.example.com/issue/123' }
  144. let(:github_link) { 'https://github.com/issue/123' }
  145. before do
  146. ticket_data[:external_references] = {
  147. gitlab: [gitlab_link],
  148. github: [github_link],
  149. idoit: [42],
  150. }
  151. end
  152. context 'when none enabled' do
  153. it 'adds no data' do
  154. ticket = service.execute(ticket_data:)
  155. expect(ticket.preferences).to be_blank
  156. end
  157. end
  158. context 'when gitlab is enabled' do
  159. before do
  160. Setting.set('gitlab_integration', true)
  161. end
  162. it 'adds gitlab links' do
  163. ticket = service.execute(ticket_data:)
  164. expect(ticket.preferences).to eq({ 'gitlab' => { 'issue_links' => [gitlab_link] } })
  165. end
  166. end
  167. context 'when github is enabled' do
  168. before do
  169. Setting.set('github_integration', true)
  170. end
  171. it 'adds github links' do
  172. ticket = service.execute(ticket_data:)
  173. expect(ticket.preferences).to eq({ 'github' => { 'issue_links' => [github_link] } })
  174. end
  175. end
  176. context 'when idoit is enabled' do
  177. before do
  178. Setting.set('idoit_integration', true)
  179. end
  180. it 'adds github links' do
  181. ticket = service.execute(ticket_data:)
  182. expect(ticket.preferences).to eq({ 'idoit' => { 'object_ids' => [42] } })
  183. end
  184. end
  185. end
  186. end
  187. end