simple_auth 0.1.8 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/.gitignore +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +13 -0
  4. data/Gemfile.lock +109 -0
  5. data/README.markdown +13 -29
  6. data/Rakefile +21 -33
  7. data/lib/simple_auth/action_controller.rb +42 -38
  8. data/lib/simple_auth/active_record.rb +59 -47
  9. data/lib/simple_auth/config.rb +8 -3
  10. data/lib/simple_auth/generator.rb +11 -0
  11. data/lib/simple_auth/railtie.rb +24 -0
  12. data/lib/simple_auth/session.rb +12 -0
  13. data/lib/simple_auth/version.rb +3 -3
  14. data/lib/simple_auth.rb +1 -7
  15. data/simple_auth.gemspec +35 -21
  16. data/spec/controllers/redirect_logged_user_spec.rb +87 -0
  17. data/spec/controllers/require_logged_user_spec.rb +105 -0
  18. data/spec/schema.rb +4 -0
  19. data/spec/simple_auth/active_record_spec.rb +32 -15
  20. data/spec/simple_auth/config_spec.rb +7 -1
  21. data/spec/simple_auth/helper_spec.rb +12 -16
  22. data/spec/simple_auth/session_spec.rb +9 -7
  23. data/spec/spec_helper.rb +9 -34
  24. data/spec/support/app/controllers/application_controller.rb +2 -0
  25. data/spec/support/app/models/account.rb +2 -0
  26. data/spec/{resources → support/app/models}/user.rb +1 -1
  27. data/spec/{resources → support/app}/views/dashboard/index.erb +0 -0
  28. data/spec/{resources → support/app}/views/session/new.erb +0 -0
  29. data/spec/support/config/boot.rb +14 -0
  30. data/spec/support/config/database.yml +3 -0
  31. data/spec/support/config/routes.rb +4 -0
  32. data/{generators/simple_auth/templates/simple_auth.rb → templates/initializer.rb} +5 -4
  33. metadata +47 -21
  34. data/generators/simple_auth/USAGE +0 -5
  35. data/generators/simple_auth/simple_auth_generator.rb +0 -7
  36. data/init.rb +0 -1
  37. data/spec/resources/controllers.rb +0 -35
  38. data/spec/simple_auth/action_controller_spec.rb +0 -117
data/simple_auth.gemspec CHANGED
@@ -5,71 +5,85 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_auth}
8
- s.version = "0.1.8"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nando Vieira"]
12
- s.date = %q{2010-05-09}
13
- s.description = %q{When Authlogic & Devise are just too much.
14
- }
12
+ s.date = %q{2010-10-12}
13
+ s.description = %q{When Authlogic & Devise are just too much.}
15
14
  s.email = %q{fnando.vieira@gmail.com}
16
15
  s.extra_rdoc_files = [
17
16
  "README.markdown"
18
17
  ]
