1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../scripts/release_environment/notification'
require 'webmock/rspec'
RSpec.describe ReleaseEnvironmentNotification, feature_category: :delivery do
describe '#initialize' do
context 'when NOT all environment variables are provided' do
before do
stub_env('ENVIRONMENT', nil)
end
it 'fail when initializing' do
expect { described_class.new }.to raise_error(RuntimeError)
end
end
context 'when an environment variable is set and empty' do
before do
stub_env('ENVIRONMENT', '')
end
it 'fail when initializing' do
expect { described_class.new }.to raise_error(RuntimeError)
end
end
context 'when all environment variables are provided' do
before do
stub_env('CI_PIPELINE_URL', '1')
stub_env('ENVIRONMENT', 'my-env')
stub_env('VERSIONS', '{"gitlab": "12.7.0"}')
stub_env('OPS_RELEASE_TOOLS_PIPELINE_TOKEN', 'token')
stub_env('RELEASE_ENVIRONMENT_NOTIFICATION_TYPE', 'qa')
end
it 'initializes' do
expect { described_class.new }.not_to raise_error
end
end
end
describe '#execute' do
context 'when all environment variables are provided' do
before do
stub_env('CI_PIPELINE_URL', '1')
stub_env('ENVIRONMENT', 'my-env')
stub_env('VERSIONS', '{"gitlab": "12.7.0"}')
stub_env('OPS_RELEASE_TOOLS_PIPELINE_TOKEN', 'token')
stub_env('RELEASE_ENVIRONMENT_NOTIFICATION_TYPE', 'qa')
end
context 'when the response is 2xx' do
before do
uri = URI.parse("#{ReleaseEnvironmentNotification::OPS_RELEASE_TOOLS_API_URL}/trigger/pipeline")
stub_request(:any, uri).to_return(status: 200, body: 'Mocked response')
end
it 'triggers a notification' do
notification = described_class.new
expect(notification).to receive(:trigger_notification).and_call_original
expect(notification.execute).to be_nil
end
end
context 'when the response is not 2xx' do
before do
uri = URI.parse("#{ReleaseEnvironmentNotification::OPS_RELEASE_TOOLS_API_URL}/trigger/pipeline")
stub_request(:any, uri).to_return(status: 404, body: 'Mocked response')
end
it 'raise an error' do
notification = described_class.new
expect(notification).to receive(:trigger_notification).and_call_original
expect { notification.execute }.to raise_error(RuntimeError)
end
end
end
end
end
|