clearance 1.8.0 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of clearance might be problematic. Click here for more details.

Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +24 -6
  4. data/.yardopts +6 -0
  5. data/Appraisals +12 -4
  6. data/CONTRIBUTING.md +4 -1
  7. data/Gemfile +2 -2
  8. data/Gemfile.lock +87 -85
  9. data/NEWS.md +672 -311
  10. data/README.md +185 -330
  11. data/app/controllers/clearance/passwords_controller.rb +25 -19
  12. data/app/controllers/clearance/sessions_controller.rb +17 -4
  13. data/app/controllers/clearance/users_controller.rb +10 -4
  14. data/app/mailers/clearance_mailer.rb +2 -3
  15. data/app/views/clearance_mailer/change_password.html.erb +6 -3
  16. data/app/views/clearance_mailer/change_password.text.erb +5 -0
  17. data/app/views/layouts/application.html.erb +2 -2
  18. data/app/views/passwords/create.html.erb +1 -1
  19. data/app/views/passwords/edit.html.erb +2 -2
  20. data/app/views/passwords/new.html.erb +2 -2
  21. data/app/views/sessions/_form.html.erb +2 -2
  22. data/app/views/sessions/new.html.erb +1 -1
  23. data/app/views/users/new.html.erb +2 -2
  24. data/bin/setup +6 -2
  25. data/config/locales/clearance.en.yml +6 -0
  26. data/db/migrate/20110111224543_create_clearance_users.rb +1 -1
  27. data/gemfiles/{rails3.2.gemfile → rails32.gemfile} +1 -1
  28. data/gemfiles/{rails4.0.gemfile → rails40.gemfile} +2 -2
  29. data/gemfiles/{rails4.1.gemfile → rails41.gemfile} +2 -2
  30. data/gemfiles/{rails4.2.gemfile → rails42.gemfile} +2 -2
  31. data/gemfiles/rails50.gemfile +19 -0
  32. data/lib/clearance/authentication.rb +52 -1
  33. data/lib/clearance/authorization.rb +47 -4
  34. data/lib/clearance/back_door.rb +1 -0
  35. data/lib/clearance/configuration.rb +127 -15
  36. data/lib/clearance/constraints/signed_in.rb +21 -0
  37. data/lib/clearance/constraints/signed_out.rb +12 -0
  38. data/lib/clearance/constraints.rb +12 -0
  39. data/lib/clearance/controller.rb +13 -0
  40. data/lib/clearance/default_sign_in_guard.rb +17 -0
  41. data/lib/clearance/engine.rb +24 -4
  42. data/lib/clearance/password_strategies/bcrypt.rb +9 -2
  43. data/lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb +19 -0
  44. data/lib/clearance/password_strategies/blowfish.rb +17 -0
  45. data/lib/clearance/password_strategies/sha1.rb +17 -0
  46. data/lib/clearance/password_strategies.rb +13 -0
  47. data/lib/clearance/rack_session.rb +13 -0
  48. data/lib/clearance/rspec.rb +15 -4
  49. data/lib/clearance/session.rb +46 -1
  50. data/lib/clearance/session_status.rb +7 -0
  51. data/lib/clearance/sign_in_guard.rb +65 -0
  52. data/lib/clearance/test_unit.rb +3 -3
  53. data/lib/clearance/testing/controller_helpers.rb +44 -0
  54. data/lib/clearance/testing/deny_access_matcher.rb +35 -1
  55. data/lib/clearance/testing/helpers.rb +9 -25
  56. data/lib/clearance/testing/view_helpers.rb +32 -0
  57. data/lib/clearance/token.rb +7 -0
  58. data/lib/clearance/user.rb +159 -0
  59. data/lib/clearance/version.rb +1 -1
  60. data/lib/clearance.rb +2 -0
  61. data/lib/generators/clearance/install/install_generator.rb +13 -3
  62. data/lib/generators/clearance/install/templates/db/migrate/add_clearance_to_users.rb +3 -3
  63. data/lib/generators/clearance/install/templates/db/migrate/create_users.rb +2 -2
  64. data/lib/generators/clearance/routes/routes_generator.rb +23 -0
  65. data/lib/generators/clearance/routes/templates/routes.rb +7 -7
  66. data/lib/generators/clearance/specs/templates/features/clearance/user_signs_out_spec.rb.tt +1 -1
  67. data/lib/generators/clearance/specs/templates/features/clearance/visitor_resets_password_spec.rb.tt +12 -2
  68. data/lib/generators/clearance/specs/templates/features/clearance/visitor_signs_in_spec.rb.tt +1 -1
  69. data/lib/generators/clearance/specs/templates/features/clearance/visitor_signs_up_spec.rb.tt +1 -1
  70. data/lib/generators/clearance/specs/templates/features/clearance/visitor_updates_password_spec.rb.tt +1 -1
  71. data/spec/acceptance/clearance_installation_spec.rb +4 -1
  72. data/spec/app_templates/config/initializers/clearance.rb +2 -0
  73. data/spec/app_templates/testapp/app/controllers/home_controller.rb +5 -1
  74. data/spec/app_templates/testapp/config/initializers/action_mailer.rb +1 -3
  75. data/spec/clearance/controller_spec.rb +11 -0
  76. data/spec/clearance/rack_session_spec.rb +5 -5
  77. data/spec/clearance/testing/{helpers_spec.rb → controller_helpers_spec.rb} +12 -12
  78. data/spec/clearance/testing/view_helpers_spec.rb +37 -0
  79. data/spec/configuration_spec.rb +24 -0
  80. data/spec/controllers/apis_controller_spec.rb +6 -2
  81. data/spec/controllers/forgeries_controller_spec.rb +6 -1
  82. data/spec/controllers/passwords_controller_spec.rb +1 -11
  83. data/spec/controllers/permissions_controller_spec.rb +13 -3
  84. data/spec/controllers/sessions_controller_spec.rb +4 -4
  85. data/spec/dummy/app/controllers/application_controller.rb +5 -1
  86. data/spec/dummy/application.rb +4 -0
  87. data/spec/generators/clearance/install/install_generator_spec.rb +15 -3
  88. data/spec/generators/clearance/routes/routes_generator_spec.rb +5 -1
  89. data/spec/generators/clearance/views/views_generator_spec.rb +11 -10
  90. data/spec/helpers/helper_helpers_spec.rb +10 -0
  91. data/spec/mailers/clearance_mailer_spec.rb +13 -19
  92. data/spec/password_strategies/bcrypt_migration_from_sha1_spec.rb +6 -0
  93. data/spec/password_strategies/blowfish_spec.rb +6 -0
  94. data/spec/password_strategies/sha1_spec.rb +6 -0
  95. data/spec/spec_helper.rb +7 -0
  96. data/spec/support/generator_spec_helpers.rb +4 -0
  97. data/spec/support/http_method_shim.rb +23 -0
  98. data/spec/user_spec.rb +9 -0
  99. data/spec/views/view_helpers_spec.rb +10 -0
  100. metadata +19 -8
