merb-auth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "MerbAuth (module)" do
4
+
5
+ it "should be registered in Merb::Slices.slices" do
6
+ Merb::Slices.slices.should include(MerbAuth)
7
+ end
8
+
9
+ it "should have an :identifier property" do
10
+ MerbAuth.identifier.should == "merb-auth"
11
+ end
12
+
13
+ it "should have a :root property" do
14
+ MerbAuth.root.should == current_slice_root
15
+ MerbAuth.root_path('app').should == current_slice_root / 'app'
16
+ end
17
+
18
+ it "should have a dir_for method" do
19
+ app_path = MerbAuth.dir_for(:application)
20
+ app_path.should == current_slice_root / 'app'
21
+ [:view, :model, :controller, :helper, :mailer, :part].each do |type|
22
+ MerbAuth.dir_for(type).should == app_path / "#{type}s"
23
+ end
24
+ public_path = MerbAuth.dir_for(:public)
25
+ public_path.should == current_slice_root / 'public'
26
+ [:stylesheet, :javascript, :image].each do |type|
27
+ MerbAuth.dir_for(type).should == public_path / "#{type}s"
28
+ end
29
+ end
30
+
31
+ it "should have a app_dir_for method" do
32
+ app_path = MerbAuth.app_dir_for(:application)
33
+ app_path.should == Merb.root / 'slices' / 'merb-auth' / 'app'
34
+ [:view, :model, :controller, :helper, :mailer, :part].each do |type|
35
+ MerbAuth.app_dir_for(type).should == app_path / "#{type}s"
36
+ end
37
+ public_path = MerbAuth.app_dir_for(:public)
38
+ public_path.should == Merb.dir_for(:public) / 'slices' / 'merb-auth'
39
+ [:stylesheet, :javascript, :image].each do |type|
40
+ MerbAuth.app_dir_for(type).should == public_path / "#{type}s"
41
+ end
42
+ end
43
+
44
+ it "should have a public_dir_for method" do
45
+ public_path = MerbAuth.public_dir_for(:public)
46
+ public_path.should == '/slices' / 'merb-auth'
47
+ [:stylesheet, :javascript, :image].each do |type|
48
+ MerbAuth.public_dir_for(type).should == public_path / "#{type}s"
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,20 @@
1
+ require File.join( File.dirname(__FILE__), "shared_user_spec")
2
+
3
+ require 'activerecord'
4
+
5
+ describe "User with Activerecord adapter" do
6
+ before(:all) do
7
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
8
+ MerbAuth.use_adapter(:activerecord)
9
+ end
10
+
11
+ before(:each) do
12
+ MerbAuth::User.create_db_table
13
+ end
14
+
15
+ after(:each) do
16
+ MerbAuth::User.drop_db_table
17
+ end
18
+
19
+ it_should_behave_like "MerbAuth::User"
20
+ end
@@ -0,0 +1,22 @@
1
+ require File.join( File.dirname(__FILE__), "shared_user_spec")
2
+
3
+ require 'dm-core'
4
+
5
+ describe "User with Datamapper adapter" do
6
+ before(:all) do
7
+ DataMapper.setup(:default, 'sqlite3::memory:')
8
+ MerbAuth.use_adapter(:datamapper)
9
+ end
10
+
11
+ before(:each) do
12
+ MerbAuth::User.create_db_table
13
+ end
14
+
15
+ after(:each) do
16
+ MerbAuth::User.drop_db_table
17
+ end
18
+
19
+ it_should_behave_like "MerbAuth::User"
20
+ end
21
+
22
+
@@ -0,0 +1,251 @@
1
+ require File.join( File.dirname(__FILE__), "..", "spec_helper" )
2
+
3
+ def valid_user_hash(options = {})
4
+ { :name => 'ctran',
5
+ :username => "ctran",
6
+ :email => "ctran@example.com",
7
+ :password => "sekret",
8
+ :password_confirmation => "sekret"}.merge(options)
9
+ end
10
+
11
+ describe "MerbAuth::User", :shared => true do
12
+ describe "username" do
13
+ it "should require username field" do
14
+ user = MerbAuth::User.new
15
+ user.should respond_to(:username)
16
+ user.should_not be_valid
17
+ user.errors.on(:username).should_not be_nil
18
+ end
19
+
20
+ it "should fail validation with username less than 3 chars" do
21
+ user = MerbAuth::User.new
22
+ user.username = "AB"
23
+ user.should_not be_valid
24
+ user.errors.on(:username).should_not be_nil
25
+ end
26
+
27
+ it "should require username with between 3 and 40 chars" do
28
+ user = MerbAuth::User.new(valid_user_hash(:username => nil))
29
+ [3,40].each do |num|
30
+ user.username = "a" * num
31
+ user.should be_valid
32
+ user.errors.on(:username).should be_nil
33
+ end
34
+ end
35
+
36
+ it "should fail validation with username over 90 chars" do
37
+ user = MerbAuth::User.new
38
+ user.username = "A" * 41
39
+ user.valid?
40
+ user.errors.on(:username).should_not be_nil
41
+ end
42
+
43
+ it "should make a valid user with all required fields set" do
44
+ user = MerbAuth::User.new(valid_user_hash)
45
+ user.save.should be_true
46
+ user.errors.should be_empty
47
+ end
48
+
49
+ it "should set timestamps after save" do
50
+ user = MerbAuth::User.new(valid_user_hash)
51
+ user.save.should be_true
52
+ user.created_at.should_not be_nil
53
+ user.updated_at.should_not be_nil
54
+ end
55
+
56
+ it "should make sure username is unique" do
57
+ user = MerbAuth::User.new( valid_user_hash(:username => "Daniel") )
58
+ user2 = MerbAuth::User.new( valid_user_hash(:username => "Daniel"))
59
+ user.save.should be_true
60
+ user.username.should == "daniel"
61
+ user2.save.should be_false
62
+ user2.errors.on(:username).should_not be_nil
63
+ end
64
+
65
+ it "should make sure username is unique regardless of case" do
66
+ user = MerbAuth::User.new( valid_user_hash(:username => "Daniel") )
67
+ user2 = MerbAuth::User.new( valid_user_hash(:username => "daniel"))
68
+ user.save.should be_true
69
+ user.username = "Daniel"
70
+ user2.save.should be_false
71
+ user2.errors.on(:username).should_not be_nil
72
+ end
73
+
74
+ it "should downcase username" do
75
+ user = MerbAuth::User.new( valid_user_hash(:username => "DaNieL"))
76
+ user.username.should == "daniel"
77
+ end
78
+
79
+ it "should authenticate a user using a class method" do
80
+ user = MerbAuth::User.new(valid_user_hash)
81
+ user.save.should be_true
82
+ MerbAuth::User.authenticate(valid_user_hash[:username], valid_user_hash[:password]).should_not be_nil
83
+ end
84
+
85
+ it "should not authenticate a user using the wrong password" do
86
+ user = MerbAuth::User.new(valid_user_hash)
87
+ user.save.should be_true
88
+ MerbAuth::User.authenticate(valid_user_hash[:username], "not_the_password").should be_nil
89
+ end
90
+
91
+ it "should not authenticate a user using the wrong username" do
92
+ user = MerbAuth::User.create(valid_user_hash)
93
+ MerbAuth::User.authenticate("not_the_username", valid_user_hash[:password]).should be_nil
94
+ end
95
+
96
+ it "should not authenticate a user that does not exist" do
97
+ MerbAuth::User.authenticate("i_dont_exist", "password").should be_nil
98
+ end
99
+ end
100
+
101
+ describe "the password fields for User" do
102
+ before(:each) do
103
+ @user = MerbAuth::User.new( valid_user_hash )
104
+ end
105
+
106
+ it "should respond to password" do
107
+ @user.should respond_to(:password)
108
+ end
109
+
110
+ it "should respond to password_confirmation" do
111
+ @user.should respond_to(:password_confirmation)
112
+ end
113
+
114
+ it "should have a protected password_required method" do
115
+ @user.protected_methods.should include("password_required?")
116
+ end
117
+
118
+ it "should respond to crypted_password" do
119
+ @user.should respond_to(:crypted_password)
120
+ end
121
+
122
+ it "should require password if password is required" do
123
+ user = MerbAuth::User.new( valid_user_hash(:password => nil))
124
+ user.stub!(:password_required?).and_return(true)
125
+ user.should_not be_valid
126
+ user.errors.on(:password).should_not be_nil
127
+ user.errors.on(:password).should_not be_empty
128
+ end
129
+
130
+ it "should set the salt" do
131
+ user = MerbAuth::User.new(valid_user_hash)
132
+ user.salt.should be_nil
133
+ user.send(:encrypt_password)
134
+ user.salt.should_not be_nil
135
+ end
136
+
137
+ it "should require the password on create" do
138
+ user = MerbAuth::User.new(valid_user_hash(:password => nil))
139
+ user.save.should_not be_true
140
+ user.errors.on(:password).should_not be_nil
141
+ user.errors.on(:password).should_not be_empty
142
+ end
143
+
144
+ it "should require password_confirmation if the password_required?" do
145
+ user = MerbAuth::User.new(valid_user_hash(:password_confirmation => nil))
146
+ user.save.should_not be_true
147
+ (user.errors.on(:password) || user.errors.on(:password_confirmation)).should_not be_nil
148
+ end
149
+
150
+ it "should fail when password is outside 4 and 40 chars" do
151
+ [3,41].each do |num|
152
+ user = MerbAuth::User.new(valid_user_hash(:password => ("a" * num)))
153
+ user.should_not be_valid
154
+ user.errors.on(:password).should_not be_nil
155
+ end
156
+ end
157
+
158
+ it "should pass when password is within 4 and 40 chars" do
159
+ [4,30,40].each do |num|
160
+ user = MerbAuth::User.new(valid_user_hash(:password => ("a" * num), :password_confirmation => ("a" * num)))
161
+ user.should be_valid
162
+ user.errors.on(:password).should be_nil
163
+ end
164
+ end
165
+
166
+ it "should autenticate against a password" do
167
+ user = MerbAuth::User.new(valid_user_hash)
168
+ user.save.should be_true
169
+ user.should be_authenticated(valid_user_hash[:password])
170
+ end
171
+
172
+ it "should not require a password when saving an existing user" do
173
+ user = MerbAuth::User.create(valid_user_hash)
174
+ user = MerbAuth::User.find_by_username(valid_user_hash[:username])
175
+ user.password.should be_nil
176
+ user.password_confirmation.should be_nil
177
+ user.username = "some_different_username_to_allow_saving"
178
+ user.save.should be_true
179
+ end
180
+ end
181
+
182
+ describe "remember_me" do
183
+ predicate_matchers[:remember_token] = :remember_token?
184
+
185
+ before do
186
+ @user = MerbAuth::User.new(valid_user_hash)
187
+
188
+ t = Date.today
189
+ Date.stub!(:today).and_return(t)
190
+ end
191
+
192
+ it "should have a remember_token_expires_at attribute" do
193
+ @user.attributes.keys.any?{|a| a.to_s == "remember_token_expires_at"}.should_not be_nil
194
+ end
195
+
196
+ it "should return true if remember_token_expires_at is set and is in the future" do
197
+ @user.remember_token_expires_at = Date.today + 7
198
+ @user.should remember_token
199
+ end
200
+
201
+ it "should set remember_token_expires_at to a specific date" do
202
+ time = Date.new(2009,12,25)
203
+ @user.remember_me_until(time)
204
+ @user.remember_token_expires_at.should == time
205
+ end
206
+
207
+ it "should set the remember_me token when remembering" do
208
+ time = Date.new(2009,12,25)
209
+ @user.remember_me_until(time)
210
+ @user.remember_token.should_not be_nil
211
+ @user.save
212
+ @user = MerbAuth::User.find_by_username(valid_user_hash[:username])
213
+ @user.remember_token.should_not be_nil
214
+ @user.remember_token_expires_at.should == time
215
+ end
216
+
217
+ it "should set remember_me token for" do
218
+ remember_until = Date.today + 7
219
+ @user.remember_me_for(7)
220
+ @user.remember_token_expires_at.should == (remember_until)
221
+ end
222
+
223
+ it "should remember_me token for two weeks" do
224
+ @user.remember_me
225
+ @user.remember_token_expires_at.should == (Date.today + 14)
226
+ end
227
+
228
+ it "should forget me" do
229
+ @user.remember_me
230
+ @user.save
231
+ @user.forget_me
232
+ @user.remember_token.should be_nil
233
+ @user.remember_token_expires_at.should be_nil
234
+ end
235
+
236
+ it "should persist the remember_me token to the database" do
237
+ @user.remember_me
238
+ @user.save
239
+
240
+ @user = MerbAuth::User.find_by_username(valid_user_hash[:username])
241
+ @user.remember_token.should_not be_nil
242
+ @user.remember_token_expires_at == (Date.today + 14)
243
+
244
+ @user.forget_me
245
+
246
+ @user = MerbAuth::User.find_by_username(valid_user_hash[:username])
247
+ @user.remember_token.should be_nil
248
+ @user.remember_token_expires_at.should be_nil
249
+ end
250
+ end
251
+ end
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'merb-core'
3
+ require 'spec'
4
+
5
+ # Add the dependency in a before_app_loads hook
6
+ Merb::BootLoader.before_app_loads { require(File.join(File.dirname(__FILE__), '..', 'lib', 'merb-auth')) }
7
+
8
+ # Using Merb.root below makes sure that the correct root is set for
9
+ # - testing standalone, without being installed as a gem and no host application
10
+ # - testing from within the host application; its root will be used
11
+ Merb.start_environment(
12
+ :testing => true,
13
+ :adapter => 'runner',
14
+ :environment => ENV['MERB_ENV'] || 'test',
15
+ :merb_root => Merb.root,
16
+ :session_store => 'memory'
17
+ )
18
+
19
+ module Merb
20
+ module Test
21
+ module SliceHelper
22
+
23
+ # The absolute path to the current slice
24
+ def current_slice_root
25
+ @current_slice_root ||= File.expand_path(File.join(File.dirname(__FILE__), '..'))
26
+ end
27
+
28
+ # Whether the specs are being run from a host application or standalone
29
+ def standalone?
30
+ not $SLICED_APP
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+
37
+ Spec::Runner.configure do |config|
38
+ config.include(Merb::Test::ViewHelper)
39
+ config.include(Merb::Test::RouteHelper)
40
+ config.include(Merb::Test::ControllerHelper)
41
+ config.include(Merb::Test::SliceHelper)
42
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merb-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Cuong Tran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-11 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb-slices
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.4
23
+ version:
24
+ description: Merb Slice that provides user authentication
25
+ email: ctran@pragmaquest.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ files:
34
+ - LICENSE
35
+ - README
36
+ - Rakefile
37
+ - lib/merb-auth
38
+ - lib/merb-auth/adapter
39
+ - lib/merb-auth/adapter/activerecord.rb
40
+ - lib/merb-auth/adapter/datamapper.rb
41
+ - lib/merb-auth/merbtasks.rb
42
+ - lib/merb-auth/model.rb
43
+ - lib/merb-auth/slicetasks.rb
44
+ - lib/merb-auth.rb
45
+ - spec/controllers
46
+ - spec/controllers/router_spec.rb
47
+ - spec/controllers/session_spec.rb
48
+ - spec/controllers/users_spec.rb
49
+ - spec/controllers/view_helper_spec.rb
50
+ - spec/merb-auth_spec.rb
51
+ - spec/models
52
+ - spec/models/ar_user_spec.rb
53
+ - spec/models/dm_user_spec.rb
54
+ - spec/models/shared_user_spec.rb
55
+ - spec/spec_helper.rb
56
+ - app/controllers
57
+ - app/controllers/application.rb
58
+ - app/controllers/controller_mixin.rb
59
+ - app/controllers/users.rb
60
+ - app/helpers
61
+ - app/helpers/application_helper.rb
62
+ - app/views
63
+ - app/views/layout
64
+ - app/views/layout/merb_auth.html.erb
65
+ - app/views/users
66
+ - app/views/users/login.html.erb
67
+ - app/views/users/signup.html.erb
68
+ - public/javascripts
69
+ - public/stylesheets
70
+ - public/stylesheets/master.css
71
+ has_rdoc: true
72
+ homepage: http://merb-slices.rubyforge.org/merb-auth/
73
+ post_install_message:
74
+ rdoc_options: []
75
+
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.1.1
94
+ signing_key:
95
+ specification_version: 2
96
+ summary: Merb Slice that provides user authentication
97
+ test_files: []
98
+