测试继承自 Typus 的 Rails 控制器

发布于 2024-11-08 18:49:53 字数 2902 浏览 0 评论 0原文

我已经为此奋斗了几天,网上似乎没有太多帮助。我查看了 Typus wiki、示例应用程序和测试,我似乎做得正确,但我仍然得到 HTTP 状态代码 302(重定向),而我在测试中预期为 200(成功)。

下面是适当的文件(删除了不相关的内容)

config/initializers/typus.rb (rails gtypus:migration 已经运行,因为我有一个 admin_users 表):

Typus.setup do |config|

  # Application name.
  config.admin_title = "Something"
  # config.admin_sub_title = ""

  # When mailer_sender is set, password recover is enabled. This email
  # address will be used in Admin::Mailer.
  config.mailer_sender = "[email protected]"

  # Define paperclip attachment styles.
  # config.file_preview = :medium
  # config.file_thumbnail = :thumb

  # Authentication: +:none+, +:http_basic+
  # Run `rails g typus:migration` if you need an advanced authentication system.
  config.authentication = :session

  # Define user_class_name.
  config.user_class_name = "AdminUser"

  # Define user_fk.
  config.user_fk = "admin_user_id"

  # Define master_role.
  config.master_role = "admin"
end

config/typus/admin_user.yml

AdminUser:
  fields:
    default: first_name, last_name, role, email, locale
    list: email, role, status
    form: first_name, last_name, role, email, password, password_confirmation, locale
    options:
      selectors: role, locale
      booleans:
        status: Active, Inactive
  filters: status, role
  search: first_name, last_name, email
  application: Admin
  description: Users Administration

test/factories/admin_users .rb:

Factory.define :admin_user do |u|
  u.first_name 'Admin'
  u.last_name 'User'
  u.email '[email protected]'
  u.role 'admin'
  u.password 'password!'
  u.token '1A2B3C4D5E6F'
  u.status true
  u.locale 'en'
end

test/function/admin/credits_controller_test.rb:

require 'test_helper'

class Admin::CreditsControllerTest < ActionController::TestCase
  setup do
    @admin_user = Factory(:admin_user)
    @request.session[:admin_user_id] = @admin_user.id
    @request.env['HTTP_REFERER'] = '/admin/credits/new'
  end

  context "new" do
    should "be successful" do
      get :new
      assert_response :success
    end
  end
end

@response.body:

<html>
  <body>You are being <a href="http://test.host/admin/session/new?back_to=%2Fadmin%2Fcredits%2Fnew">redirected</a>.
  </body>
</html>

如您所见,我已经设置了打字使用 admin_user 和 admin_user_id 作为会话密钥。但由于某种原因,测试失败,得到 302 而不是 200。我确信这是因为我做错了一些我看不到的事情。我还创建了所有这些 gist,以防万一有人喜欢。

于中部时间 2011-05-19 09:58am 编辑:添加了每个请求的响应正文文本。

I've been fighting with this for a couple of days and there doesn't seem to be much help online. I've looked at the Typus wiki, sample app, and tests and I appear to be doing things correctly but I stil get HTTP Status Code 302 (Redirect) where I expect 200 (Success) in my tests.

Below are what should be the appropriate files (with irrelevant stuff removed)

config/initializers/typus.rb (rails g typus:migration has been run as I have an admin_users table):

Typus.setup do |config|

  # Application name.
  config.admin_title = "Something"
  # config.admin_sub_title = ""

  # When mailer_sender is set, password recover is enabled. This email
  # address will be used in Admin::Mailer.
  config.mailer_sender = "[email protected]"

  # Define paperclip attachment styles.
  # config.file_preview = :medium
  # config.file_thumbnail = :thumb

  # Authentication: +:none+, +:http_basic+
  # Run `rails g typus:migration` if you need an advanced authentication system.
  config.authentication = :session

  # Define user_class_name.
  config.user_class_name = "AdminUser"

  # Define user_fk.
  config.user_fk = "admin_user_id"

  # Define master_role.
  config.master_role = "admin"
end

config/typus/admin_user.yml

AdminUser:
  fields:
    default: first_name, last_name, role, email, locale
    list: email, role, status
    form: first_name, last_name, role, email, password, password_confirmation, locale
    options:
      selectors: role, locale
      booleans:
        status: Active, Inactive
  filters: status, role
  search: first_name, last_name, email
  application: Admin
  description: Users Administration

test/factories/admin_users.rb:

Factory.define :admin_user do |u|
  u.first_name 'Admin'
  u.last_name 'User'
  u.email '[email protected]'
  u.role 'admin'
  u.password 'password!'
  u.token '1A2B3C4D5E6F'
  u.status true
  u.locale 'en'
end

test/functional/admin/credits_controller_test.rb:

require 'test_helper'

class Admin::CreditsControllerTest < ActionController::TestCase
  setup do
    @admin_user = Factory(:admin_user)
    @request.session[:admin_user_id] = @admin_user.id
    @request.env['HTTP_REFERER'] = '/admin/credits/new'
  end

  context "new" do
    should "be successful" do
      get :new
      assert_response :success
    end
  end
end

@response.body:

<html>
  <body>You are being <a href="http://test.host/admin/session/new?back_to=%2Fadmin%2Fcredits%2Fnew">redirected</a>.
  </body>
</html>

As you can see, I've set up the typus to use admin_user and admin_user_id for the session key. But for some reason that test fails getting 302 rather than 200. I'm sure this is because I'm doing something wrong that I just don't see. I've also created all these a gist, just in case someone prefers that.

Edited 2011-05-19 09:58am Central Time: Added Response body text per request.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-11-15 18:49:53

我明白了这一点。这是 config/typus/admin_roles.yml 文件的问题。

之前:

admin:
  Category: create, read, update
  Credit: read
  ...

之后:

admin:
  Category: create, read, update
  Credit: read, create
  ...

问题是管理员用户无权访问 admin/credits_controller 上的 CREATE 操作,这导致用户被发送回管理员登录地址。

授予管理员用户访问该操作的权限并更改

@session[:admin_user_id]

@session[:typus_user_id] #Just like in the Typus docs

解决问题。由于typus配置文件中的原因,我已将其更改为:admin_user_id

config.user_fk = "admin_user_id"

,同时尝试解决此问题。

I figured this out. It was a problem with the config/typus/admin_roles.yml file.

Before:

admin:
  Category: create, read, update
  Credit: read
  ...

After:

admin:
  Category: create, read, update
  Credit: read, create
  ...

The problem was that admin users didn't have access to the CREATE action on the admin/credits_controller which resulted in the user being sent back to the admin login address.

Giving admin users access to the action and changing the

@session[:admin_user_id]

to

@session[:typus_user_id] #Just like in the Typus docs

solved the problem. I had changed it to :admin_user_id because of the

config.user_fk = "admin_user_id"

in the typus config files, while trying to troubleshoot this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文