data/lib/clearance.rb CHANGED
@@ -10,6 +10,8 @@ require 'clearance/password_strategies'
10
10
  require 'clearance/constraints'
11
11
 
12
12
  module Clearance
13
+ # @deprecated Use `Gem::Specification` API if you need to access Clearance's
14
+ # Gem root.
13
15
  def self.root
14
16
  warn "#{Kernel.caller.first}: [DEPRECATION] `Clearance.root` is " +
15
17
  "deprecated and will be removed in the next major release. If you need " +
@@ -23,7 +23,7 @@ module Clearance
23
23
  if File.exist? "app/models/user.rb"
24
24
  inject_into_file(
25
25
  "app/models/user.rb",
26
- "include Clearance::User\n\n",
26
+ " include Clearance::User\n\n",
27
27
  after: "class User < ActiveRecord::Base\n"
28
28
  )
29
29
  else
@@ -61,7 +61,7 @@ module Clearance
61
61
  migration_template(
62
62
  "db/migrate/#{migration_name}",
63
63
  "db/migrate/#{migration_name}",
64
- config
64
+ config.merge(migration_version: migration_version),
65
65
  )
66
66
  end
67
67
  end
@@ -101,7 +101,11 @@ module Clearance
101
101
  end
102
102
 
103
103
  def users_table_exists?
