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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Groups::ImportExport::ExportService, feature_category: :importers do
let_it_be(:user) { create(:user) }
let_it_be_with_refind(:group) { create(:group) }
let(:exported_by_admin) { false }
describe '#async_execute' do
context 'when the job can be successfully scheduled' do
let(:export_service) { described_class.new(group: group, user: user, exported_by_admin: exported_by_admin) }
it 'enqueues an export job' do
expect(GroupExportWorker)
.to receive(:perform_async)
.with(user.id, group.id, { exported_by_admin: exported_by_admin })
export_service.async_execute
end
it 'returns truthy' do
expect(export_service.async_execute).to be_present
end
context 'when the user was an admin' do
let(:exported_by_admin) { true }
it 'passes `exported_by_admin` correctly in the `params` hash' do
expect(GroupExportWorker).to receive(:perform_async).with(user.id, group.id, { exported_by_admin: true })
export_service.async_execute
end
end
end
context 'when the job cannot be scheduled' do
let(:export_service) { described_class.new(group: group, user: user, exported_by_admin: exported_by_admin) }
before do
allow(GroupExportWorker).to receive(:perform_async).and_return(nil)
end
it 'returns falsey' do
expect(export_service.async_execute).to be_falsey
end
end
end
describe '#execute' do
let(:shared) { Gitlab::ImportExport::Shared.new(group) }
let(:archive_path) { shared.archive_path }
let(:service) do
described_class.new(
group: group,
user: user,
exported_by_admin: exported_by_admin,
params: { shared: shared }
)
end
before_all do
group.add_owner(user)
end
after do
FileUtils.rm_rf(archive_path)
end
it 'saves the version' do
expect(Gitlab::ImportExport::VersionSaver).to receive(:new).and_call_original
service.execute
end
it 'saves the models using ndjson tree saver' do
expect(Gitlab::ImportExport::Group::TreeSaver).to receive(:new).and_call_original
service.execute
end
it 'compresses and removes tmp files' do
expect(group.import_export_uploads).to be_empty
expect(Gitlab::ImportExport::Saver).to receive(:new).and_call_original
service.execute
expect(Dir.exist?(shared.archive_path)).to eq false
expect(File.exist?(group.import_export_upload_by_user(user).export_file.path)).to eq true
end
it 'notifies the user' do
expect_next_instance_of(NotificationService) do |instance|
expect(instance).to receive(:group_was_exported)
end
service.execute
end
it 'creates an audit event' do
expect(Gitlab::Audit::Auditor).to receive(:audit).with(
{
name: 'group_export_created',
author: user,
scope: group,
target: group,
message: 'Group file export was created'
}
)
service.execute
end
context 'when the user was an admin' do
let(:exported_by_admin) { true }
it 'logs group exported audit event' do
expect(Gitlab::Audit::Auditor).to receive(:audit).with(hash_including(name: 'group_export_created'))
service.execute
end
context 'when silent exports are enabled' do
before do
stub_application_setting(silent_admin_exports_enabled: true)
end
it 'does not create an audit event' do
expect(Gitlab::Audit::Auditor).not_to receive(:audit)
expect { service.execute }.not_to change { AuditEvent.count }
end
it 'does not create any Todos' do
expect { service.execute }.not_to change { Todo.count }
end
end
end
context 'when saver succeeds' do
it 'saves the group in the file system' do
service.execute
expect(group.import_export_upload_by_user(user).export_file.file).not_to be_nil
expect(File.directory?(archive_path)).to eq(false)
expect(File.exist?(shared.archive_path)).to eq(false)
end
end
context 'when user does not have admin_group permission' do
let_it_be(:another_user) { create(:user) }
let(:service) do
described_class.new(
group: group,
user: another_user,
exported_by_admin: exported_by_admin,
params: { shared: shared }
)
end
let(:expected_message) do
"User with ID: %s does not have required permissions for Group: %s with ID: %s" %
[another_user.id, group.name, group.id]
end
it 'fails' do
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error).with_message(expected_message)
end
it 'logs the error' do
expect_next_instance_of(Gitlab::Export::Logger) do |logger|
expect(logger).to receive(:error).with(
group_id: group.id,
group_name: group.name,
errors: expected_message,
message: 'Group Export failed'
)
end
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
end
it 'tracks the error' do
expect(shared).to receive(:error) { |param| expect(param.message).to eq expected_message }
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
end
end
context 'when export fails' do
context 'when file saver fails' do
before do
allow_next_instance_of(Gitlab::ImportExport::Saver) do |saver|
allow(saver).to receive(:save).and_return(false)
end
end
it 'removes the remaining exported data' do
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
expect(group.import_export_uploads).to be_empty
expect(Dir.exist?(shared.archive_path)).to eq(false)
end
it 'notifies the user about failed group export' do
expect_next_instance_of(NotificationService) do |instance|
expect(instance).to receive(:group_was_not_exported)
end
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
end
end
context 'when file compression fails' do
before do
allow(service).to receive_message_chain(:tree_exporter, :save).and_return(false)
end
it 'removes the remaining exported data' do
allow_next_instance_of(Gitlab::ImportExport::Saver) do |saver|
allow(saver).to receive(:compress_and_save).and_return(false)
end
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
expect(group.import_export_uploads).to be_empty
expect(Dir.exist?(shared.archive_path)).to eq(false)
end
it 'notifies logger' do
allow(service).to receive_message_chain(:tree_exporter, :save).and_return(false)
expect(service.instance_variable_get(:@logger)).to receive(:error)
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
end
end
end
context 'when there is an existing export file' do
subject(:export_service) { described_class.new(group: group, user: user, exported_by_admin: exported_by_admin) }
let(:import_export_upload) do
create(
:import_export_upload,
group: group,
export_file: fixture_file_upload('spec/fixtures/group_export.tar.gz'),
user: user
)
end
it 'replaces it' do
existing_file = import_export_upload.export_file
export_service.execute
expect(import_export_upload.reload.export_file.path).not_to eq existing_file.path
end
end
end
end
|