simple_auth 2.0.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +6 -9
  4. data/CHANGELOG.md +4 -0
  5. data/Gemfile +1 -1
  6. data/MIGRATE.md +40 -0
  7. data/README.md +34 -137
  8. data/Rakefile +8 -18
  9. data/bin/console +5 -0
  10. data/gemfiles/{rails_4_1.gemfile → rails_4_2.gemfile} +1 -1
  11. data/gemfiles/{rails_4_0.gemfile → rails_5_0.gemfile} +1 -1
  12. data/lib/simple_auth.rb +26 -11
  13. data/lib/simple_auth/action_controller.rb +53 -81
  14. data/lib/simple_auth/action_controller/require_login_action.rb +47 -0
  15. data/lib/simple_auth/config.rb +13 -36
  16. data/lib/simple_auth/generator.rb +2 -2
  17. data/lib/simple_auth/railtie.rb +0 -11
  18. data/lib/simple_auth/session.rb +19 -143
  19. data/lib/simple_auth/templates/install/initializer.rb +23 -0
  20. data/lib/simple_auth/version.rb +1 -6
  21. data/simple_auth.gemspec +6 -3
  22. data/test/controllers/admin/dashboard_controller_test.rb +31 -0
  23. data/test/controllers/dashboard_controller_test.rb +56 -0
  24. data/test/controllers/pages_controller_test.rb +16 -0
  25. data/test/generators/install_test.rb +13 -0
  26. data/test/support/dummy/app/controllers/admin/dashboard_controller.rb +35 -0
  27. data/{spec/support → test/support/dummy}/app/controllers/application_controller.rb +0 -0
  28. data/test/support/dummy/app/controllers/dashboard_controller.rb +23 -0
  29. data/test/support/dummy/app/controllers/pages_controller.rb +7 -0
  30. data/{spec/support → test/support/dummy}/app/models/user.rb +1 -1
  31. data/test/support/dummy/config/application.rb +17 -0
  32. data/test/support/dummy/config/initializers/simple_auth.rb +23 -0
  33. data/test/support/dummy/config/routes.rb +23 -0
  34. data/test/support/schema.rb +6 -0
  35. data/test/test_helper.rb +15 -0
  36. metadata +75 -65
  37. data/.rspec +0 -1
  38. data/gemfiles/rails_3_1.gemfile +0 -5
  39. data/gemfiles/rails_3_2.gemfile +0 -5
  40. data/lib/simple_auth/active_record.rb +0 -95
  41. data/lib/simple_auth/compat.rb +0 -2
  42. data/lib/simple_auth/compat/active_record.rb +0 -28
  43. data/lib/simple_auth/compat/config.rb +0 -17
  44. data/lib/simple_auth/exceptions.rb +0 -4
  45. data/lib/simple_auth/helper.rb +0 -12
  46. data/lib/simple_auth/rspec.rb +0 -29
  47. data/locales/en.yml +0 -5
  48. data/locales/pt-BR.yml +0 -5
  49. data/spec/controllers/redirect_logged_user_spec.rb +0 -87
  50. data/spec/controllers/require_logged_user_spec.rb +0 -146
  51. data/spec/schema.rb +0 -9
  52. data/spec/simple_auth/active_record_spec.rb +0 -146
  53. data/spec/simple_auth/compat_spec.rb +0 -45
  54. data/spec/simple_auth/config_spec.rb +0 -21
  55. data/spec/simple_auth/helper_spec.rb +0 -24
  56. data/spec/simple_auth/initializer_spec.rb +0 -9
  57. data/spec/simple_auth/session_spec.rb +0 -212
  58. data/spec/spec_helper.rb +0 -23
  59. data/spec/support/app/models/customer.rb +0 -3
  60. data/spec/support/app/models/person.rb +0 -4
  61. data/spec/support/app/views/dashboard/index.erb +0 -0
  62. data/spec/support/app/views/session/new.erb +0 -0
  63. data/spec/support/config/boot.rb +0 -16
  64. data/spec/support/config/database.yml +0 -3
  65. data/spec/support/config/routes.rb +0 -4
  66. data/templates/initializer.rb +0 -22
