Skip to content

activeldap/activeldap-fabrication

Repository files navigation

ActiveLdap Fabrication

ActiveLdap adapter for Fabrication.

Description

‘ActiveLdap Fabrication’ is an ActiveLdap adapter for Fabrication. It means that you can use Fabrication as fixture replacement for ActiveLdap.

Requirements

Install

Here are install steps for Rails 3:

  • Add activeldap-fabrication gem to your Gemfile.
  • Change fixture_replacement to Fabrication in your config/application.rb.
  • Add require "active_ldap_fabrication" in your test/test_helper.rb or spec/spec_helper.rb.

Gemfile:

group :development, :test do
  # ...
  gem "activeldap-fabrication" # <- Add this.
end

config/application.rb:

config.generators do |g|
  # For test-unit
  g.test_framework :test_unit, :fixture => true, :fixture_replacement => "fabrication"
  g.fixture_replacement :fabrication, :dir => "test/fabricators"
  # For RSpec
  # g.test_framework      :rspec, :fixture => true, :fixture_replacement => "fabrication"
  # g.fixture_replacement :fabrication
end

test/test_helper.rb:

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "active_ldap_fabrication" # <- Add this.

class ActiveSupport::TestCase
  # Add more helper methods to be used by all tests here...

  # LDAP: start
  setup do
    @dumped_data = nil
    begin
      @dumped_data = ActiveLdap::Base.dump(:scope => :sub)
    rescue ActiveLdap::ConnectionError
    end
    ActiveLdap::Base.delete_all(nil, :scope => :sub)
    populate_ldap
  end

  teardown do
    if @dumped_data
      ActiveLdap::Base.setup_connection
      ActiveLdap::Base.delete_all(nil, :scope => :sub)
      ActiveLdap::Base.load(@dumped_data)
    end
  end

  def populate_ldap
    populate_ldap_base
    populate_ldap_ou
  end

  def populate_ldap_base
    ActiveLdap::Populate.ensure_base
  end

  def populate_ldap_ou
  end
  # LDAP: end
end

Usage

We generate User model:

% script/rails generate model User --classes person

We populate ou=Users on setup in test/test_helper.rb:

# ...
class ActiveSupport::TestCase
  # ...
  def populate_ldap_ou
    ActiveLdap::Populate.ensure_ou("Users") # <- Add this.
  end
end

We define test data in test/fabricators/user_fabricator.rb:

Fabricator(:user) do
end

Fabricator(:bob, :from => :user) do
  cn "Bob"
  sn "Dyran"
end

We add a test for User model in test/unit/user_test.rb:

require 'test_helper'

class UserTest < ActiveSupport::TestCase
   test "cn" do
     assert_equal("Bob", Fabricate(:bob).cn)
   end
end

We run tests:

% bundle exec rake test
Loaded suite /var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/rake_test_loader
Started

UserTest:
     PASS cn (1.10s)

Finished in 1.101005 seconds.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

Author

Licence

This program is free software; you can redistribute it and/or modify it. It is dual licensed under Ruby’s license and under the terms of the Lesser GNU General Public License as published by the Free Software Foundation; either version 2.1, or (at your option) any later version.

Please see the file COPYING for the terms of the licence.

Thanks