14.4 Iteration I4: Adding a Sidebar 14.2 Iteration I2: Authenticating Users
56 (tests|runs), 170 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:411: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_index_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
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# ...
# 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
rake test
rake aborted!
ActiveRecord::PendingMigrationError: Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=test' to resolve this issue.
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:383:in `check_pending!'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/test_helper.rb:6:in `<class:TestCase>'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/test_helper.rb:5:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/helpers/admin_helper_test.rb:1:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `block (3 levels) in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `block (2 levels) in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:72:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:72:in `block in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:61:in `block in <top (required)>'
/home/rubys/.rvm/gems/ruby-2.2.5/gems/rake-11.2.1/exe/rake:27:in `<top (required)>'
/home/rubys/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `eval'
/home/rubys/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:run => test:units
(See full trace by running task with --trace)
Run options: --seed 26376
# Running tests:
Finished tests in 0.000868s, 0.0000 tests/s, 0.0000 assertions/s.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
14.4 Iteration I4: Adding a Sidebar 14.2 Iteration I2: Authenticating Users