diff --git a/app/services/forest_liana/scope_manager.rb b/app/services/forest_liana/scope_manager.rb index d162e4d1..43c05146 100644 --- a/app/services/forest_liana/scope_manager.rb +++ b/app/services/forest_liana/scope_manager.rb @@ -49,20 +49,15 @@ def self.invalidate_scope_cache(rendering_id) end def self.fetch_scopes(rendering_id) - response = ForestLiana::ForestApiRequester.get("/liana/v4/permissions/renderings/#{rendering_id}") - - if response.is_a?(Net::HTTPOK) - Rails.cache.fetch('forest.scopes.' + rendering_id.to_s, expires_in: @@scope_cache_expiration_delta) do - data = {} - parse_response = JSON.parse(response.body) - - data['scopes'] = decode_scope(parse_response['collections']) - data['team'] = parse_response['team'] - - data - end - else - raise 'Unable to fetch scopes' + Rails.cache.fetch("forest.scopes.#{rendering_id}", expires_in: @@scope_cache_expiration_delta) do + response = ForestLiana::ForestApiRequester.get("/liana/v4/permissions/renderings/#{rendering_id}") + raise 'Unable to fetch scopes' unless response.is_a?(Net::HTTPOK) + parsed_response = JSON.parse(response.body) + + { + 'scopes' => decode_scope(parsed_response['collections']), + 'team' => parsed_response['team'] + } end end diff --git a/spec/services/forest_liana/scope_manager_spec.rb b/spec/services/forest_liana/scope_manager_spec.rb index 7ab57303..9497d34e 100644 --- a/spec/services/forest_liana/scope_manager_spec.rb +++ b/spec/services/forest_liana/scope_manager_spec.rb @@ -73,6 +73,14 @@ module ForestLiana 'team' => {'id' => '1', 'name' => 'Operations'}, }) end + + it 'calls the API only once, even with multiple fetch_scopes calls' do + described_class.fetch_scopes(rendering_id) + described_class.fetch_scopes(rendering_id) + + expect(ForestLiana::ForestApiRequester).to have_received(:get).once + end + end describe 'when scope contains dynamic values' do