Add coverage for malformed version cleanup in SoftwareUpdateCheckService, add helper query methods (#32876)

This commit is contained in:
Matt Jankowski 2024-11-14 09:03:57 -05:00 committed by GitHub
parent 62d65504f6
commit 766358e52b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 73 additions and 4 deletions

View file

@ -22,6 +22,14 @@ class SoftwareUpdate < ApplicationRecord
Gem::Version.new(version) Gem::Version.new(version)
end end
def outdated?
runtime_version >= gem_version
end
def pending?
gem_version > runtime_version
end
class << self class << self
def check_enabled? def check_enabled?
Rails.configuration.x.mastodon.software_update_url.present? Rails.configuration.x.mastodon.software_update_url.present?
@ -30,11 +38,17 @@ class SoftwareUpdate < ApplicationRecord
def pending_to_a def pending_to_a
return [] unless check_enabled? return [] unless check_enabled?
all.to_a.filter { |update| update.gem_version > Mastodon::Version.gem_version } all.to_a.filter(&:pending?)
end end
def urgent_pending? def urgent_pending?
pending_to_a.any?(&:urgent?) pending_to_a.any?(&:urgent?)
end end
end end
private
def runtime_version
Mastodon::Version.gem_version
end
end end

View file

@ -12,7 +12,7 @@ class SoftwareUpdateCheckService < BaseService
def clean_outdated_updates! def clean_outdated_updates!
SoftwareUpdate.find_each do |software_update| SoftwareUpdate.find_each do |software_update|
software_update.delete if Mastodon::Version.gem_version >= software_update.gem_version software_update.delete if software_update.outdated?
rescue ArgumentError rescue ArgumentError
software_update.delete software_update.delete
end end

View file

@ -3,6 +3,60 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe SoftwareUpdate do RSpec.describe SoftwareUpdate do
describe '#pending?' do
subject { described_class.new(version: update_version) }
before { allow(Mastodon::Version).to receive(:gem_version).and_return(Gem::Version.new(mastodon_version)) }
context 'when the runtime version is older than the update' do
let(:mastodon_version) { '4.0.0' }
let(:update_version) { '5.0.0' }
it { is_expected.to be_pending }
end
context 'when the runtime version is newer than the update' do
let(:mastodon_version) { '6.0.0' }
let(:update_version) { '5.0.0' }
it { is_expected.to_not be_pending }
end
context 'when the runtime version is same as the update' do
let(:mastodon_version) { '4.0.0' }
let(:update_version) { '4.0.0' }
it { is_expected.to_not be_pending }
end
end
describe '#outdated?' do
subject { described_class.new(version: update_version) }
before { allow(Mastodon::Version).to receive(:gem_version).and_return(Gem::Version.new(mastodon_version)) }
context 'when the runtime version is older than the update' do
let(:mastodon_version) { '4.0.0' }
let(:update_version) { '5.0.0' }
it { is_expected.to_not be_outdated }
end
context 'when the runtime version is newer than the update' do
let(:mastodon_version) { '6.0.0' }
let(:update_version) { '5.0.0' }
it { is_expected.to be_outdated }
end
context 'when the runtime version is same as the update' do
let(:mastodon_version) { '4.0.0' }
let(:update_version) { '4.0.0' }
it { is_expected.to be_outdated }
end
end
describe '.pending_to_a' do describe '.pending_to_a' do
before do before do
allow(Mastodon::Version).to receive(:gem_version).and_return(Gem::Version.new(mastodon_version)) allow(Mastodon::Version).to receive(:gem_version).and_return(Gem::Version.new(mastodon_version))

View file

@ -27,6 +27,7 @@ RSpec.describe SoftwareUpdateCheckService do
before do before do
Fabricate(:software_update, version: '3.5.0', type: 'major', urgent: false) Fabricate(:software_update, version: '3.5.0', type: 'major', urgent: false)
Fabricate(:software_update, version: '42.13.12', type: 'major', urgent: false) Fabricate(:software_update, version: '42.13.12', type: 'major', urgent: false)
Fabricate(:software_update, version: 'Malformed', type: 'major', urgent: false)
owner_user.settings.update('notification_emails.software_updates': 'all') owner_user.settings.update('notification_emails.software_updates': 'all')
owner_user.save! owner_user.save!
@ -50,7 +51,7 @@ RSpec.describe SoftwareUpdateCheckService do
end end
it 'deletes outdated update records but keeps valid update records' do it 'deletes outdated update records but keeps valid update records' do
expect { subject.call }.to change { SoftwareUpdate.pluck(:version).sort }.from(['3.5.0', '42.13.12']).to(['42.13.12']) expect { subject.call }.to change { SoftwareUpdate.pluck(:version).sort }.from(['3.5.0', '42.13.12', 'Malformed']).to(['42.13.12'])
end end
end end
@ -85,7 +86,7 @@ RSpec.describe SoftwareUpdateCheckService do
end end
it 'updates the list of known updates' do it 'updates the list of known updates' do
expect { subject.call }.to change { SoftwareUpdate.pluck(:version).sort }.from(['3.5.0', '42.13.12']).to(['4.2.1', '4.3.0', '5.0.0']) expect { subject.call }.to change { SoftwareUpdate.pluck(:version).sort }.from(['3.5.0', '42.13.12', 'Malformed']).to(['4.2.1', '4.3.0', '5.0.0'])
end end
context 'when no update is urgent' do context 'when no update is urgent' do