update_spec.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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(:ticket_data) { { title: new_title } }
  10. describe '#execute' do
  11. it 'updates a ticket with given metadata' do
  12. service.execute(ticket: ticket, ticket_data:)
  13. expect(ticket)
  14. .to have_attributes(
  15. title: new_title,
  16. )
  17. end
  18. it 'fails to update ticket without access' do
  19. allow_any_instance_of(TicketPolicy)
  20. .to receive(:update?).and_return(false)
  21. expect { service.execute(ticket: ticket, ticket_data:) }
  22. .to raise_error(Pundit::NotAuthorizedError)
  23. end
  24. it 'adds article when present' do
  25. sample_body = Faker::Lorem.sentence
  26. ticket_data[:article] = {
  27. body: sample_body
  28. }
  29. service.execute(ticket: ticket, ticket_data:)
  30. expect(Ticket.last.articles.last)
  31. .to have_attributes(
  32. body: sample_body
  33. )
  34. end
  35. end
  36. end