使用 LDAP 服务器进行 Rails Cucumber 测试

发布于 2024-08-05 06:44:42 字数 397 浏览 8 评论 0原文

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

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

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

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

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

The application seems to work fine, but where I am running into trouble is writing tests for it (I know, I know, I should've wrote the tests first.) It's easy to have a test database where the data is cleared out after each run, but not so easy with an LDAP server.

My idea was to write a rake task (like rake ldap:test:prepare) to refresh the ldap server before each run (or make it a dependency), but that seems pretty time consuming when I am working on tests (and makes autotest near impossible.)

Is there a better way to do this? Is there a ruby-based fake LDAP server I can bind to with pre-defined fixtures? Is there some other even more elegant solution that I am not thinking of? (not using LDAP isn't an option.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

£冰雨忧蓝° 2024-08-12 06:44:42

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

https://github.com/NUBIC/ladle

Have a go using Ladle as a test LDAP server: "Ladle dishes out steaming helpings of lightweight directory access (LDAP) for use in testing with rspec, cucumber, or any other ruby test framework".

https://github.com/NUBIC/ladle

亢潮 2024-08-12 06:44:42

所以一般来说,Cucumber 测试用于集成和验收测试。既然如此,它应该对系统进行端到端测试,因此它也应该测试 LDAP 集成。如果您可以的话,我的建议是设置另一台 LDAP 服务器,并从您的实时服务器中定期转储,以便使用您需要的任何测试数据来设置它。

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

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

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

So in general Cucumber tests are for integration and acceptance testing. That being the case it is supposed to test the system end-to-end, so it should be testing the LDAP integration as well. My suggestion, if you can swing it, would be to set up another LDAP server and do a periodic dump from your live one to set it up with whatever test data you need.

I will say though that your first idea of having the dependency that refreshes the LDAP db before each run is the "right" way to do it. Integration/acceptance testing is supposed to take a long time. It is testing the entirety of the functionality of the system, not just small (unit) pieces.

Cucumber is not a unit testing framework, and shouldn't be used in that manner. If your application broke after migrating to 2.3.4 because you didn't have tests I think you should get in there and start writing some unit tests...

Now this is my personal bias, but if you have no unit tests in place I would take a look at RSpec. If you like the english-like syntax of Cucumber, RSpec will definitely feel similar. If you are already somewhat tested 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 feel within the Test::Unit framework.

油饼 2024-08-12 06:44:42

我终于能够在运行每个 Cucumber 场景之前对 LDAP 服务器进行基本清理。我通过在 cucumber 中添加一个钩子

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 |
    | [email protected] | right_password | externalPerson | external | person |
    | [email protected] | right_password | internalPerson | internal | person |
    | [email protected] | right_password | adminPerson | admin | person |

  Scenario: Success Login Check
    Given I am logged in as "[email protected]" 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

I was finally able to get around to basically cleaning the ldap server before each cucumber scenario was run. I did this by adding a hook into cucumber

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

And then my LdapConnect class (since multiple models might need to touch the ldap server, I can just pass around this object). I am using the ruby-net-ldap gem for LDAP interaction

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 feature looks something like:

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 |
    | [email protected] | right_password | externalPerson | external | person |
    | [email protected] | right_password | internalPerson | internal | person |
    | [email protected] | right_password | adminPerson | admin | person |

  Scenario: Success Login Check
    Given I am logged in as "[email protected]" 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
阳光的暖冬 2024-08-12 06:44:42

我自己刚刚对此进行了研究,并发现了相当不为人所知的 fakeldap 宝石。

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

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

I've just been looking into this myself, and have come across the rather under-the-radar fakeldap gem.

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

I may add to this answer with some experience after i've used it.

夏夜暖风 2024-08-12 06:44:42

并不是真正的答案,但是......我正在解决一个非常相似的问题,用 cucumber 测试 LDAP 身份验证和查找代码。您是否考虑过在测试中使用存根?我正在考虑存根我的 LDAP 响应...只是还没有弄清楚如何做到这一点。

马特

Not really an answer but...I'm working on a very similar problem, testing LDAP authentication and lookup code with cucumber. Have you looked into using a stub in your test? I was thinking of stubbing my LDAP responses...just haven't figured out how to do it yet.

Matt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文