slack_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'slack-ruby-client' # Only load this gem when it is really used.
  4. RSpec.describe 'Slack Integration', integration: true, performs_jobs: true, required_envs: %w[SLACK_CI_CHANNEL_NAME SLACK_CI_OAUTH_TOKEN SLACK_CI_WEBHOOK_URL], time_zone: 'Europe/London', use_vcr: true do # rubocop:disable RSpec/DescribeClass
  5. let(:slack_group) { create(:group) }
  6. let(:types) { %w[create update reminder_reached] }
  7. let(:items) do
  8. [
  9. {
  10. group_ids: [slack_group.id],
  11. types: types,
  12. webhook: ENV['SLACK_CI_WEBHOOK_URL'],
  13. channel: ENV['SLACK_CI_CHANNEL_NAME'],
  14. username: 'zammad_agent',
  15. expand: false,
  16. }
  17. ]
  18. end
  19. let(:group) { slack_group }
  20. let(:customer) { create(:customer) }
  21. let(:state_name) { 'new' }
  22. let(:ticket) { create(:ticket, customer: customer, group: group, title: message, state_name: state_name) }
  23. let(:article) { create(:ticket_article, :outbound_note, ticket: ticket, body: message, sender_name: 'Customer', from: customer.fullname) }
  24. before :all do # rubocop:disable RSpec/BeforeAfterAll
  25. delete_all_test_chat_messages if live_mode?
  26. end
  27. before do
  28. Setting.set('slack_integration', true)
  29. Setting.set('slack_config', { items: items })
  30. ticket && article
  31. perform_enqueued_jobs commit_transaction: true
  32. end
  33. context 'with default group' do
  34. let(:message) { 'foo' }
  35. let(:group) { Group.first }
  36. it 'publishes no ticket updates', :aggregate_failures do
  37. expect(message).to have_message_count(0)
  38. ticket.update!(state: Ticket::State.find_by(name: 'open'))
  39. perform_enqueued_jobs commit_transaction: true
  40. expect(message).to have_message_count(0)
  41. end
  42. context 'with create event only' do
  43. let(:types) { 'create' }
  44. let(:message) { 'bar' }
  45. it 'publishes no ticket updates', :aggregate_failures do
  46. expect(message).to have_message_count(0)
  47. ticket.update!(state: Ticket::State.find_by(name: 'open'))
  48. perform_enqueued_jobs commit_transaction: true
  49. expect(message).to have_message_count(0)
  50. end
  51. end
  52. end
  53. context 'with slack group' do
  54. let(:message) { 'baz' }
  55. it 'publishes ticket updates', :aggregate_failures do
  56. expect(message).to have_message_count(1)
  57. new_message = 'qux'
  58. ticket.update!(title: new_message)
  59. perform_enqueued_jobs commit_transaction: true
  60. expect(new_message).to have_message_count(1)
  61. ticket.update!(state: Ticket::State.find_by(name: 'pending reminder'), pending_time: Time.zone.local(2023, 2, 7, 12))
  62. perform_enqueued_jobs commit_transaction: true
  63. expect(new_message).to have_message_count(2)
  64. Ticket.process_pending
  65. perform_enqueued_jobs commit_transaction: true
  66. expect(new_message).to have_message_count(3)
  67. Ticket.process_pending
  68. perform_enqueued_jobs commit_transaction: true
  69. expect(new_message).to have_message_count(3)
  70. end
  71. context 'with create event only' do
  72. let(:types) { 'create' }
  73. let(:message) { 'corge' }
  74. it 'publishes no ticket updates', :aggregate_failures do
  75. expect(message).to have_message_count(1)
  76. new_message = 'grault'
  77. ticket.update!(title: new_message)
  78. perform_enqueued_jobs commit_transaction: true
  79. expect(new_message).to have_message_count(0)
  80. end
  81. end
  82. end
  83. end