Skip to content

Commit 6b13ae6

Browse files
committed
switching from cucumber syntax to rspec because i got it to work with rspec it seems.
factory_girl seems to want to be loaded explicitly, so whatever.
1 parent 610e08d commit 6b13ae6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+96
-93
lines changed

Rakefile

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
require File.expand_path('test/rails_app/config/environment', File.dirname(__FILE__))
2-
require 'rake/testtask'
1+
require File.expand_path('spec/rails_app/config/environment', File.dirname(__FILE__))
32
require 'rdoc/task'
43

54
desc 'Default: run test suite.'
6-
task :default => :test
5+
task :default => :spec
76

87
desc 'Generate documentation for the devise_ldap_authenticatable plugin.'
98
Rake::RDocTask.new(:rdoc) do |rdoc|

devise_ldap_authenticatable.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
2626
s.add_development_dependency('sqlite3')
2727
s.add_development_dependency('ruby-prof')
2828
s.add_development_dependency('factory_girl_rails', '~> 1.0')
29+
s.add_development_dependency('factory_girl', '~> 2.0')
2930
s.add_development_dependency('shoulda', '~> 2.11')
3031
s.add_development_dependency('rspec-rails')
3132

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/test_helper.rb spec/spec_helper.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
require File.expand_path("rails_app/config/environment.rb", File.dirname(__FILE__))
44
require 'rspec/rails'
55
require 'rspec/autorun'
6+
require 'factory_girl' # not sure why this is not already required
67

7-
Rails.backtrace_cleaner.remove_silencers!
8+
Dir[File.expand_path("support/**/*.rb", File.dirname(__FILE__))].each {|f| require f}
89

910
RSpec.configure do |config|
1011
config.mock_with :rspec

spec/support/factories.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FactoryGirl.define do
2+
factory :user do
3+
4+
password "secret"
5+
end
6+
7+
factory :admin, :class => User do
8+
9+
password "admin_secret"
10+
end
11+
12+
factory :other, :class => User do
13+
14+
password "other_secret"
15+
end
16+
end

test/unit/user_test.rb spec/unit/user_spec.rb

+75-75
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require 'test_helper'
1+
require 'spec_helper'
22

3-
class UserTest < ActiveSupport::TestCase
3+
describe 'Users' do
44

