2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2014-09-23 13:50:26 +08:00
|
|
|
|
|
|
|
require_dependency 'jobs/scheduled/clean_up_uploads'
|
|
|
|
|
|
|
|
describe Jobs::CleanUpUploads do
|
2016-08-02 00:35:57 +08:00
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
def fabricate_upload(attributes = {})
|
|
|
|
Fabricate(:upload, { created_at: 2.hours.ago }.merge(attributes))
|
2016-08-02 00:35:57 +08:00
|
|
|
end
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
let(:upload) { fabricate_upload }
|
|
|
|
|
2016-06-21 04:05:41 +08:00
|
|
|
before do
|
2014-09-23 13:50:26 +08:00
|
|
|
SiteSetting.clean_up_uploads = true
|
2016-06-21 04:05:41 +08:00
|
|
|
SiteSetting.clean_orphan_uploads_grace_period_hours = 1
|
2016-08-02 00:35:57 +08:00
|
|
|
@upload = fabricate_upload
|
2016-06-21 04:05:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes orphan uploads" do
|
2018-06-05 10:03:26 +08:00
|
|
|
expect do
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
end.to change { Upload.count }.by(-1)
|
2016-06-21 04:05:41 +08:00
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when clean_up_uploads is disabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.clean_up_uploads = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should still delete invalid upload records' do
|
|
|
|
upload2 = fabricate_upload(
|
|
|
|
url: "",
|
|
|
|
retain_hours: nil
|
|
|
|
)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
end.to change { Upload.count }.by(-1)
|
2016-06-21 04:05:41 +08:00
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(true)
|
|
|
|
expect(Upload.exists?(id: upload2.id)).to eq(false)
|
|
|
|
end
|
2014-09-23 13:50:26 +08:00
|
|
|
end
|
2016-06-21 04:05:41 +08:00
|
|
|
|
2016-09-09 04:58:07 +08:00
|
|
|
it "does not clean up uploads in site settings" do
|
|
|
|
logo_upload = fabricate_upload
|
|
|
|
SiteSetting.logo_url = logo_upload.url
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: logo_upload.id)).to eq(true)
|
2016-09-09 04:58:07 +08:00
|
|
|
end
|
|
|
|
|
2017-06-08 04:53:15 +08:00
|
|
|
it "does not clean up uploads in site settings when they use the CDN" do
|
|
|
|
Discourse.stubs(:asset_host).returns("//my.awesome.cdn")
|
|
|
|
|
|
|
|
logo_small_upload = fabricate_upload
|
|
|
|
SiteSetting.logo_small_url = "#{Discourse.asset_host}#{logo_small_upload.url}"
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: logo_small_upload.id)).to eq(true)
|
2017-06-08 04:53:15 +08:00
|
|
|
end
|
|
|
|
|
2016-07-01 15:22:30 +08:00
|
|
|
it "does not delete profile background uploads" do
|
2016-08-02 00:35:57 +08:00
|
|
|
profile_background_upload = fabricate_upload
|
2016-07-01 15:22:30 +08:00
|
|
|
UserProfile.last.update_attributes!(profile_background: profile_background_upload.url)
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: profile_background_upload.id)).to eq(true)
|
2016-07-01 15:22:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete card background uploads" do
|
2016-08-02 00:35:57 +08:00
|
|
|
card_background_upload = fabricate_upload
|
2016-07-01 15:22:30 +08:00
|
|
|
UserProfile.last.update_attributes!(card_background: card_background_upload.url)
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: card_background_upload.id)).to eq(true)
|
2016-07-01 15:22:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete category logo uploads" do
|
2016-08-02 00:35:57 +08:00
|
|
|
category_logo_upload = fabricate_upload
|
2016-12-02 15:15:34 +08:00
|
|
|
Fabricate(:category, uploaded_logo: category_logo_upload)
|
2016-07-01 15:22:30 +08:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: category_logo_upload.id)).to eq(true)
|
2016-07-01 15:22:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete category background url uploads" do
|
2016-12-02 15:15:34 +08:00
|
|
|
category_logo_upload = fabricate_upload
|
|
|
|
Fabricate(:category, uploaded_background: category_logo_upload)
|
2016-07-01 15:22:30 +08:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: category_logo_upload.id)).to eq(true)
|
2016-07-01 15:22:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete post uploads" do
|
2016-08-02 00:35:57 +08:00
|
|
|
upload = fabricate_upload
|
2016-09-09 04:58:07 +08:00
|
|
|
Fabricate(:post, uploads: [upload])
|
2016-07-01 15:22:30 +08:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: upload.id)).to eq(true)
|
2016-07-01 15:22:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete user uploaded avatar" do
|
2016-08-02 00:35:57 +08:00
|
|
|
upload = fabricate_upload
|
2016-09-09 04:58:07 +08:00
|
|
|
Fabricate(:user, uploaded_avatar: upload)
|
2016-07-01 15:22:30 +08:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: upload.id)).to eq(true)
|
2016-07-01 15:22:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete user gravatar" do
|
2016-08-02 00:35:57 +08:00
|
|
|
upload = fabricate_upload
|
2016-09-09 04:58:07 +08:00
|
|
|
Fabricate(:user, user_avatar: Fabricate(:user_avatar, gravatar_upload: upload))
|
2016-07-01 15:22:30 +08:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: upload.id)).to eq(true)
|
2016-07-01 15:22:30 +08:00
|
|
|
end
|
2016-08-02 00:35:57 +08:00
|
|
|
|
2016-11-02 11:14:02 +08:00
|
|
|
it "does not delete user custom upload" do
|
|
|
|
upload = fabricate_upload
|
|
|
|
Fabricate(:user, user_avatar: Fabricate(:user_avatar, custom_upload: upload))
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: upload.id)).to eq(true)
|
2016-11-02 11:14:02 +08:00
|
|
|
end
|
|
|
|
|
2016-08-02 00:35:57 +08:00
|
|
|
it "does not delete uploads in a queued post" do
|
|
|
|
upload = fabricate_upload
|
2017-11-13 22:01:31 +08:00
|
|
|
upload2 = fabricate_upload
|
2016-08-02 00:35:57 +08:00
|
|
|
|
|
|
|
QueuedPost.create(
|
|
|
|
queue: "uploads",
|
|
|
|
state: QueuedPost.states[:new],
|
|
|
|
user_id: Fabricate(:user).id,
|
2017-11-13 22:01:31 +08:00
|
|
|
raw: "#{upload.sha1}\n#{upload2.short_url}",
|
2016-08-02 00:35:57 +08:00
|
|
|
post_options: {}
|
|
|
|
)
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: upload.id)).to eq(true)
|
|
|
|
expect(Upload.exists?(id: upload2.id)).to eq(true)
|
2016-08-02 00:35:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete uploads in a draft" do
|
|
|
|
upload = fabricate_upload
|
2017-11-13 22:01:31 +08:00
|
|
|
upload2 = fabricate_upload
|
|
|
|
|
|
|
|
Draft.set(Fabricate(:user), "test", 0, "#{upload.sha1}\n#{upload2.short_url}")
|
2016-08-02 00:35:57 +08:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: upload.id)).to eq(true)
|
|
|
|
expect(Upload.exists?(id: upload2.id)).to eq(true)
|
2016-08-02 00:35:57 +08:00
|
|
|
end
|
|
|
|
|
2017-02-02 17:41:57 +08:00
|
|
|
it "does not delete custom emojis" do
|
|
|
|
upload = fabricate_upload
|
|
|
|
CustomEmoji.create!(name: 'test', upload: upload)
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: upload.id)).to eq(true)
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
|
|
|
|
2018-04-19 19:30:31 +08:00
|
|
|
it "does not delete user exported csv uploads" do
|
|
|
|
csv_file = fabricate_upload
|
|
|
|
UserExport.create(file_name: "export.csv", user_id: Fabricate(:user).id, upload_id: csv_file.id)
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
2018-06-05 10:03:26 +08:00
|
|
|
expect(Upload.exists?(id: @upload.id)).to eq(false)
|
|
|
|
expect(Upload.exists?(id: csv_file.id)).to eq(true)
|
2018-04-19 19:30:31 +08:00
|
|
|
end
|
2014-09-23 13:50:26 +08:00
|
|
|
end
|