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

fix(permissions): fetch permissions return an exception when the server doesn't return an 200 response #635

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions app/controllers/forest_liana/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module ForestLiana
class ApplicationController < ForestLiana::BaseController
rescue_from ForestLiana::Ability::Exceptions::AccessDenied, with: :render_error
rescue_from ForestLiana::Errors::HTTP403Error, with: :render_error
rescue_from ForestLiana::Errors::HTTP422Error, with: :render_error

def self.papertrail?
Expand Down
18 changes: 5 additions & 13 deletions app/services/forest_liana/ability/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@ module ForestLiana
module Ability
module Fetch
def get_permissions(route)
begin
response = ForestLiana::ForestApiRequester.get(route)
response = ForestLiana::ForestApiRequester.get(route)

if response.is_a?(Net::HTTPOK)
JSON.parse(response.body)
else
raise "Forest API returned an #{ForestLiana::Errors::HTTPErrorHelper.format(response)}"
end
rescue => exception
FOREST_REPORTER.report exception
FOREST_LOGGER.error 'Cannot retrieve the permissions from the Forest server.'
FOREST_LOGGER.error 'Which was caused by:'
ForestLiana::Errors::ExceptionHelper.recursively_print(exception, margin: ' ', is_error: true)
nil
if response.is_a?(Net::HTTPOK)
JSON.parse(response.body)
else
raise ForestLiana::Errors::HTTP403Error.new("Permission could not be retrieved")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 Better <3

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/forest_liana/ability/permission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Ability
module Permission
include Fetch

TTL = (ENV['FOREST_PERMISSIONS_EXPIRATION_IN_SECONDS'] || 1).to_i.second
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hihi

TTL = (ENV['FOREST_PERMISSIONS_EXPIRATION_IN_SECONDS'] || 900).to_i.second

def is_crud_authorized?(action, user, collection)
return true unless has_permission_system?
Expand Down
11 changes: 11 additions & 0 deletions spec/services/forest_liana/ability/permission_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,17 @@ module Ability
expect {dummy_class.is_smart_action_authorized?(user, String, parameters, '/forest/actions/my_action', 'POST')}.to raise_error(ForestLiana::Errors::ExpectedError, 'The collection String doesn\'t exist')
end
end

describe 'when the server doesn\'t return an success response' do
before do
Rails.cache.clear
end

it 'should return an exception' do
allow(ForestLiana::ForestApiRequester).to receive(:get).and_return(instance_double(HTTParty::Response, code: 500, body: nil))
expect { dummy_class.is_crud_authorized?('browse', user, Island.first) }.to raise_error(ForestLiana::Errors::HTTP403Error, 'Request approval not retrieved')
end
end
end
end
end
Loading