55
def should_be_validated(user, password, message = "Password is invalid")
66
assert(user.valid_ldap_authentication?(password), message)
@@ -10,47 +10,47 @@ def should_not_be_validated(user, password, message = "Password is not properly
1010
assert(!user.valid_ldap_authentication?(password), message)
1111
end
1212

13-
context "With default settings" do
14-
setup do
13+
describe "With default settings" do
14+
before do
1515
default_devise_settings!
1616
reset_ldap_server!
1717
end
1818

19-
context "look up and ldap user" do
20-
should "return true for a user that does exist in LDAP" do
19+
describe "look up and ldap user" do
20+
it "should return true for a user that does exist in LDAP" do
2121
assert_equal true, ::Devise::LdapAdapter.valid_login?('[email protected]')
2222
end
2323

24-
should "return false for a user that doesn't exist in LDAP" do
24+
it "should return false for a user that doesn't exist in LDAP" do
2525
assert_equal false, ::Devise::LdapAdapter.valid_login?('barneystinson')
2626
end
2727
end
2828

29-
context "create a basic user" do
30-
setup do
31-
@user = Factory(:user)
29+
describe "create a basic user" do
30+
before do
31+
@user = Factory.create(:user)
3232
end
3333

34-
should "check for password validation" do
34+
it "should check for password validation" do
3535
assert_equal(@user.email, "[email protected]")
3636
should_be_validated @user, "secret"
3737
should_not_be_validated @user, "wrong_secret"
3838
should_not_be_validated @user, "Secret"
3939
end
4040
end
4141

42-
context "change a LDAP password" do
43-
setup do
44-
@user = Factory(:user)
42+
describe "change a LDAP password" do
43+
before do
44+
@user = Factory.create(:user)
4545
end
4646

47-
should "change password" do
47+
it "should change password" do
4848
should_be_validated @user, "secret"
4949
@user.reset_password!("changed","changed")
5050
should_be_validated @user, "changed", "password was not changed properly on the LDAP sevrer"
5151
end
5252

53-
should "not allow to change password if setting is false" do
53+
it "should not allow to change password if setting is false" do
5454
should_be_validated @user, "secret"
5555
::Devise.ldap_update_password = false
5656
@user.reset_password!("wrong_secret", "wrong_secret")
@@ -59,57 +59,57 @@ def should_not_be_validated(user, password, message = "Password is not properly
5959
end
6060
end
6161

62-
context "create new local user if user is in LDAP" do
62+
describe "create new local user if user is in LDAP" do
6363

64-
setup do
64+
before do
6565
assert(User.all.blank?, "There shouldn't be any users in the database")
6666
end
6767

68-
should "don't create user in the database" do
68+
it "should don't create user in the database" do
6969
@user = User.authenticate_with_ldap(:email => "[email protected]", :password => "secret")
7070
assert(User.all.blank?)
7171
end
7272

73-
context "creating users is enabled" do
74-
setup do
73+
describe "creating users is enabled" do
74+
before do
7575
::Devise.ldap_create_user = true
7676
end
7777

78-
should "create a user in the database" do
78+
it "should create a user in the database" do
7979
@user = User.authenticate_with_ldap(:email => "[email protected]", :password => "secret")
8080
assert_equal(User.all.size, 1)
8181
assert_contains(User.all.collect(&:email), "[email protected]", "user not in database")
8282
end
8383

84-
should "not create a user in the database if the password is wrong_secret" do
84+
it "should not create a user in the database if the password is wrong_secret" do
8585
@user = User.authenticate_with_ldap(:email => "example.user", :password => "wrong_secret")
8686
assert(User.all.blank?, "There's users in the database")
8787
end
8888

89-
should "create a user if the user is not in LDAP" do
89+
it "should create a user if the user is not in LDAP" do
9090
@user = User.authenticate_with_ldap(:email => "[email protected]", :password => "wrong_secret")
9191
assert(User.all.blank?, "There's users in the database")
9292
end
9393

94-
should "create a user in the database if case insensitivity does not matter" do
94+
it "should create a user in the database if case insensitivity does not matter" do
9595
::Devise.case_insensitive_keys = false
96-
@user = Factory(:user)
96+
@user = Factory.create(:user)
9797

9898
assert_difference "User.count", +1 do
9999
User.authenticate_with_ldap(:email => "[email protected]", :password => "secret")
100100
end
101101
end
102102

103-
should "not create a user in the database if case insensitivity matters" do
103+
it "should not create a user in the database if case insensitivity matters" do
104104
::Devise.case_insensitive_keys = [:email]
105-
@user = Factory(:user)
105+
@user = Factory.create(:user)
106106

107107
assert_no_difference "User.count" do
108108
User.authenticate_with_ldap(:email => "[email protected]", :password => "secret")
109109
end
110110
end
111111

112-
should "create a user with downcased email in the database if case insensitivity matters" do
112+
it "should create a user with downcased email in the database if case insensitivity matters" do
113113
::Devise.case_insensitive_keys = [:email]
114114

115115
@user = User.authenticate_with_ldap(:email => "[email protected]", :password => "secret")
@@ -119,94 +119,94 @@ def should_not_be_validated(user, password, message = "Password is not properly
119119

120120
end
121121

122-
context "use groups for authorization" do
123-
setup do
124-
@admin = Factory(:admin)
125-
@user = Factory(:user)
122+
describe "use groups for authorization" do
123+
before do
124+
@admin = Factory.create(:admin)
125+
@user = Factory.create(:user)
126126
::Devise.authentication_keys = [:email]
127127
::Devise.ldap_check_group_membership = true
128128
end
129129

130-
should "admin should be allowed in" do
130+
it "should admin should be allowed in" do
131131
should_be_validated @admin, "admin_secret"
132132
end
133133

134-
should "admin should have the proper groups set" do
134+
it "should admin should have the proper groups set" do
135135
assert_contains(@admin.ldap_groups, /cn=admins/, "groups attribute not being set properly")
136136
end
137137

138-
should "user should not be allowed in" do
138+
it "should user should not be allowed in" do
139139
should_not_be_validated @user, "secret"
140140
end
141141

142-
should "not be validated if group with different attribute is removed" do
142+
it "should not be validated if group with different attribute is removed" do
143143
`ldapmodify #{ldap_connect_string} -f ../ldap/delete_authorization_role.ldif`
144144
should_not_be_validated @admin, "admin_secret"
145145
end
146146
end
147147

148-
context "use role attribute for authorization" do
149-
setup do
150-
@admin = Factory(:admin)
151-
@user = Factory(:user)
148+
describe "use role attribute for authorization" do
149+
before do
150+
@admin = Factory.create(:admin)
151+
@user = Factory.create(:user)
152152
::Devise.ldap_check_attributes = true
153153
end
154154

155-
should "admin should be allowed in" do
155+
it "should admin should be allowed in" do
156156
should_be_validated @admin, "admin_secret"
157157
end
158158

159-
should "user should not be allowed in" do
159+
it "should user should not be allowed in" do
160160
should_not_be_validated @user, "secret"
161161
end
162162
end
163163

164-
context "use admin setting to bind" do
165-
setup do
166-
@admin = Factory(:admin)
167-
@user = Factory(:user)
164+
describe "use admin setting to bind" do
165+
before do
166+
@admin = Factory.create(:admin)
167+
@user = Factory.create(:user)
168168
::Devise.ldap_use_admin_to_bind = true
169169
end
170170

171-
should "description" do
171+
it "should description" do
172172
should_be_validated @admin, "admin_secret"
173173
end
174174
end
175175

176176
end
177177

178-
context "use uid for login" do
179-
setup do
178+
describe "use uid for login" do
179+
before do
180180
default_devise_settings!
181181
reset_ldap_server!
182182
::Devise.ldap_config = "#{Rails.root}/config/#{"ssl_" if ENV["LDAP_SSL"]}ldap_with_uid.yml"
183183
::Devise.authentication_keys = [:uid]
184184
end
185185

186-
context "description" do
187-
setup do
188-
@admin = Factory(:admin)
189-
@user = Factory(:user, :uid => "example_user")
186+
describe "description" do
187+
before do
188+
@admin = Factory.create(:admin)
189+
@user = Factory.create(:user, :uid => "example_user")
190190
end
191191

192-
should "be able to authenticate using uid" do
192+
it "should be able to authenticate using uid" do
193193
should_be_validated @user, "secret"
194194
should_not_be_validated @admin, "admin_secret"
195195
end
196196
end
197197

198-
context "create user" do
199-
setup do
198+
describe "create user" do
199+
before do
200200
::Devise.ldap_create_user = true
201201
end
202202

203-
should "create a user in the database" do
203+
it "should create a user in the database" do
204204
@user = User.authenticate_with_ldap(:uid => "example_user", :password => "secret")
205205
assert_equal(User.all.size, 1)
206206
assert_contains(User.all.collect(&:uid), "example_user", "user not in database")
207207
end
208208

209-
should "call ldap_before_save hooks" do
209+
it "should call ldap_before_save hooks" do
210210
User.class_eval do
211211
def ldap_before_save
212212
@foobar = 'foobar'
@@ -219,59 +219,59 @@ def ldap_before_save
219219
end
220220
end
221221

222-
should "not call ldap_before_save hook if not defined" do
222+
it "should not call ldap_before_save hook if not defined" do
223223
assert_nothing_raised do
224-
should_be_validated Factory(:user, :uid => "example_user"), "secret"
224+
should_be_validated Factory.create(:user, :uid => "example_user"), "secret"
225225
end
226226
end
227227
end
228228
end
229229

230-
context "using ERB in the config file" do
231-
setup do
230+
describe "using ERB in the config file" do
231+
before do
232232
default_devise_settings!
233233
reset_ldap_server!
234234
::Devise.ldap_config = "#{Rails.root}/config/#{"ssl_" if ENV["LDAP_SSL"]}ldap_with_erb.yml"
235235
end
236236

237-
context "authenticate" do
238-
setup do
239-
@admin = Factory(:admin)
240-
@user = Factory(:user)
237+
describe "authenticate" do
238+
before do
239+
@admin = Factory.create(:admin)
240+
@user = Factory.create(:user)
241241
end
242242

243-
should "be able to authenticate" do
243+
it "should be able to authenticate" do
244244
should_be_validated @user, "secret"
245245
should_be_validated @admin, "admin_secret"
246246
end
247247
end
248248
end
249249

250-
context "using variants in the config file" do
251-
setup do
250+
describe "using variants in the config file" do
251+
before do
252252
default_devise_settings!
253253
reset_ldap_server!
254254
::Devise.ldap_config = Rails.root.join 'config', 'ldap_with_boolean_ssl.yml'
255255
end
256256

257-
should "not fail if config file has ssl: true" do
257+
it "should not fail if config file has ssl: true" do
258258
assert_nothing_raised do
259259
Devise::LdapAdapter::LdapConnect.new
260260
end
261261
end
262262
end
263263

264-
context "use username builder" do
265-
setup do
264+
describe "use username builder" do
265+
before do
266266
default_devise_settings!
267267
reset_ldap_server!
268268
::Devise.ldap_auth_username_builder = Proc.new() do |attribute, login, ldap|
269269
"#{attribute}=#{login},ou=others,dc=test,dc=com"
270270
end
271-
@other = Factory(:other)
271+
@other = Factory.create(:other)
272272
end
273273

274-
should "be able to authenticate" do
274+
it "should be able to authenticate" do
275275
should_be_validated @other, "other_secret"
276276
end
277277
end

0 commit comments

Comments
 (0)