4

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

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

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

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

4

5 回答 5

8

尝试使用 Ladle 作为测试 LDAP 服务器:“Ladle 提供轻量级目录访问 (LDAP) 的强大帮助,用于使用 rspec、cucumber 或任何其他 ruby​​ 测试框架进行测试”。

https://github.com/NUBIC/ladle

于 2011-08-10T03:06:11.510 回答
3

所以总的来说 Cucumber 测试是用于集成和验收测试。在这种情况下,它应该测试端到端的系统,所以它也应该测试 LDAP 集成。我的建议是,如果您可以使用它,可以设置另一台 LDAP 服务器,并定期从您的现场转储服务器,以使用您需要的任何测试数据进行设置。

我会说,虽然您的第一个想法是在每次运行之前拥有刷新 LDAP 数据库的依赖项是“正确”的做法。集成/验收测试应该需要很长时间。它正在测试系统的全部功能,而不仅仅是小(单元)部分。

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

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

于 2009-09-22T12:08:12.813 回答
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
于 2009-09-30T16:56:48.660 回答
1

我自己一直在研究这个,并且遇到了相当不为人知的 fakeldap gem。

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

在我使用它之后,我可能会以一些经验来补充这个答案。

于 2010-10-28T09:51:16.130 回答
0

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

马特

于 2009-09-21T19:56:49.607 回答