2020-09-29 15:10:08 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe BulkCreateIntegrationService do
|
2022-06-02 21:09:25 +00:00
|
|
|
include JiraIntegrationHelpers
|
2020-09-29 15:10:08 +00:00
|
|
|
|
2020-10-30 18:08:56 +00:00
|
|
|
before_all do
|
2021-06-18 15:10:16 +00:00
|
|
|
stub_jira_integration_test
|
2020-09-29 15:10:08 +00:00
|
|
|
end
|
|
|
|
|
2020-10-30 18:08:56 +00:00
|
|
|
let_it_be(:excluded_group) { create(:group) }
|
|
|
|
let_it_be(:excluded_project) { create(:project, group: excluded_group) }
|
2021-06-29 03:07:32 +00:00
|
|
|
|
2021-06-18 15:10:16 +00:00
|
|
|
let(:instance_integration) { create(:jira_integration, :instance) }
|
2022-03-03 00:20:18 +00:00
|
|
|
let(:excluded_attributes) do
|
|
|
|
%w[
|
|
|
|
id project_id group_id inherit_from_id instance template
|
|
|
|
created_at updated_at
|
|
|
|
encrypted_properties encrypted_properties_iv
|
|
|
|
]
|
|
|
|
end
|
2020-09-29 15:10:08 +00:00
|
|
|
|
2022-05-30 09:09:35 +00:00
|
|
|
shared_examples 'creates integration successfully' do
|
2022-03-03 00:20:18 +00:00
|
|
|
def attributes(record)
|
|
|
|
record.reload.attributes.except(*excluded_attributes)
|
|
|
|
end
|
|
|
|
|
2020-09-29 15:10:08 +00:00
|
|
|
it 'updates the inherited integrations' do
|
|
|
|
described_class.new(integration, batch, association).execute
|
|
|
|
|
2022-03-03 00:20:18 +00:00
|
|
|
expect(attributes(created_integration)).to eq attributes(integration)
|
2020-09-29 15:10:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'integration with data fields' do
|
2021-11-09 12:12:15 +00:00
|
|
|
let(:excluded_attributes) { %w[id service_id integration_id created_at updated_at] }
|
2020-09-29 15:10:08 +00:00
|
|
|
|
|
|
|
it 'updates the data fields from inherited integrations' do
|
|
|
|
described_class.new(integration, batch, association).execute
|
|
|
|
|
2022-03-03 00:20:18 +00:00
|
|
|
expect(attributes(created_integration.data_fields))
|
|
|
|
.to eq attributes(integration.data_fields)
|
2020-09-29 15:10:08 +00:00
|
|
|
end
|
2022-05-30 09:09:35 +00:00
|
|
|
|
|
|
|
it 'sets created_at and updated_at timestamps', :freeze_time do
|
|
|
|
described_class.new(integration, batch, association).execute
|
|
|
|
|
|
|
|
expect(created_integration.data_fields.reload).to have_attributes(
|
|
|
|
created_at: eq(Time.current),
|
|
|
|
updated_at: eq(Time.current)
|
|
|
|
)
|
|
|
|
end
|
2020-09-29 15:10:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates inherit_from_id attributes' do
|
|
|
|
described_class.new(integration, batch, association).execute
|
|
|
|
|
2020-10-23 18:08:31 +00:00
|
|
|
expect(created_integration.reload.inherit_from_id).to eq(inherit_from_id)
|
2020-09-29 15:10:08 +00:00
|
|
|
end
|
2022-05-30 09:09:35 +00:00
|
|
|
|
|
|
|
it 'sets created_at and updated_at timestamps', :freeze_time do
|
|
|
|
described_class.new(integration, batch, association).execute
|
|
|
|
|
|
|
|
expect(created_integration.reload).to have_attributes(
|
|
|
|
created_at: eq(Time.current),
|
|
|
|
updated_at: eq(Time.current)
|
|
|
|
)
|
|
|
|
end
|
2020-09-29 15:10:08 +00:00
|
|
|
end
|
|
|
|
|
2020-10-30 18:08:56 +00:00
|
|
|
context 'passing an instance-level integration' do
|
2020-09-29 15:10:08 +00:00
|
|
|
let(:integration) { instance_integration }
|
2020-10-23 18:08:31 +00:00
|
|
|
let(:inherit_from_id) { integration.id }
|
2020-09-29 15:10:08 +00:00
|
|
|
|
|
|
|
context 'with a project association' do
|
|
|
|
let!(:project) { create(:project) }
|
2021-06-18 15:10:16 +00:00
|
|
|
let(:created_integration) { project.jira_integration }
|
2020-10-30 18:08:56 +00:00
|
|
|
let(:batch) { Project.where(id: project.id) }
|
2020-09-29 15:10:08 +00:00
|
|
|
let(:association) { 'project' }
|
|
|
|
|
2022-05-30 09:09:35 +00:00
|
|
|
it_behaves_like 'creates integration successfully'
|
2020-09-29 15:10:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a group association' do
|
2020-10-30 18:08:56 +00:00
|
|
|
let!(:group) { create(:group) }
|
2021-05-12 12:10:24 +00:00
|
|
|
let(:created_integration) { Integration.find_by(group: group) }
|
2020-10-30 18:08:56 +00:00
|
|
|
let(:batch) { Group.where(id: group.id) }
|
2020-09-29 15:10:08 +00:00
|
|
|
let(:association) { 'group' }
|
|
|
|
|
2022-05-30 09:09:35 +00:00
|
|
|
it_behaves_like 'creates integration successfully'
|
2020-10-30 18:08:56 +00:00
|
|
|
end
|
|
|
|
end
|
2020-10-23 18:08:31 +00:00
|
|
|
|
2020-10-30 18:08:56 +00:00
|
|
|
context 'passing a group integration' do
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
|
|
|
|
context 'with a project association' do
|
|
|
|
let!(:project) { create(:project, group: group) }
|
2021-11-12 06:10:23 +00:00
|
|
|
let(:integration) { create(:jira_integration, :group, group: group) }
|
2021-06-18 15:10:16 +00:00
|
|
|
let(:created_integration) { project.jira_integration }
|
2020-10-30 18:08:56 +00:00
|
|
|
let(:batch) { Project.where(id: Project.minimum(:id)..Project.maximum(:id)).without_integration(integration).in_namespace(integration.group.self_and_descendants) }
|
|
|
|
let(:association) { 'project' }
|
|
|
|
let(:inherit_from_id) { integration.id }
|
|
|
|
|
2022-05-30 09:09:35 +00:00
|
|
|
it_behaves_like 'creates integration successfully'
|
2021-11-09 12:12:15 +00:00
|
|
|
|
|
|
|
context 'with different foreign key of data_fields' do
|
2021-11-12 06:10:23 +00:00
|
|
|
let(:integration) { create(:zentao_integration, :group, group: group) }
|
2021-11-09 12:12:15 +00:00
|
|
|
let(:created_integration) { project.zentao_integration }
|
|
|
|
|
2022-05-30 09:09:35 +00:00
|
|
|
it_behaves_like 'creates integration successfully'
|
2021-11-09 12:12:15 +00:00
|
|
|
end
|
2020-10-30 18:08:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a group association' do
|
|
|
|
let!(:subgroup) { create(:group, parent: group) }
|
2021-11-12 06:10:23 +00:00
|
|
|
let(:integration) { create(:jira_integration, :group, group: group, inherit_from_id: instance_integration.id) }
|
2021-05-12 12:10:24 +00:00
|
|
|
let(:created_integration) { Integration.find_by(group: subgroup) }
|
2020-10-30 18:08:56 +00:00
|
|
|
let(:batch) { Group.where(id: subgroup.id) }
|
|
|
|
let(:association) { 'group' }
|
|
|
|
let(:inherit_from_id) { instance_integration.id }
|
|
|
|
|
2022-05-30 09:09:35 +00:00
|
|
|
it_behaves_like 'creates integration successfully'
|
2021-11-09 12:12:15 +00:00
|
|
|
|
|
|
|
context 'with different foreign key of data_fields' do
|
2021-11-12 06:10:23 +00:00
|
|
|
let(:integration) { create(:zentao_integration, :group, group: group, inherit_from_id: instance_integration.id) }
|
2021-11-09 12:12:15 +00:00
|
|
|
|
2022-05-30 09:09:35 +00:00
|
|
|
it_behaves_like 'creates integration successfully'
|
2021-11-09 12:12:15 +00:00
|
|
|
end
|
2020-09-29 15:10:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|