104
- ActiveRecord::Base.connection.table_exists?(:users)
104
+ if ActiveRecord::Base.connection.respond_to?(:data_source_exists?)
105
+ ActiveRecord::Base.connection.data_source_exists?(:users)
106
+ else
107
+ ActiveRecord::Base.connection.table_exists?(:users)
108
+ end
105
109
  end
106
110
 
107
111
  def existing_users_columns
@@ -116,6 +120,12 @@ module Clearance
116
120
  def self.next_migration_number(dir)
117
121
  ActiveRecord::Generators::Base.next_migration_number(dir)
118
122
  end
123
+
124
+ def migration_version
125
+ if Rails.version >= "5.0.0"
126
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
127
+ end
128
+ end
119
129
  end
120
130
  end
121
131
  end
@@ -1,6 +1,6 @@
1
- class AddClearanceToUsers < ActiveRecord::Migration
1
+ class AddClearanceToUsers < ActiveRecord::Migration<%= migration_version %>
2
2
  def self.up
3
- change_table :users do |t|
3
+ change_table :users do |t|
4
4
  <% config[:new_columns].values.each do |column| -%>
5
5
  <%= column %>
6
6
  <% end -%>
@@ -24,7 +24,7 @@ class AddClearanceToUsers < ActiveRecord::Migration
24
24
  def self.down
25
25
  change_table :users do |t|
26
26
  <% if config[:new_columns].any? -%>
27
- t.remove <%= new_columns.keys.map { |column| ":#{column}" }.join(",") %>
27
+ t.remove <%= new_columns.keys.map { |column| ":#{column}" }.join(", ") %>
28
28
  <% end -%>
29
29
  end
30
30
  end
@@ -1,6 +1,6 @@
1
- class CreateUsers < ActiveRecord::Migration
1
+ class CreateUsers < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
- create_table :users do |t|
3
+ create_table :users do |t|
4
4
  t.timestamps null: false
5
5
  t.string :email, null: false
6
6
  t.string :encrypted_password, limit: 128, null: false
@@ -9,6 +9,14 @@ module Clearance
9
9
  route(clearance_routes)
10
10
  end
11
11
 
12
+ def disable_clearance_internal_routes
13
+ inject_into_file(
14
+ "config/initializers/clearance.rb",
15
+ " config.routes = false\n",
16
+ after: "Clearance.configure do |config|\n",
17
+ )
18
+ end
19
+
12
20
  private
13
21
 
14
22
  def clearance_routes
@@ -18,6 +26,21 @@ module Clearance
18
26
  def routes_file_path
19
27
  File.expand_path(find_in_source_paths('routes.rb'))
20
28
  end
29
+
30
+ def route(routing_code)
31
+ log :route, "all clearance routes"
32
+ sentinel = /\.routes\.draw do\s*\n/m
33
+
34
+ in_root do
35
+ inject_into_file(
36
+ "config/routes.rb",
37
+ routing_code,
38
+ after: sentinel,
39
+ verbose: false,
40
+ force: true,
41
+ )
42
+ end
43
+ end
21
44
  end
22
45
  end
23
46
  end
