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

SingleUseLinksViewerController current_ability behavior fixes #6739

Merged
merged 2 commits into from
Mar 28, 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
11 changes: 9 additions & 2 deletions app/controllers/hyrax/single_use_links_viewer_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/hyrax/single_use_links_viewer_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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