create_spec.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. service.execute(ticket_data:)
  19. expect(Ticket.last)
  20. .to have_attributes(
  21. title: sample_title,
  22. group: group,
  23. customer: customer
  24. )
  25. end
  26. it 'fails to create ticket without access' do
  27. allow_any_instance_of(TicketPolicy)
  28. .to receive(:create?).and_return(false)
  29. expect { service.execute(ticket_data:) }
  30. .to raise_error(Pundit::NotAuthorizedError)
  31. end
  32. it 'adds article when present' do
  33. sample_body = Faker::Lorem.sentence
  34. ticket_data[:article] = {
  35. body: sample_body
  36. }
  37. service.execute(ticket_data:)
  38. expect(Ticket.last.articles.first)
  39. .to have_attributes(
  40. body: sample_body
  41. )
  42. end
  43. it 'adds tags when present' do
  44. sample_tags = [Faker::Lorem.word]
  45. ticket_data[:tags] = sample_tags
  46. service.execute(ticket_data:)
  47. expect(Ticket.last.tag_list)
  48. .to eq sample_tags
  49. end
  50. context 'when tag creation is disabled' do
  51. before do
  52. Setting.set('tag_new', false)
  53. end
  54. it 'does not adds tags when present' do
  55. sample_tags = [Faker::Lorem.word]
  56. ticket_data[:tags] = sample_tags
  57. service.execute(ticket_data:)
  58. expect(Ticket.last.tag_list).to eq([])
  59. end
  60. end
  61. end
  62. end