14.1 Playtime 13.4 Iteration H4: Adding a Sidebar
See that requiring a login causes most tests to fail (good!)
rake test
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
(in /home/rubys/svn/rails4/Book/util/work-193/depot)
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
..........
Finished in 0.238094 seconds.
10 tests, 31 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 41363
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
F.FFFFF...FFFFFF....FFF.FFFFFFFFFFFF..FFFFFFF
Finished in 1.094821 seconds.
1) Failure:
test_should_get_index(AdminControllerTest) [test/functional/admin_controller_test.rb:6]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
2) Failure:
test_should_destroy_cart(CartsControllerTest) [test/functional/carts_controller_test.rb:44]:
"Cart.count" didn't change by -1.
<1> expected but was
<2>.
3) Failure:
test_should_get_edit(CartsControllerTest) [test/functional/carts_controller_test.rb:34]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
4) Failure:
test_should_get_index(CartsControllerTest) [test/functional/carts_controller_test.rb:10]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
5) Failure:
test_should_get_new(CartsControllerTest) [test/functional/carts_controller_test.rb:16]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
6) Failure:
test_should_show_cart(CartsControllerTest) [test/functional/carts_controller_test.rb:29]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
7) Failure:
test_should_destroy_line_item(LineItemsControllerTest) [test/functional/line_items_controller_test.rb:46]:
"LineItem.count" didn't change by -1.
<1> expected but was
<2>.
8) Failure:
test_should_get_edit(LineItemsControllerTest) [test/functional/line_items_controller_test.rb:37]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
9) Failure:
test_should_get_index(LineItemsControllerTest) [test/functional/line_items_controller_test.rb:10]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
10) Failure:
test_should_get_new(LineItemsControllerTest) [test/functional/line_items_controller_test.rb:16]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
11) Failure:
test_should_show_line_item(LineItemsControllerTest) [test/functional/line_items_controller_test.rb:32]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
12) Failure:
test_should_update_line_item(LineItemsControllerTest) [test/functional/line_items_controller_test.rb:42]:
Expected response to be a redirect to <http://test.host/line_items/980190962> but was a redirect to <http://test.host/login>
13) Failure:
test_should_destroy_order(OrdersControllerTest) [test/functional/orders_controller_test.rb:66]:
"Order.count" didn't change by -1.
<1> expected but was
<2>.
14) Failure:
test_should_get_edit(OrdersControllerTest) [test/functional/orders_controller_test.rb:57]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
15) Failure:
test_should_get_index(OrdersControllerTest) [test/functional/orders_controller_test.rb:13]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
16) Failure:
test_should_show_order(OrdersControllerTest) [test/functional/orders_controller_test.rb:52]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
17) Failure:
test_should_update_order(OrdersControllerTest) [test/functional/orders_controller_test.rb:62]:
Expected response to be a redirect to <http://test.host/orders/980190962> but was a redirect to <http://test.host/login>
18) Failure:
test_can't_delete_product_in_cart(ProductsControllerTest) [test/functional/products_controller_test.rb:62]:
Expected response to be a redirect to <http://test.host/products> but was a redirect to <http://test.host/login>
19) Failure:
test_should_create_product(ProductsControllerTest) [test/functional/products_controller_test.rb:31]:
"Product.count" didn't change by 1.
<4> expected but was
<3>.
20) Failure:
test_should_destroy_product(ProductsControllerTest) [test/functional/products_controller_test.rb:66]:
"Product.count" didn't change by -1.
<2> expected but was
<3>.
21) Failure:
test_should_get_edit(ProductsControllerTest) [test/functional/products_controller_test.rb:46]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
22) Failure:
test_should_get_index(ProductsControllerTest) [test/functional/products_controller_test.rb:20]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
23) Failure:
test_should_get_new(ProductsControllerTest) [test/functional/products_controller_test.rb:26]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
24) Failure:
test_should_show_product(ProductsControllerTest) [test/functional/products_controller_test.rb:41]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
25) Failure:
test_should_update_product(ProductsControllerTest) [test/functional/products_controller_test.rb:52]:
Expected response to be a redirect to <http://test.host/products/980190962> but was a redirect to <http://test.host/login>
26) Failure:
test_should_get_create(SessionsControllerTest) [test/functional/sessions_controller_test.rb:11]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
27) Failure:
test_should_get_destroy(SessionsControllerTest) [test/functional/sessions_controller_test.rb:16]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
28) Failure:
test_should_create_user(UsersControllerTest) [test/functional/users_controller_test.rb:20]:
"User.count" didn't change by 1.
<3> expected but was
<2>.
29) Failure:
test_should_destroy_user(UsersControllerTest) [test/functional/users_controller_test.rb:43]:
"User.count" didn't change by -1.
<1> expected but was
<2>.
30) Failure:
test_should_get_edit(UsersControllerTest) [test/functional/users_controller_test.rb:34]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
31) Failure:
test_should_get_index(UsersControllerTest) [test/functional/users_controller_test.rb:10]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
32) Failure:
test_should_get_new(UsersControllerTest) [test/functional/users_controller_test.rb:16]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
33) Failure:
test_should_show_user(UsersControllerTest) [test/functional/users_controller_test.rb:29]:
Expected response to be a <:success>, but was <302>.
Expected block to return true value.
34) Failure:
test_should_update_user(UsersControllerTest) [test/functional/users_controller_test.rb:39]:
Expected response to be a redirect to <http://test.host/users/980190962> but was a redirect to <http://test.host/login>
45 tests, 67 assertions, 34 failures, 0 errors, 0 skips
Test run options: --seed 41069
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
...
Finished in 1.017311 seconds.
3 tests, 47 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 55620
Errors running test:functionals!
Cause all tests to do an implicit login
edit test/test_helper.rb
class ActiveSupport::TestCase
# ...
# 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
First, lets fix the sessions controller test
edit test/functional/sessions_controller_test.rb
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
test "should login" do
dave = users(:one)
post :create, :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 :create, :name => dave.name, :password => 'wrong'
assert_redirected_to login_url
end
test "should logout" do
delete :destroy
assert_redirected_to store_url
end
end
edit test/fixtures/users.yml
<% SALT = "NaCl" unless defined?(SALT) %>
one:
name: dave
hashed_password: <%= User.encrypt_password('secret', SALT) %>
salt: <%= SALT %>
two:
name: MyString
hashed_password: MyString
salt: MyString
ruby -I test test/functional/sessions_controller_test.rb
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
Loaded suite test/functional/sessions_controller_test
Started
....
Finished in 0.158994 seconds.
4 tests, 5 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 30190
edit test/functional/users_controller_test.rb
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
setup do
@input_attributes = {
"name" => "sam",
"password" => "private",
"password_confirmation" => "private"
}
@user = users(:one)
end
#...
test "should create user" do
assert_difference('User.count') do
post :create, :user => @input_attributes
end
assert_redirected_to users_path
end
#...
test "should update user" do
put :update, :id => @user.to_param, :user => @input_attributes
assert_redirected_to users_path
end
end
rake test
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
(in /home/rubys/svn/rails4/Book/util/work-193/depot)
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
..........
Finished in 0.237096 seconds.
10 tests, 31 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 4656
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
..............................................
Finished in 1.488952 seconds.
46 tests, 76 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 13549
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
...
Finished in 1.023122 seconds.
3 tests, 47 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 23878
Try requesting the xml... see auth failure.
curl --silent http://localhost:3000/products/3/who_bought.xml
<html><body>You are being <a href="http://localhost:3000/login">redirected</a>.</body></html>
Is this the best way to detect request format?
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.authenticate(username, password)
end
end
end
Try requesting the xml... see auth succeed.
curl --silent --user dave:secret http://localhost:3000/products/3/who_bought.xml
<order_list for_product="Programming Ruby 1.9">
<order>
<name>Dave Thomas</name>
<email>customer@example.com</email>
</order>
</order_list>