@@ -1,45 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe SimpleAuth, "compatibility mode" do
4
- before do
5
- SimpleAuth::Config.model = :customer
6
- load "./lib/simple_auth/compat.rb"
7
- require "customer"
8
- end
9
-
10
- after :all do
11
- mod = SimpleAuth::ActiveRecord::InstanceMethods
12
- mod.send :remove_method, :password=
13
- mod.send :remove_method, :authenticate
14
- end
15
-
16
- it "finds user based on the hashing system" do
17
- password_salt = SecureRandom.hex
18
- password_hash = SimpleAuth::Config.crypter.call("test", password_salt)
19
- password_digest = BCrypt::Password.create(password_hash, cost: BCrypt::Engine::MIN_COST)
20
-
21
- ActiveRecord::Base.connection.execute <<-SQL
22
- INSERT INTO customers
23
- (email, login, password_digest, password_salt)
24
- VALUES
25
- ('john@example.org', 'johndoe', '#{password_digest}', '#{password_salt}')
26
- SQL
27
-
28
- expect(Customer.authenticate("johndoe", "test")).to be_a(Customer)
29
- end
30
-
31
- it "assigns password_digest" do
32
- customer = Customer.create(password: "test")
33
- expect(customer.password_digest).to be_present
34
- end
35
-
36
- it "sets password" do
37
- customer = Customer.create(password: "test", password_confirmation: "test")
38
- expect(customer.password).to eql("test")
39
- end
40
-
41
- it "sets password confirmation" do
42
- customer = Customer.create(password: "test", password_confirmation: "test")
43
- expect(customer.password_confirmation).to eql("test")
44
- end
45
- end
@@ -1,21 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe SimpleAuth::Config do
4
- it "yields SimpleAuth::Config class" do
5
- SimpleAuth.setup do |config|
6
- expect(config).to eq(SimpleAuth::Config)
7
- end
8
- end
9
-
10
- it "uses [:email, :login] as credential attributes" do
11
- expect(SimpleAuth::Config.credentials).to eq([:email, :login])
12
- end
13
-
14
- it "uses User as default model" do
15
- expect(SimpleAuth::Config.model).to eq(:user)
16
- end
17
-
18
- it "disables session wipeout" do
19
- expect(SimpleAuth::Config.wipeout_session).to be_falsey
20
- end
21
- end
@@ -1,24 +0,0 @@
1
- require "spec_helper"
2
-
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)
9
- end
10
-
11
- it "includes module" do
12
- ApplicationController.included_modules.include?(SimpleAuth::Helper)
13
- end
14
-
15
- it "renders block when user is logged" do
16
- expect(@helper).to receive(:logged_in?).and_return(true)
17
- expect(@helper.when_logged { "logged" }).to eq("logged")
18
- end
19
-
20
- it "doesn't render block when user is unlogged" do
21
- expect(@helper).to receive(:logged_in?).and_return(false)
22
- expect(@helper.when_logged { "logged" }).to be_nil
23
- end
24
- end
@@ -1,9 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Initializer" do
4
- it "runs smoothly" do
5
- expect {
6
- load File.dirname(__FILE__) + "/../../templates/initializer.rb"
7
- }.to_not raise_error
8
- end
9
- end
@@ -1,212 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe SimpleAuth::Session do
4
- before do
5
- User.delete_all
6
-
7
- @user = User.create!(
8
- :login => "johndoe",
9
- :email => "john@doe.com",
10
- :password => "test",
11
- :password_confirmation => "test"
12
- )
13
-
14
- @session = Hash.new
15
- @controller = ActionController::Base.new
16
- allow(@controller).to receive_messages :session => @session, :reset_session => nil
17
-
18
- SimpleAuth::Config.controller = @controller
19
- @user_session = SimpleAuth::Session.new(:credential => "johndoe", :password => "test")
20
- end
21
-
22
- it "doesn't raise when trying to find a session without activating controller" do
23
- SimpleAuth::Config.controller = nil
24
-
25
- expect {
26
- expect(SimpleAuth::Session.find).to be_nil
27
- }.to_not raise_error
28
- end
29
-
30
- it "returns session key" do
31
- SimpleAuth::Session.session_key == :user_id
32
- end
33
-
34
- it "returns record id" do
35
- @session[:user_id] = 42
36
- SimpleAuth::Session.record_id == 42
37
- end
38
-
39
- context "with valid credentials" do
40
- before do
41
- @user_session.save!
42
- end
43
-
44
- it "returns existing session" do
45
- @user_session = SimpleAuth::Session.find
46
- expect(@user_session).to be_valid
47
- expect(@user_session.record).to eq(@user)
48
- end
49
-
50
- it "doesn't be new record" do
51
- expect(@user_session).not_to be_new_record
52
- end
53
-
54
- it "is invalid when record is not authorized" do
55
- allow(@controller).to receive_messages :authorized? => false
56
- expect(@user_session).not_to be_valid
57
- end
58
-
59
- it "is valid when record is authorized" do
60
- allow(@user_session.record).to receive_messages :authorized? => true
61
- expect(@user_session).to be_valid
62
- end
63
-
64
- it "finds record" do
65
- expect(@user_session.record).to eq(@user)
66
- end
67
-
68
- it "is saved" do
69
- expect(@user_session.save).to be_truthy
70
- end
71
-
72
- it "resets session before saving" do
73
- @session[:session_id] = "xWA1"
74
- @user_session.save
75
- expect(@session).not_to have_key(:session_id)
76
- end
77
-
78
- it "automatically saves session when calling create!" do
79
- @user_session = SimpleAuth::Session.create!(:credential => "johndoe", :password => "test")
80
- expect(@user_session).to be_valid
81
- expect(@user_session.record).to eq(@user)
82
- expect(@session[:user_id]).to eq(@user.id)
83
- end
84
-
85
- it "destroys session" do
86
- expect(@user_session.destroy).to be_truthy
87
- expect(@user_session.record).to be_nil
88
- expect(@session).not_to have_key(:user)
89
- end
90
-
91
- it "initializes record session" do
92
- @user_session.save
93
- expect(@session[:user_id]).to eq(@user.id)
94
- end
95
- end
96
-
97
- context "with invalid credentials" do
98
- before do
99
- @user_session.credential = "invalid"
100
- @user_session.save
101
- end
102
-
103
- it "unsets previous record id when is not valid" do
104
- @session[:user_id] = 1
105
- expect(@user_session).not_to be_valid
106
- expect(@session).not_to have_key(:user)
107
- end
108
-
109
- it "unsets previous record id when is not saved" do
110
- @session[:user_id] = 1
111
- expect(@user_session.save).to be_falsey
112
- expect(@session).not_to have_key(:user)
113
- end
114
-
115
- it "is new record" do
116
- expect(SimpleAuth::Session.new).to be_new_record
117
- expect(@user_session).to be_new_record
118
- end
119
-
120
- it "has error message" do
121
- expect(@user_session.errors.full_messages[0]).to eq("Invalid username or password")
122
- end
123
-
124
- it "doesn't return error messages for attributes" do
125
- expect(@user_session.errors.on(:credential)).to be_nil
126
- expect(@user_session.errors.on(:password)).to be_nil
127
- end
128
-
129
- it "returns empty array when trying to get errors by using hash syntax" do
130
- expect(@user_session.errors[:credential]).to be_empty
131
- expect(@user_session.errors[:password]).to be_empty
132
- end
133
-
134
- it "has errors" do
135
- expect(@user_session.errors).not_to be_empty
136
- end
137
-
138
- it "doesn't find existing session" do
139
- expect(SimpleAuth::Session.find).to be_nil
140
- end
141
-
142
- it "doesn't find record" do
143
- expect(@user_session.record).to be_nil
144
- end
145
-
146
- it "doesn't be a valid session" do
147
- expect(@user_session).not_to be_valid
148
- end
149
-
150
- it "unsets record store from session" do
151
- expect(@session).not_to have_key(:user)
152
- end
153
-
154
- it "doesn't be saved" do
155
- expect(@user_session.save).to be_falsey
156
- end
157
-
158
- it "raises error with save!" do
159
- expect { @user_session.save! }.to raise_error(SimpleAuth::NotAuthorized)
160
- end
161
-
162
- it "raises error with create!" do
163
- expect { SimpleAuth::Session.create!({}) }.to raise_error(SimpleAuth::NotAuthorized)
164
- end
165
- end
166
-
167
- context "when destroying session" do
168
- before do
169
- @user_session.save!
170
- end
171
-
172
- it "keeps return to url" do
173
- @session[:return_to] = "/some/path"
174
- @user_session.destroy
175
- expect(@session[:return_to]).to eq("/some/path")
176
- end
177
-
178
- it "removes record session" do
179
- @user_session.destroy
180
- expect(@session).not_to have_key(:user_id)
181
- end
182
-
183
- it "keeps keys composed by user_*" do
184
- SimpleAuth::Config.wipeout_session = false
185
-
186
- @session[:user_friends_count] = 42
187
- @user_session.destroy
188
-
189
- expect(@session[:user_friends_count]).to eq(42)
190
- end
191
-
192
- it "erases keys composed by user_*" do
193
- SimpleAuth::Config.wipeout_session = true
194
-
195
- @session[:user_friends_count] = 100
196
- @session[:user_preferred_number] = 42
197
-
198
- @user_session.destroy
199
-
200
- expect(@session).not_to have_key(:user_friends_count)
201
- expect(@session).not_to have_key(:user_preferred_number)
202
- end
203
-
204
- it "unsets current_user instance variable" do
205
- @user_session.destroy
206
-
207
- expect(SimpleAuth::Config.controller.send(:current_user)).to be_nil
208
- expect(SimpleAuth::Config.controller.instance_variable_get("@current_user")).to be_nil
209
- expect(SimpleAuth::Config.controller.instance_variable_get("@current_session")).to be_nil
210
- end
211
- end
212
- end
@@ -1,23 +0,0 @@
1
- ENV["RAILS_ENV"] = "test"
2
- require "bundler/setup"
3
- Bundler.require
4
-
5
- I18n.load_path += Dir[File.expand_path("../../locales/*.yml", __FILE__)]
6
- I18n.enforce_available_locales = false
7
-
8
- require "rails"
9
- require "simple_auth"
10
- require File.dirname(__FILE__) + "/support/config/boot"
11
- require "rspec/rails"
12
-
13
- $rails_version = Rails::VERSION::STRING
14
-
15
- # Load database schema
16
- load File.dirname(__FILE__) + "/schema.rb"
17
-
18
- # Restore default configuration
19
- RSpec.configure do |config|
20
- config.before :each do
21
- load File.dirname(__FILE__) + "/../lib/simple_auth/config.rb"
22
- end
23
- end
@@ -1,3 +0,0 @@
1
- class Customer < ActiveRecord::Base
2
- authentication
3
- end
@@ -1,4 +0,0 @@
1
- class Person < ActiveRecord::Base
2
- self.table_name = "users"
3
- authentication validations: false
4
- end
File without changes
@@ -1,16 +0,0 @@
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
- config.secret_key_base = "secret"
12
- config.eager_load = false
13
- end
14
- end
15
-
16
- SimpleAuth::Application.initialize!
@@ -1,3 +0,0 @@
1
- test:
2
- adapter: sqlite3
3
- database: ":memory:"
@@ -1,4 +0,0 @@
1
- SimpleAuth::Application.routes.draw do
2
- get "dashboard", :to => "dashboard#index"
3
- get "login", :to => "session#new"
4
- end
@@ -1,22 +0,0 @@
1
- # Use this file to setup SimpleAuth.
2
- SimpleAuth.setup do |config|
3
- # Set which attributes will be used for authentication.
4
- config.credentials = [:email, :login]
5
-
6
- # Set the login url.
7
- config.login_url = proc { login_path }
8
-
9
- # Logged users will be redirect to this url
10
- # when +redirect_logged_user+ helper is used.
11
- config.logged_url = proc { root_path }
12
-
13
- # Automatically remove all session values that start with your model name.
14
- #
15
- # When an existing session is destroyed or a new session is created,
16
- # SimpleAuth will remove the record id stored as <tt>#{SimpleAuth::Config.model}</tt>.
17
- #
18
- # Additionally, you can enable this option to remove any other key composed by
19
- # <tt>#{SimpleAuth::Config.model}_*</tt>.
20
- #
21
- # config.wipeout_session = true
22
- end