Agile Web Development with Rails, Edition 5

14.3 Iteration I3: Limiting Access 14.1 Iteration I1: Adding Users

14.2 Iteration I2: Authenticating Users

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:392:in `block in <class:DepotTest>'

Generate empty controllers for sessions and administration

rails generate controller Sessions new create destroy
      create  app/controllers/sessions_controller.rb
       route  get 'sessions/destroy'
       route  get 'sessions/create'
       route  get 'sessions/new'
      invoke  erb
      create    app/views/sessions
      create    app/views/sessions/new.html.erb
      create    app/views/sessions/create.html.erb
      create    app/views/sessions/destroy.html.erb
      invoke  test_unit
      create    test/controllers/sessions_controller_test.rb
      invoke  helper
      create    app/helpers/sessions_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/sessions.coffee
      invoke    scss
      create      app/assets/stylesheets/sessions.scss
rails generate controller Admin index
      create  app/controllers/admin_controller.rb
       route  get 'admin/index'
      invoke  erb
      create    app/views/admin
      create    app/views/admin/index.html.erb
      invoke  test_unit
      create    test/controllers/admin_controller_test.rb
      invoke  helper
      create    app/helpers/admin_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/admin.coffee
      invoke    scss
      create      app/assets/stylesheets/admin.scss

Implement login in and out by storing the user_id in the session

edit app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
  def new
  end
 
  def create
    user = User.find_by(name: params[:name])
    if user and user.authenticate(params[:password])
      session[:user_id] = user.id
      redirect_to admin_url
    else
      redirect_to login_url, alert: "Invalid user/password combination"
    end
  end
 
  def destroy
    session[:user_id] = nil
    redirect_to store_url, notice: "Logged out"
  end
end

Create the view using form_for as there is no underlying model

edit app/views/sessions/new.html.erb
<div class="depot_form">
  <% if flash[:alert] %>
    <p id="notice"><%= flash[:alert] %></p>
  <% end %>
 
  <%= form_tag do %>
    <fieldset>
      <legend>Please Log In</legend>
 
      <div>
        <%= label_tag :name, 'Name:' %>
        <%= text_field_tag :name, params[:name] %>
      </div>
 
      <div>
        <%= label_tag :password, 'Password:' %>
        <%= password_field_tag :password, params[:password] %>
      </div>
  
      <div>
        <%= submit_tag "Login" %>
      </div>
    </fieldset>
  <% end %>
</div>

Create a landing page for the administrator

edit app/views/admin/index.html.erb
<h1>Welcome</h1>
 
It's <%= Time.now %>
We have <%= pluralize(@total_orders, "order") %>.

Make the orders count available to the admin page

edit app/controllers/admin_controller.rb
class AdminController < ApplicationController
  def index
    @total_orders = Order.count
  end
end

Connect the routes to the controller actions

edit config/routes.rb

Do a login

get /login
Please Log In
post /login
You are being redirected.
get http://localhost:3000/admin

Welcome

It's 2016-03-07 09:43:40 -0500 We have 2 orders.
edit test/controllers/admin_controller_test.rb
require 'test_helper'
 
class AdminControllerTest < ActionDispatch::IntegrationTest
  test "should get index" do
    get admin_url
    assert_response :success
  end
 
end

Fix the sessions controller test

edit test/controllers/sessions_controller_test.rb
require 'test_helper'
 
class SessionsControllerTest < ActionDispatch::IntegrationTest
  test "should prompt for login" do
    get login_url
    assert_response :success
  end
 
  test "should login" do
    dave = users(:one)
    post login_url, params: { name: dave.name, password: 'secret' }
    assert_redirected_to admin_url
    assert_equal dave.id, session[:user_id]
  end
 
  test "should fail login" do
    dave = users(:one)
    post login_url, params: { name: dave.name, password: 'wrong' }
    assert_redirected_to login_url
  end
 
  test "should logout" do
    delete logout_url
    assert_redirected_to store_url
  end
 
end
rails test
Run options: --seed 59292
 
# Running:
 
........................F
 
Failure:
ProductsControllerTest#test_should_destroy_product [/home/rubys/git/awdwr/edition4/work-223/depot/test/controllers/products_controller_test.rb:64]:
"Product.count" didn't change by -1.
Expected: 2
  Actual: 3
 
bin/rails test test/controllers/products_controller_test.rb:63
 
...F
 
Failure:
ProductsControllerTest#test_should_create_product [/home/rubys/git/awdwr/edition4/work-223/depot/test/controllers/products_controller_test.rb:29]:
"Product.count" didn't change by 1.
Expected: 4
  Actual: 3
 
bin/rails test test/controllers/products_controller_test.rb:28
 
.........F
 
Failure:
UsersControllerTest#test_should_create_user [/home/rubys/git/awdwr/edition4/work-223/depot/test/controllers/users_controller_test.rb:20]:
"User.count" didn't change by 1.
Expected: 3
  Actual: 2
 
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
 
....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: 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/user_stories_test.rb:26:in `block in <class:UserStoriesTest>'
 
bin/rails test test/integration/user_stories_test.rb:12
 
.........
 
Finished in 16.693336s, 3.4145 runs/s, 6.8291 assertions/s.
 
57 runs, 114 assertions, 4 failures, 3 errors, 0 skips

14.3 Iteration I3: Limiting Access 14.1 Iteration I1: Adding Users