Finally, I was able to get around to clear the ldap server before each cucumber script was run. I did this by adding a hook to the cucumber
Before do |scenario| puts "Cleaning Up LDAP Server" LdapConnect.new(:admin => true).clear_users! end
And then my LdapConnect class (since several models may have to touch the ldap server, I can just bypass this object). I am using ruby-net-ldap gem to interact with LDAP
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 function looks something like this:
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 | | external@test.com | right_password | externalPerson | external | person | | internal@test.com | right_password | internalPerson | internal | person | | admin@test.com | right_password | adminPerson | admin | person | Scenario: Success Login Check Given I am logged in as " external@test.com " 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
source share