@@ -1,12 +1,12 @@
1
- resources :passwords, controller: 'clearance/passwords', only: [:create, :new]
2
- resource :session, controller: 'clearance/sessions', only: [:create]
1
+ resources :passwords, controller: "clearance/passwords", only: [:create, :new]
2
+ resource :session, controller: "clearance/sessions", only: [:create]
3
3
 
4
- resources :users, controller: 'clearance/users', only: [:create] do
4
+ resources :users, controller: "clearance/users", only: [:create] do
5
5
  resource :password,
6
- controller: 'clearance/passwords',
6
+ controller: "clearance/passwords",
7
7
  only: [:create, :edit, :update]
8
8
  end
9
9
 
10
- get '/sign_in' => 'clearance/sessions#new', as: 'sign_in'
11
- delete '/sign_out' => 'clearance/sessions#destroy', as: 'sign_out'
12
- get '/sign_up' => 'clearance/users#new', as: 'sign_up'
10
+ get "/sign_in" => "clearance/sessions#new", as: "sign_in"
11
+ delete "/sign_out" => "clearance/sessions#destroy", as: "sign_out"
12
+ get "/sign_up" => "clearance/users#new", as: "sign_up"
@@ -1,7 +1,7 @@
1
1
  require "<%= @helper_file %>"
2
2
  require "support/features/clearance_helpers"
3
3
 
4
- feature "User signs out" do
4
+ RSpec.feature "User signs out" do
5
5
  scenario "signs out" do
6
6
  sign_in
7
7
  sign_out
@@ -1,8 +1,17 @@
1
1
  require "<%= @helper_file %>"
2
2
  require "support/features/clearance_helpers"
3
3
 
4
- feature "Visitor resets password" do
4
+ RSpec.feature "Visitor resets password" do
5
5
  before { ActionMailer::Base.deliveries.clear }
6
+ <% if defined?(ActiveJob) -%>
7
+
8
+ around do |example|
9
+ original_adapter = ActiveJob::Base.queue_adapter
10
+ ActiveJob::Base.queue_adapter = :inline
11
+ example.run
12
+ ActiveJob::Base.queue_adapter = original_adapter
13
+ end
14
+ <% end -%>
6
15
 
7
16
  scenario "by navigating to the page" do
8
17
  visit sign_in_path
@@ -47,7 +56,8 @@ feature "Visitor resets password" do
47
56
  message = ActionMailer::Base.deliveries.any? do |email|
48
57
  email.to == [recipient] &&
49
58
  email.subject =~ /#{subject}/i &&
50
- email.body =~ /#{body}/
59
+ email.html_part.body =~ /#{body}/ &&
60
+ email.text_part.body =~ /#{body}/
51
61
  end
52
62
 
53
63
  expect(message).to be
@@ -1,7 +1,7 @@
1
1
  require "<%= @helper_file %>"
2
2
  require "support/features/clearance_helpers"
3
3
 
4
- feature "Visitor signs in" do
4
+ RSpec.feature "Visitor signs in" do
5
5
  scenario "with valid email and password" do
6
6
  create_user "user@example.com", "password"
7
7
  sign_in_with "user@example.com", "password"
@@ -1,7 +1,7 @@
1
1
  require "<%= @helper_file %>"
2
2
  require "support/features/clearance_helpers"
3
3
 
4
- feature "Visitor signs up" do
4
+ RSpec.feature "Visitor signs up" do
5
5
  scenario "by navigating to the page" do
6
6
  visit sign_in_path
7
7
 
@@ -1,7 +1,7 @@
1
1
  require "<%= @helper_file %>"
2
2
  require "support/features/clearance_helpers"
3
3
 
4
- feature "Visitor updates password" do
4
+ RSpec.feature "Visitor updates password" do
5
5
  scenario "with valid password" do
6
6
  user = user_with_reset_password
7
7
  update_password user, "newpassword"
@@ -10,6 +10,7 @@ describe "Clearance Installation" do
10
10
 
11
11
  it "can successfully run specs" do
