Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assets:get_id_by_legacy_url_path rake task #1439

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ namespace :assets do
asset = WhitehallAsset.find_by!(legacy_url_path: args.fetch(:legacy_url_path))
Rake::Task["assets:redirect"].invoke(asset.id, args.fetch(:redirect_url))
end

desc "Get a Whitehall asset's ID by its legacy_url_path, e.g. /government/uploads/system/uploads/attachment_data/file/1234/document.pdf"
task :get_id_by_legacy_url_path, %i[legacy_url_path] => :environment do |_t, args|
legacy_url_path = args.fetch(:legacy_url_path)
asset = WhitehallAsset.find_by!(legacy_url_path:)
puts "Asset ID for #{legacy_url_path} is #{asset.id}."
end
end
32 changes: 32 additions & 0 deletions spec/lib/tasks/assets_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "rails_helper"
require "rake"

RSpec.describe "assets.rake" do
before do
AssetManager::Application.load_tasks if Rake::Task.tasks.empty?
end

describe "get_id_by_legacy_url_path" do
before do
task.reenable # without this, calling `invoke` does nothing after first test
end

let(:task) { Rake::Task["assets:get_id_by_legacy_url_path"] }

it "returns ID of asset by its legacy URL path" do
id = "abc123def456ghi789"
legacy_url_path = "/government/uploads/system/uploads/attachment_data/file/1234/document.pdf"
FactoryBot.create(:whitehall_asset, id:, legacy_url_path:)

expected_output = <<~OUTPUT
Asset ID for #{legacy_url_path} is #{id}.
OUTPUT

expect { task.invoke(legacy_url_path) }.to output(expected_output).to_stdout
end

it "raises exception if no asset found" do
expect { task.invoke("foo") }.to raise_error(Mongoid::Errors::DocumentNotFound)
end
end
end