Agile Web Development with Rails, Edition 5

15.1 Task J1: Selecting the locale 14.4 Iteration I4: Adding a Sidebar

14.5 Playtime

4[68] (tests|runs), [789]\d 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:425:in `block in <class:DepotTest>'

Verify that accessing product information requires login

edit test/controllers/products_controller_test.rb
  test "should require login" do
    logout
    get products_url
    follow_redirect!
    assert_select 'legend', 'Please Log In'
  end

Verify that the test passes

rails test:controllers
Run options: --seed 57037
 
# Running:
 
........................F
 
Failure:
ProductsControllerTest#test_should_create_product [/home/rubys/git/awdwr/edition4/work-223/depot/test/controllers/products_controller_test.rb:25]:
"Product.count" didn't change by 1.
Expected: 4
  Actual: 3
 
bin/rails test test/controllers/products_controller_test.rb:24
 
.....E
 
Error:
ProductsControllerTest#test_should_update_product:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: commit transaction
    app/controllers/products_controller.rb:47:in `block in update'
    app/controllers/products_controller.rb:46:in `update'
    test/controllers/products_controller_test.rb:43:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:42
 
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:57:in `block (2 levels) in <class:ProductsControllerTest>'
    test/controllers/products_controller_test.rb:56:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:55
 
.....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_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_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
 
F
 
Failure:
UsersControllerTest#test_should_destroy_user [/home/rubys/git/awdwr/edition4/work-223/depot/test/controllers/users_controller_test.rb:54]:
"User.count" didn't change by -1.
Expected: 1
  Actual: 2
 
bin/rails test test/controllers/users_controller_test.rb:53
 
.
 
Finished in 39.309195s, 1.1702 runs/s, 1.5772 assertions/s.
 
46 runs, 62 assertions, 2 failures, 6 errors, 0 skips

Look at the data in the database

sqlite3 db/development.sqlite3 .schema
CREATE TABLE "schema_migrations" ("version" varchar PRIMARY KEY);
CREATE TABLE "ar_internal_metadata" ("key" varchar PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar, "description" text, "image_url" varchar, "price" decimal(8,2), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
CREATE TABLE "carts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
CREATE TABLE "line_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "product_id" integer, "cart_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "quantity" integer DEFAULT 1, "price" decimal, "order_id" integer);
CREATE INDEX "index_line_items_on_product_id" ON "line_items" ("product_id");
CREATE INDEX "index_line_items_on_cart_id" ON "line_items" ("cart_id");
CREATE TABLE "orders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "address" text, "email" varchar, "pay_type" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "password_digest" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);

Try requesting the xml... see auth failure.

curl --max-time 15 --silent http://localhost:3000/products/2/who_bought.xml
<html><body>You are being <a href="http://localhost:3000/login">redirected</a>.</body></html>

Enable basic auth

edit app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :authorize
 
    # ...
 
  protected
 
    def authorize
      if request.format == Mime[:html]
        unless User.find_by(id: session[:user_id])
          redirect_to login_url, notice: "Please log in"
        end
      else
        authenticate_or_request_with_http_basic do |username, password|
          user = User.find_by(name: username)
          user && user.authenticate(password)
        end
      end
    end

Try requesting the xml... see auth succeed.

curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought.xml
<order_list for_product="CoffeeScript">
  <order>
    <name>Dave Thomas</name>
    <email>customer@example.com</email>
  </order>
  <order>
    <name>Dave Thomas</name>
    <email>customer@example.com</email>
  </order>
</order_list>

15.1 Task J1: Selecting the locale 14.4 Iteration I4: Adding a Sidebar