2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2014-09-23 00:50:26 -05:00
|
|
|
|
|
|
|
require_dependency 'jobs/scheduled/clean_up_uploads'
|
|
|
|
|
|
|
|
describe Jobs::CleanUpUploads do
|
2016-08-01 11:35:57 -05:00
|
|
|
|
|
|
|
def fabricate_upload
|
|
|
|
Fabricate(:upload, created_at: 2.hours.ago)
|
|
|
|
end
|
|
|
|
|
2016-06-20 15:05:41 -05:00
|
|
|
before do
|
|
|
|
Upload.destroy_all
|
2014-09-23 00:50:26 -05:00
|
|
|
SiteSetting.clean_up_uploads = true
|
2016-06-20 15:05:41 -05:00
|
|
|
SiteSetting.clean_orphan_uploads_grace_period_hours = 1
|
2016-08-01 11:35:57 -05:00
|
|
|
@upload = fabricate_upload
|
2016-06-20 15:05:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes orphan uploads" do
|
|
|
|
expect(Upload.count).to be(1)
|
|
|
|
|
2014-09-23 00:50:26 -05:00
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
2016-06-20 15:05:41 -05:00
|
|
|
|
|
|
|
expect(Upload.count).to be(0)
|
2014-09-23 00:50:26 -05:00
|
|
|
end
|
2016-06-20 15:05:41 -05:00
|
|
|
|
2016-09-08 15:58:07 -05: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)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: logo_upload.id)).to eq(logo_upload)
|
|
|
|
end
|
|
|
|
|
2016-07-01 02:22:30 -05:00
|
|
|
it "does not delete profile background uploads" do
|
2016-08-01 11:35:57 -05:00
|
|
|
profile_background_upload = fabricate_upload
|
2016-07-01 02:22:30 -05:00
|
|
|
UserProfile.last.update_attributes!(profile_background: profile_background_upload.url)
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: profile_background_upload.id)).to eq(profile_background_upload)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete card background uploads" do
|
2016-08-01 11:35:57 -05:00
|
|
|
card_background_upload = fabricate_upload
|
2016-07-01 02:22:30 -05:00
|
|
|
UserProfile.last.update_attributes!(card_background: card_background_upload.url)
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: card_background_upload.id)).to eq(card_background_upload)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete category logo uploads" do
|
2016-08-01 11:35:57 -05:00
|
|
|
category_logo_upload = fabricate_upload
|
2016-12-02 01:15:34 -06:00
|
|
|
Fabricate(:category, uploaded_logo: category_logo_upload)
|
2016-07-01 02:22:30 -05:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: category_logo_upload.id)).to eq(category_logo_upload)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete category background url uploads" do
|
2016-12-02 01:15:34 -06:00
|
|
|
category_logo_upload = fabricate_upload
|
|
|
|
Fabricate(:category, uploaded_background: category_logo_upload)
|
2016-07-01 02:22:30 -05:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
2016-12-02 01:15:34 -06:00
|
|
|
expect(Upload.find_by(id: category_logo_upload.id)).to eq(category_logo_upload)
|
2016-07-01 02:22:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete post uploads" do
|
2016-08-01 11:35:57 -05:00
|
|
|
upload = fabricate_upload
|
2016-09-08 15:58:07 -05:00
|
|
|
Fabricate(:post, uploads: [upload])
|
2016-07-01 02:22:30 -05:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: upload.id)).to eq(upload)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete user uploaded avatar" do
|
2016-08-01 11:35:57 -05:00
|
|
|
upload = fabricate_upload
|
2016-09-08 15:58:07 -05:00
|
|
|
Fabricate(:user, uploaded_avatar: upload)
|
2016-07-01 02:22:30 -05:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: upload.id)).to eq(upload)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete user gravatar" do
|
2016-08-01 11:35:57 -05:00
|
|
|
upload = fabricate_upload
|
2016-09-08 15:58:07 -05:00
|
|
|
Fabricate(:user, user_avatar: Fabricate(:user_avatar, gravatar_upload: upload))
|
2016-07-01 02:22:30 -05:00
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: upload.id)).to eq(upload)
|
|
|
|
end
|
2016-08-01 11:35:57 -05:00
|
|
|
|
2016-11-01 22:14:02 -05: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)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: upload.id)).to eq(upload)
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:35:57 -05:00
|
|
|
it "does not delete uploads in a queued post" do
|
|
|
|
upload = fabricate_upload
|
|
|
|
|
|
|
|
QueuedPost.create(
|
|
|
|
queue: "uploads",
|
|
|
|
state: QueuedPost.states[:new],
|
|
|
|
user_id: Fabricate(:user).id,
|
|
|
|
raw: upload.sha1,
|
|
|
|
post_options: {}
|
|
|
|
)
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: upload.id)).to eq(upload)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete uploads in a draft" do
|
|
|
|
upload = fabricate_upload
|
|
|
|
Draft.set(Fabricate(:user), "test", 0, upload.sha1)
|
|
|
|
|
|
|
|
Jobs::CleanUpUploads.new.execute(nil)
|
|
|
|
|
|
|
|
expect(Upload.find_by(id: @upload.id)).to eq(nil)
|
|
|
|
expect(Upload.find_by(id: upload.id)).to eq(upload)
|
|
|
|
end
|
|
|
|
|
2014-09-23 00:50:26 -05:00
|
|
|
end
|