19
18
  s.files = [
20
- "README.markdown",
19
+ ".gitignore",
20
+ ".rspec",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "README.markdown",
21
24
  "Rakefile",
22
25
  "config/locales/en.yml",
23
26
  "config/locales/pt.yml",
24
- "generators/simple_auth/USAGE",
25
- "generators/simple_auth/simple_auth_generator.rb",
26
- "generators/simple_auth/templates/simple_auth.rb",
27
- "init.rb",
28
27
  "lib/simple_auth.rb",
29
28
  "lib/simple_auth/action_controller.rb",
30
29
  "lib/simple_auth/active_record.rb",
31
30
  "lib/simple_auth/config.rb",
31
+ "lib/simple_auth/generator.rb",
32
32
  "lib/simple_auth/helper.rb",
33
+ "lib/simple_auth/railtie.rb",
33
34
  "lib/simple_auth/session.rb",
34
35
  "lib/simple_auth/version.rb",
35
36
  "simple_auth.gemspec",
36
- "spec/resources/controllers.rb",
37
- "spec/resources/user.rb",
38
- "spec/resources/views/dashboard/index.erb",
39
- "spec/resources/views/session/new.erb",
37
+ "spec/controllers/redirect_logged_user_spec.rb",
38
+ "spec/controllers/require_logged_user_spec.rb",
40
39
  "spec/schema.rb",
41
- "spec/simple_auth/action_controller_spec.rb",
42
40
  "spec/simple_auth/active_record_spec.rb",
43
41
  "spec/simple_auth/config_spec.rb",
44
42
  "spec/simple_auth/helper_spec.rb",
45
43
  "spec/simple_auth/session_spec.rb",
46
- "spec/spec_helper.rb"
44
+ "spec/spec_helper.rb",
45
+ "spec/support/app/controllers/application_controller.rb",
46
+ "spec/support/app/models/account.rb",
47
+ "spec/support/app/models/user.rb",
48
+ "spec/support/app/views/dashboard/index.erb",
49
+ "spec/support/app/views/session/new.erb",
50
+ "spec/support/config/boot.rb",
51
+ "spec/support/config/database.yml",
52
+ "spec/support/config/routes.rb",
53
+ "templates/initializer.rb"
47
54
  ]
