update_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::Ticket::Update, current_user_id: -> { user.id } do
  4. subject(:service) { described_class.new(current_user: user) }
  5. let(:user) { create(:agent, groups: [group]) }
  6. let(:ticket) { create(:ticket) }
  7. let(:group) { ticket.group }
  8. let(:new_title) { Faker::Lorem.word }
  9. let(:new_body) { Faker::Lorem.sentence }
  10. let(:ticket_data) { { title: new_title, time_unit: 2 } }
  11. let(:ticket_data_with_article) do
  12. ticket_data.merge(article: { body: new_body })
  13. end
  14. describe '#execute' do
  15. it 'updates a ticket with given metadata' do
  16. service.execute(ticket: ticket, ticket_data:)
  17. expect(ticket)
  18. .to have_attributes(
  19. title: new_title,
  20. )
  21. end
  22. it 'fails to update ticket without access' do
  23. allow_any_instance_of(TicketPolicy)
  24. .to receive(:update?).and_return(false)
  25. expect { service.execute(ticket: ticket, ticket_data:) }
  26. .to raise_error(Pundit::NotAuthorizedError)
  27. end
  28. it 'adds article when present' do
  29. service.execute(ticket: ticket, ticket_data: ticket_data_with_article)
  30. expect(Ticket.last.articles.last)
  31. .to have_attributes(
  32. body: new_body,
  33. )
  34. end
  35. it 'adds article accounted time to ticket' do
  36. expect(service.execute(ticket: ticket, ticket_data: ticket_data_with_article).time_unit).to eq(2)
  37. end
  38. it 'updates ticket with given macro' do
  39. macro = create(:macro, perform: { 'ticket.title' => { 'value' => new_title } })
  40. service.execute(ticket: ticket, ticket_data:, macro:)
  41. expect(ticket)
  42. .to have_attributes(
  43. title: new_title,
  44. )
  45. end
  46. it 'adds article note via macro' do
  47. macro = create(:macro, perform: {
  48. 'article.note' => { 'body' => 'note body', 'internal' => 'true', 'subject' => 'test' }
  49. })
  50. service.execute(ticket: ticket, ticket_data: ticket_data_with_article, macro:)
  51. expect(ticket.articles.reload)
  52. .to contain_exactly(
  53. have_attributes(body: new_body),
  54. have_attributes(body: 'note body'),
  55. )
  56. end
  57. end
  58. end