diff --git a/app/controllers/hyrax/single_use_links_viewer_controller.rb b/app/controllers/hyrax/single_use_links_viewer_controller.rb index e7ab099bd9..9e246526cc 100644 --- a/app/controllers/hyrax/single_use_links_viewer_controller.rb +++ b/app/controllers/hyrax/single_use_links_viewer_controller.rb @@ -84,7 +84,13 @@ def asset end def current_ability - @current_ability ||= SingleUseLinksViewerController::Ability.new current_user, single_use_link + link_instance = nil + begin + link_instance = single_use_link + rescue ActiveRecord::RecordNotFound + Rails.logger.debug("Single user link was not found while getting current ability") + end + @current_ability ||= SingleUseLinksViewerController::Ability.new current_user, link_instance end def render_single_use_error(exception) @@ -102,9 +108,10 @@ class Ability include CanCan::Ability attr_reader :single_use_link + attr_reader :current_user def initialize(user, single_use_link) - @user = user || ::User.new + @current_user = user || ::User.new return unless single_use_link @single_use_link = single_use_link diff --git a/spec/controllers/hyrax/single_use_links_viewer_controller_spec.rb b/spec/controllers/hyrax/single_use_links_viewer_controller_spec.rb index e9dca30c10..368bce9df8 100644 --- a/spec/controllers/hyrax/single_use_links_viewer_controller_spec.rb +++ b/spec/controllers/hyrax/single_use_links_viewer_controller_spec.rb @@ -64,5 +64,19 @@ expect(response).to render_template("hyrax/single_use_links_viewer/single_use_error", "layouts/error") end end + + describe "#current_ability" do + context "when the key is not found" do + before { SingleUseLink.find_by_download_key!(show_link_hash).destroy } + + it "returns the current ability" do + expect(subject.send(:current_ability)).to be_present + end + + it "returns the current user" do + expect(subject.send(:current_ability).current_user).to be_present + end + end + end end end