Browse Source

Maintenance: Refactored Slack integration tests to avoid global constants.

Martin Gruner 1 year ago
parent
commit
7a4b512d67

+ 2 - 6
spec/integration/slack_spec.rb

@@ -3,10 +3,6 @@
 require 'rails_helper'
 require 'slack-ruby-client' # Only load this gem when it is really used.
 
-CHANNEL_NAME = ENV['SLACK_CI_CHANNEL_NAME']
-OAUTH_TOKEN = ENV['SLACK_CI_OAUTH_TOKEN']
-WEBHOOK_URL = ENV['SLACK_CI_WEBHOOK_URL']
-
 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
   let(:slack_group)  { create(:group) }
   let(:types)        { %w[create update reminder_reached] }
@@ -15,8 +11,8 @@ RSpec.describe 'Slack Integration', integration: true, performs_jobs: true, requ
       {
         group_ids: [slack_group.id],
         types:     types,
-        webhook:   WEBHOOK_URL,
-        channel:   CHANNEL_NAME,
+        webhook:   ENV['SLACK_CI_WEBHOOK_URL'],
+        channel:   ENV['SLACK_CI_CHANNEL_NAME'],
         username:  'zammad_agent',
         expand:    false,
       }

+ 1 - 5
spec/models/webhook/pre_defined/slack_spec.rb

@@ -3,12 +3,8 @@
 require 'rails_helper'
 require 'slack-ruby-client' # Only load this gem when it is really used.
 
-CHANNEL_NAME = ENV['SLACK_CI_CHANNEL_NAME']
-OAUTH_TOKEN = ENV['SLACK_CI_OAUTH_TOKEN']
-WEBHOOK_URL = ENV['SLACK_CI_WEBHOOK_URL']
-
 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
-  let(:webhook)   { create(:slack_webhook, endpoint: WEBHOOK_URL) }
+  let(:webhook)   { create(:slack_webhook, endpoint: ENV['SLACK_CI_WEBHOOK_URL']) }
   let(:perform)   { { 'notification.webhook' => { 'webhook_id' => webhook.id.to_s } } }
   let(:activator) { 'action' }
   let(:trigger)   { create(:trigger, activator: activator, condition: condition, perform: perform) }

+ 5 - 5
spec/support/slack.rb

@@ -21,7 +21,7 @@ module RSpecSlackHelper
 
   def slack_client
     Slack.configure do |config|
-      config.token = OAUTH_TOKEN
+      config.token = ENV['SLACK_CI_OAUTH_TOKEN']
     end
 
     client = Slack::Web::Client.new
@@ -34,13 +34,13 @@ module RSpecSlackHelper
     channels = client.conversations_list['channels']
     channel_id = nil
     channels.each do |channel|
-      next if channel['name'] != CHANNEL_NAME
+      next if channel['name'] != ENV['SLACK_CI_CHANNEL_NAME']
 
       channel_id = channel['id']
     end
 
     if !channel_id
-      raise "ERROR: No such channel '#{CHANNEL_NAME}'"
+      raise "ERROR: No such channel '#{ENV['SLACK_CI_CHANNEL_NAME']}'"
     end
 
     channel_id
@@ -50,11 +50,11 @@ module RSpecSlackHelper
     channel_history = client.conversations_history(channel: channel_id)
 
     if !channel_history
-      raise "ERROR: No history for channel #{CHANNEL_NAME}/#{channel_id}"
+      raise "ERROR: No history for channel #{ENV['SLACK_CI_CHANNEL_NAME']}/#{channel_id}"
     end
 
     if !channel_history['messages']
-      raise "ERROR: No history messages for channel #{CHANNEL_NAME}/#{channel_id}"
+      raise "ERROR: No history messages for channel #{ENV['SLACK_CI_CHANNEL_NAME']}/#{channel_id}"
     end
 
     channel_history