12
12
  app_name = "testapp"
13
+
13
14
  generate_test_app(app_name)
14
15
 
15
16
  Dir.chdir(app_name) do
@@ -28,7 +29,8 @@ describe "Clearance Installation" do
28
29
  --skip-git \
29
30
  --skip-javascript \
30
31
  --skip-sprockets \
31
- --skip-keeps"
32
+ --skip-keeps \
33
+ --no-rc"
32
34
 
33
35
  FileUtils.rm_f("public/index.html")
34
36
  FileUtils.rm_f("app/views/layouts/application.html.erb")
@@ -70,6 +72,7 @@ describe "Clearance Installation" do
70
72
  end
71
73
 
72
74
  return_value = system("#{command} #{silencer}")
75
+
73
76
  expect(return_value).to eq true
74
77
  end
75
78
  end
@@ -0,0 +1,2 @@
1
+ Clearance.configure do |config|
2
+ end
@@ -1,5 +1,9 @@
1
1
  class HomeController < ApplicationController
2
2
  def show
3
- render text: "", layout: "application"
3
+ if Rails::VERSION::MAJOR >= 5
4
+ render html: "", layout: "application"
5
+ else
6
+ render text: "", layout: "application"
7
+ end
4
8
  end
5
9
  end
@@ -1,3 +1 @@
1
- Rails.application.config.action_mailer.default_url_options = {
2
- host: "localhost"
3
- }
1
+ ActionMailer::Base.default_url_options[:host] = "localhost"
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe Clearance::Controller, type: :controller do
4
+ controller(ActionController::Base) do
5
+ include Clearance::Controller
6
+ end
7
+
8
+ it "exposes no action methods" do
9
+ expect(controller.action_methods).to be_empty
10
+ end
11
+ end
@@ -2,21 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  describe Clearance::RackSession do
4
4
  it 'injects a clearance session into the environment' do
5
- expected_session = 'the session'
6
- allow(expected_session).to receive(:add_cookie_to_headers)
7
- allow(Clearance::Session).to receive(:new).and_return(expected_session)
8
5
  headers = { 'X-Roaring-Lobster' => 'Red' }
9
-
10
6
  app = Rack::Builder.new do
11
7
  use Clearance::RackSession
12
8
  run lambda { |env| Rack::Response.new(env[:clearance], 200, headers).finish }
13
9
  end
14
10
 
15
11
  env = Rack::MockRequest.env_for('/')
12
+ expected_session = "the session"
13
+ allow(expected_session).to receive(:add_cookie_to_headers)
14
+ allow(Clearance::Session).to receive(:new).
15
+ with(env).
16
+ and_return(expected_session)
16
17
 
17
18
  response = Rack::MockResponse.new(*app.call(env))
18
19
 
19
- expect(Clearance::Session).to have_received(:new).with(env)
20
20
  expect(response.body).to eq expected_session
21
21
  expect(expected_session).to have_received(:add_cookie_to_headers).
22
22
  with(hash_including(headers))
@@ -1,20 +1,20 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
- describe Clearance::Testing::Helpers do
3
+ describe Clearance::Testing::ControllerHelpers do
4
4
  class TestClass
5
- include Clearance::Testing::Helpers
5
+ include Clearance::Testing::ControllerHelpers
6
6
 
7
7
  def initialize
8
- @controller = Controller.new
9
- end
10
-
11
- class Controller
12
- def sign_in(user); end
8
+ @request = Class.new do
9
+ def env
10
+ { clearance: Clearance::Session.new({}) }
11
+ end
12
+ end.new
13
13
  end
14
14
  end
15
15
 
16
- describe '#sign_in' do
17
- it 'creates an instance of the clearance user model with FactoryGirl' do
16
+ describe "#sign_in" do
17
+ it "creates an instance of the clearance user model with FactoryGirl" do
18
18
  MyUserModel = Class.new
19
19
  allow(FactoryGirl).to receive(:create)
