Commit fa4069e8 authored by Mike Kozono's avatar Mike Kozono

Simplify calculate_checksum! method

The `update_verification_state!` method also sets
`verification_checksum`, so we don't need to do it here.
parent dcbe6ed0
......@@ -46,13 +46,13 @@ module Gitlab
raise NotImplementedError, 'There is no Replicator defined for this model'
end
# Clear model verification checksum and force recalculation
# Returns a checksum of the file (assumed to be a "blob" type)
#
# @return [String] SHA256 hash of the carrierwave file
def calculate_checksum!
self.verification_checksum = nil
return unless needs_checksum?
return unless checksummable?
self.verification_checksum = self.class.hexdigest(replicator.carrierwave_uploader.path)
self.class.hexdigest(replicator.carrierwave_uploader.path)
end
# Checks whether model needs checksum to be performed
......
......@@ -7,21 +7,16 @@ RSpec.describe Packages::PackageFile, type: :model do
describe '#calculate_checksum!' do
let(:package_file) { create(:conan_package_file, :conan_recipe_file) }
it 'sets `verification_checksum` to SHA256 sum of the file' do
it 'returns SHA256 sum of the file' do
expected = Digest::SHA256.file(package_file.file.path).hexdigest
expect { package_file.calculate_checksum! }
.to change { package_file.verification_checksum }.from(nil).to(expected)
expect(package_file.calculate_checksum!).to eq(expected)
end
it 'sets `checksum` to nil for a non-existent file' do
checksum = Digest::SHA256.file(package_file.file.path).hexdigest
package_file.verification_checksum = checksum
it 'returns nil for a non-existent file' do
allow(package_file).to receive(:file_exist?).and_return(false)
expect { package_file.calculate_checksum! }
.to change { package_file.verification_checksum }.from(checksum).to(nil)
expect(package_file.calculate_checksum!).to eq(nil)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment