Skip to content

Commit

Permalink
Add direct upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ehasrouni committed Nov 19, 2024
1 parent b9ae4dc commit 5f5058b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/views/maintenance_tasks/tasks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% if @task.csv_task? %>
<div class="block">
<%= form.label :csv_file %>
<%= form.file_field :csv_file, accept: "text/csv" %>
<%= form.file_field :csv_file, accept: "text/csv", direct_upload: MaintenanceTasks.direct_upload %>
</div>
<% end %>
<% parameter_names = @task.parameter_names %>
Expand Down
7 changes: 7 additions & 0 deletions lib/maintenance_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ module MaintenanceTasks
#
# @return [ActiveSupport::Duration] the threshold in seconds after which a task is considered stuck.
mattr_accessor :stuck_task_duration, default: 5.minutes

# @!attribute direct_upload
# @scope class
# Controls direct upload support for CSV files.
#
# @return [Boolean] whether to enable direct upload for CSV files.
mattr_accessor :direct_upload, default: false
end
37 changes: 36 additions & 1 deletion test/system/maintenance_tasks/runs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ class RunsTest < ApplicationSystemTestCase
MaintenanceTasks.metadata = nil
end

test "run a CSV Task" do
test "run a CSV Task without direct upload" do
visit maintenance_tasks_path

click_on("Maintenance::ImportPostsTask")

assert_selector("input[type=file]") do |input|
assert_nil(input["data-direct-upload-url"])
end

attach_file("csv_file", "test/fixtures/files/sample.csv")
click_on "Run"

Expand All @@ -72,6 +77,26 @@ class RunsTest < ApplicationSystemTestCase
assert_text "Waiting to start."
end

test "run a CSV Task with direct upload" do
with_direct_upload(true) do
visit maintenance_tasks_path

click_on("Maintenance::ImportPostsTask")

attach_file("csv_file", "test/fixtures/files/sample.csv")
click_on "Run"
upload_url = page.server_url + "/rails/active_storage/direct_uploads"

assert_selector("input[type=file]") do |input|
assert_equal(input["data-direct-upload-url"], upload_url)
end

assert_title "Maintenance::ImportPostsTask"
assert_text "Enqueued"
assert_text "Waiting to start."
end
end

test "run a Task that accepts parameters" do
visit maintenance_tasks_path

Expand Down Expand Up @@ -272,5 +297,15 @@ class RunsTest < ApplicationSystemTestCase

assert_text "Validation failed: Status Cannot transition run from status cancelling to pausing"
end

private

def with_direct_upload(enabled)
previous = MaintenanceTasks.direct_upload
MaintenanceTasks.direct_upload = enabled
yield
ensure
MaintenanceTasks.direct_upload = previous
end
end
end

0 comments on commit 5f5058b

Please sign in to comment.