simplest_auth 0.2.10 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ /pkg/
2
+ /doc/
3
+ /coverage/
4
+ Gemfile.lock
5
+ /package/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@simplest_auth --create
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Tony Pitale of Viget Labs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake/gempackagetask'
3
3
  require 'rake/testtask'
4
4
 
5
- require 'lib/simplest_auth/version'
5
+ require File.expand_path('../lib/simplest_auth/version', __FILE__)
6
6
 
7
7
  task :default => :test
8
8
 
@@ -15,8 +15,12 @@ spec = Gem::Specification.new do |s|
15
15
  s.homepage = 'http://viget.com/extend'
16
16
  s.files = %w(README.textile Rakefile) + Dir.glob("lib/**/*")
17
17
  s.test_files = Dir.glob("test/**/*_test.rb")
18
-
18
+
19
19
  s.add_dependency('bcrypt-ruby', '~> 2.1.1')
20
+
21
+ s.add_development_dependency('shoulda')
22
+ s.add_development_dependency('mocha')
23
+ s.add_development_dependency('activemodel')
20
24
  end
21
25
 
22
26
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -38,7 +42,7 @@ end
38
42
 
39
43
  begin
40
44
  require 'rcov/rcovtask'
41
-
45
+
42
46
  desc "Generate RCov coverage report"
43
47
  Rcov::RcovTask.new(:rcov) do |t|
44
48
  t.test_files = FileList['test/**/*_test.rb']
@@ -10,4 +10,6 @@ end
10
10
 
11
11
  # SimplestAuth
12
12
  require 'simplest_auth/model'
13
- require 'simplest_auth/controller'
13
+ require 'simplest_auth/controller'
14
+ require 'simplest_auth/sessions_controller'
15
+ require 'simplest_auth/session'
@@ -46,7 +46,7 @@ module SimplestAuth
46
46
  if user_class.respond_to?(:get)
47
47
  user_class.get(current_user_id)
48
48
  else
49
- user_class.find(current_user_id)
49
+ current_user_id && user_class.where(:id => current_user_id).first
50
50
  end
51
51
  end || clear_session
52
52
  end
@@ -34,27 +34,27 @@ module SimplestAuth
34
34
 
35
35
  def authenticate(email, password)
36
36
  if active_record?
37
- klass = find_by_email(email)
37
+ found = where(:email => email).first
38
38
  elsif data_mapper? || mongo_mapper?
39
- klass = first(:email => email)
39
+ found = first(:email => email)
40
40
  end
41
41
 
42
- (klass && klass.authentic?(password)) ? klass : nil
42
+ (found && found.authentic?(password)) ? found : nil
43
43
  end
44
44
 
45
45
  def authenticate_by(ident)
46
46
  if active_record?
47
47
  instance_eval <<-EOM
