Fix prepared upload specs

This commit is contained in:
Oliver Günther
2024-02-19 09:51:46 +01:00
parent c4995e3121
commit 2508ede65c
8 changed files with 28 additions and 23 deletions
+13 -9
View File
@@ -72,8 +72,8 @@ class Attachment < ApplicationRecord
after_commit :enqueue_jobs, on: :create, if: -> { !internal_container? }
scope :pending_direct_upload, -> { where(digest: "", downloads: -1) }
scope :not_pending_direct_upload, -> { where.not(digest: "", downloads: -1) }
scope :pending_direct_upload, -> { status_prepared }
scope :not_pending_direct_upload, -> { not_status_prepared }
##
# Returns an URL if the attachment is stored in an external (fog) attachment storage
@@ -248,15 +248,19 @@ class Attachment < ApplicationRecord
end
def enqueue_jobs
if OpenProject::Database.allows_tsv? && (!container || container.class.attachment_tsv_extracted?)
Attachments::ExtractFulltextJob.perform_later(id)
end
extract_fulltext
if pending_virus_scan?
Attachments::VirusScanJob.perform_later(self)
end
end
def extract_fulltext
if OpenProject::Database.allows_tsv? && (!container || container.class.attachment_tsv_extracted?)
Attachments::ExtractFulltextJob.perform_later(id)
end
end
# Extract the fulltext of any attachments where fulltext is still nil.
# This runs inline and not in an asynchronous worker.
def self.extract_fulltext_where_missing(run_now: true)
@@ -315,6 +319,10 @@ class Attachment < ApplicationRecord
digest == "" && downloads == -1
end
def internal_container?
container&.is_a?(Export)
end
private
def filesize_below_allowed_maximum
@@ -323,10 +331,6 @@ class Attachment < ApplicationRecord
end
end
def internal_container?
container&.is_a?(Export)
end
def container_changed_more_than_once
if container_id_changed_more_than_once? || container_type_changed_more_than_once?
errors.add(:container, :unchangeable)
@@ -91,7 +91,7 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
options:
end
def schedule_jobs
def schedule_jobs(attachment)
attachment.extract_fulltext
end
+1 -2
View File
@@ -58,8 +58,7 @@ FactoryBot.define do
end
factory :pending_direct_upload do
digest { "" }
downloads { -1 }
status { 'prepared' }
created_at { DateTime.now - 2.weeks }
end
end
+3 -1
View File
@@ -323,7 +323,9 @@ RSpec.describe Attachment do
end
end
context 'with setting enabled', with_settings: { antivirus_scan_mode: :clamav_socket } do
context 'with setting enabled',
with_ee: %i[virus_scanning],
with_settings: { antivirus_scan_mode: :clamav_socket } do
it 'runs the job' do
attachment.save
expect(attachment.pending_virus_scan?).to be true
+3 -3
View File
@@ -81,9 +81,9 @@ RSpec.describe API::V3::Attachments::AttachmentsAPI do
let(:container_href) { nil }
describe 'GET /uploaded' do
let(:digest) { "" }
let(:status) { :prepared }
let(:attachment) do
create(:attachment, digest:, author: current_user, container: nil, container_type: nil, downloads: -1)
create(:attachment, status:, author: current_user, container: nil, container_type: nil)
end
before do
@@ -91,7 +91,7 @@ RSpec.describe API::V3::Attachments::AttachmentsAPI do
end
context 'with no pending attachments' do
let(:digest) { "0xFF" }
let(:status) { :uploaded }
it 'returns 404' do
expect(last_response.status).to eq 404
@@ -79,9 +79,8 @@ RSpec.describe Attachments::PrepareUploadService,
.to eql ""
end
it 'sets the download count to -1' do
expect(attachment.downloads)
.to be -1
it 'sets the status to prepared' do
expect(attachment.status).to eq 'prepared'
end
context 'with a special character in the filename' do
@@ -40,13 +40,13 @@ RSpec.describe Attachments::CleanupUncontaineredJob, type: :job do
end
let!(:finished_upload) do
create(:attachment, created_at: Time.now - grace_period.minutes, digest: "0x42")
create(:attachment, created_at: Time.now - grace_period.minutes, status: :uploaded)
end
let!(:old_pending_upload) do
create(:attachment, created_at: Time.now - grace_period.minutes, digest: "", downloads: -1)
create(:attachment, created_at: Time.now - grace_period.minutes, status: :prepared)
end
let!(:new_pending_upload) do
create(:attachment, created_at: Time.now - (grace_period - 1).minutes, digest: "", downloads: -1)
create(:attachment, created_at: Time.now - (grace_period - 1).minutes, status: :prepared)
end
let(:job) { described_class.new }
@@ -34,7 +34,7 @@ RSpec.describe Attachments::FinishDirectUploadJob, 'integration', type: :job do
let!(:pending_attachment) do
create(:attachment,
author: user,
downloads: -1,
status: :prepared,
digest: '',
container:)
end
@@ -47,6 +47,7 @@ RSpec.describe Attachments::FinishDirectUploadJob, 'integration', type: :job do
attachment = Attachment.find(pending_attachment.id)
expect(attachment.status).to eq 'uploaded'
expect(attachment.downloads)
.to be(0)
# expect to replace the content type with the actual value