15.1 Task J1: Selecting the locale 14.4 Iteration I4: Adding a Sidebar
Verify that accessing product information requires login
edit test/functional/products_controller_test.rb
test "should require login" do
logout
get :index
assert_redirected_to login_path
end
Verify that the test passes
rake test:functionals
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
AdminControllerTest:
PASS should get index (0.36s)
CartsControllerTest:
PASS should create cart (0.01s)
PASS should destroy cart (0.05s)
PASS should get edit (0.05s)
PASS should get index (0.01s)
PASS should get new (0.01s)
PASS should show cart (0.01s)
PASS should update cart (0.01s)
LineItemsControllerTest:
PASS should create line item (0.01s)
PASS should create line item via ajax (0.10s)
PASS should destroy line item (0.01s)
PASS should get edit (0.01s)
PASS should get index (0.01s)
PASS should get new (0.01s)
PASS should show line item (0.01s)
PASS should update line item (0.01s)
OrderNotifierTest:
PASS received (0.09s)
PASS shipped (0.06s)
OrdersControllerTest:
PASS requires item in cart (0.01s)
PASS should create order (0.02s)
PASS should destroy order (0.04s)
PASS should get edit (0.01s)
PASS should get index (0.01s)
PASS should get new (0.01s)
PASS should show order (0.01s)
PASS should update order (0.01s)
ProductsControllerTest:
PASS can't delete product in cart (0.01s)
PASS should create product (0.02s)
PASS should destroy product (0.04s)
PASS should get edit (0.01s)
PASS should get index (0.01s)
PASS should get new (0.01s)
PASS should require login (0.00s)
PASS should show product (0.01s)
PASS should update product (0.01s)
SessionsControllerTest:
PASS should fail login (0.09s)
PASS should get new (0.01s)
PASS should login (0.08s)
PASS should logout (0.00s)
StoreControllerTest:
PASS markup needed for store.js.coffee is in place (0.02s)
PASS should get index (0.01s)
UsersControllerTest:
PASS should create user (0.09s)
PASS should destroy user (0.01s)
PASS should get edit (0.01s)
PASS should get index (0.01s)
PASS should get new (0.01s)
PASS should show user (0.04s)
PASS should update user (0.09s)
Finished in 1.500445 seconds.
48 tests, 79 assertions, 0 failures, 0 errors, 0 skips
Look at the data in the database
sqlite3 db/development.sqlite3 .schema
CREATE TABLE "carts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime, "updated_at" datetime);
CREATE TABLE "line_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "product_id" integer, "cart_id" integer, "created_at" datetime, "updated_at" datetime, "quantity" integer DEFAULT 1, "price" decimal, "order_id" integer);
CREATE TABLE "orders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "address" text, "email" varchar(255), "pay_type" varchar(255), "created_at" datetime, "updated_at" datetime);
CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "description" text, "image_url" varchar(255), "price" decimal(8,2), "created_at" datetime, "updated_at" datetime);
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "password_digest" varchar(255), "created_at" datetime, "updated_at" datetime);
CREATE INDEX "index_line_items_on_cart_id" ON "line_items" ("cart_id");
CREATE INDEX "index_line_items_on_product_id" ON "line_items" ("product_id");
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
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_filter :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_list>
15.1 Task J1: Selecting the locale 14.4 Iteration I4: Adding a Sidebar