Skip to content

Commit 9a7590b

Browse files
authored
fix(permissions): fetch permissions return an exception when the server doesn't return an 200 response (#635)
1 parent a19ca37 commit 9a7590b

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

app/controllers/forest_liana/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module ForestLiana
55
class ApplicationController < ForestLiana::BaseController
66
rescue_from ForestLiana::Ability::Exceptions::AccessDenied, with: :render_error
7+
rescue_from ForestLiana::Errors::HTTP403Error, with: :render_error
78
rescue_from ForestLiana::Errors::HTTP422Error, with: :render_error
89

910
def self.papertrail?

app/services/forest_liana/ability/fetch.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@ module ForestLiana
22
module Ability
33
module Fetch
44
def get_permissions(route)
5-
begin
6-
response = ForestLiana::ForestApiRequester.get(route)
5+
response = ForestLiana::ForestApiRequester.get(route)
76

8-
if response.is_a?(Net::HTTPOK)
9-
JSON.parse(response.body)
10-
else
11-
raise "Forest API returned an #{ForestLiana::Errors::HTTPErrorHelper.format(response)}"
12-
end
13-
rescue => exception
14-
FOREST_REPORTER.report exception
15-
FOREST_LOGGER.error 'Cannot retrieve the permissions from the Forest server.'
16-
FOREST_LOGGER.error 'Which was caused by:'
17-
ForestLiana::Errors::ExceptionHelper.recursively_print(exception, margin: ' ', is_error: true)
18-
nil
7+
if response.is_a?(Net::HTTPOK)
8+
JSON.parse(response.body)
9+
else
10+
raise ForestLiana::Errors::HTTP403Error.new("Permission could not be retrieved")
1911
end
2012
end
2113
end

app/services/forest_liana/ability/permission.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Ability
66
module Permission
77
include Fetch
88

9-
TTL = (ENV['FOREST_PERMISSIONS_EXPIRATION_IN_SECONDS'] || 1).to_i.second
9+
TTL = (ENV['FOREST_PERMISSIONS_EXPIRATION_IN_SECONDS'] || 900).to_i.second
1010

1111
def is_crud_authorized?(action, user, collection)
1212
return true unless has_permission_system?

spec/services/forest_liana/ability/permission_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,17 @@ module Ability
327327
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')
328328
end
329329
end
330+
331+
describe 'when the server doesn\'t return an success response' do
332+
before do
333+
Rails.cache.clear
334+
end
335+
336+
it 'should return an exception' do
337+
allow(ForestLiana::ForestApiRequester).to receive(:get).and_return(instance_double(HTTParty::Response, code: 500, body: nil))
338+
expect { dummy_class.is_crud_authorized?('browse', user, Island.first) }.to raise_error(ForestLiana::Errors::HTTP403Error, 'Permission could not be retrieved')
339+
end
340+
end
330341
end
331342
end
332343
end

0 commit comments

Comments
 (0)