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

应用程序看起来工作正常,但在那里我遇到了麻烦编写测试它(我知道,我知道,我应该先写测试。)这很容易有一个测试数据库,其中的数据是每次运行之后清除出去,但不是那么容易与LDAP服务器。

我的想法是写一耙的任务(如耙LDAP:测试:准备)刷新每次运行前的LDAP服务器(或使其依赖),但似乎是相当的时间,当我在测试工作消耗(和使得自动测试几乎是不可能的。)

有没有更好的方式来做到这一点?有没有我可以绑定到与预先定义的灯具基于红宝石假LDAP服务器?有一些甚至其他更优雅的解决方案,我没有想到的? (不使用LDAP是不是一个选项。)

有帮助吗?

解决方案

所以一般黄瓜检测是用于集成和验收测试。这是它应该是测试系统的终端到终端的情况下,所以应该测试LDAP的集成。我的建议,如果你能摆动它,将成立另一个LDAP服务器,并从你的生活一个与你需要的任何测试数据设置它做一个定期的转储。

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

黄瓜是不是一个单元测试框架,并且不应该以这种方式来使用。如果您的应用程序迁移到2.3.4之后,因为你没有测试,我想你应该在那里得到的,并开始编写一些单元测试打破了......

现在,这是我个人的偏见,但如果你在的地方没有单元测试,我想看看RSpec的。如果你喜欢黄瓜的类似英语的语法,RSpec的肯定会觉得类似。如果你已经在一定程度上测试::单位进行测试,我肯定会建议把早该党或可能上下文/中规中矩(所有这一切都可以在GitHub上)来获得的RSpec测试::单位范围内的感觉。

其他提示

有使用钢包作为测试LDAP服务器一去:“钢包菜肴汽蒸的轻量级目录访问(LDAP),用于使用RSpec,黄瓜,或任何其他测试红宝石框架测试发展帮助”

https://github.com/NUBIC/ladle

我终于可以得到解决,基本清洁的LDAP服务器的每个场景黄瓜运行前。我通过将钩到黄瓜这样做

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

然后我LdapConnect类(因为多个模型可能需要触摸LDAP服务器,我可以绕过这个对象)。我使用的红宝石净LDAP宝石为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

我只是在寻找这个我自己,和所遇到的,而下的雷达fakeldap宝石。

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

我使用后,我可以添加到这个答案有一定的经验。

不是一个真正的答案,但...我工作在一个非常类似的问题,测试LDAP身份验证和查找代码黄瓜。你有没有到你的测试使用存根?我在想我的磕碰LDAP响应...只是还没有想出如何做到这一点呢。

马特

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top