OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/spec/workers/backup_job_spec.rb

138 lines
4.3 KiB

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
require 'spec_helper'
describe BackupJob, type: :model do
shared_examples "it creates a backup" do |opts = {}|
let(:job) { BackupJob.new }
let(:previous_backup) { FactoryBot.create :backup }
let(:backup) { FactoryBot.create :backup }
let(:status) { :in_queue }
let(:job_id) { 42 }
let(:job_status) do
FactoryBot.create(
:delayed_job_status,
user: user,
reference: backup,
status: JobStatus::Status.statuses[status],
job_id: job_id
)
end
let(:db_dump_process_status) do
success = db_dump_success
Object.new.tap do |o|
o.define_singleton_method(:success?) { success }
end
end
let(:db_dump_success) { false }
let(:arguments) { [{ backup: backup, user: user, **opts }] }
[37868] Whitelist for attachment mime types and extensions on upload (#9431) * Add setting for whitelist * Make attachments API BaseServices compatible * Add prepare service and contract * Correctly pass the filename to the UploadedFile * Add presence check to filename * Fix expected validation message * We no longer raise a multipart error when metadata is empty * Fix filesize validation on prepared uploads * Add parser error if invalid metadata json * When attachment is not saved, use filename property * Return correct error message on JSON parser erroro * Fix specs * Use attachment upload representer * Fix direct uploads mocks with new service layer * Lint * Fix export job using attachment service * Fix IFC controller using attachment prepare service * Fix export job * RenameRename params_getter to params_source * Fix mail handler using attachment service * Fix usage of attachment create service in documents * Reuse shared examples for document attachment spec * Fix stubbed attachment service in export job spec * Use admin user in backup spec * Fix export job for bim * Fix attachment integration spec * Fix issues_controller spec * Make budget resource spec reuse common examples * Fix attachment parsing representer spec * Replace prepare part of attachment spec into separate service spec * Clear cache for login spec * Convert document create/update into services * Budget services * Allow options to be passed to property twin * Remove setting author on budget initialize * Replace meetings update with services * Replace ifc models attachment handling with services * Don't check uploader if changed by system * Fix uploader being changed by system * Replace wiki page attach_files with attachable services * Replace avatar saving * Replace snapshot attach_files * Skip double validation when container present * Set snapshot through attachment service * Remove attach_files * Validate content type in contract * Enforce writing the content type without accepting user input * Expect changed content_type * Fix content of viewpoint image to get correct content type * Fix tsv spec * Add create contract spec * Bypass whitelist in internal services when conflicting with user * Fix expects in specs after whitelist bypass * Render contract errors for wiki * Add before_hook to bodied to allow to pre-authorize permissions * Budget errors from contract * Document errors from contract
3 years ago
let(:user) { FactoryBot.create :admin }
before do
previous_backup; backup; status # create
allow(job).to receive(:arguments).and_return arguments
allow(job).to receive(:job_id).and_return job_id
expect(Open3).to receive(:capture3).and_return [nil, "Dump failed", db_dump_process_status]
allow_any_instance_of(BackupJob)
.to receive(:tmp_file_name).with("openproject", ".sql").and_return("/tmp/openproject.sql")
allow_any_instance_of(BackupJob)
.to receive(:tmp_file_name).with("openproject-backup", ".zip").and_return("/tmp/openproject.zip")
allow(File).to receive(:read).and_call_original
allow(File).to receive(:read).with("/tmp/openproject.sql").and_return "SOME SQL"
end
def perform
job.perform **arguments.first
end
context "with a failed database dump" do
let(:db_dump_success) { false }
before { perform }
it "retains previous backups" do
expect(Backup.find_by(id: previous_backup.id)).not_to be_nil
end
end
context "with a successful database dump" do
let(:db_dump_success) { true }
let!(:attachment) { FactoryBot.create :attachment }
let(:stored_backup) { Attachment.where(container_type: "Export").last }
let(:backup_files) { Zip::File.open(stored_backup.file.path) { |zip| zip.entries.map(&:name) } }
let(:backed_up_attachment) { "attachment/file/#{attachment.id}/#{attachment.filename}" }
before { perform }
it "destroys any previous backups" do
expect(Backup.find_by(id: previous_backup.id)).to be_nil
end
it "stores a new backup as an attachment" do
expect(stored_backup.filename).to eq "openproject.zip"
end
it "includes the database dump in the backup" do
expect(backup_files).to include "openproject.sql"
end
if opts[:include_attachments] != false
it "includes attachments in the backup" do
expect(backup_files).to include backed_up_attachment
end
else
it "does not include attachments in the backup" do
expect(backup_files).not_to include backed_up_attachment
end
end
end
end
context "per default" do
it_behaves_like "it creates a backup"
end
context "with include_attachments: false" do
it_behaves_like "it creates a backup", include_attachments: false
end
end