UploadRecovery should recover attachments too.

This commit is contained in:
Guo Xiang Tan
2018-09-19 10:44:36 +08:00
parent 5a9b029014
commit 4a92c5b2d6
4 changed files with 63 additions and 23 deletions

View File

@ -6,14 +6,24 @@ RSpec.describe UploadRecovery do
let(:upload) do
UploadCreator.new(
file_from_fixtures("logo.png"),
file_from_fixtures("smallest.png"),
"logo.png"
).create_for(user.id)
end
let(:upload2) do
UploadCreator.new(
file_from_fixtures("small.pdf", "pdf"),
"some.pdf"
).create_for(user.id)
end
let(:post) do
Fabricate(:post,
raw: "![logo.png](#{upload.short_url})",
raw: <<~SQL,
![logo.png](#{upload.short_url})
<a class="attachment" href="#{upload2.url}">some.pdf</a>
SQL
user: user
).tap(&:link_post_uploads)
end
@ -21,17 +31,20 @@ RSpec.describe UploadRecovery do
let(:upload_recovery) { UploadRecovery.new }
before do
SiteSetting.authorized_extensions = 'png|pdf'
SiteSetting.queue_jobs = false
end
describe '#recover' do
after do
public_path = "#{Discourse.store.public_dir}#{upload.url}"
[upload, upload2].each do |u|
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) }
[
public_path,
public_path.sub("uploads", "uploads/tombstone")
].each { |path| File.delete(path) if File.exists?(path) }
end
end
describe 'when given an invalid sha1' do
@ -54,17 +67,18 @@ RSpec.describe UploadRecovery do
upload_recovery.recover(Post.where("updated_at >= ?", 1.day.ago))
end
it 'should recover the upload' do
it 'should recover uploads and attachments' 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)
upload2.destroy!
end.to change { post.reload.uploads.count }.from(2).to(0)
expect do
upload_recovery.recover
end.to change { post.reload.uploads.count }.from(0).to(1)
end.to change { post.reload.uploads.count }.from(0).to(2)
end
end
end