Skip to content

Commit

Permalink
set owner on uploaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Aug 30, 2024
1 parent d80b11d commit 039ed61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion app/jobs/process_uploaded_file_job.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand All @@ -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
Expand Down
31 changes: 27 additions & 4 deletions spec/jobs/process_uploaded_file_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,58 @@
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"],
[]
])).to eq 0
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"]
])).to eq 0
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") }
Expand Down

0 comments on commit 039ed61

Please sign in to comment.