software_update_check_service_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe SoftwareUpdateCheckService, type: :service do
  4. subject { described_class.new }
  5. shared_examples 'when the feature is enabled' do
  6. let(:full_update_check_url) { "#{update_check_url}?version=#{Mastodon::Version.to_s.split('+')[0]}" }
  7. let(:devops_role) { Fabricate(:user_role, name: 'DevOps', permissions: UserRole::FLAGS[:view_devops]) }
  8. let(:owner_user) { Fabricate(:user, role: UserRole.find_by(name: 'Owner')) }
  9. let(:old_devops_user) { Fabricate(:user) }
  10. let(:none_user) { Fabricate(:user, role: devops_role) }
  11. let(:patch_user) { Fabricate(:user, role: devops_role) }
  12. let(:critical_user) { Fabricate(:user, role: devops_role) }
  13. around do |example|
  14. queue_adapter = ActiveJob::Base.queue_adapter
  15. ActiveJob::Base.queue_adapter = :test
  16. example.run
  17. ActiveJob::Base.queue_adapter = queue_adapter
  18. end
  19. before do
  20. Fabricate(:software_update, version: '3.5.0', type: 'major', urgent: false)
  21. Fabricate(:software_update, version: '42.13.12', type: 'major', urgent: false)
  22. owner_user.settings.update('notification_emails.software_updates': 'all')
  23. owner_user.save!
  24. old_devops_user.settings.update('notification_emails.software_updates': 'all')
  25. old_devops_user.save!
  26. none_user.settings.update('notification_emails.software_updates': 'none')
  27. none_user.save!
  28. patch_user.settings.update('notification_emails.software_updates': 'patch')
  29. patch_user.save!
  30. critical_user.settings.update('notification_emails.software_updates': 'critical')
  31. critical_user.save!
  32. end
  33. context 'when the update server errors out' do
  34. before do
  35. stub_request(:get, full_update_check_url).to_return(status: 404)
  36. end
  37. it 'deletes outdated update records but keeps valid update records' do
  38. expect { subject.call }.to change { SoftwareUpdate.pluck(:version).sort }.from(['3.5.0', '42.13.12']).to(['42.13.12'])
  39. end
  40. end
  41. context 'when the server returns new versions' do
  42. let(:server_json) do
  43. {
  44. updatesAvailable: [
  45. {
  46. version: '4.2.1',
  47. urgent: false,
  48. type: 'patch',
  49. releaseNotes: 'https://github.com/mastodon/mastodon/releases/v4.2.1',
  50. },
  51. {
  52. version: '4.3.0',
  53. urgent: false,
  54. type: 'minor',
  55. releaseNotes: 'https://github.com/mastodon/mastodon/releases/v4.3.0',
  56. },
  57. {
  58. version: '5.0.0',
  59. urgent: false,
  60. type: 'minor',
  61. releaseNotes: 'https://github.com/mastodon/mastodon/releases/v5.0.0',
  62. },
  63. ],
  64. }
  65. end
  66. before do
  67. stub_request(:get, full_update_check_url).to_return(body: Oj.dump(server_json))
  68. end
  69. it 'updates the list of known updates' do
  70. 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'])
  71. end
  72. context 'when no update is urgent' do
  73. it 'sends e-mail notifications according to settings', :aggregate_failures do
  74. expect { subject.call }.to have_enqueued_mail(AdminMailer, :new_software_updates)
  75. .with(hash_including(params: { recipient: owner_user.account })).once
  76. .and(have_enqueued_mail(AdminMailer, :new_software_updates).with(hash_including(params: { recipient: patch_user.account })).once)
  77. .and(have_enqueued_mail.at_most(2))
  78. end
  79. end
  80. context 'when an update is urgent' do
  81. let(:server_json) do
  82. {
  83. updatesAvailable: [
  84. {
  85. version: '5.0.0',
  86. urgent: true,
  87. type: 'minor',
  88. releaseNotes: 'https://github.com/mastodon/mastodon/releases/v5.0.0',
  89. },
  90. ],
  91. }
  92. end
  93. it 'sends e-mail notifications according to settings', :aggregate_failures do
  94. expect { subject.call }.to have_enqueued_mail(AdminMailer, :new_critical_software_updates)
  95. .with(hash_including(params: { recipient: owner_user.account })).once
  96. .and(have_enqueued_mail(AdminMailer, :new_critical_software_updates).with(hash_including(params: { recipient: patch_user.account })).once)
  97. .and(have_enqueued_mail(AdminMailer, :new_critical_software_updates).with(hash_including(params: { recipient: critical_user.account })).once)
  98. .and(have_enqueued_mail.at_most(3))
  99. end
  100. end
  101. end
  102. end
  103. context 'when update checking is disabled' do
  104. around do |example|
  105. ClimateControl.modify UPDATE_CHECK_URL: '' do
  106. example.run
  107. end
  108. end
  109. before do
  110. Fabricate(:software_update, version: '3.5.0', type: 'major', urgent: false)
  111. end
  112. it 'deletes outdated update records' do
  113. expect { subject.call }.to change(SoftwareUpdate, :count).from(1).to(0)
  114. end
  115. end
  116. context 'when using the default update checking API' do
  117. let(:update_check_url) { 'https://api.joinmastodon.org/update-check' }
  118. it_behaves_like 'when the feature is enabled'
  119. end
  120. context 'when using a custom update check URL' do
  121. let(:update_check_url) { 'https://api.example.com/update_check' }
  122. around do |example|
  123. ClimateControl.modify UPDATE_CHECK_URL: 'https://api.example.com/update_check' do
  124. example.run
  125. end
  126. end
  127. it_behaves_like 'when the feature is enabled'
  128. end
  129. end