uploads: fix more tests.
This commit is contained in:
10
test/components/file_upload_component_test.rb
Normal file
10
test/components/file_upload_component_test.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
require "test_helper"
|
||||
|
||||
class FileUploadComponentTest < ViewComponent::TestCase
|
||||
context "The FileUploadComponent" do
|
||||
should "render" do
|
||||
render_inline(FileUploadComponent.new)
|
||||
assert_text("Choose file")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,14 +2,14 @@ FactoryBot.define do
|
||||
factory(:upload) do
|
||||
uploader factory: :user
|
||||
uploader_ip_addr { "127.0.0.1" }
|
||||
status { "pending" }
|
||||
rating { "s" }
|
||||
tag_string { "" }
|
||||
source { "https://files.catbox.moe/om3tcw.webm" }
|
||||
|
||||
status { "pending" }
|
||||
source { "https://cdn.donmai.us/original/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg" }
|
||||
|
||||
factory(:completed_source_upload) do
|
||||
status { "completed" }
|
||||
source { "https://cdn.donmai.us/original/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg" }
|
||||
upload_media_assets { [build(:upload_media_asset)] }
|
||||
end
|
||||
|
||||
|
||||
@@ -647,6 +647,22 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
|
||||
context "create action" do
|
||||
should "create a post" do
|
||||
@post = create_post!(rating: "s", tag_string: "test")
|
||||
|
||||
assert_redirected_to @post
|
||||
assert_equal("s", @post.rating)
|
||||
assert_equal("test", @post.tag_string)
|
||||
end
|
||||
|
||||
should "apply the rating from the tags" do
|
||||
@post = create_post!(rating: nil, tag_string: "rating:s")
|
||||
|
||||
assert_redirected_to @post
|
||||
assert_equal("s", @post.rating)
|
||||
assert_equal("tagme", @post.tag_string)
|
||||
end
|
||||
|
||||
should "autoban the post when it is tagged banned_artist" do
|
||||
@post = create_post!(tag_string: "banned_artist")
|
||||
assert_equal(true, @post.is_banned?)
|
||||
@@ -670,6 +686,53 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
|
||||
create_post!(user: @user)
|
||||
end
|
||||
end
|
||||
|
||||
should "mark the post as pending for Member users" do
|
||||
@post = create_post!(user: create(:user), is_pending: false)
|
||||
assert_equal(true, @post.is_pending?)
|
||||
end
|
||||
|
||||
should "mark the post as active for users with unrestricted uploads" do
|
||||
@post = create_post!(user: create(:contributor_user))
|
||||
assert_equal(false, @post.is_pending?)
|
||||
end
|
||||
|
||||
should "mark the post as pending for users with unrestricted uploads who upload for approval" do
|
||||
@post = create_post!(user: create(:contributor_user), is_pending: true)
|
||||
assert_equal(true, @post.is_pending?)
|
||||
end
|
||||
|
||||
should "create a commentary record if the commentary is present" do
|
||||
assert_difference("ArtistCommentary.count", 1) do
|
||||
@post = create_post!(
|
||||
user: @user,
|
||||
artist_commentary_title: "original title",
|
||||
artist_commentary_desc: "original desc",
|
||||
translated_commentary_title: "translated title",
|
||||
translated_commentary_desc: "translated desc",
|
||||
)
|
||||
end
|
||||
|
||||
assert_equal(true, @post.artist_commentary.present?)
|
||||
assert_equal("original title", @post.artist_commentary.original_title)
|
||||
assert_equal("original desc", @post.artist_commentary.original_description)
|
||||
assert_equal("translated title", @post.artist_commentary.translated_title)
|
||||
assert_equal("translated desc", @post.artist_commentary.translated_description)
|
||||
end
|
||||
|
||||
should "not create a commentary record if the commentary is blank" do
|
||||
assert_no_difference("ArtistCommentary.count") do
|
||||
@post = create_post!(
|
||||
user: @user,
|
||||
artist_commentary_title: "",
|
||||
artist_commentary_desc: "",
|
||||
translated_commentary_title: "",
|
||||
translated_commentary_desc: "",
|
||||
)
|
||||
end
|
||||
|
||||
assert_equal(false, @post.artist_commentary.present?)
|
||||
end
|
||||
end
|
||||
|
||||
context "update action" do
|
||||
|
||||
13
test/jobs/process_upload_job_test.rb
Normal file
13
test/jobs/process_upload_job_test.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ProcessUploadJobTest < ActiveJob::TestCase
|
||||
context "ProcessUploadJob" do
|
||||
should "process a pending upload" do
|
||||
upload = create(:upload, status: "pending", source: "https://cdn.donmai.us/original/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg")
|
||||
ProcessUploadJob.perform_now(upload)
|
||||
|
||||
assert_equal("completed", upload.status)
|
||||
assert_equal("d34e4cf0a437a5d65f8e82b7bcd02606", upload.media_assets.first.md5)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,19 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PruneUploadsJobTest < ActiveJob::TestCase
|
||||
context "PruneUploadsJob" do
|
||||
should "prune all old uploads" do
|
||||
@uploader = create(:user)
|
||||
|
||||
as(@uploader) do
|
||||
@completed_upload = travel_to(2.hours.ago) { create(:upload, uploader: @uploader, status: "completed") }
|
||||
@stale_upload = travel_to(2.days.ago) { create(:upload, uploader: @uploader, status: "preprocessed") }
|
||||
@failed_upload = travel_to(4.days.ago) { create(:upload, uploader: @uploader, status: "error") }
|
||||
end
|
||||
|
||||
assert_equal(3, Upload.count)
|
||||
PruneUploadsJob.perform_now
|
||||
assert_equal(0, Upload.count)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -343,93 +343,4 @@ class UploadServiceTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "#create_post_from_upload" do
|
||||
subject { UploadService }
|
||||
|
||||
setup do
|
||||
CurrentUser.user = travel_to(1.month.ago) do
|
||||
FactoryBot.create(:user)
|
||||
end
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
end
|
||||
|
||||
teardown do
|
||||
CurrentUser.user = nil
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
|
||||
context "for a pixiv" do
|
||||
setup do
|
||||
@source = "https://i.pximg.net/img-original/img/2017/11/21/05/12/37/65981735_p0.jpg"
|
||||
@ref = "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=65981735"
|
||||
@upload = FactoryBot.create(:jpg_upload, file_size: 1000, md5: "12345", file_ext: "jpg", image_width: 100, image_height: 100, source: @source, referer_url: @ref)
|
||||
end
|
||||
|
||||
should "record the canonical source" do
|
||||
post = subject.new({}).create_post_from_upload(@upload)
|
||||
assert_equal(@source, post.source)
|
||||
end
|
||||
end
|
||||
|
||||
context "for a twitter" do
|
||||
setup do
|
||||
@source = "https://pbs.twimg.com/media/C1kt72yVEAEGpOv.jpg:large"
|
||||
@ref = "https://twitter.com/aranobu/status/817736083567820800"
|
||||
@upload = FactoryBot.create(:jpg_upload, file_size: 1000, md5: "12345", file_ext: "jpg", image_width: 100, image_height: 100, source: @source, referer_url: @ref)
|
||||
end
|
||||
|
||||
should "record the canonical source" do
|
||||
post = subject.new({}).create_post_from_upload(@upload)
|
||||
assert_equal(@ref, post.source)
|
||||
end
|
||||
end
|
||||
|
||||
context "for nijie" do
|
||||
should "record the canonical source" do
|
||||
page_url = "https://nijie.info/view.php?id=728995"
|
||||
image_url = "https://pic03.nijie.info/nijie_picture/728995_20170505014820_0.jpg"
|
||||
upload = FactoryBot.create(:jpg_upload, file_size: 1000, md5: "12345", file_ext: "jpg", image_width: 100, image_height: 100, source: image_url, referer_url: page_url)
|
||||
|
||||
post = UploadService.new({}).create_post_from_upload(upload)
|
||||
assert_equal(page_url, post.source)
|
||||
end
|
||||
end
|
||||
|
||||
context "for an image" do
|
||||
setup do
|
||||
@upload = FactoryBot.create(:source_upload, file_size: 1000, md5: "12345", file_ext: "jpg", image_width: 100, image_height: 100)
|
||||
end
|
||||
|
||||
should "create a commentary record if the commentary is present" do
|
||||
assert_difference("ArtistCommentary.count", 1) do
|
||||
@upload.update!(
|
||||
artist_commentary_title: "blah",
|
||||
artist_commentary_desc: "blah",
|
||||
translated_commentary_title: "blah",
|
||||
translated_commentary_desc: "blah"
|
||||
)
|
||||
UploadService.new({}).create_post_from_upload(@upload)
|
||||
end
|
||||
end
|
||||
|
||||
should "not create a commentary record if the commentary is blank" do
|
||||
assert_difference("ArtistCommentary.count", 0) do
|
||||
@upload.update!(
|
||||
artist_commentary_title: "",
|
||||
artist_commentary_desc: "",
|
||||
translated_commentary_title: "",
|
||||
translated_commentary_desc: ""
|
||||
)
|
||||
UploadService.new({}).create_post_from_upload(@upload)
|
||||
end
|
||||
end
|
||||
|
||||
should "create a post" do
|
||||
post = subject.new({}).create_post_from_upload(@upload)
|
||||
assert_equal([], post.errors.full_messages)
|
||||
assert_not_nil(post.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user