microsoft_teams_spec.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Webhook > MS Teams', integration: true, performs_jobs: true, required_envs: %w[MS_TEAMS_CI_WEBHOOK_URL], retry: 5, retry_wait: 30.seconds, use_vcr: true do # rubocop:disable RSpec/DescribeClass
  4. let(:webhook) { create(:ms_teams_webhook, endpoint: ENV['MS_TEAMS_CI_WEBHOOK_URL']) }
  5. let(:perform) { { 'notification.webhook' => { 'webhook_id' => webhook.id.to_s } } }
  6. let(:trigger) { create(:trigger, activator: 'action', condition: condition, perform: perform) }
  7. # At first glance, we tried to use the MS Graph API to delete all messages in the channel.
  8. # After some research, we found out, that it is quite hard to accomplish this
  9. # due to protected access to the required API endpoints.
  10. # Following code could be used to get an access token for the MS Graph API:
  11. #
  12. # token_response = UserAgent.post(
  13. # "https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/token",
  14. # {
  15. # client_id: 'xxx',
  16. # client_secret: 'xxx',
  17. # scope: 'https://graph.microsoft.com/.default ',
  18. # grant_type: 'client_credentials',
  19. # }
  20. # )
  21. # token_data = JSON.parse(token_response.body)
  22. # @token = token_data['access_token']
  23. context 'with ticket create as condition' do
  24. let(:condition) { { 'ticket.action' => { 'operator' => 'is', 'value' => 'create' } } }
  25. let(:message) { 'MS Teams Webhook Test' }
  26. before do
  27. trigger
  28. end
  29. it 'creates a message in the MS Teams channel', :aggregate_failures do
  30. create(:ticket, group: Group.first, title: message)
  31. perform_enqueued_jobs commit_transaction: true
  32. expect(HttpLog.last).to have_attributes(
  33. direction: 'out',
  34. facility: 'webhook',
  35. method: 'POST',
  36. url: ENV['MS_TEAMS_CI_WEBHOOK_URL'],
  37. status: 200.to_s,
  38. )
  39. end
  40. end
  41. end