48
55
  s.homepage = %q{http://github.com/fnando/simple_auth}
49
56
  s.rdoc_options = ["--charset=UTF-8"]
50
57
  s.require_paths = ["lib"]
51
- s.rubygems_version = %q{1.3.6}
58
+ s.rubygems_version = %q{1.3.7}
52
59
  s.summary = %q{A simple authentication system for Rails apps}
53
60
  s.test_files = [
54
- "spec/resources/controllers.rb",
55
- "spec/resources/user.rb",
61
+ "spec/controllers/redirect_logged_user_spec.rb",
62
+ "spec/controllers/require_logged_user_spec.rb",
56
63
  "spec/schema.rb",
57
- "spec/simple_auth/action_controller_spec.rb",
58
64
  "spec/simple_auth/active_record_spec.rb",
59
65
  "spec/simple_auth/config_spec.rb",
60
66
  "spec/simple_auth/helper_spec.rb",
61
67
  "spec/simple_auth/session_spec.rb",
62
- "spec/spec_helper.rb"
68
+ "spec/spec_helper.rb",
69
+ "spec/support/app/controllers/application_controller.rb",
70
+ "spec/support/app/models/account.rb",
71
+ "spec/support/app/models/user.rb",
72
+ "spec/support/config/boot.rb",
73
+ "spec/support/config/routes.rb"
63
74
  ]
64
75
 
65
76
  if s.respond_to? :specification_version then
66
77
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
67
78
  s.specification_version = 3
68
79
 
69
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
80
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
81
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
70
82
  else
83
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
71
84
  end
72
85
  else
86
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
73
87
  end
74
88
  end
75
89
 
@@ -0,0 +1,87 @@
1
+ require "spec_helper"
2
+
3
+ describe ApplicationController do
4
+ let(:user) {
5
+ User.create(
6
+ :login => "johndoe",
7
+ :email => "john@doe.com",
8
+ :password => "test",
9
+ :password_confirmation => "test"
10
+ )
11
+ }
12
+
13
+ context "redirecting logged users" do
14
+ context "using hash" do
15
+ controller do
16
+ redirect_logged_user :to => { :controller => "dashboard" }
17
+
18
+ def index
19
+ render :text => "Rendered"
20
+ end
21
+ end
22
+
23
+ it "should redirect logged users" do
24
+ session[:record_id] = user.id
25
+ get :index
26
+
27
+ response.code.should match(/302/)
28
+ response.should redirect_to("/dashboard")
29
+ end
30
+ end
31
+
32
+ context "using block" do
33
+ controller do
34
+ redirect_logged_user :to => proc { dashboard_path }
35
+
36
+ def index
37
+ render :text => "Rendered"
38
+ end
39
+ end
40
+
41
+ it "should redirect logged users" do
42
+ session[:record_id] = user.id
43
+ get :index
44
+
45
+ response.code.should match(/302/)
46
+ response.should redirect_to("/dashboard")
47
+ end
48
+ end
49
+
50
+ context "using configuration" do
51
+ controller do
52
+ redirect_logged_user
53
+
54
+ def index
55
+ render :text => "Rendered"
56
+ end
57
+ end
58
+
59
+ it "should redirect logged users" do
60
+ SimpleAuth::Config.logged_url = proc { dashboard_path }
61
+ session[:record_id] = user.id
62
+ get :index
63
+
64
+ response.code.should match(/302/)
65
+ response.should redirect_to("/dashboard")
66
+ end
67
+ end
68
+
69
+ context "when unlogged" do
70
+ controller do
71
+ redirect_logged_user :to => { :controller => "dashboard" }
72
+
73
+ def index
74
+ render :text => "Rendered"
75
+ end
76
+ end
77
+
78
+ it "should render page" do
79
+ session[:record_id] = nil
80
+ get :index
81
+
82
+ response.code.should match(/200/)
83
+ response.body.should == "Rendered"
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,105 @@
1
+ require "spec_helper"
2
+
3
+ describe ApplicationController do
4
+ let(:user) {
5
+ User.create(
6
+ :login => "johndoe",
7
+ :email => "john@doe.com",
8
+ :password => "test",
9
+ :password_confirmation => "test"
10
+ )
11
+ }
12
+
13
+ context "redirecting to requested page" do
14
+ controller do
15
+ require_logged_user :to => "/login"
16
+
17
+ def index
18
+ render :text => "Rendered"
19
+ end
20
+ end
21
+
22
+ it "should set return to" do
23
+ get :index
24
+ session[:return_to].should == "/stub_resources"
25
+ end
26
+
27
+ it "should set warning message" do
28
+ get :index
29
+ flash[:alert].should == "You need to be logged"
30
+ end
31
+
32
+ it "should redirect when user is not authorized" do
33
+ @controller.should_receive(:logged_in?).and_return(true)
34
+ @controller.should_receive(:authorized?).and_return(false)
35
+
36
+ get :index
37
+ response.should redirect_to("/login")
38
+ end
39
+
40
+ context "using hash" do
41
+ controller do
42
+ require_logged_user :to => {:controller => "session", :action => "new"}
43
+
44
+ def index
45
+ render :text => "Rendered"
46
+ end
47
+ end
48
+
49
+ it "should be redirected" do
50
+ get :index
51
+ response.should redirect_to("/login")
52
+ end
53
+ end
54
+
55
+ context "using block" do
56
+ controller do
57
+ require_logged_user :to => proc { login_path }
58
+
59
+ def index
60
+ render :text => "Rendered"
61
+ end
62
+ end
63
+
64
+ it "should be redirected" do
65
+ get :index
66
+ response.should redirect_to("/login")
67
+ end
68
+ end
69
+
70
+ context "using configuration" do
71
+ controller do
72
+ require_logged_user
73
+
74
+ def index
75
+ render :text => "Rendered"
76
+ end
77
+ end
78
+
79
+ it "should be redirected" do
80
+ SimpleAuth::Config.login_url = "/login"
81
+ get :index
82
+ response.should redirect_to("/login")
83
+ end
84
+ end
85
+ end
86
+
87
+ context "when logged" do
88
+ controller do
89
+ require_logged_user
90
+
91
+ def index
92
+ render :text => "Rendered"
93
+ end
94
+ end
95
+
96
+ before do
97
+ session[:record_id] = user.id
98
+ get :index
99
+ end
100
+
101
+ it "should render page" do
102
+ response.body.should == "Rendered"
103
+ end
104
+ end
105
+ end
data/spec/schema.rb CHANGED
@@ -2,4 +2,8 @@ ActiveRecord::Schema.define(:version => 0) do
2
2
  create_table :users do |t|
3
3
  t.string :email, :login, :password_hash, :password_salt, :username
4
4
  end
5
+
6
+ create_table :accounts do |t|
7
+ t.string :email, :login, :password_hash, :password_salt, :username
8
+ end
5
9
  end
@@ -1,31 +1,49 @@
1
- require File.dirname(__FILE__) + "/../spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe SimpleAuth::ActiveRecord do
4
- context "new record" do
5
- subject { User.new }
4
+ subject { User.new }
5
+
6
+ context "configuration" do
7
+ it "should set credentials" do
8
+ User.authentication do |config|
9
+ config.credentials = ["uid"]
10
+ end
11
+
12
+ SimpleAuth::Config.credentials.should == ["uid"]
13
+ end
6
14
 
15
+ it "should automatically set model" do
16
+ User.authentication do |config|
17
+ config.model = nil
18
+ end
19
+
20
+ SimpleAuth::Config.model.should == :user
21
+ end
22
+ end
23
+
24
+ context "new record" do
7
25
  before do
8
26
  subject.should_not be_valid
9
27
  end
10
28
 
11
29
  it "should require password" do
12
- [subject.errors[:password]].flatten.should have(1).item
30
+ subject.errors[:password].should_not be_empty
13
31
  end
14
32
 
15
33
  it "should require password to be at least 4-chars long" do
16
34
  subject.password = "123"
17
35
  subject.should_not be_valid
18
- [subject.errors[:password]].flatten.should have(1).item
36
+ subject.errors[:password].should_not be_empty
19
37
  end
20
38
 
21
39
  it "should require password confirmation not to be empty" do
22
40
  subject.password_confirmation = ""
23
- [subject.errors[:password_confirmation]].flatten.should have(1).item
41
+ subject.errors[:password_confirmation].should_not be_empty
24
42
  end
25
43
 
26
44
  it "should require password confirmation not to be nil" do
27
45
  subject.password_confirmation = nil
28
- [subject.errors[:password_confirmation]].flatten.should have(1).item
46
+ subject.errors[:password_confirmation].should_not be_empty
29
47
  end
30
48
 
31
49
  it "should unset password after saving" do
@@ -53,20 +71,19 @@ describe SimpleAuth::ActiveRecord do
53
71
  end
54
72
 
55
73
  context "existing record" do
56
- subject {
57
- User.new(
74
+ before do
75
+ User.delete_all
76
+ User.create(
58
77
  :email => "john@doe.com",
59
78
  :login => "johndoe",
60
79
  :password => "test",
61
80
  :password_confirmation => "test",
62
81
  :username => "john"
63
82
  )
64
- }
65
-
66
- before do
67
- subject.save!
68
83
  end
69
84
 
85
+ subject { User.first }
86
+
70
87
  it "should not require password when it hasn't changed" do
71
88
  subject.login = "john"
72
89
  subject.should be_valid
@@ -75,13 +92,13 @@ describe SimpleAuth::ActiveRecord do
75
92
  it "should require password confirmation when it has changed" do
76
93
  subject.password = "newpass"
77
94
  subject.should_not be_valid
78
- [subject.errors[:password_confirmation]].flatten.should have(1).item
95
+ subject.errors[:password_confirmation].should_not be_empty
79
96
  end
80
97
 
81
98
  it "should require password when it has changed to blank" do
82
99
  subject.password = nil
83
100
  subject.should_not be_valid
84
- [subject.errors[:password]].flatten.should have(1).item
101
+ subject.errors[:password].should_not be_empty
85
102
  end
86
103
 
87
104
  it "should authenticate using email" do
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + "/../spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe SimpleAuth::Config do
4
4
  it "should yield SimpleAuth::Config class" do
@@ -7,6 +7,12 @@ describe SimpleAuth::Config do
7
7
  end
8
8
  end
9
9
 
10
+ context "injecting behavior" do
11
+ it "should not respond to helper methods" do
12
+ Account.should_not respond_to(:authenticate)
13
+ end
14
+ end
15
+
10
16
  it "should use [:email, :login] as credential attributes" do
11
17
  SimpleAuth::Config.credentials.should == [:email, :login]
12
18
  end
@@ -1,28 +1,24 @@
1
- require File.dirname(__FILE__) + "/../spec_helper"
1
+ require "spec_helper"
2
2
 
3
- describe SimpleAuth::Helper, :type => :helper do
4
- if ENV["TARGET"] == "rails3"
5
- attr_accessor :helper
6
-
7
- before do
8
- @helper = Object.new
9
- @helper.class_eval { attr_accessor :output_buffer }
10
- @helper.extend(SimpleAuth::Helper)
11
- @helper.extend(ActionView::Helpers::CaptureHelper)
12
- end
3
+ describe SimpleAuth::Helper do
4
+ before do
5
+ @helper = Object.new
6
+ @helper.class_eval { attr_accessor :output_buffer }
7
+ @helper.extend(SimpleAuth::Helper)
8
+ @helper.extend(ActionView::Helpers::CaptureHelper)
13
9
  end
14
-
10
+
15
11
  it "should include module" do
16
12
  ApplicationController.included_modules.include?(SimpleAuth::Helper)
17
13
  end
18
14
 
19
15
  it "should render block when user is logged" do
20
- helper.should_receive(:logged_in?).and_return(true)
21
- helper.when_logged { "logged" }.should == "logged"
16
+ @helper.should_receive(:logged_in?).and_return(true)
17
+ @helper.when_logged { "logged" }.should == "logged"
22
18
  end
23
19
 
24
20
  it "should not render block when user is unlogged" do
25
- helper.should_receive(:logged_in?).and_return(false)
26
- helper.when_logged { "logged" }.should be_nil
21
+ @helper.should_receive(:logged_in?).and_return(false)
22
+ @helper.when_logged { "logged" }.should be_nil
27
23
  end
28
24
  end
@@ -1,7 +1,9 @@
1
- require File.dirname(__FILE__) + "/../spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe SimpleAuth::Session do
4
4
  before do
5
+ User.delete_all
6
+
5
7
  @user = User.create!(
6
8
  :login => "johndoe",
7
9
  :email => "john@doe.com",
@@ -10,14 +12,14 @@ describe SimpleAuth::Session do
10
12
  )
11
13
 
12
14
  @session = Hash.new
13
- @controller = SampleController.new
15
+ @controller = ActionController::Base.new
14
16
  @controller.stub!(:session).and_return(@session)
15
17
 
16
18
  SimpleAuth::Config.controller = @controller
17
19
  @user_session = SimpleAuth::Session.new(:credential => "johndoe", :password => "test")
18
20
  end
19
21
 
20
- context "valid credentials" do
22
+ context "with valid credentials" do
21
23
  before do
22
24
  @user_session.save!
23
25
  end
@@ -68,7 +70,7 @@ describe SimpleAuth::Session do
68
70
  end
69
71
  end
70
72
 
71
- context "invalid credentials" do
73
+ context "with invalid credentials" do
72
74
  before do
73
75
  @user_session.credential = "invalid"
74
76
  @user_session.save
@@ -130,15 +132,15 @@ describe SimpleAuth::Session do
130
132
  end
131
133
 
132
134
  it "should raise error with save!" do
133
- doing { @user_session.save! }.should raise_error(SimpleAuth::NotAuthorized)
135
+ expect { @user_session.save! }.to raise_error(SimpleAuth::NotAuthorized)
134
136
  end
135
137
 
136
138
  it "should raise error with create!" do
137
- doing { SimpleAuth::Session.create!({}) }.should raise_error(SimpleAuth::NotAuthorized)
139
+ expect { SimpleAuth::Session.create!({}) }.to raise_error(SimpleAuth::NotAuthorized)
138
140
  end
139
141
  end
140
142
 
141
- context "destroying valid session" do
143
+ context "when destroying session" do
142
144
  before do
143
145
  @user_session.save!
144
146
  @user_session.destroy
data/spec/spec_helper.rb CHANGED
@@ -1,41 +1,16 @@
1
- # Load application RSpec helper
2
- begin
3
- require File.dirname(__FILE__) + "/../../../../spec/spec_helper"
4
- rescue LoadError
5
- puts "Your application hasn't been bootstraped with RSpec.\nI'll do it on my own!\n\n"
6
- system "cd '#{File.dirname(__FILE__) + "/../../../../"}' && script/generate rspec"
7
- puts "\n\nRun `rake spec` again."
8
- exit
9
- end
10
-
11
- # Establish connection with in memory SQLite 3 database
12
- ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
1
+ ENV["RAILS_ENV"] = "test"
2
+ require "rails"
3
+ require "simple_auth"
4
+ require File.dirname(__FILE__) + "/support/config/boot"
5
+ require "rspec/rails"
13
6
 
14
7
  # Load database schema
15
8
  load File.dirname(__FILE__) + "/schema.rb"
16
9
 
17
- # Create an alias for lambda
18
- alias :doing :lambda
19
-
20
- # Load resources
21
- require File.dirname(__FILE__) + "/resources/user"
22
- require File.dirname(__FILE__) + "/resources/controllers"
23
-
24
10
  # Restore default configuration
25
- if ENV["TARGET"] == "rails3"
26
- Rspec.configure do |config|
27
- config.before :each do
28
- load File.dirname(__FILE__) + "/../lib/simple_auth/config.rb"
29
- SimpleAuth::Config.model = :user
30
- end
31
- end
32
-
33
- ActionController::Base.prepend_view_path File.dirname(__FILE__) + "/resources/views"
34
- else
35
- Spec::Runner.configure do |config|
36
- config.before :each do
37
- load File.dirname(__FILE__) + "/../lib/simple_auth/config.rb"
38
- SimpleAuth::Config.model = :user
39
- end
11
+ RSpec.configure do |config|
12
+ config.before :each do
13
+ load File.dirname(__FILE__) + "/../lib/simple_auth/config.rb"
14
+ SimpleAuth::Config.model = :user
40
15
  end
41
16
  end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Account < ActiveRecord::Base
2
+ end
@@ -1,3 +1,3 @@
1
1
  class User < ActiveRecord::Base
2
- has_authentication
2
+ authentication
3
3
  end
@@ -0,0 +1,14 @@
1
+ ENV["BUNDLE_GEMFILE"] = File.dirname(__FILE__) + "/../../../../Gemfile"
2
+ require "bundler"
3
+ Bundler.setup
4
+ require "rails/all"
5
+ Bundler.require(:default)
6
+
7
+ module SimpleAuth
8
+ class Application < Rails::Application
9
+ config.root = File.dirname(__FILE__) + "/.."
10
+ config.active_support.deprecation = :log
11
+ end
12
+ end
13
+
14
+ SimpleAuth::Application.initialize!
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
@@ -0,0 +1,4 @@
1
+ SimpleAuth::Application.routes.draw do
2
+ get "dashboard", :to => "dashboard#index"
3
+ get "login", :to => "session#new"
4
+ end
@@ -11,9 +11,10 @@ SimpleAuth.setup do |config|
11
11
  # Set which attributes will be used for authentication.
12
12
  config.credentials = [:email, :login]
13
13
 
14
- # Set the User model that should be used
15
- config.model = :user
14
+ # Set the login url.
15
+ config.login_url = proc { login_path }
16
16
 
17
- # Set the login url
18
- config.redirect_to = proc { login_path }
17
+ # Logged users will be redirect to this url
18
+ # when +redirect_logged_user+ helper is used.
19
+ config.logged_url = proc { root_path }
19
20
  end