create_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. it 'adds tags when present' do
  53. sample_tags = [Faker::Lorem.word]
  54. ticket_data[:tags] = sample_tags
  55. ticket = service.execute(ticket_data:)
  56. expect(ticket.tag_list)
  57. .to eq sample_tags
  58. end
  59. context 'when tag creation is disabled' do
  60. before do
  61. Setting.set('tag_new', false)
  62. end
  63. it 'does not adds tags when present' do
  64. sample_tags = [Faker::Lorem.word]
  65. ticket_data[:tags] = sample_tags
  66. ticket = service.execute(ticket_data:)
  67. expect(ticket.tag_list).to be_empty
  68. end
  69. end
  70. describe 'shared draft handling' do
  71. let(:shared_draft) { create(:ticket_shared_draft_start, group:) }
  72. before { ticket_data[:shared_draft] = shared_draft }
  73. it 'destroys given shared draft' do
  74. service.execute(ticket_data:)
  75. expect(Ticket::SharedDraftStart).not_to exist(shared_draft.id)
  76. end
  77. it 'raises error if shared drafts are disabled on that group' do
  78. group.update! shared_drafts: false
  79. expect { service.execute(ticket_data:) }
  80. .to raise_error(Exceptions::UnprocessableEntity)
  81. end
  82. it 'raises error if shared draft group does not match ticket group' do
  83. shared_draft.update! group: create(:group)
  84. group.update! shared_drafts: false
  85. expect { service.execute(ticket_data:) }
  86. .to raise_error(Exceptions::UnprocessableEntity)
  87. end
  88. end
  89. end
  90. end