20
20
  allow(Clearance.configuration).to receive(:user_model).
@@ -26,8 +26,8 @@ describe Clearance::Testing::Helpers do
26
26
  end
27
27
  end
28
28
 
29
- describe '#sign_in_as' do
30
- it 'returns the user if signed in successfully' do
29
+ describe "#sign_in_as" do
30
+ it "returns the user if signed in successfully" do
31
31
  user = build(:user)
32
32
 
33
33
  returned_user = TestClass.new.sign_in_as user
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ describe Clearance::Testing::ViewHelpers do
4
+ describe "#sign_in" do
5
+ it "sets the signed in user to a new user object" do
6
+ user_model = Class.new
7
+ allow(Clearance.configuration).to receive(:user_model).
8
+ and_return(user_model)
9
+
10
+ view = test_view_class.new
11
+ view.sign_in
12
+
13
+ expect(view.current_user).to be_an_instance_of(user_model)
14
+ end
15
+ end
16
+
17
+ describe "#sign_in_as" do
18
+ it "sets the signed in user to the object provided" do
19
+ user = double("User")
20
+
21
+ view = test_view_class.new
22
+ view.sign_in_as(user)
23
+
24
+ expect(view.current_user).to eq user
25
+ end
26
+ end
27
+
28
+ def test_view_class
29
+ Class.new do
30
+ include Clearance::Testing::ViewHelpers
31
+
32
+ def view
33
+ @view ||= extend Clearance::Testing::ViewHelpers::CurrentUser
34
+ end
35
+ end
36
+ end
37
+ end
@@ -143,6 +143,15 @@ describe Clearance::Configuration do
143
143
  end
144
144
  end
145
145
 
146
+ describe "#user_parameter" do
147
+ it "returns the parameter key to use based on the user_model" do
148
+ Account = Class.new(ActiveRecord::Base)
149
+ Clearance.configure { |config| config.user_model = Account }
150
+
151
+ expect(Clearance.configuration.user_parameter).to eq :account
152
+ end
153
+ end
154
+
146
155
  describe '#user_id_parameter' do
147
156
  it 'returns the parameter key to use based on the user_model' do
148
157
  CustomUser = Class.new(ActiveRecord::Base)
@@ -162,4 +171,19 @@ describe Clearance::Configuration do
162
171
  expect(Clearance.configuration.routes_enabled?).to be false
163
172
  end
164
173
  end
174
+
175
+ describe "#reload_user_model" do
176
+ it "returns the user model class if one has already been configured" do
177
+ ConfiguredUser = Class.new
178
+ Clearance.configure { |config| config.user_model = ConfiguredUser }
179
+
180
+ expect(Clearance.configuration.reload_user_model).to eq ConfiguredUser
181
+ end
182
+
183
+ it "returns nil if the user_model has not been configured" do
184
+ Clearance.configuration = Clearance::Configuration.new
185
+
186
+ expect(Clearance.configuration.reload_user_model).to be_nil
187
+ end
188
+ end
165
189
  end
@@ -3,10 +3,14 @@ require 'spec_helper'
3
3
  class ApisController < ActionController::Base
4
4
  include Clearance::Controller
5
5
 
6
- before_filter :require_login
6
+ if respond_to?(:before_action)
7
+ before_action :require_login
8
+ else
9
+ before_filter :require_login
10
+ end
7
11
 
8
12
  def show
9
- render text: 'response'
13
+ head :ok
10
14
  end
11
15
  end
12
16
 
@@ -4,7 +4,12 @@ class ForgeriesController < ActionController::Base
4
4
  include Clearance::Controller
5
5
 
6
6
  protect_from_forgery
7
- before_filter :require_login
7
+
8
+ if respond_to?(:before_action)
9
+ before_action :require_login
10
+ else
11
+ before_filter :require_login
12
+ end
8
13
 
