Goldberg is a great plugin for user/role authentication, with simplistic CMS features and an administrative interface. It wraps itself around your application without adding a single line of code to it.
The Dark Con
But for a plugin that makes authentication so easy, testing really sucks. To boot, a goldberg-enabled application is currently very unintuitive to deploy (more on that in a separate post).
Once goldberg is installed, your functional/integration tests will fail because goldberg intercepts all url requests and redirects you to /goldberg/auth/login. Fret not, here’s what to do.
Loading Goldberg’s data into the test environment
Goldberg provides two handy rake tasks:
- rake goldberg:dump_bootstrap which dumps your goldberg specific data to yaml files under vendor/plugins/goldberg/db/
- rake goldberg:load_bootstrap loads the data, from the yaml files that dump_bootstrap created, into your current database environment.
Dig into the second task and you’ll see that it uses a GoldbergMigration.load_bootstrap method.
Add this to both your functional and integration tests:
def setup
GoldbergMigration.load_bootstrap
end
This loads the goldberg-specific data into your test environment. (of course, you need to dump it at least once using rake goldberg:dump_bootstrap !)
Integration Tests
In your integration tests, add the above code to your setup method, and use the below code in your test methods.
def test_index
get "/goldberg/auth/login"
assert_response :success
post "/goldberg/auth/login", :login => {:name => "yourusername", :password => "yourpass"}
follow_redirect!
assert_response :success
# do your testing here
get "yourcontroller/index"
assert_response :success
assert_template "yourcontroller/index"
end
Functional Tests
Put the following into your setup method and they’ll run fine.
def setup
get 'goldberg/auth/login' # otherwise @request.session will be empty
@ouruser = Goldberg::User.find(:first, :conditions => 'name="yourusername"')
Goldberg::AuthController.set_user(@request.session, @ouruser.id)
end
The downside to this is that calling GoldbergMigration.load_bootstrap for every test method slows down your tests, as it reads the .yml files from disk everytime. Awful, I know. But I can’t find a better way.
Don’t forget
Before you run your tests, don’t forget to create the proper goldberg tables in your test database: run ‘rake RAILS_ENV=test goldberg:plugin_migrations‘. Also, I am assuming you have used goldberg’s admin interface to create a user to test with - you have, haven’t you ?
Live Free and DRY Hard
For my project, I created two helper methods in test_helper.rb called goldberg_setup and goldberg_migrate. The first method has the single line (GoldbergMigration.load_bootstrap) and the second one has the code that the functional tests require.
For those who know not
The goldberg plugin, currently at version 0.2.1, is under active development, and the authors are active at the Goldberg community forum.
Thanks to unclecheese whose travails preceded mine.