Testing Rails Cucumbers with an LDAP Server

I am trying to write some cucumber tests for my application that uses Authlogic for authentication but actually stores users on an LDAP server.

The application works fine, but where I encountered difficulties, I write tests for it (I know, I know, I had to write tests first.) It's easy to have a test database where the data is cleared after each run, but not so easy with LDAP server.

My idea was to write a rake task (e.g. rake ldap: test: prepare) to update the ldap server before each run (or make it dependent), but it seems pretty time consuming when I work on tests (and does an autotest almost impossible.)

Is there a better way to do this? Is there a fake Ruby-based LDAP server that I can contact with predefined devices? Is there an even more elegant solution that I don't think about? (Not using LDAP is not an option.)

+4
source share
5 answers

So, in general, tests for cucumbers are intended for integration and acceptance testing. In this case, it is supposed to test the system from end to end, so it should also test LDAP integration. My suggestion, if you can swing it, is to set up another LDAP server and make a periodic dump from your live to configure it with any necessary data.

I will say that your first idea to have a dependency that updates the LDAP db before each run is the “right” way to do it. Integration / acceptance testing is expected to take a long time. He tests all the functionality of the system, and not just small (single) parts.

Cucumber is not a single testing system and should not be used in this way. If your application crashed after switching to 2.3.4 because you did not have tests, I think you should get there and start writing some unit tests ...

Now this is my personal prejudice, but if you do not have unit tests, I would look at RSpec. If you like the Cucumber English-like syntax, RSpec will definitely be similar. If you have already tested a few in Test :: Unit, I would definitely suggest bringing Shoulda to the party or, possibly, Context / Matchy (all of which are available on github) to get the RSpec presentation as part of Test :: Unit.

+2
source

Try using Ladle as a test LDAP server: "Bucket plates using Lightweight Directory Access Processing (LDAP) to use when testing with rspec, cucumber, or any other ruby ​​test structure."

https://github.com/NUBIC/ladle

+8
source

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 
+2
source

I just looked into it myself and ran into a rather uncontrollable fakeldap stone.

http://github.com/aanand/fakeldap http://rubygems.org/gems/fakeldap

I can add some experience to this answer after I used it.

+1
source

Not quite the answer, but ... I am working on a very similar problem, checking LDAP authentication and cucumber search code. Have you studied the use of stubs in your test? I was thinking about sorting out my LDAP answers ... just haven't figured out how to do this yet.

Matt

0
source

All Articles