9
14
  # This is off in test by default, but we need it for this test
10
15
  self.allow_forgery_protection = true
@@ -101,16 +101,6 @@ describe Clearance::PasswordsController do
101
101
  expect(user.reload.encrypted_password).not_to eq old_encrypted_password
102
102
  end
103
103
 
104
- it "sets the remember token and clears the confirmation token" do
105
- user = create(:user, :with_forgotten_password)
106
-
107
- put :update, update_parameters(user, new_password: "my_new_password")
108
-
109
- user.reload
110
- expect(user.remember_token).not_to be_nil
111
- expect(user.confirmation_token).to be_nil
112
- end
113
-
114
104
  it "signs the user in and redirects" do
115
105
  user = create(:user, :with_forgotten_password)
116
106
 
@@ -121,7 +111,7 @@ describe Clearance::PasswordsController do
121
111
  end
122
112
  end
123
113
 
124
- context "no password provided" do
114
+ context "password update fails" do
125
115
  it "does not update the password" do
126
116
  user = create(:user, :with_forgotten_password)
127
117
  old_encrypted_password = user.encrypted_password
@@ -3,14 +3,18 @@ require 'spec_helper'
3
3
  class PermissionsController < ActionController::Base
4
4
  include Clearance::Controller
5
5
 
6
- before_filter :require_login, only: :show
6
+ if respond_to?(:before_action)
7
+ before_action :require_login, only: :show
8
+ else
9
+ before_filter :require_login, only: :show
10
+ end
7
11
 
8
12
  def new
9
- render text: 'New page'
13
+ head :ok
10
14
  end
11
15
 
12
16
  def show
13
- render text: 'Show page'
17
+ head :ok
14
18
  end
15
19
  end
16
20
 
@@ -54,6 +58,12 @@ describe PermissionsController do
54
58
 
55
59
  expect(subject).to deny_access(redirect: sign_in_url)
56
60
  end
61
+
62
+ it "denies access to show and display a flash message" do
63
+ get :show
64
+
65
+ expect(flash[:notice]).to match(/^Please sign in to continue/)
66
+ end
57
67
  end
58
68
 
59
69
  context 'when remember_token is blank' do
@@ -9,7 +9,7 @@ describe Clearance::SessionsController do
9
9
 
10
10
  it { should respond_with(:success) }
11
11
  it { should render_template(:new) }
12
- it { should_not set_the_flash }
12
+ it { should_not set_flash }
13
13
  end
14
14
 
15
15
  context "when a user is signed in" do
@@ -19,7 +19,7 @@ describe Clearance::SessionsController do
19
19
  end
20
20
 
21
21
  it { should redirect_to(Clearance.configuration.redirect_url) }
22
- it { should_not set_the_flash }
22
+ it { should_not set_flash }
23
23
  end
24
24
  end
25
25
 
@@ -45,7 +45,7 @@ describe Clearance::SessionsController do
45
45
  it { should redirect_to_url_after_create }
46
46
 
47
47
  it "sets the user in the clearance session" do
48
- expect(controller.current_user).to eq @user
48
+ expect(request.env[:clearance].current_user).to eq @user
49
49
  end
50
50
 
51
51
  it "should not change the remember token" do
@@ -92,7 +92,7 @@ describe Clearance::SessionsController do
92
92
  end
93
93
 
94
94
  it "should unset the current user" do
95
- expect(@controller.current_user).to be_nil
95
+ expect(request.env[:clearance].current_user).to be_nil
96
96
  end
97
97
  end
98
98
  end
@@ -2,6 +2,10 @@ class ApplicationController < ActionController::Base
2
2
  include Clearance::Controller
3
3
 
4
4
  def show
5
- render text: '', layout: 'application'
5
+ if Rails::VERSION::MAJOR >= 5
6
+ render html: "", layout: "application"
7
+ else
8
+ render text: "", layout: "application"
9
+ end
6
10
  end
