14.4 Iteration I4: Adding a Sidebar 14.2 Iteration I2: Authenticating Users
57 (tests|runs), 172 assertions, 0 failures, 0 errors. <0> expected to be >= <1>. Traceback: /home/rubys/git/awdwr/edition4/checkdepot.rb:38:in `assert_test_summary' /home/rubys/git/awdwr/edition4/checkdepot.rb:348:in `block in <class:DepotTest>'
require authorization before every access
edit app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :authorize
# ...
protected
def authorize
unless User.find_by(id: session[:user_id])
redirect_to login_url, notice: "Please log in"
end
end
end
whitelist the sessions and store controllers
edit app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
skip_before_action :authorize
edit app/controllers/store_controller.rb
class StoreController < ApplicationController
skip_before_action :authorize
whitelist cart operations
edit app/controllers/carts_controller.rb
class CartsController < ApplicationController
skip_before_action :authorize, only: [:create, :update, :destroy]
# ...
private
# ...
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid cart'
end
end
whitelist line_item operations
edit app/controllers/line_items_controller.rb
class LineItemsController < ApplicationController
skip_before_action :authorize, only: :create
whitelist order operations
edit app/controllers/orders_controller.rb
class OrdersController < ApplicationController
skip_before_action :authorize, only: [:new, :create]
Cause all tests to do an implicit login
edit test/test_helper.rb
class ActiveSupport::TestCase
# ...
# Add more helper methods to be used by all tests here...
def login_as(user)
session[:user_id] = users(user).id
end
def logout
session.delete :user_id
end
def setup
login_as :one if defined? session
end
end
Show that the now pass
rails test
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
rails aborted!
ActiveRecord::NoEnvironmentInSchemaError:
Environment data not found in the schema. To resolve this issue, run:
bin/rails db:environment:set RAILS_ENV=test
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:1259:in `last_stored_environment'
/home/rubys/git/rails/activerecord/lib/active_record/tasks/database_tasks.rb:48:in `check_protected_environments!'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:11:in `block (2 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:375:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
/home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:test:load => db:test:purge => db:check_protected_environments
(See full trace by running task with --trace)
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:578:in `check_pending!': (ActiveRecord::PendingMigrationError)
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:591:in `load_schema_if_pending!'
from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `block in maintain_test_schema!'
from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:828:in `suppress_messages'
from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:602:in `method_missing'
from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `maintain_test_schema!'
from /home/rubys/git/rails/railties/lib/rails/test_help.rb:15:in `<top (required)>'
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `block in require'
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
from /home/rubys/git/awdwr/edition4/work-230/depot/test/test_helper.rb:3:in `<top (required)>'
from /home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/admin_controller_test.rb:1:in `require'
from /home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/admin_controller_test.rb:1:in `<top (required)>'
from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `require'
from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `each'
from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
from /home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:75:in `plugin_rails_init'
from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:74:in `block in init_plugins'
from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `each'
from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `init_plugins'
from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:123:in `run'
from /home/rubys/git/rails/railties/lib/rails/commands/test.rb:9:in `<top (required)>'
from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:138:in `require'
from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:138:in `require_command!'
from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:95:in `test'
from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
from /home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
14.4 Iteration I4: Adding a Sidebar 14.2 Iteration I2: Authenticating Users