Agile Web Development with Rails, Edition 5

14.4 Iteration I4: Adding a Sidebar 14.2 Iteration I2: Authenticating Users

14.3 Iteration I3: Limiting Access

57 (tests|runs), 167 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:407: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
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
 
class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all
 
  # Add more helper methods to be used by all tests here...
end
class ActionDispatch::IntegrationTest
  def login_as(user)
    post login_url, params: { name: user.name, password: 'secret' }
  end
 
  def logout
    delete logout_url
  end
 
  def setup
    login_as users(:one)
  end
end

Show that the now pass

rails test
Run options: --seed 31182
 
# Running:
 
.........................E
 
Error:
ProductsControllerTest#test_should_create_product:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: commit transaction
    app/controllers/products_controller.rb:30:in `block in create'
    app/controllers/products_controller.rb:29:in `create'
    test/controllers/products_controller_test.rb:30:in `block (2 levels) in <class:ProductsControllerTest>'
    test/controllers/products_controller_test.rb:29:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:28
 
.....E
 
Error:
ProductsControllerTest#test_should_destroy_product:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: commit transaction
    app/controllers/products_controller.rb:62:in `destroy'
    test/controllers/products_controller_test.rb:65:in `block (2 levels) in <class:ProductsControllerTest>'
    test/controllers/products_controller_test.rb:64:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:63
 
....E
 
Error:
StoreControllerTest#test_markup_needed_for_store.js.coffee_is_in_place:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: commit transaction
    app/controllers/concerns/current_cart.rb:9:in `rescue in set_cart'
    app/controllers/concerns/current_cart.rb:7:in `set_cart'
    test/controllers/store_controller_test.rb:15:in `block in <class:StoreControllerTest>'
 
bin/rails test test/controllers/store_controller_test.rb:14
 
E
 
Error:
StoreControllerTest#test_should_get_index:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: commit transaction
    app/controllers/concerns/current_cart.rb:9:in `rescue in set_cart'
    app/controllers/concerns/current_cart.rb:7:in `set_cart'
    test/controllers/store_controller_test.rb:5:in `block in <class:StoreControllerTest>'
 
bin/rails test test/controllers/store_controller_test.rb:4
 
E
 
Error:
UsersControllerTest#test_should_create_user:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: commit transaction
    app/controllers/users_controller.rb:35:in `block in create'
    app/controllers/users_controller.rb:34:in `create'
    test/controllers/users_controller_test.rb:22:in `block (2 levels) in <class:UsersControllerTest>'
    test/controllers/users_controller_test.rb:20:in `block in <class:UsersControllerTest>'
 
bin/rails test test/controllers/users_controller_test.rb:19
 
.E
 
Error:
UsersControllerTest#test_should_update_user:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: commit transaction
    app/controllers/users_controller.rb:54:in `block in update'
    app/controllers/users_controller.rb:53:in `update'
    test/controllers/users_controller_test.rb:45:in `block in <class:UsersControllerTest>'
 
bin/rails test test/controllers/users_controller_test.rb:44
 
.E
 
Error:
UsersControllerTest#test_should_destroy_user:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: commit transaction
    app/controllers/users_controller.rb:71:in `destroy'
    test/controllers/users_controller_test.rb:55:in `block (2 levels) in <class:UsersControllerTest>'
    test/controllers/users_controller_test.rb:54:in `block in <class:UsersControllerTest>'
 
bin/rails test test/controllers/users_controller_test.rb:53
 
..E
 
Error:
DslUserStoriesTest#test_buying_a_product:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: INSERT INTO "carts" ("created_at", "updated_at") VALUES (?, ?)
    app/controllers/concerns/current_cart.rb:9:in `rescue in set_cart'
    app/controllers/concerns/current_cart.rb:7:in `set_cart'
    test/integration/dsl_user_stories_test.rb:78:in `buys_a'
    test/integration/dsl_user_stories_test.rb:43:in `block in test_buying_a_product'
    test/integration/dsl_user_stories_test.rb:39:in `test_buying_a_product'
 
bin/rails test test/integration/dsl_user_stories_test.rb:38
 
E
 
Error:
DslUserStoriesTest#test_two_people_buying:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: INSERT INTO "carts" ("created_at", "updated_at") VALUES (?, ?)
    app/controllers/concerns/current_cart.rb:9:in `rescue in set_cart'
    app/controllers/concerns/current_cart.rb:7:in `set_cart'
    test/integration/dsl_user_stories_test.rb:78:in `buys_a'
    test/integration/dsl_user_stories_test.rb:58:in `block in test_two_people_buying'
    test/integration/dsl_user_stories_test.rb:54:in `test_two_people_buying'
 
bin/rails test test/integration/dsl_user_stories_test.rb:53
 
E
 
Error:
UserStoriesTest#test_buying_a_product:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: DELETE FROM "line_items"
    test/integration/user_stories_test.rb:14:in `block in <class:UserStoriesTest>'
 
bin/rails test test/integration/user_stories_test.rb:12
 
.........
 
Finished in 54.372529s, 1.0483 runs/s, 1.8208 assertions/s.
 
57 runs, 99 assertions, 0 failures, 10 errors, 0 skips

14.4 Iteration I4: Adding a Sidebar 14.2 Iteration I2: Authenticating Users