Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/workers/integrations')
-rw-r--r--spec/workers/integrations/execute_worker_spec.rb61
-rw-r--r--spec/workers/integrations/irker_worker_spec.rb118
2 files changed, 179 insertions, 0 deletions
diff --git a/spec/workers/integrations/execute_worker_spec.rb b/spec/workers/integrations/execute_worker_spec.rb
new file mode 100644
index 00000000000..19600f35c8f
--- /dev/null
+++ b/spec/workers/integrations/execute_worker_spec.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+require 'spec_helper'
+
+RSpec.describe Integrations::ExecuteWorker, '#perform' do
+ let_it_be(:integration) { create(:jira_integration) }
+
+ let(:worker) { described_class.new }
+
+ it 'executes integration with given data' do
+ data = { test: 'test' }
+
+ expect_next_found_instance_of(integration.class) do |integration|
+ expect(integration).to receive(:execute).with(data)
+ end
+
+ worker.perform(integration.id, data)
+ end
+
+ it 'logs error messages' do
+ error = StandardError.new('invalid URL')
+
+ expect_next_found_instance_of(integration.class) do |integration|
+ expect(integration).to receive(:execute).and_raise(error)
+ expect(integration).to receive(:log_exception).with(error)
+ end
+
+ worker.perform(integration.id, {})
+ end
+
+ context 'when integration cannot be found' do
+ it 'completes silently and does not log an error' do
+ expect(Gitlab::IntegrationsLogger).not_to receive(:error)
+
+ expect do
+ worker.perform(non_existing_record_id, {})
+ end.not_to raise_error
+ end
+ end
+
+ context 'when using the old worker class' do
+ let(:described_class) { ProjectServiceWorker }
+
+ it 'uses the correct worker attributes', :aggregate_failures do
+ expect(described_class.sidekiq_options).to include('retry' => 3, 'dead' => false)
+ expect(described_class.get_data_consistency).to eq(:always)
+ expect(described_class.get_feature_category).to eq(:integrations)
+ expect(described_class.get_urgency).to eq(:low)
+ expect(described_class.worker_has_external_dependencies?).to be(true)
+ end
+
+ it 'executes integration with given data' do
+ data = { test: 'test' }
+
+ expect_next_found_instance_of(integration.class) do |integration|
+ expect(integration).to receive(:execute).with(data)
+ end
+
+ worker.perform(integration.id, data)
+ end
+ end
+end
diff --git a/spec/workers/integrations/irker_worker_spec.rb b/spec/workers/integrations/irker_worker_spec.rb
new file mode 100644
index 00000000000..27dc08212ea
--- /dev/null
+++ b/spec/workers/integrations/irker_worker_spec.rb
@@ -0,0 +1,118 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Integrations::IrkerWorker, '#perform' do
+ let_it_be(:project) { create(:project, :repository) }
+ let_it_be(:user) { create(:user) }
+ let_it_be(:push_data) { HashWithIndifferentAccess.new(Gitlab::DataBuilder::Push.build_sample(project, user)) }
+ let_it_be(:channels) { ['irc://test.net/#test'] }
+
+ let_it_be(:server_settings) do
+ {
+ server_host: 'localhost',
+ server_port: 6659
+ }
+ end
+
+ let_it_be(:arguments) do
+ [
+ project.id,
+ channels,
+ false,
+ push_data,
+ HashWithIndifferentAccess.new(server_settings)
+ ]
+ end
+
+ let(:tcp_socket) { instance_double(TCPSocket) }
+
+ subject(:worker) { described_class.new }
+
+ before do
+ allow(TCPSocket).to receive(:new).and_return(tcp_socket)
+ allow(tcp_socket).to receive(:puts).and_return(true)
+ allow(tcp_socket).to receive(:close).and_return(true)
+ end
+
+ context 'when local requests are not allowed' do
+ before do
+ allow(Gitlab::CurrentSettings).to receive(:allow_local_requests_from_web_hooks_and_services?).and_return(false)
+ end
+
+ it { expect(worker.perform(*arguments)).to be_falsey }
+ end
+
+ context 'when connection fails' do
+ before do
+ allow(TCPSocket).to receive(:new).and_raise(Errno::ECONNREFUSED.new('test'))
+ end
+
+ it { expect(subject.perform(*arguments)).to be_falsey }
+ end
+
+ context 'when connection successful' do
+ before do
+ allow(Gitlab::CurrentSettings)
+ .to receive(:allow_local_requests_from_web_hooks_and_services?).and_return(true)
+ end
+
+ it { expect(subject.perform(*arguments)).to be_truthy }
+
+ context 'with new branch' do
+ it 'sends a correct message with branches url' do
+ branches_url = Gitlab::Routing.url_helpers
+ .project_branches_url(project)
+
+ push_data['before'] = '0000000000000000000000000000000000000000'
+
+ message = "has created a new branch master: #{branches_url}"
+
+ expect(tcp_socket).to receive(:puts).with(wrap_message(message))
+
+ subject.perform(*arguments)
+ end
+ end
+
+ context 'with deleted branch' do
+ it 'sends a correct message' do
+ push_data['after'] = '0000000000000000000000000000000000000000'
+
+ message = "has deleted the branch master"
+
+ expect(tcp_socket).to receive(:puts).with(wrap_message(message))
+
+ subject.perform(*arguments)
+ end
+ end
+
+ context 'with new commits to existing branch' do
+ it 'sends a correct message with a compare url' do
+ compare_url = Gitlab::Routing.url_helpers
+ .project_compare_url(project,
+ from: Commit.truncate_sha(push_data[:before]),
+ to: Commit.truncate_sha(push_data[:after]))
+
+ message = "pushed #{push_data['total_commits_count']} " \
+ "new commits to master: #{compare_url}"
+
+ expect(tcp_socket).to receive(:puts).with(wrap_message(message))
+
+ subject.perform(*arguments)
+ end
+ end
+
+ context 'when using the old worker class' do
+ let(:described_class) { ::IrkerWorker }
+
+ it { expect(subject.perform(*arguments)).to be_truthy }
+ end
+ end
+
+ def wrap_message(text)
+ message = "[#{project.path}] #{push_data['user_name']} #{text}"
+ to_send = { to: channels, privmsg: message }
+
+ Gitlab::Json.dump(to_send)
+ end
+end