From 039ed61b6665adf70be29e26de8ff28248476a7d Mon Sep 17 00:00:00 2001 From: James Smith Date: Fri, 30 Aug 2024 16:09:19 +0100 Subject: [PATCH] set owner on uploaded files --- app/controllers/models_controller.rb | 2 +- app/jobs/process_uploaded_file_job.rb | 3 +- spec/jobs/process_uploaded_file_job_spec.rb | 31 ++++++++++++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/controllers/models_controller.rb b/app/controllers/models_controller.rb index 4cd1bf0ef..9464bf63d 100644 --- a/app/controllers/models_controller.rb +++ b/app/controllers/models_controller.rb @@ -71,7 +71,7 @@ def create end uploads.each do |upload| - ProcessUploadedFileJob.perform_later(library.id, upload["response"]["body"]) + ProcessUploadedFileJob.perform_later(library.id, upload["response"]["body"], owner: current_user) end redirect_to libraries_path, notice: t(".success") diff --git a/app/jobs/process_uploaded_file_job.rb b/app/jobs/process_uploaded_file_job.rb index 4b80a9f2e..4a2b5fa76 100644 --- a/app/jobs/process_uploaded_file_job.rb +++ b/app/jobs/process_uploaded_file_job.rb @@ -1,7 +1,7 @@ class ProcessUploadedFileJob < ApplicationJob queue_as :default - def perform(library_id, uploaded_file) + def perform(library_id, uploaded_file, owner: nil) # Find library library = Library.find(library_id) return if library.nil? @@ -14,6 +14,7 @@ def perform(library_id, uploaded_file) model_name = model_path.humanize.tr("+", " ").titleize # Create model model = library.models.create(name: model_name, path: "#{model_path}##{SecureRandom.hex(4)}") + model.grant_permission_to "owner", owner model.update! path: "#{model_path}##{model.id}" # Set to proper ID after saving # Handle different file types begin diff --git a/spec/jobs/process_uploaded_file_job_spec.rb b/spec/jobs/process_uploaded_file_job_spec.rb index 54c99ead4..a21f199a4 100644 --- a/spec/jobs/process_uploaded_file_job_spec.rb +++ b/spec/jobs/process_uploaded_file_job_spec.rb @@ -1,13 +1,15 @@ require "rails_helper" RSpec.describe ProcessUploadedFileJob do + subject(:job) { described_class.new } + context "when counting common path prefixes" do it "returns zero if there are no directories at all" do - expect(described_class.new.send(:count_common_elements, [])).to eq 0 + expect(job.send(:count_common_elements, [])).to eq 0 end it "returns zero if there are no common prefixes" do - expect(described_class.new.send(:count_common_elements, [ + expect(job.send(:count_common_elements, [ ["folder1"], ["folder2"], [] @@ -15,14 +17,14 @@ end it "returns the number of common prefixes if present" do - expect(described_class.new.send(:count_common_elements, [ + expect(job.send(:count_common_elements, [ ["root", "sub", "folder1"], ["root", "sub", "folder2"] ])).to eq 2 end it "returns zero for *some* common prefixes but not on everything" do - expect(described_class.new.send(:count_common_elements, [ + expect(job.send(:count_common_elements, [ ["folder1", "sub1"], ["folder1", "sub2"], ["folder2", "sub1"] @@ -30,6 +32,27 @@ end end + context "when uploading a file" do + let!(:admin) { create(:admin) } + let(:uploader) { create(:contributor) } + let(:library) { create(:library) } + let(:file) { Rack::Test::UploadedFile.new(StringIO.new("solid\n"), original_filename: "test.stl") } + + it "Creates a new model" do + expect { job.perform(library.id, file) }.to change(Model, :count).by(1) + end + + it "Sets default owner permission if no owner set" do + job.perform(library.id, file) + expect(Model.last.permitted_users.with_permission(:owner)).to include admin + end + + it "Sets owner permission to provided user" do + job.perform(library.id, file, owner: uploader) + expect(Model.last.permitted_users.with_permission(:owner)).to include uploader + end + end + context "when errors occur during processing" do let(:library) { create(:library) } let(:file) { Rack::Test::UploadedFile.new(StringIO.new, original_filename: "test.zip") }