From d1443c62a06ef07ba0fa26f7009eeab1d30d58b8 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 14 Sep 2009 13:00:57 +0000 Subject: [PATCH] Functional tests now work properly, bearing in mind whether a user is logged in or not. --- test/fixtures/line_items.yml | 2 +- test/fixtures/orders.yml | 16 ++--- test/fixtures/products.yml | 4 +- test/functional/line_items_controller_test.rb | 64 +++++++++++++++-- test/functional/orders_controller_test.rb | 66 +++++++++++++++-- test/functional/products_controller_test.rb | 63 ++++++++++++++-- test/functional/users_controller_test.rb | 71 ++++++++++++++++--- 7 files changed, 246 insertions(+), 40 deletions(-) diff --git a/test/fixtures/line_items.yml b/test/fixtures/line_items.yml index ab7fc85..0e01b73 100644 --- a/test/fixtures/line_items.yml +++ b/test/fixtures/line_items.yml @@ -10,4 +10,4 @@ two: product_id: 1 order_id: 1 quantity: 1 - total_price: 9.99 + total_price: 19.99 diff --git a/test/fixtures/orders.yml b/test/fixtures/orders.yml index e4a667a..d1dd8d3 100644 --- a/test/fixtures/orders.yml +++ b/test/fixtures/orders.yml @@ -1,13 +1,13 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html one: - name: MyString - address: MyText - email: MyString - pay_type: MyString + name: First Order Person + address: First Address + email: first@email.com + pay_type: cc two: - name: MyString - address: MyText - email: MyString - pay_type: MyString + name: Second Order Person + address: Second address + email: second@email.com + pay_type: po diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml index 0afc479..ad988f4 100644 --- a/test/fixtures/products.yml +++ b/test/fixtures/products.yml @@ -5,10 +5,12 @@ ruby_book: description: Dummy description price: 1234 image_url: ruby.png + date_available: 2009-01-01 rails_book: title: Agile Web Development with Rails description: Dummy description price: 2345 image_url: rails.png - \ No newline at end of file + date_available: 2009-01-01 + diff --git a/test/functional/line_items_controller_test.rb b/test/functional/line_items_controller_test.rb index 305a261..fca3d53 100644 --- a/test/functional/line_items_controller_test.rb +++ b/test/functional/line_items_controller_test.rb @@ -1,45 +1,95 @@ require 'test_helper' class LineItemsControllerTest < ActionController::TestCase + + fixtures :orders, :products, :line_items, :users + test "should get index" do - get :index + get :index, {}, { :user_id => users(:dave).id } assert_response :success assert_not_nil assigns(:line_items) end test "should get new" do - get :new + get :new, {}, { :user_id => users(:dave).id } assert_response :success end test "should create line_item" do assert_difference('LineItem.count') do - post :create, :line_item => { } + post :create, {:line_item => { :order_id => 1, :product_id => 1, :quantity => 5, :total_price => 9.99 }}, + { :user_id => users(:dave).id } end assert_redirected_to line_item_path(assigns(:line_item)) end test "should show line_item" do - get :show, :id => line_items(:one).id + get :show, {:id => line_items(:one).id}, { :user_id => users(:dave).id } assert_response :success end test "should get edit" do - get :edit, :id => line_items(:one).id + get :edit, {:id => line_items(:one).id}, { :user_id => users(:dave).id } assert_response :success end test "should update line_item" do - put :update, :id => line_items(:one).id, :line_item => { } + put :update, {:id => line_items(:one).id, :line_item => { } }, { :user_id => users(:dave).id } assert_redirected_to line_item_path(assigns(:line_item)) end test "should destroy line_item" do assert_difference('LineItem.count', -1) do - delete :destroy, :id => line_items(:one).id + delete :destroy, {:id => line_items(:one).id}, { :user_id => users(:dave).id } end assert_redirected_to line_items_path end + + + + + test "index without user" do + get :index + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "new without user" do + get :new + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "create line_item without user" do + post :create, :line_item => { :order_id => 1, :product_id => 1, :quantity => 5, :total_price => 9.99 } + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "show line_item without user" do + get :show, :id => line_items(:one).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "get edit without user" do + get :edit, :id => line_items(:one).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "update line_item without user" do + put :update, :id => line_items(:one).id, :line_item => { } + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "destroy line_item without user" do + delete :destroy, :id => line_items(:one).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + end diff --git a/test/functional/orders_controller_test.rb b/test/functional/orders_controller_test.rb index ec6232f..1addde4 100644 --- a/test/functional/orders_controller_test.rb +++ b/test/functional/orders_controller_test.rb @@ -1,45 +1,97 @@ require 'test_helper' class OrdersControllerTest < ActionController::TestCase + + fixtures :orders, :users + test "should get index" do - get :index + get :index, {}, { :user_id => users(:dave).id } assert_response :success assert_not_nil assigns(:orders) end test "should get new" do - get :new + get :new, {}, { :user_id => users(:dave).id } assert_response :success end test "should create order" do assert_difference('Order.count') do - post :create, :order => { } + post :create, {:order => { :name => "a name", :address => "an address", + :email => "name@nowhere.com", + :pay_type => "po"}}, + { :user_id => users(:dave).id } end assert_redirected_to order_path(assigns(:order)) end test "should show order" do - get :show, :id => orders(:one).id + get :show, {:id => orders(:one).id}, { :user_id => users(:dave).id } assert_response :success end test "should get edit" do - get :edit, :id => orders(:one).id + get :edit, {:id => orders(:one).id}, { :user_id => users(:dave).id } assert_response :success end test "should update order" do - put :update, :id => orders(:one).id, :order => { } + put :update, {:id => orders(:one).id, :order => { }}, { :user_id => users(:dave).id } assert_redirected_to order_path(assigns(:order)) end test "should destroy order" do assert_difference('Order.count', -1) do - delete :destroy, :id => orders(:one).id + delete :destroy, {:id => orders(:one).id}, { :user_id => users(:dave).id } end assert_redirected_to orders_path end + + + + test "get index without user" do + get :index + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "get new without user" do + get :new + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "create order without user" do + post :create, :order => { } + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "show order without user" do + get :show, :id => orders(:one).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "get edit without user" do + get :edit, :id => orders(:one).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "update order without user" do + put :update, :id => orders(:one).id, :order => { } + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "destroy order without user" do + delete :destroy, :id => orders(:one).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + end diff --git a/test/functional/products_controller_test.rb b/test/functional/products_controller_test.rb index 935e491..0329bd1 100644 --- a/test/functional/products_controller_test.rb +++ b/test/functional/products_controller_test.rb @@ -1,45 +1,94 @@ require 'test_helper' class ProductsControllerTest < ActionController::TestCase + + fixtures :products, :users + test "should get index" do - get :index + get :index, {}, { :user_id => users(:dave).id } assert_response :success assert_not_nil assigns(:products) end test "should get new" do - get :new + get :new, {}, { :user_id => users(:dave).id } assert_response :success end test "should create product" do assert_difference('Product.count') do - post :create, :product => { } + post :create, {:product => {:title => "test product", :description => "test description", + :image_url => "/images/test.jpg", :price => 9.99, :date_available => Time.now}}, + { :user_id => users(:dave).id } end assert_redirected_to product_path(assigns(:product)) end test "should show product" do - get :show, :id => products(:one).id + get :show, {:id => products(:ruby_book).id}, { :user_id => users(:dave).id } assert_response :success end test "should get edit" do - get :edit, :id => products(:one).id + get :edit, {:id => products(:ruby_book).id}, { :user_id => users(:dave).id } assert_response :success end test "should update product" do - put :update, :id => products(:one).id, :product => { } + put :update, {:id => products(:ruby_book).id, :product => { }}, { :user_id => users(:dave).id } assert_redirected_to product_path(assigns(:product)) end test "should destroy product" do assert_difference('Product.count', -1) do - delete :destroy, :id => products(:one).id + delete :destroy, {:id => products(:ruby_book).id}, { :user_id => users(:dave).id } end assert_redirected_to products_path end + + + test "get index without user" do + get :index + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "get new without user" do + get :new + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "create product without user" do + post :create, :product => { } + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "show product without user" do + get :show, :id => products(:ruby_book).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "get edit without user" do + get :edit, :id => products(:ruby_book).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "update product without user" do + put :update, :id => products(:ruby_book).id, :product => { } + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "destroy product without user" do + delete :destroy, :id => products(:ruby_book).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + end diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb index c4a4804..a966d45 100644 --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -1,45 +1,98 @@ require 'test_helper' class UsersControllerTest < ActionController::TestCase + + fixtures :users + + test "should get index" do - get :index + get :index, {}, { :user_id => users(:dave).id } assert_response :success assert_not_nil assigns(:users) end test "should get new" do - get :new + get :new, {}, { :user_id => users(:dave).id } assert_response :success end test "should create user" do assert_difference('User.count') do - post :create, :user => { } + post :create, {:user => {:name => "noone", :password => "password" }}, { :user_id => users(:dave).id } end - assert_redirected_to user_path(assigns(:user)) + # redirect to index, not user show page + assert_redirected_to users_path end test "should show user" do - get :show, :id => users(:one).id + get :show, {:id => users(:dave).id}, { :user_id => users(:dave).id } assert_response :success end test "should get edit" do - get :edit, :id => users(:one).id + get :edit, {:id => users(:dave).id}, { :user_id => users(:dave).id } assert_response :success end test "should update user" do - put :update, :id => users(:one).id, :user => { } - assert_redirected_to user_path(assigns(:user)) + put :update, {:id => users(:dave).id, :user => { }}, { :user_id => users(:dave).id } + # redirect to index, not user show page + assert_redirected_to users_path end test "should destroy user" do assert_difference('User.count', -1) do - delete :destroy, :id => users(:one).id + delete :destroy, {:id => users(:dave).id}, { :user_id => users(:dave).id } end assert_redirected_to users_path end + + + + + test "get index without logged-in user" do + get :index + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "get new without logged-in user" do + get :new + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "create user without logged-in user" do + post :create, :user => { } + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "show user without logged-in user" do + get :show, :id => users(:dave).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "get edit without logged-in user" do + get :edit, :id => users(:dave).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "update user without logged-in user" do + put :update, :id => users(:dave).id, :user => { } + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + test "destroy user without logged-in user" do + delete :destroy, :id => users(:dave).id + assert_redirected_to :controller => "admin", :action => "login" + assert_equal "Please log in", flash[:notice] + end + + end -- 2.34.1