2018-09-13 00:24:08 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
require_dependency "upload_recovery"
|
|
|
|
|
|
|
|
RSpec.describe UploadRecovery do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
let(:upload) do
|
|
|
|
UploadCreator.new(
|
2018-09-18 21:44:36 -05:00
|
|
|
file_from_fixtures("smallest.png"),
|
2018-09-13 00:24:08 -05:00
|
|
|
"logo.png"
|
|
|
|
).create_for(user.id)
|
|
|
|
end
|
|
|
|
|
2018-09-18 21:44:36 -05:00
|
|
|
let(:upload2) do
|
|
|
|
UploadCreator.new(
|
|
|
|
file_from_fixtures("small.pdf", "pdf"),
|
|
|
|
"some.pdf"
|
|
|
|
).create_for(user.id)
|
|
|
|
end
|
|
|
|
|
2018-09-13 00:24:08 -05:00
|
|
|
let(:post) do
|
|
|
|
Fabricate(:post,
|
2018-09-18 21:44:36 -05:00
|
|
|
raw: <<~SQL,
|
|
|
|
![logo.png](#{upload.short_url})
|
|
|
|
SQL
|
2018-09-13 00:24:08 -05:00
|
|
|
user: user
|
2018-09-13 00:41:38 -05:00
|
|
|
).tap(&:link_post_uploads)
|
2018-09-13 00:24:08 -05:00
|
|
|
end
|
|
|
|
|
2018-09-13 00:59:17 -05:00
|
|
|
let(:upload_recovery) { UploadRecovery.new }
|
|
|
|
|
2018-09-13 00:24:08 -05:00
|
|
|
before do
|
2018-09-18 21:44:36 -05:00
|
|
|
SiteSetting.authorized_extensions = 'png|pdf'
|
2019-03-14 09:47:38 -05:00
|
|
|
Jobs.run_immediately!
|
2018-09-13 00:24:08 -05:00
|
|
|
end
|
|
|
|
|
2018-09-30 21:51:25 -05:00
|
|
|
after do
|
|
|
|
[upload, upload2].each do |u|
|
2018-10-01 01:20:50 -05:00
|
|
|
next if u
|
2018-09-30 21:51:25 -05:00
|
|
|
public_path = "#{Discourse.store.public_dir}#{u.url}"
|
|
|
|
|
|
|
|
[
|
|
|
|
public_path,
|
|
|
|
public_path.sub("uploads", "uploads/tombstone")
|
|
|
|
].each { |path| File.delete(path) if File.exists?(path) }
|
2018-09-13 00:59:17 -05:00
|
|
|
end
|
2018-09-30 21:51:25 -05:00
|
|
|
end
|
2018-09-13 00:59:17 -05:00
|
|
|
|
2018-09-30 21:51:25 -05:00
|
|
|
describe '#recover' do
|
2018-09-13 00:59:17 -05:00
|
|
|
describe 'when given an invalid sha1' do
|
|
|
|
it 'should not do anything' do
|
|
|
|
upload_recovery.expects(:recover_from_local).never
|
|
|
|
|
|
|
|
post.update!(
|
|
|
|
raw: "![logo.png](upload://#{'a' * 28}.png)"
|
|
|
|
)
|
|
|
|
|
|
|
|
upload_recovery.recover
|
2018-09-20 01:21:57 -05:00
|
|
|
|
|
|
|
post.update!(
|
|
|
|
raw: "<a href=#{"/uploads/test/original/3X/a/6%0A/#{upload.sha1}.png"}>test</a>"
|
|
|
|
)
|
|
|
|
|
|
|
|
upload_recovery.recover
|
2018-09-13 00:24:08 -05:00
|
|
|
end
|
|
|
|
end
|
2018-09-13 00:59:17 -05:00
|
|
|
|
2018-09-13 03:32:35 -05:00
|
|
|
it 'accepts a custom ActiveRecord relation' do
|
|
|
|
post.update!(updated_at: 2.days.ago)
|
|
|
|
upload.destroy!
|
|
|
|
|
|
|
|
upload_recovery.expects(:recover_from_local).never
|
|
|
|
upload_recovery.recover(Post.where("updated_at >= ?", 1.day.ago))
|
|
|
|
end
|
|
|
|
|
2018-09-18 22:52:57 -05:00
|
|
|
describe 'for a missing attachment' do
|
|
|
|
let(:post) do
|
|
|
|
Fabricate(:post,
|
|
|
|
raw: <<~SQL,
|
|
|
|
<a class="attachment" href="#{upload2.url}">some.pdf</a>
|
|
|
|
<a>blank</a>
|
|
|
|
SQL
|
|
|
|
user: user
|
|
|
|
).tap(&:link_post_uploads)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should recover the attachment' do
|
|
|
|
expect do
|
|
|
|
upload2.destroy!
|
|
|
|
end.to change { post.reload.uploads.count }.from(1).to(0)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
upload_recovery.recover
|
|
|
|
end.to change { post.reload.uploads.count }.from(0).to(1)
|
2018-09-19 03:03:52 -05:00
|
|
|
|
|
|
|
expect(File.read(Discourse.store.path_for(post.uploads.first)))
|
|
|
|
.to eq(File.read(file_from_fixtures("small.pdf", "pdf")))
|
2018-09-18 22:52:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-18 21:44:36 -05:00
|
|
|
it 'should recover uploads and attachments' do
|
2018-09-13 00:59:17 -05:00
|
|
|
stub_request(:get, "http://test.localhost#{upload.url}")
|
|
|
|
.to_return(status: 200)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
upload.destroy!
|
2018-09-18 22:52:57 -05:00
|
|
|
end.to change { post.reload.uploads.count }.from(1).to(0)
|
2018-09-13 00:59:17 -05:00
|
|
|
|
|
|
|
expect do
|
|
|
|
upload_recovery.recover
|
2018-09-18 22:52:57 -05:00
|
|
|
end.to change { post.reload.uploads.count }.from(0).to(1)
|
2018-09-19 03:03:52 -05:00
|
|
|
|
|
|
|
expect(File.read(Discourse.store.path_for(post.uploads.first)))
|
|
|
|
.to eq(File.read(file_from_fixtures("smallest.png")))
|
2018-09-13 00:59:17 -05:00
|
|
|
end
|
2019-04-01 22:41:00 -05:00
|
|
|
|
|
|
|
describe 'image tag' do
|
|
|
|
let(:post) do
|
|
|
|
Fabricate(:post,
|
|
|
|
raw: <<~SQL,
|
|
|
|
<img src='#{upload.url}'>
|
|
|
|
SQL
|
|
|
|
user: user
|
|
|
|
).tap(&:link_post_uploads)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should recover the upload' do
|
|
|
|
stub_request(:get, "http://test.localhost#{upload.url}")
|
|
|
|
.to_return(status: 200)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
upload.destroy!
|
|
|
|
end.to change { post.reload.uploads.count }.from(1).to(0)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
upload_recovery.recover
|
|
|
|
end.to change { post.reload.uploads.count }.from(0).to(1)
|
|
|
|
|
|
|
|
expect(File.read(Discourse.store.path_for(post.uploads.first)))
|
|
|
|
.to eq(File.read(file_from_fixtures("smallest.png")))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'bbcode' do
|
|
|
|
let(:post) do
|
|
|
|
Fabricate(:post,
|
|
|
|
raw: <<~SQL,
|
|
|
|
[img]#{upload.url}[/img]
|
|
|
|
SQL
|
|
|
|
user: user
|
|
|
|
).tap(&:link_post_uploads)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should recover the upload' do
|
|
|
|
stub_request(:get, "http://test.localhost#{upload.url}")
|
|
|
|
.to_return(status: 200)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
upload.destroy!
|
|
|
|
end.to change { post.reload.uploads.count }.from(1).to(0)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
upload_recovery.recover
|
|
|
|
end.to change { post.reload.uploads.count }.from(0).to(1)
|
|
|
|
|
|
|
|
expect(File.read(Discourse.store.path_for(post.uploads.first)))
|
|
|
|
.to eq(File.read(file_from_fixtures("smallest.png")))
|
|
|
|
end
|
|
|
|
end
|
2018-09-13 00:24:08 -05:00
|
|
|
end
|
2018-09-30 21:51:25 -05:00
|
|
|
|
|
|
|
describe "#recover_user_profile_backgrounds" do
|
|
|
|
before do
|
|
|
|
user.user_profile.update!(
|
|
|
|
profile_background: upload.url,
|
|
|
|
card_background: upload.url
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should recover the background uploads" do
|
|
|
|
user_profile = user.user_profile
|
|
|
|
upload.destroy!
|
|
|
|
|
|
|
|
user_profile.update_columns(
|
|
|
|
profile_background: user_profile.profile_background.sub("default", "X"),
|
|
|
|
card_background: user_profile.card_background.sub("default", "X")
|
|
|
|
)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
upload_recovery.recover_user_profile_backgrounds
|
|
|
|
end.to change { Upload.count }.by(1)
|
|
|
|
|
|
|
|
user_profile.reload
|
|
|
|
|
|
|
|
expect(user_profile.profile_background).to eq(upload.url)
|
|
|
|
expect(user_profile.card_background).to eq(upload.url)
|
|
|
|
end
|
2018-10-01 01:20:50 -05:00
|
|
|
|
|
|
|
describe 'for a bad upload' do
|
|
|
|
it 'should not update the urls' do
|
|
|
|
user_profile = user.user_profile
|
|
|
|
upload.destroy!
|
|
|
|
|
|
|
|
profile_background = user_profile.profile_background.sub("default", "X")
|
|
|
|
card_background = user_profile.card_background.sub("default", "X")
|
|
|
|
|
|
|
|
user_profile.update_columns(
|
|
|
|
profile_background: profile_background,
|
|
|
|
card_background: card_background
|
|
|
|
)
|
|
|
|
|
|
|
|
SiteSetting.authorized_extensions = ''
|
|
|
|
|
|
|
|
expect do
|
|
|
|
upload_recovery.recover_user_profile_backgrounds
|
|
|
|
end.to_not change { Upload.count }
|
|
|
|
|
|
|
|
user_profile.reload
|
|
|
|
|
|
|
|
expect(user_profile.profile_background).to eq(profile_background)
|
|
|
|
expect(user_profile.card_background).to eq(card_background)
|
|
|
|
end
|
|
|
|
end
|
2018-09-30 21:51:25 -05:00
|
|
|
end
|
2018-09-13 00:24:08 -05:00
|
|
|
end
|