diff --git a/app/models/api_key_scope.rb b/app/models/api_key_scope.rb index ba57361fa9..f0dcc91415 100644 --- a/app/models/api_key_scope.rb +++ b/app/models/api_key_scope.rb @@ -88,32 +88,29 @@ class ApiKeyScope < ActiveRecord::Base end def find_urls(actions:, methods:) - action_urls = [] - method_urls = [] + urls = [] if actions.present? - Rails.application.routes.routes.reduce([]) do |memo, route| + Rails.application.routes.routes.each do |route| defaults = route.defaults action = "#{defaults[:controller].to_s}##{defaults[:action]}" path = route.path.spec.to_s.gsub(/\(\.:format\)/, '') api_supported_path = path.end_with?('.rss') || route.path.requirements[:format]&.match?('json') excluded_paths = %w[/new-topic /new-message /exception] - memo.tap do |m| - if actions.include?(action) && api_supported_path && !excluded_paths.include?(path) - m << "#{path} (#{route.verb})" - end + if actions.include?(action) && api_supported_path && !excluded_paths.include?(path) + urls << "#{path} (#{route.verb})" end end end if methods.present? methods.each do |method| - method_urls << "* (#{method})" + urls << "* (#{method})" end end - action_urls + method_urls + urls end end diff --git a/spec/models/api_key_scope_spec.rb b/spec/models/api_key_scope_spec.rb new file mode 100644 index 0000000000..48ad48d4e9 --- /dev/null +++ b/spec/models/api_key_scope_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ApiKeyScope do + describe '.find_urls' do + it 'should return the right urls' do + expect(ApiKeyScope.find_urls(actions: ["posts#create"], methods: [])) + .to contain_exactly("/posts (POST)") + end + end +end