slack_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 'Webhook > Slack', 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(:webhook) { create(:slack_webhook, endpoint: ENV['SLACK_CI_WEBHOOK_URL']) }
  6. let(:perform) { { 'notification.webhook' => { 'webhook_id' => webhook.id.to_s } } }
  7. let(:activator) { 'action' }
  8. let(:trigger) { create(:trigger, activator: activator, condition: condition, perform: perform) }
  9. before :all do # rubocop:disable RSpec/BeforeAfterAll
  10. delete_all_test_chat_messages if live_mode?
  11. end
  12. context 'with ticket create as condition' do
  13. let(:condition) { { 'ticket.action' => { 'operator' => 'is', 'value' => 'create' } } }
  14. let(:message) { 'Slack Webhook Test' }
  15. before do
  16. trigger
  17. end
  18. it 'creates a message in the slack channel' do
  19. create(:ticket, group: Group.first, title: message)
  20. perform_enqueued_jobs commit_transaction: true
  21. expect(message).to have_message_count(1)
  22. end
  23. end
  24. context 'with ticket update as condition' do
  25. let(:condition) { { 'ticket.action' => { 'operator' => 'is', 'value' => 'update' } } }
  26. let(:message) { 'Another Slack Webhook Test' }
  27. let(:update_message) { 'New article' }
  28. let(:ticket) { create(:ticket, group: Group.first, title: message) }
  29. before do
  30. trigger
  31. ticket
  32. perform_enqueued_jobs commit_transaction: true
  33. end
  34. it 'creates a message in the slack channel' do
  35. ticket.update!(title: update_message)
  36. create(:ticket_article, ticket_id: ticket.id)
  37. perform_enqueued_jobs commit_transaction: true
  38. expect(update_message).to have_message_count(1)
  39. end
  40. end
  41. context 'with ticket reminder reached as condition' do
  42. let(:condition) { { 'ticket.group_id' => { 'operator' => 'is', 'value' => [Group.first.id] } } }
  43. let(:message) { 'Reminder reached!' }
  44. let(:activator) { 'time' }
  45. let(:ticket) do
  46. create(
  47. :ticket,
  48. group: Group.first,
  49. title: 'Slack Webhook Test (Reminder reached)',
  50. state: Ticket::State.find_by(name: 'pending reminder'),
  51. pending_time: Time.zone.local(2023, 2, 7, 12)
  52. )
  53. end
  54. before do
  55. trigger
  56. ticket
  57. Ticket.process_pending
  58. perform_enqueued_jobs commit_transaction: true
  59. end
  60. it 'creates a message in the slack channel' do
  61. expect(message).to have_message_count(1)
  62. end
  63. end
  64. end