Rails 3:以编程方式获取命名空间中的路由列表

时间:2021-01-14 11:00:20

Question

How can I get a list of all the routes in my Admin namespace so that I can use it in one of my tests?

如何在Admin命名空间中获取所有路由的列表,以便我可以在其中一个测试中使用它?

Rationale

I frequently make the mistake of inheriting from ApplicationController instead of AdminController when creating new controllers in my Admin namespace. So, I want to write a test that visits all the routes in my Admin namespace and verifies that each one requires a logged in user.

在管理员命名空间中创建新控制器时,我经常犯错误,继承ApplicationController而不是AdminController。所以,我想编写一个测试来访问我的Admin命名空间中的所有路由,并验证每个路由都需要登录用户。

2 个解决方案

#1


17  

test_routes = []

Rails.application.routes.routes.each do |route|
  route = route.path.spec.to_s
  test_routes << route if route.starts_with?('/admin')
end

#2


9  

In case anybody else wants to test that all routes in a namespace require a logged in user, here's how I accomplished it:

如果其他人想要测试命名空间中的所有路由都需要登录用户,以下是我如何完成它:

ROUTES = Rails.application.routes.routes.map do |route|
  # Turn route path spec into string; use "1" for all params
  path = route.path.spec.to_s.gsub(/\(\.:format\)/, "").gsub(/:[a-zA-Z_]+/, "1")
  verb = %W{ GET POST PUT PATCH DELETE }.grep(route.verb).first.downcase.to_sym
  { path: path, verb: verb }
end

test "admin routes should redirect to admin login page when no admin user is logged in" do
  admin_routes = ROUTES.select { |route| route[:path].starts_with? "/admin" }
  unprotected_routes = []

  admin_routes.each do |route|
    begin
      reset!
      request_via_redirect(route[:verb], route[:path])
      unprotected_routes << "#{route[:verb]} #{route[:path]}" unless path == admin_login_path
    rescue ActiveRecord::RecordNotFound
      unprotected_routes << "#{route[:verb]} #{route[:path]}" unless path == admin_login_path
    rescue AbstractController::ActionNotFound
    end
  end

  assert unprotected_routes.empty?,
    "The following routes are not secure: \n\t#{unprotected_routes.uniq.join("\n\t")}"
  end
end

I welcome improvements.

我欢迎改进。

#1


17  

test_routes = []

Rails.application.routes.routes.each do |route|
  route = route.path.spec.to_s
  test_routes << route if route.starts_with?('/admin')
end

#2


9  

In case anybody else wants to test that all routes in a namespace require a logged in user, here's how I accomplished it:

如果其他人想要测试命名空间中的所有路由都需要登录用户,以下是我如何完成它:

ROUTES = Rails.application.routes.routes.map do |route|
  # Turn route path spec into string; use "1" for all params
  path = route.path.spec.to_s.gsub(/\(\.:format\)/, "").gsub(/:[a-zA-Z_]+/, "1")
  verb = %W{ GET POST PUT PATCH DELETE }.grep(route.verb).first.downcase.to_sym
  { path: path, verb: verb }
end

test "admin routes should redirect to admin login page when no admin user is logged in" do
  admin_routes = ROUTES.select { |route| route[:path].starts_with? "/admin" }
  unprotected_routes = []

  admin_routes.each do |route|
    begin
      reset!
      request_via_redirect(route[:verb], route[:path])
      unprotected_routes << "#{route[:verb]} #{route[:path]}" unless path == admin_login_path
    rescue ActiveRecord::RecordNotFound
      unprotected_routes << "#{route[:verb]} #{route[:path]}" unless path == admin_login_path
    rescue AbstractController::ActionNotFound
    end
  end

  assert unprotected_routes.empty?,
    "The following routes are not secure: \n\t#{unprotected_routes.uniq.join("\n\t")}"
  end
end

I welcome improvements.

我欢迎改进。