在运行每个黄瓜场景之前,我终于能够基本上清理 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