I was finally able to get around to basically cleaning the ldap server before each cucumber scenario was run. I did this by adding a hook into cucumber
Before do |scenario|
puts "Cleaning Up LDAP Server"
LdapConnect.new(:admin => true).clear_users!
end
And then my LdapConnect class (since multiple models might need to touch the ldap server, I can just pass around this object). I am using the ruby-net-ldap gem for LDAP interaction
class LdapConnect
def initialize(params = {})
ldap_config = YAML.load_file("#{RAILS_ROOT}/config/ldap.yml")[RAILS_ENV]
ldap_options = params.merge({:encryption => :simple_tls})
@ldap = Net::LDAP.new(ldap_options)
@ldap.host = ldap_config["host"]
@ldap.port = ldap_config["port"]
@ldap.base = ldap_config["base"]
@ldap.auth ldap_config["admin_user"], ldap_config["admin_password"] if params[:admin]
end
def ldap
@ldap
end
def clear_users!(base = "ou=people,dc=test,dc=com")
raise "You should ONLY do this on the test enviornment! It will clear out all of the users in the LDAP server" if RAILS_ENV != "test"
if @ldap.bind
@ldap.search(:filter => "cn=*", :base => base) do |entry|
@ldap.delete(:dn => entry.dn)
end
end
end
end
So, my cucumber feature looks something like:
Feature: Check to make sure users can login
In order to make sure users can login with the LDAP server
As a user
I want to make sure the user can login
Background:
Given I have the following users
| email | password | user_class | first_name | last_name |
| [email protected] | right_password | externalPerson | external | person |
| [email protected] | right_password | internalPerson | internal | person |
| [email protected] | right_password | adminPerson | admin | person |
Scenario: Success Login Check
Given I am logged in as "[email protected]" with password "right_password"
Then I should be on the homepage
And finally the steps
Given /^I have the following users$/ do |table|
# table is a Cucumber::Ast::Table
table.hashes.each do |hash|
hash[:password_confirmation] == hash[:password] unless hash[:password_confirmation]
User.create(hash)
end
end
Given /^I am logged in as "([^\"]*)" with password "([^\"]*)"$/ do |email, password|
visit login_url
fill_in "Email", :with => email
fill_in "Password", :with => password
click_button "Login"
end