7
11
  end
@@ -37,6 +37,10 @@ module Dummy
37
37
  config.paths.add "config/routes", with: "#{APP_ROOT}/config/routes.rb"
38
38
  end
39
39
 
40
+ if config.respond_to?(:active_job)
41
+ config.active_job.queue_adapter = :inline
42
+ end
43
+
40
44
  def require_environment!
41
45
  initialize!
42
46
  end
@@ -61,9 +61,7 @@ describe Clearance::Generators::InstallGenerator, :generator do
61
61
  context "users table does not exist" do
62
62
  it "creates a migration to create the users table" do
63
63
  provide_existing_application_controller
64
- allow(ActiveRecord::Base.connection).to receive(:table_exists?).
65
- with(:users).
66
- and_return(false)
64
+ table_does_not_exist(:users)
67
65
 
68
66
  run_generator
69
67
  migration = migration_file("db/migrate/create_users.rb")
@@ -115,4 +113,18 @@ describe Clearance::Generators::InstallGenerator, :generator do
115
113
  end
116
114
  end
117
115
  end
116
+
117
+ def table_does_not_exist(name)
118
+ connection = ActiveRecord::Base.connection
119
+
120
+ if connection.respond_to?(:data_source_exists?)
121
+ allow(connection).to receive(:data_source_exists?).
122
+ with(name).
123
+ and_return(false)
124
+ else
125
+ allow(connection).to receive(:table_exists?).
126
+ with(name).
127
+ and_return(false)
128
+ end
129
+ end
118
130
  end
@@ -4,14 +4,18 @@ require "generators/clearance/routes/routes_generator"
4
4
  describe Clearance::Generators::RoutesGenerator, :generator do
5
5
  it "adds clearance routes to host application routes" do
6
6
  provide_existing_routes_file
7
+ provide_existing_initializer
7
8
 
8
9
  routes = file("config/routes.rb")
10
+ initializer = file("config/initializers/clearance.rb")
9
11
 
10
12
  run_generator
11
13
 
14
+ expect(initializer).to have_correct_syntax
15
+ expect(initializer).to contain("config.routes = false")
12
16
  expect(routes).to have_correct_syntax
13
17
  expect(routes).to contain(
14
- "get '/sign_in' => 'clearance/sessions#new', as: 'sign_in'"
18
+ 'get "/sign_in" => "clearance/sessions#new", as: "sign_in"'
15
19
  )
16
20
  end
17
21
  end
@@ -6,18 +6,19 @@ describe Clearance::Generators::ViewsGenerator, :generator do
6
6
  run_generator
7
7
 
8
8
  views = %w(
9
- clearance_mailer/change_password
10
- layouts/application
11
- passwords/create
12
- passwords/edit
13
- passwords/new
14
- sessions/_form
15
- sessions/new
16
- users/_form
17
- users/new
9
+ clearance_mailer/change_password.html.erb
10
+ clearance_mailer/change_password.text.erb
11
+ layouts/application.html.erb
12
+ passwords/create.html.erb
13
+ passwords/edit.html.erb
14
+ passwords/new.html.erb
15
+ sessions/_form.html.erb
16
+ sessions/new.html.erb
17
+ users/_form.html.erb
18
+ users/new.html.erb
18
19
  )
19
20
 
20
- view_files = views.map { |view| file("app/views/#{view}.html.erb") }
21
+ view_files = views.map { |view| file("app/views/#{view}") }
21
22
 
22
23
  view_files.each do |each|
23
24
  expect(each).to exist
@@ -0,0 +1,10 @@
1
+ require "spec_helper"
2
+
3
+ describe "Clearance RSpec helper spec configuration", type: :helper do
4
+ it "lets me use clearance's helper methods in helper specs" do
5
+ user = double("User")
6
+ sign_in_as(user)
7
+
8
+ expect(helper.current_user).to eq user
9
+ end
10
+ end