48
48
  def authenticate(#{ident}, password)
49
- klass = find_by_#{ident}(#{ident})
50
- (klass && klass.authentic?(password)) ? klass : nil
49
+ found = where(:#{ident} => #{ident}).first
50
+ (found && found.authentic?(password)) ? found : nil
51
51
  end
52
52
  EOM
53
53
  elsif data_mapper? || mongo_mapper?
54
54
  instance_eval <<-EOM
55
55
  def authenticate(#{ident}, password)
56
- klass = first(:#{ident} => #{ident})
57
- (klass && klass.authentic?(password)) ? klass : nil
56
+ found = first(:#{ident} => #{ident})
57
+ (found && found.authentic?(password)) ? found : nil
58
58
  end
59
59
  EOM
60
60
  end
@@ -0,0 +1,57 @@
1
+ module SimplestAuth
2
+ module Session
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ include ActiveModel::Validations
7
+ include ActiveModel::Conversion
8
+
9
+ attr_accessor :email, :password
10
+
11
+ validates :email, :presence => true
12
+ validates :password, :presence => true
13
+
14
+ validate :user_exists_for_credentials, :if => :credentials_supplied?
15
+ end
16
+
17
+ module ClassMethods
18
+ def set_user_class_name(user_class_name)
19
+ @user_class_name = user_class_name
20
+ end
21
+
22
+ def user_class_name
23
+ @user_class_name || 'User'
24
+ end
25
+
26
+ def user_class
27
+ user_class_name.constantize
28
+ end
29
+ end
30
+
31
+ def initialize(attributes = {})
32
+ attributes.each {|k,v| send("#{k}=", v) }
33
+ end
34
+
35
+ def user_class
36
+ self.class.user_class
37
+ end
38
+
39
+ def user
40
+ @user ||= user_class.authenticate(email, password)
41
+ end
42
+
43
+ def persisted?
44
+ false
45
+ end
46
+
47
+ private
48
+
49
+ def user_exists_for_credentials
50
+ errors.add(:base, "#{user_class} not found for supplied credentials") unless user.present?
51
+ end
52
+
53
+ def credentials_supplied?
54
+ email.present? && password.present?
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,59 @@
1
+ module SimplestAuth
2
+ module SessionsController
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def set_session_class_name(session_class_name)
8
+ @session_class_name = session_class_name
9
+ end
10
+
11
+ def session_class_name
12
+ @session_class_name || 'Session'
13
+ end
14
+
15
+ end
16
+
17
+ def new
18
+ @session = session_class.new
19
+ end
20
+
21
+ def create
22
+ sign_user_in_or_render
23
+ end
24
+
25
+ def destroy
26
+ sign_user_out
27
+ end
28
+
29
+ private
30
+
31
+ def sign_user_in_or_render(options = {})
32
+ message = options[:message] || 'You have signed in successfully'
33
+ redirect_url = options[:url] || root_url
34
+
35
+ @session = session_class.new(params[:session])
36
+ if @session.valid?
37
+ self.current_user = @session.user
38
+ flash[:notice] = message
39
+ redirect_to redirect_url
40
+ else
41
+ render :new
42
+ end
43
+ end
44
+
45
+ def sign_user_out(options = {})
46
+ message = options[:message] || 'You have signed out'
47
+ redirect_url = options[:url] || root_url
48
+
49
+ self.current_user = nil
50
+ flash[:notice] = message
51
+ redirect_to redirect_url
52
+ end
53
+
54
+ def session_class
55
+ self.class.session_class_name.constantize
56
+ end
57
+
58
+ end
59
+ end
@@ -2,8 +2,8 @@ module SimplestAuth
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 2
6
- TINY = 10
5
+ MINOR = 3
6
+ TINY = 1
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -0,0 +1,43 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib/', __FILE__)
3
+ $:.unshift lib unless $:.include?(lib)
4
+
5
+ require 'simplest_auth/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = %q{simplest_auth}
9
+ s.version = SimplestAuth::Version.to_s
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Tony Pitale"]
13
+ s.date = %q{2011-03-30}
14
+ s.email = %q{developers@viget.com}
15
+ s.homepage = %q{http://viget.com/extend}
16
+ s.require_paths = ["lib"]
17
+ s.rubygems_version = %q{1.3.7}
18
+ s.summary = %q{Simple implementation of authentication for Rails}
19
+ s.files = `git ls-files`.split("\n") rescue ''
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<bcrypt-ruby>, [">= 2.1.1"])
28
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
29
+ s.add_development_dependency(%q<mocha>, [">= 0"])
30
+ s.add_development_dependency(%q<activemodel>, [">= 0"])
31
+ else
32
+ s.add_dependency(%q<bcrypt-ruby>, ["~> 2.1.1"])
33
+ s.add_dependency(%q<shoulda>, [">= 0"])
34
+ s.add_dependency(%q<mocha>, [">= 0"])
35
+ s.add_dependency(%q<activemodel>, [">= 0"])
36
+ end
37
+ else
38
+ s.add_dependency(%q<bcrypt-ruby>, ["~> 2.1.1"])
39
+ s.add_dependency(%q<shoulda>, [">= 0"])
40
+ s.add_dependency(%q<mocha>, [">= 0"])
41
+ s.add_dependency(%q<activemodel>, [">= 0"])
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ require 'active_model'
7
+
8
+ require File.dirname(__FILE__) + '/../lib/simplest_auth'
9
+
10
+ # Global dummy objects used in multiple tests
11
+
12
+ class Session
13
+ include SimplestAuth::Session
14
+ end
15
+
16
+ class User
17
+ def self.authenticate(email, password)
18
+ end
19
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
2
 
3
3
  ARUser = Class.new
4
4
 
@@ -20,10 +20,11 @@ class ARUserTest < Test::Unit::TestCase
20
20
 
21
21
  should "have a default authenticate to email" do
22
22
  user = mock do |m|
23
+ m.expects(:first).returns(m)
23
24
  m.expects(:authentic?).with('password').returns(true)
24
25
  end
25
26
 
26
- ARUser.expects(:find_by_email).with('joe@schmoe.com').returns(user)
27
+ ARUser.expects(:where).with(:email => 'joe@schmoe.com').returns(user)
27
28
  assert_equal user, ARUser.authenticate('joe@schmoe.com', 'password')
28
29
  end
29
30
 
@@ -32,12 +33,13 @@ class ARUserTest < Test::Unit::TestCase
32
33
  ARUser.authenticate_by :username
33
34
  end
34
35
 
35
- should "find a user with email for authentication" do
36
+ should "find a user with username for authentication" do
36
37
  user = mock do |m|
38
+ m.expects(:first).returns(m)
37
39
  m.expects(:authentic?).with('password').returns(true)
38
40
  end
39
41
 
40
- ARUser.expects(:find_by_username).with('joeschmoe').returns(user)
42
+ ARUser.expects(:where).with(:username => 'joeschmoe').returns(user)
41
43
  assert_equal user, ARUser.authenticate('joeschmoe', 'password')
42
44
  end
43
45
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
2
 
3
3
  class User
4
4
  class RecordNotFound < StandardError; end
@@ -15,7 +15,7 @@ class ControllerTest < Test::Unit::TestCase
15
15
  stubs(:logged_in?).returns(true)
16
16
  assert authorized?
17
17
  end
18
-
18
+
19
19
  should "redirect to a new session if access is denied" do
20
20
  stubs(:store_location)
21
21
  expects(:redirect_to).with("")
@@ -23,7 +23,7 @@ class ControllerTest < Test::Unit::TestCase
23
23
  stubs(:flash).returns({})
24
24
  access_denied
25
25
  end
26
-
26
+
27
27
  should "set the error flash if access is denied" do
28
28
  stubs(:store_location)
29
29
  stubs(:redirect_to).with("")
@@ -34,7 +34,7 @@ class ControllerTest < Test::Unit::TestCase
34
34
  access_denied
35
35
  assert_equal "blah", flash_stub[:error]
36
36
  end
37
-
37
+
38
38
  should "store the location of the desired page before redirecting" do
39
39
  expects(:store_location)
40
40
  stubs(:redirect_to)
@@ -42,25 +42,25 @@ class ControllerTest < Test::Unit::TestCase
42
42
  stubs(:flash).returns({})
43
43
  access_denied
44
44
  end
45
-
45
+
46
46
  should "store the location of the current request to session" do
47
47
  expects(:session).returns({})
48
48
  stubs(:request).returns(stub(:request_uri => ''))
49
49
  store_location
50
50
  end
51
-
51
+
52
52
  should "redirect back to the stored uri" do
53
53
  stubs(:session).returns({:return_to => 'somewhere'})
54
54
  expects(:redirect_to).with('somewhere')
55
55
  redirect_back_or_default('')
56
56
  end
57
-
57
+
58
58
  should "redirect to a default location if the session url is nil" do
59
59
  stubs(:session).returns({:return_to => nil})
60
60
  expects(:redirect_to).with('default')
61
61
  redirect_back_or_default('default')
62
62
  end
63
-
63
+
64
64
  should "clear the session stored url after redirect" do
65
65
  session = {:return_to => 'somewhere'}
66
66
  stubs(:session).returns(session)
@@ -68,23 +68,23 @@ class ControllerTest < Test::Unit::TestCase
68
68
  redirect_back_or_default('')
69
69
  assert_nil session[:return_to]
70
70
  end
71
-
71
+
72
72
  should "know if login is required from authorized method" do
73
73
  stubs(:authorized?).returns(true)
74
74
  assert login_required
75
75
  end
76
-
76
+
77
77
  should "consider access denied if login is required and not authorized" do
78
78
  stubs(:authorized?).returns(false)
79
79
  expects(:access_denied)
80
80
  login_required
81
81
  end
82
-
82
+
83
83
  should "know if a user is logged in" do
84
84
  stubs(:current_user_id).returns(1)
85
85
  assert logged_in?
86
86
  end
87
-
87
+
88
88
  should "know if a user is not logged in" do
89
89
  stubs(:current_user_id).returns(nil)
90
90
  assert_equal false, logged_in?
@@ -101,53 +101,62 @@ class ControllerTest < Test::Unit::TestCase
101
101
  end
102
102
 
103
103
  should "#find the current user when #get fails" do
104
- user_stub = stub()
105
- user_stub.stubs(:find).with(1).returns("user")
106
-
107
- stubs(:current_user_id).returns(1)
108
- stubs(:user_class).returns(user_stub)
109
-
104
+ user = mock do |m|
105
+ m.expects(:where).with(:id => '1').returns(m)
106
+ m.expects(:first).returns("user")
107
+ end
108
+ stubs(:current_user_id).returns('1')
109
+ stubs(:user_class).returns(user)
110
+
110
111
  assert_equal "user", current_user
111
112
  end
112
-
113
+
113
114
  should "clear session and return nil for the current user if it doesn't exist" do
114
- User.stubs(:find).with('1').returns(nil)
115
+ user = mock do |m|
116
+ m.expects(:where).with(:id => '1').returns(m)
117
+ m.expects(:first).returns(nil)
118
+ end
115
119
  stubs(:current_user_id).with().returns('1')
120
+ stubs(:user_class).returns(user)
116
121
  stubs(:clear_session)
117
122
 
118
123
  assert_nil current_user
119
124
  end
120
-
125
+
121
126
  should "be able to clear its session variables" do
122
127
  expects(:session).with().returns(mock() {|m| m.expects(:[]=).with(:user_id, nil) })
123
128
  clear_session
124
129
  end
125
-
130
+
126
131
  should "allow assigning to the current user" do
127
132
  stubs(:session).returns({})
128
133
  user = mock(:id => 1)
129
134
  self.current_user = user
130
135
  end
131
-
136
+
132
137
  should "save the current user to avoid lookup" do
133
138
  stubs(:session).returns({})
134
139
  user = stub(:id => 1)
135
140
  self.current_user = user
136
141
  assert_equal user, current_user
137
142
  end
138
-
143
+
139
144
  should "know the current user id from session" do
140
145
  stubs(:session).returns({:user_id => 1})
141
146
  assert_equal 1, current_user_id
142
147
  end
143
-
148
+
144
149
  should "have a default login error message" do
145
150
  assert_equal "Login or Registration Required", login_message
146
151
  end
147
152
 
148
153
  should "return the current_user, repeatedly" do
149
- User.expects(:find).with(1).returns("user")
150
- expects(:current_user_id).returns(1)
154
+ user = mock do |m|
155
+ m.expects(:where).with(:id => 1).returns(m)
156
+ m.expects(:first).returns("user")
157
+ end
158
+ stubs(:user_class).returns(user)
159
+ stubs(:current_user_id).returns(1)
151
160
 
152
161
  assert_equal "user", current_user
153
162
  end
@@ -157,5 +166,5 @@ class ControllerTest < Test::Unit::TestCase
157
166
  assert_equal :user_id, session_key
158
167
  end
159
168
  end
160
-
169
+
161
170
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
2
 
3
3
  DMUser = Class.new
4
4
 
@@ -27,7 +27,7 @@ class DMUserTest < Test::Unit::TestCase
27
27
  DMUser.expects(:first).with(:email => 'joe@schmoe.com').returns(user)
28
28
  assert_equal user, DMUser.authenticate('joe@schmoe.com', 'password')
29
29
  end
30
-
30
+
31
31
  context "with authenticate_by set to username" do
32
32
  setup do
33
33
  DMUser.authenticate_by :username
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
2
 
3
3
  class User; end
4
4
 
@@ -42,7 +42,7 @@ class UserTest < Test::Unit::TestCase
42
42
  password_stub = stub
43
43
  password_stub.stubs(:==).with('password').returns(false)
44
44
  Password.stubs(:new).with('abcdefg').returns(password_stub)
45
-
45
+
46
46
  assert_equal false, @user.authentic?('password')
47
47
  end
48
48
 
@@ -50,7 +50,7 @@ class UserTest < Test::Unit::TestCase
50
50
  password_stub = mock
51
51
  password_stub.expects(:==).with('password').returns(true)
52
52
  Password.stubs(:new).with('abcdefg').returns(password_stub)
53
-
53
+
54
54
  @user.authentic?('password')
55
55
  end
56
56
 
@@ -58,7 +58,7 @@ class UserTest < Test::Unit::TestCase
58
58
  password_stub = stub
59
59
  password_stub.stubs(:==).with('password').returns(true)
60
60
  Password.expects(:new).with('abcdefg').returns(password_stub)
61
-
61
+
62
62
  @user.authentic?('password')
63
63
  end
64
64
 
@@ -0,0 +1,107 @@
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
+
3
+ class AdminSession
4
+ include SimplestAuth::Session
5
+ end
6
+
7
+ class Admin
8
+ end
9
+
10
+ class SimplestAuth::SessionTest < Test::Unit::TestCase
11
+
12
+ context "The Session class" do
13
+ should "have a default value for the user class name" do
14
+ assert_equal 'User', ::Session.user_class_name
15
+ end
16
+
17
+ should "know the user class name when it's set" do
18
+ OtherSession = Class.new do
19
+ include SimplestAuth::Session
20
+ set_user_class_name 'Admin'
21
+ end
22
+ assert_equal 'Admin', OtherSession.user_class_name
23
+ end
24
+
25
+ should "know the user class" do
26
+ AdminSession.stubs(:user_class_name).with().returns('Admin')
27
+ assert_equal Admin, AdminSession.user_class
28
+ end
29
+ end
30
+
31
+ context "An instance of the Session class" do
32
+ should "not have an email by default" do
33
+ assert_nil Session.new.email
34
+ end
35
+
36
+ should "not have a password by default" do
37
+ assert_nil Session.new.password
38
+ end
39
+
40
+ should "know the email address when set" do
41
+ session = Session.new(:email => 'user@host.com')
42
+ assert_equal 'user@host.com', session.email
43
+ end
44
+
45
+ should "know the password when set" do
46
+ session = Session.new(:password => 'password')
47
+ assert_equal 'password', session.password
48
+ end
49
+
50
+ should "require an email to be present" do
51
+ session = Session.new
52
+ session.valid?
53
+
54
+ assert_equal ["can't be blank"], session.errors[:email]
55
+ end
56
+
57
+ should "require the password to be present" do
58
+ session = Session.new
59
+ session.valid?
60
+
61
+ assert_equal ["can't be blank"], session.errors[:password]
62
+ end
63
+
64
+ should "know the user class" do
65
+ session = Session.new
66
+ assert_equal User, session.user_class
67
+ end
68
+
69
+ should "know that there's no user" do
70
+ User.stubs(:authenticate).with('user@host.com', 'password').returns(nil)
71
+
72
+ session = Session.new(:email => 'user@host.com', :password => 'password')
73
+ assert_nil session.user
74
+ end
75
+
76
+ should "know that there's a user" do
77
+ User.stubs(:authenticate).with('user@host.com', 'password').returns('user')
78
+
79
+ session = Session.new(:email => 'user@host.com', :password => 'password')
80
+ assert_equal 'user', session.user
81
+ end
82
+
83
+ should "not set errors on base if there is no email or password" do
84
+ session = Session.new(:email => ' ', :password => ' ')
85
+ session.valid?
86
+
87
+ assert_equal [], session.errors[:base]
88
+ end
89
+
90
+ should "set an error when there is no user" do
91
+ session = Session.new(:email => 'user@host.com', :password => 'password')
92
+ User.stubs(:authenticate).with('user@host.com', 'password').returns(nil)
93
+
94
+ session.valid?
95
+ assert_equal ["User not found for supplied credentials"], session.errors[:base]
96
+ end
97
+
98
+ should "not set an error when there is a user" do
99
+ session = Session.new
100
+ session.stubs(:user).with().returns(User.new)
101
+
102
+ session.valid?
103
+ assert_equal [], session.errors[:base]
104
+ end
105
+ end
106
+
107
+ end
@@ -0,0 +1,201 @@
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
+
3
+ module DummyController
4
+ def params
5
+ {}
6
+ end
7
+
8
+ def current_user=(user)
9
+ end
10
+
11
+ def flash
12
+ Hash.new
13
+ end
14
+
15
+ def redirect_to(path)
16
+ end
17
+
18
+ def render(action)
19
+ end
20
+
21
+ def root_url
22
+ '/'
23
+ end
24
+ end
25
+
26
+ class SessionsController
27
+ include SimplestAuth::SessionsController
28
+ include DummyController
29
+ end
30
+
31
+ class CustomSession
32
+ end
33
+
34
+ class CustomSessionsController
35
+ include SimplestAuth::SessionsController
36
+ include DummyController
37
+
38
+ set_session_class_name 'CustomSession'
39
+
40
+ def create
41
+ sign_user_in_or_render(:message => 'Hi', :url => '/admin')
42
+ end
43
+
44
+ def destroy
45
+ sign_user_out(:message => 'Bye', :url => '/survey')
46
+ end
47
+ end
48
+
49
+ class SimplestAuth::SessionsControllerTest < Test::Unit::TestCase
50
+
51
+ context "An instance of the SessionsController" do
52
+ should "know the default session class" do
53
+ assert_equal Session, SessionsController.new.send(:session_class)
54
+ end
55
+
56
+ should "be able to override the session class that is used" do
57
+ controller = CustomSessionsController.new
58
+ assert_equal CustomSession, controller.send(:session_class)
59
+ end
60
+ end
61
+
62
+ context "Requests" do
63
+ setup do
64
+ @controller = ::SessionsController.new
65
+ @session = ::Session.new
66
+ end
67
+
68
+ context "a GET to :new" do
69
+ should "assign to @session" do
70
+ ::Session.stubs(:new).with().returns(@session)
71
+
72
+ @controller.new
73
+
74
+ assert_equal @session, @controller.instance_variable_get(:@session)
75
+ end
76
+ end
77
+
78
+ context "a POST to :create" do
79
+ should "assign to @session" do
80
+ ::Session.stubs(:new).with('key' => 'value').returns(@session)
81
+ @controller.stubs(:params).with().returns(:session => {'key' => 'value'})
82
+
83
+ @controller.create
84
+
85
+ assert_equal @session, @controller.instance_variable_get(:@session)
86
+ end
87
+
88
+ context "when successful" do
89
+ setup do
90
+ @session.stubs(:valid?).with().returns(true)
91
+ ::Session.stubs(:new).returns(@session)
92
+ end
93
+
94
+ should "save the user in session when successful" do
95
+ user = User.new
96
+ @session.stubs(:user).with().returns(user)
97
+
98
+ @controller.expects(:current_user=).with(user)
99
+
100
+ @controller.create
101
+ end
102
+
103
+ should "set the flash when successful" do
104
+ flash = mock()
105
+ flash.expects(:[]=).with(:notice, 'You have signed in successfully')
106
+ @controller.stubs(:flash).with().returns(flash)
107
+
108
+ @controller.create
109
+ end
110
+
111
+ should "redirect when successful" do
112
+ @controller.expects(:redirect_to).with('/')
113
+ @controller.create
114
+ end
115
+ end
116
+
117
+ context "when unsuccessful" do
118
+ setup do
119
+ @session.stubs(:valid?).with().returns(false)
120
+ ::Session.stubs(:new).returns(@session)
121
+ end
122
+
123
+ should "render when unsuccessful" do
124
+ @controller.expects(:render).with(:new)
125
+ @controller.create
126
+ end
127
+
128
+ should "not redirect when unsuccessful" do
129
+ @controller.expects(:redirect_to).never
130
+ @controller.create
131
+ end
132
+ end
133
+ end
134
+
135
+ context "a DELETE to :destroy" do
136
+
137
+ should "remove the user from session" do
138
+ @controller.expects(:current_user=).with(nil)
139
+ @controller.destroy
140
+ end
141
+
142
+ should "set the flash" do
143
+ flash = mock() {|f| f.expects(:[]=).with(:notice, 'You have signed out') }
144
+ @controller.stubs(:flash).with().returns(flash)
145
+
146
+ @controller.destroy
147
+ end
148
+
149
+ should "redirect" do
150
+ @controller.expects(:redirect_to).with('/')
151
+ @controller.destroy
152
+ end
153
+
154
+ end
155
+
156
+ context "with a custom controller" do
157
+ setup do
158
+ @session = ::CustomSession.new
159
+ @session.stubs(:user).returns(stub())
160
+ @session.stubs(:valid?).returns(true)
161
+
162
+ ::CustomSession.stubs(:new).returns(@session)
163
+
164
+ @controller = CustomSessionsController.new
165
+ end
166
+
167
+ context "a POST to :create" do
168
+ should "set the appropriate flash message" do
169
+ flash = mock() {|f| f.expects(:[]=).with(:notice, 'Hi') }
170
+
171
+ @controller.stubs(:flash).with().returns(flash)
172
+
173
+ @controller.create
174
+ end
175
+
176
+ should "redirect to the specified URL" do
177
+ @controller.expects(:redirect_to).with('/admin')
178
+
179
+ @controller.create
180
+ end
181
+ end
182
+
183
+ context "a DELETE to :destroy" do
184
+ should "set the appropriate flash message" do
185
+ flash = mock() {|f| f.expects(:[]=).with(:notice, 'Bye') }
186
+ @controller.stubs(:flash).with().returns(flash)
187
+
188
+ @controller.destroy
189
+ end
190
+
191
+ should "redirect to the specified URL" do
192
+ @controller.expects(:redirect_to).with('/survey')
193
+
194
+ @controller.destroy
195
+ end
196
+ end
197
+ end
198
+
199
+ end
200
+
201
+ end
metadata CHANGED
@@ -1,94 +1,115 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: simplest_auth
3
- version: !ruby/object:Gem::Version
4
- hash: 3
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 2
9
- - 10
10
- version: 0.2.10
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Tony Pitale
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-07-15 00:00:00 -04:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2011-03-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: bcrypt-ruby
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70100279761160 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 9
30
- segments:
31
- - 2
32
- - 1
33
- - 1
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
34
21
  version: 2.1.1
35
22
  type: :runtime
36
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: *70100279761160
25
+ - !ruby/object:Gem::Dependency
26
+ name: shoulda
27
+ requirement: &70100279760320 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70100279760320
36
+ - !ruby/object:Gem::Dependency
37
+ name: mocha
38
+ requirement: &70100279759400 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70100279759400
47
+ - !ruby/object:Gem::Dependency
48
+ name: activemodel
49
+ requirement: &70100279757840 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70100279757840
37
58
  description:
38
- email: tony.pitale@viget.com
59
+ email: developers@viget.com
39
60
  executables: []
40
-
41
61
  extensions: []
42
-
43
62
  extra_rdoc_files: []
44
-
45
- files:
63
+ files:
64
+ - .gitignore
65
+ - .rvmrc
66
+ - Gemfile
67
+ - MIT-LICENSE
46
68
  - README.textile
47
69
  - Rakefile
70
+ - lib/simplest_auth.rb
48
71
  - lib/simplest_auth/controller.rb
49
72
  - lib/simplest_auth/model.rb
73
+ - lib/simplest_auth/session.rb
74
+ - lib/simplest_auth/sessions_controller.rb
50
75
  - lib/simplest_auth/version.rb
51
- - lib/simplest_auth.rb
76
+ - simplest_auth.gemspec
77
+ - test/test_helper.rb
52
78
  - test/unit/simplest_auth/ar_model_test.rb
53
79
  - test/unit/simplest_auth/controller_test.rb
54
80
  - test/unit/simplest_auth/dm_model_test.rb
55
81
  - test/unit/simplest_auth/model_test.rb
56
- has_rdoc: true
82
+ - test/unit/simplest_auth/session_test.rb
83
+ - test/unit/simplest_auth/sessions_controller_test.rb
57
84
  homepage: http://viget.com/extend
58
85
  licenses: []
59
-
60
86
  post_install_message:
61
87
  rdoc_options: []
62
-
63
- require_paths:
88
+ require_paths:
64
89
  - lib
65
- required_ruby_version: !ruby/object:Gem::Requirement
90
+ required_ruby_version: !ruby/object:Gem::Requirement
66
91
  none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- hash: 3
71
- segments:
72
- - 0
73
- version: "0"
74
- required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
97
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- hash: 3
80
- segments:
81
- - 0
82
- version: "0"
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
83
102
  requirements: []
84
-
85
103
  rubyforge_project:
86
- rubygems_version: 1.3.7
104
+ rubygems_version: 1.8.10
87
105
  signing_key:
88
106
  specification_version: 3
89
107
  summary: Simple implementation of authentication for Rails
90
- test_files:
108
+ test_files:
109
+ - test/test_helper.rb
91
110
  - test/unit/simplest_auth/ar_model_test.rb
92
111
  - test/unit/simplest_auth/controller_test.rb
93
112
  - test/unit/simplest_auth/dm_model_test.rb
94
113
  - test/unit/simplest_auth/model_test.rb
114
+ - test/unit/simplest_auth/session_test.rb
115
+ - test/unit/simplest_auth/sessions_controller_test.rb