add drag and drop file uploads w/async processing

[skip ci]
This commit is contained in:
Albert Yi
2018-06-06 14:55:27 -07:00
parent e808a4f7cb
commit 0e6c358701
9 changed files with 375 additions and 155 deletions

View File

@@ -326,7 +326,7 @@ class UploadServiceTest < ActiveSupport::TestCase
should "work for a jpeg" do
@service = subject.new(source: @jpeg)
@upload = @service.start!
@upload = @service.start!(CurrentUser.id)
assert_equal("preprocessed", @upload.status)
assert_not_nil(@upload.md5)
assert_equal("jpg", @upload.file_ext)
@@ -339,7 +339,7 @@ class UploadServiceTest < ActiveSupport::TestCase
should "work for an ugoira" do
@service = subject.new(source: @ugoira)
@upload = @service.start!
@upload = @service.start!(CurrentUser.id)
assert_equal("preprocessed", @upload.status)
assert_not_nil(@upload.md5)
assert_equal("zip", @upload.file_ext)
@@ -351,7 +351,7 @@ class UploadServiceTest < ActiveSupport::TestCase
should "work for a video" do
@service = subject.new(source: @video)
@upload = @service.start!
@upload = @service.start!(CurrentUser.id)
assert_equal("preprocessed", @upload.status)
assert_not_nil(@upload.md5)
assert_equal("mp4", @upload.file_ext)
@@ -368,7 +368,7 @@ class UploadServiceTest < ActiveSupport::TestCase
should "leave the upload in an error state" do
@service = subject.new(source: @video)
@upload = @service.start!
@upload = @service.start!(CurrentUser.id)
assert_match(/error:/, @upload.status)
end
end
@@ -376,6 +376,103 @@ class UploadServiceTest < ActiveSupport::TestCase
end
end
context "::Replacer" do
context "for a file replacement" do
setup do
@new_file = upload_file("test/files/test.jpg")
travel_to(1.month.ago) do
@user = FactoryBot.create(:user)
end
as_user do
@post = FactoryBot.create(:post)
@post.stubs(:queue_delete_files)
@replacement = FactoryBot.create(:post_replacement, post: @post, replacement_url: "", replacement_file: @new_file)
end
end
subject { UploadService::Replacer.new(post: @post, replacement: @replacement) }
context "#process!" do
should "create a new upload" do
assert_difference(-> { Upload.count }) do
as_user { subject.process! }
end
end
should "create a comment" do
assert_difference(-> { @post.comments.count }) do
as_user { subject.process! }
@post.reload
end
end
should "not create a new post" do
assert_difference(-> { Post.count }, 0) do
as_user { subject.process! }
end
end
should "update the post's MD5" do
assert_changes(-> { @post.md5 }) do
as_user { subject.process! }
@post.reload
end
end
end
end
context "for a source replacement" do
setup do
@new_url = "https://upload.wikimedia.org/wikipedia/commons/c/c5/Moraine_Lake_17092005.jpg"
travel_to(1.month.ago) do
@user = FactoryBot.create(:user)
end
as_user do
@post = FactoryBot.create(:post)
@post.stubs(:queue_delete_files)
@replacement = FactoryBot.create(:post_replacement, post: @post, replacement_url: @new_url)
end
end
subject { UploadService::Replacer.new(post: @post, replacement: @replacement) }
context "#process!" do
should "create a new upload" do
assert_difference(-> { Upload.count }) do
as_user { subject.process! }
end
end
should "create a comment" do
assert_difference(-> { @post.comments.count }) do
as_user { subject.process! }
@post.reload
end
end
should "not create a new post" do
assert_difference(-> { Post.count }, 0) do
as_user { subject.process! }
end
end
should "update the post's MD5" do
assert_changes(-> { @post.md5 }) do
as_user { subject.process! }
@post.reload
end
end
should "update the post's source" do
assert_changes(-> { @post.source }, nil, from: @post.source, to: @new_url) do
as_user { subject.process! }
@post.reload
end
end
end
end
end
context "#start!" do
subject { UploadService }
@@ -450,7 +547,7 @@ class UploadServiceTest < ActiveSupport::TestCase
context "with a preprocessed predecessor" do
setup do
@predecessor = FactoryBot.create(:source_upload, status: "preprocessed", source: @source, image_height: 0, image_width: 0, file_ext: "jpg")
@predecessor = FactoryBot.create(:source_upload, status: "preprocessed", source: @source, file_size: 0, md5: "something", image_height: 0, image_width: 0, file_ext: "jpg")
@tags = 'hello world'
end

View File

@@ -27,8 +27,9 @@ class PostReplacementTest < ActiveSupport::TestCase
context "Replacing" do
setup do
CurrentUser.scoped(@uploader, "127.0.0.2") do
upload = FactoryBot.create(:jpg_upload, as_pending: "0", tag_string: "lowres tag1")
upload.process!
attributes = FactoryBot.attributes_for(:jpg_upload, as_pending: "0", tag_string: "lowres tag1")
service = UploadService.new(attributes)
upload = service.start!
@post = upload.post
end
end
@@ -65,6 +66,7 @@ class PostReplacementTest < ActiveSupport::TestCase
end
should "create a post replacement record" do
binding.pry
assert_equal(@post.id, PostReplacement.last.post_id)
end