From 68a7ae3091488f7c045e7b6bc69cbe9521e6df2a Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Wed, 15 Jan 2020 16:41:02 +0100 Subject: [PATCH] REFACTOR: Simplify backup version check Adds specs for inalid version number in metadata file. Follow-up to c3cd2389fee19ba209d99192ceda99b3cd3e5578 --- lib/backup_restore/meta_data_handler.rb | 6 +- .../backup_restore/meta_data_handler_spec.rb | 89 ++++++++++++------- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/lib/backup_restore/meta_data_handler.rb b/lib/backup_restore/meta_data_handler.rb index 2f11767eb8d..002bbd0d285 100644 --- a/lib/backup_restore/meta_data_handler.rb +++ b/lib/backup_restore/meta_data_handler.rb @@ -19,11 +19,7 @@ module BackupRestore def validate metadata = extract_metadata - if metadata[:version].blank? - raise MetaDataError.new("Version not defined in metadata file.") - end - - if !metadata[:version].is_a?(String) && !metadata[:version].is_a?(Integer) + if !metadata[:version].is_a?(Integer) raise MetaDataError.new("Version is not in a valid format.") end diff --git a/spec/lib/backup_restore/meta_data_handler_spec.rb b/spec/lib/backup_restore/meta_data_handler_spec.rb index 42b082326ce..c9ba6cbaffc 100644 --- a/spec/lib/backup_restore/meta_data_handler_spec.rb +++ b/spec/lib/backup_restore/meta_data_handler_spec.rb @@ -23,52 +23,77 @@ describe BackupRestore::MetaDataHandler do BackupRestore::MetaDataHandler.new(logger, filename, tmp_directory).validate end - it "extracts metadata from file when metadata file exists" do - metadata = '{"source":"discourse","version":20160329101122}' + context "metadata file" do + it "extracts metadata from file when metadata file exists" do + metadata = '{"source":"discourse","version":20160329101122}' - with_metadata_file(metadata) do |dir| - expect(validate_metadata(backup_filename, dir)) - .to include(version: 20160329101122) + with_metadata_file(metadata) do |dir| + expect(validate_metadata(backup_filename, dir)) + .to include(version: 20160329101122) + end + end + + it "raises an exception when the metadata file contains invalid JSON" do + currupt_metadata = '{"version":20160329101122' + + with_metadata_file(currupt_metadata) do |dir| + expect { validate_metadata(backup_filename, dir) } + .to raise_error(BackupRestore::MetaDataError) + end + end + + it "raises an exception when the metadata file is empty" do + with_metadata_file('') do |dir| + expect { validate_metadata(backup_filename, dir) } + .to raise_error(BackupRestore::MetaDataError) + end + end + + it "raises an exception when the metadata file contains an invalid version number" do + metadata = '{"source":"discourse","version":"1abcdefghijklm"}' + + with_metadata_file(metadata) do |dir| + expect { validate_metadata(backup_filename, dir) } + .to raise_error(BackupRestore::MetaDataError) + end + end + + it "raises an exception when the metadata file contains an empty version number" do + metadata = '{"source":"discourse","version":""}' + + with_metadata_file(metadata) do |dir| + expect { validate_metadata(backup_filename, dir) } + .to raise_error(BackupRestore::MetaDataError) + end end end - it "extracts metadata from filename when metadata file does not exist" do - with_metadata_file(nil) do |dir| - expect(validate_metadata(backup_filename, dir)) - .to include(version: 20191108000414) + context "filename" do + it "extracts metadata from filename when metadata file does not exist" do + with_metadata_file(nil) do |dir| + expect(validate_metadata(backup_filename, dir)) + .to include(version: 20191108000414) + end end - end - it "raises an exception when the metadata file contains invalid JSON" do - currupt_metadata = '{"version":20160329101122' + it "raises an exception when the filename contains no version number" do + filename = 'discourse-2019-11-18-143242.tar.gz' - with_metadata_file(currupt_metadata) do |dir| - expect { validate_metadata(backup_filename, dir) } + expect { validate_metadata(filename, nil) } .to raise_error(BackupRestore::MetaDataError) end - end - it "raises an exception when the metadata file is empty" do - with_metadata_file('') do |dir| - expect { validate_metadata(backup_filename, dir) } + it "raises an exception when the filename contains an invalid version number" do + filename = 'discourse-2019-11-18-143242-v123456789.tar.gz' + expect { validate_metadata(filename, nil) } + .to raise_error(BackupRestore::MetaDataError) + + filename = 'discourse-2019-11-18-143242-v1abcdefghijklm.tar.gz' + expect { validate_metadata(filename, nil) } .to raise_error(BackupRestore::MetaDataError) end end - it "raises an exception when the filename contains no version number" do - filename = 'discourse-2019-11-18-143242.tar.gz' - - expect { validate_metadata(filename, nil) } - .to raise_error(BackupRestore::MetaDataError) - end - - it "raises an exception when the filename contains an invalid version number" do - filename = 'discourse-2019-11-18-143242-v123456789.tar.gz' - - expect { validate_metadata(filename, nil) } - .to raise_error(BackupRestore::MetaDataError) - end - it "raises an exception when the backup's version is newer than the current version" do new_backup_filename = 'discourse-2019-11-18-143242-v20191113193141.sql.gz'