使用LDAP服务器进行Rails黄瓜测试

时间:2009-09-21 18:56:59

标签: ruby-on-rails ldap cucumber authlogic

我正在尝试为使用Authlogic进行身份验证的应用程序编写一些黄瓜测试,但实际上是将用户存储在LDAP服务器中。

该应用程序似乎工作正常,但我遇到麻烦的是为它编写测试(我知道,我应该先编写测试。)很容易有一个测试数据库,数据是每次运行后清除,但使用LDAP服务器则不那么容易。

我的想法是编写一个rake任务(比如rake ldap:test:prepare)在每次运行之前刷新ldap服务器(或使它成为一个依赖项),但是当我正在进行测试时这似乎相当耗时(和使自动测试几乎不可能。)

有更好的方法吗?是否有基于ruby的假LDAP服务器,我可以使用预定义的灯具绑定?还有其他更优雅的解决方案我没想到吗? (不使用LDAP不是一种选择。)

5 个答案:

答案 0 :(得分:8)

使用Ladle作为测试LDAP服务器:“Ladle将轻量级目录访问(LDAP)的蒸汽帮助用于测试rspec,黄瓜或任何其他ruby测试框架”。

https://github.com/NUBIC/ladle

答案 1 :(得分:3)

因此,一般而言,Cucumber测试用于集成和验收测试。在这种情况下,它应该是端到端测试系统,所以它也应该测试LDAP集成。我的建议是,如果你可以解决它,那就是设置另一个LDAP服务器并从你的实时转发中定期转储,以便用你需要的任何测试数据进行设置。

我会说,尽管你在每次运行之前拥有刷新LDAP数据库的依赖关系的第一个想法是“正确”的方法。集成/验收测试假设需要很长时间。它正在测试系统的全部功能,而不仅仅是小型(单元)。

Cucumber不是单元测试框架,不应该以这种方式使用。如果您的应用程序在迁移到2.3.4后因为您没有测试而中断,我认为您应该进入并开始编写一些单元测试......

现在这是我个人的偏见,但如果你没有进行单元测试,我会看一下RSpec。如果你喜欢Cucumber的类似英语的语法,那么RSpec肯定会有类似的感觉。如果您已经在Test :: Unit中进行了一些测试,我肯定会建议将Shoulda带到聚会上,或者可能将Context / Matchy(所有这些都可以在github上使用)中获得RSpec在Test :: Unit框架内的感觉。

答案 2 :(得分:2)

在每个黄瓜方案运行之前,我终于能够彻底清理ldap服务器了。我通过在黄瓜中添加一个钩子来做到这一点

Before do |scenario|
  puts "Cleaning Up LDAP Server"
  LdapConnect.new(:admin => true).clear_users!
end

然后是我的LdapConnect类(因为多个模型可能需要触摸ldap服务器,我只能传递这个对象)。我正在使用ruby-net-ldap gem进行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

所以,我的黄瓜功能看起来像:

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

最后是步骤

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

答案 3 :(得分:1)

我自己一直在研究这个问题,并且遇到了相当低调的fakeldap宝石。

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

在使用它之后,我可以通过一些经验添加这个答案。

答案 4 :(得分:0)

不是真正的答案,但是......我正在研究一个非常类似的问题,用黄瓜测试LDAP身份验证和查找代码。您是否考虑过在测试中使用存根?我正在考虑将我的LDAP响应存根...只是还没弄明白该怎么做。

马特