Search code examples
ruby-on-railsfunctional-testing

Testing Rails Controllers Inherited from Typus


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.


Solution

  • 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.