update_spec.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 } }
  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 'updates ticket with given macro' do
  36. macro = create(:macro, perform: { 'ticket.title' => { 'value' => new_title } })
  37. service.execute(ticket: ticket, ticket_data:, macro:)
  38. expect(ticket)
  39. .to have_attributes(
  40. title: new_title,
  41. )
  42. end
  43. it 'adds article note via macro' do
  44. macro = create(:macro, perform: {
  45. 'article.note' => { 'body' => 'note body', 'internal' => 'true', 'subject' => 'test' }
  46. })
  47. service.execute(ticket: ticket, ticket_data: ticket_data_with_article, macro:)
  48. expect(ticket.articles.reload)
  49. .to contain_exactly(
  50. have_attributes(body: new_body),
  51. have_attributes(body: 'note body'),
  52. )
  53. end
  54. end
  55. end