From 686e24825dbccaf22692bc0bb6e9d34d0e6cd280 Mon Sep 17 00:00:00 2001
From: "Butler, Alexis (UG - Computer Science)" <ab02259@surrey.ac.uk>
Date: Sat, 28 Nov 2020 10:53:51 +0000
Subject: [PATCH] Wrote cart controller tests and added signout test helper

---
 test/controllers/carts_controller_test.rb | 65 +++++++++++++++++++----
 test/test_helper.rb                       |  3 ++
 2 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/test/controllers/carts_controller_test.rb b/test/controllers/carts_controller_test.rb
index 43318c2..89387a8 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 f637ba4..c2e4ac9 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
-- 
GitLab