diff --git a/test/controllers/carts_controller_test.rb b/test/controllers/carts_controller_test.rb index 43318c250fe12a8551d2a9043a4dfb3b03e6897e..89387a8f2086ba58445ebb3e7be2f6e43402b95e 100644 --- a/test/controllers/carts_controller_test.rb +++ b/test/controllers/carts_controller_test.rb @@ -1,16 +1,63 @@ require 'test_helper' -#TODO: Write controller tests for cart class CartsControllerTest < ActionDispatch::IntegrationTest - test "should add item to cart" do - + test 'should add item to cart if logged in' do + sign_in_as_tester + put add_item_carts_url(product_id: Product.first.id) + assert_response(:redirect) + assert_equal 'Item added to cart', flash[:success] end - - test "should remove item from cart" do - + test 'should not item to cart if not logged in' do + put add_item_carts_url(product_id: Product.first.id) + assert_response(:redirect) + assert_redirected_to(login_url) + assert_equal 'Login to view this content', flash[:danger] end - - test "should show the cart" do - + test 'should remove item from cart if logged in' do + sign_in_as_tester + put add_item_carts_url(product_id: Product.first.id) + delete remove_item_carts_url(product_id: Product.first.id) + assert_response(:redirect) + assert_equal 'Item removed from cart', flash[:success] end + test 'should not remove item from cart if not logged in' do + delete remove_item_carts_url(product_id: Product.first.id) + assert_response(:redirect) + assert_redirected_to(login_url) + assert_equal 'Login to view this content', flash[:danger] + end + test 'should show the cart if logged in and items present' do + sign_in_as_tester + put add_item_carts_url(product_id: Product.first.id) + put add_item_carts_url(product_id: Product.second.id) + get carts_url + assert_response(:success) + assert_select 'body.carts' do + assert_select 'img', 2 #Product Image for each product + end + end + test 'should not show the cart if not logged in' do + sign_in_as_tester + put add_item_carts_url(product_id: Product.first.id) + put add_item_carts_url(product_id: Product.second.id) + signout + get carts_url + assert_response(:redirect) + assert_redirected_to(login_url) + assert_equal 'Login to view this content', flash[:danger] + end + test 'should indicate the cart is empty if logged in' do + sign_in_as_tester + get carts_url + assert_response(:success) + assert_select 'body.carts' do + assert_select 'h1.alert', 'your cart is empty' ,1 #say that it's empty + end + end + test 'should not indicate the cart is empty if not logged in' do + get carts_url + assert_response(:redirect) + assert_redirected_to(login_url) + assert_equal 'Login to view this content', flash[:danger] + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index f637ba4370bcf8d320cbfe14c2fb36f9f0ba1baa..c2e4ac9b8eae9fa6f9e9cb28e5874c8fdda862e2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,4 +12,7 @@ class ActiveSupport::TestCase def sign_in_as_admin post login_url, params: {email: users(:admin).email, password: "superSecret"} end + def signout + get logout_url + end end