From 6a78edd85966d875d4a922c7f95c236e005488ae Mon Sep 17 00:00:00 2001 From: VirtualLindsey Date: Wed, 25 Oct 2017 20:53:32 -0700 Subject: [PATCH] updating tests for product controller --- app/controllers/products_controller.rb | 25 ++++++++++++----- app/helpers/application_helper.rb | 2 +- app/jobs/helpers/application_helper.rb | 28 ++++++++++++++++++++ app/jobs/helpers/categories_helper.rb | 2 ++ app/jobs/helpers/main_helper.rb | 2 ++ app/jobs/helpers/merchants_helper.rb | 2 ++ app/jobs/helpers/order_items_helper.rb | 2 ++ app/jobs/helpers/orders_helper.rb | 2 ++ app/jobs/helpers/products_helper.rb | 2 ++ app/jobs/helpers/reviews_helper.rb | 2 ++ test/controllers/products_controller_test.rb | 28 ++++++++++++++++---- test/fixtures/products.yml | 13 ++++++++- test/test_helper.rb | 4 ++- 13 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 app/jobs/helpers/application_helper.rb create mode 100644 app/jobs/helpers/categories_helper.rb create mode 100644 app/jobs/helpers/main_helper.rb create mode 100644 app/jobs/helpers/merchants_helper.rb create mode 100644 app/jobs/helpers/order_items_helper.rb create mode 100644 app/jobs/helpers/orders_helper.rb create mode 100644 app/jobs/helpers/products_helper.rb create mode 100644 app/jobs/helpers/reviews_helper.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 646fd638f8..a584d97e42 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -57,28 +57,39 @@ def update def index + @products = Product.all.where("stock > 0") + end +=begin + None of these should ever be hit because the index + page doesn't take parameters. + Plus the logic of showing products of a specific + category or merchant is handled elsewhere. if params[:merchant_id] unless allowed_user(params[:merchant_id]) + raise return end end if params[:merchant_id] && params[:category_id] - @merchant = Merchant.find_by(id: params[:merchant_id]) - @category = Category.find_by(id: params[:category_id]) - @products = @category.products.where(merchant: @merchant) + raise + @merchant = Merchant.find_by(id: params[:merchant_id]) + @category = Category.find_by(id: params[:category_id]) + @products = @category.products.where(merchant: @merchant) elsif params[:category_id] - category = Category.find_by(id: params[:category_id]) - @products = category.products + raise + category = Category.find_by(id: params[:category_id]) + @products = category.products elsif params[:merchant_id] + raise @merchant = Merchant.find_by(id: params[:merchant_id]) @products = @merchant.products render :merchant_products else - @products = Product.all + end - end +=end def index_by_merchant @merchant = Merchant.find_by(id: params[:id]) @products = @merchant.products diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e1fa307179..c3d2a7da0d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -21,7 +21,7 @@ def readable_date(date) end def format_money(amount) - Money.new(amount, "USD").format + #Money.new(amount, "USD").format end module_function :set_class diff --git a/app/jobs/helpers/application_helper.rb b/app/jobs/helpers/application_helper.rb new file mode 100644 index 0000000000..e1fa307179 --- /dev/null +++ b/app/jobs/helpers/application_helper.rb @@ -0,0 +1,28 @@ +module ApplicationHelper + def set_class(rating) + case rating + when 0 + "zero-stars" + when 1 + "one-star" + when 2 + "two-stars" + when 3 + "three-stars" + when 4 + "four-stars" + when 5 + "five-stars" + end + end + + def readable_date(date) + ("" + date.strftime("%b. %d, %Y %I:%M %p (%Z)") + "").html_safe + end + + def format_money(amount) + Money.new(amount, "USD").format + end + + module_function :set_class +end diff --git a/app/jobs/helpers/categories_helper.rb b/app/jobs/helpers/categories_helper.rb new file mode 100644 index 0000000000..e06f31554c --- /dev/null +++ b/app/jobs/helpers/categories_helper.rb @@ -0,0 +1,2 @@ +module CategoriesHelper +end diff --git a/app/jobs/helpers/main_helper.rb b/app/jobs/helpers/main_helper.rb new file mode 100644 index 0000000000..826effed96 --- /dev/null +++ b/app/jobs/helpers/main_helper.rb @@ -0,0 +1,2 @@ +module MainHelper +end diff --git a/app/jobs/helpers/merchants_helper.rb b/app/jobs/helpers/merchants_helper.rb new file mode 100644 index 0000000000..5337747b0f --- /dev/null +++ b/app/jobs/helpers/merchants_helper.rb @@ -0,0 +1,2 @@ +module MerchantsHelper +end diff --git a/app/jobs/helpers/order_items_helper.rb b/app/jobs/helpers/order_items_helper.rb new file mode 100644 index 0000000000..e197528ae1 --- /dev/null +++ b/app/jobs/helpers/order_items_helper.rb @@ -0,0 +1,2 @@ +module OrderItemsHelper +end diff --git a/app/jobs/helpers/orders_helper.rb b/app/jobs/helpers/orders_helper.rb new file mode 100644 index 0000000000..443227fd48 --- /dev/null +++ b/app/jobs/helpers/orders_helper.rb @@ -0,0 +1,2 @@ +module OrdersHelper +end diff --git a/app/jobs/helpers/products_helper.rb b/app/jobs/helpers/products_helper.rb new file mode 100644 index 0000000000..ab5c42b325 --- /dev/null +++ b/app/jobs/helpers/products_helper.rb @@ -0,0 +1,2 @@ +module ProductsHelper +end diff --git a/app/jobs/helpers/reviews_helper.rb b/app/jobs/helpers/reviews_helper.rb new file mode 100644 index 0000000000..682b7b1abc --- /dev/null +++ b/app/jobs/helpers/reviews_helper.rb @@ -0,0 +1,2 @@ +module ReviewsHelper +end diff --git a/test/controllers/products_controller_test.rb b/test/controllers/products_controller_test.rb index 4056240858..7c8abe2d59 100644 --- a/test/controllers/products_controller_test.rb +++ b/test/controllers/products_controller_test.rb @@ -133,6 +133,27 @@ must_respond_with :success end + it "checking if auth works this way" do + OmniAuth.config.test_mode = true + merchant = merchants(:eva) + auth_hash = { + provider: merchant.provider, + uid: merchant.uid, + info: { + nickname: merchant.username, + email: merchant.email + } + } + OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(merchant)) + get auth_callback_path(:github) + + Merchant.find_by(uid: merchant.uid).uid.must_equal merchant.uid + test_merchant = Merchant.find_by(uid: merchant.uid) + + get products_path + must_respond_with :success + end + it "returns success for no products" do Product.destroy_all get products_path @@ -171,11 +192,8 @@ it "renders bad_request and does not update the DB for bogus data" do - product_data = { - product: { - name: "" - } - } + + start_count = Product.count post products_path, params: product_data diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml index 16a448fa1f..1aa06dca06 100644 --- a/test/fixtures/products.yml +++ b/test/fixtures/products.yml @@ -5,6 +5,7 @@ # below each fixture, per the syntax in the comments below # one: + id: 1 name: product price: 2.0 stock: 3 @@ -14,6 +15,7 @@ one: merchant: eva two: + id: 2 name: product2 price: 3.0 stock: 4 @@ -23,6 +25,7 @@ two: merchant: eva three: + id: 3 name: product3 price: 4.0 stock: 5 @@ -32,6 +35,7 @@ three: merchant: eva four: + id: 4 name: product4 price: 6.0 stock: 7 @@ -41,6 +45,7 @@ four: merchant: emma five: + id: 5 name: product5 price: 8.0 stock: 9 @@ -50,6 +55,7 @@ five: merchant: emma six: + id: 6 name: product6 price: 10.0 stock: 11 @@ -59,6 +65,7 @@ six: merchant: emma seven: + id: 7 name: product7 price: 12.0 stock: 13 @@ -68,6 +75,7 @@ seven: merchant: emma eight: + id: 8 name: product8 price: 14.0 stock: 15 @@ -77,6 +85,7 @@ eight: merchant: emma nine: + id: 9 name: product9 price: 16.0 stock: 17 @@ -86,6 +95,7 @@ nine: merchant: emma ten: + id: 10 name: product9 price: 18.0 stock: 19 @@ -94,7 +104,8 @@ ten: image_url: http://www.fillmurray.com/ merchant: emma -ninty: +eleven: + id: 11 id: 90 name: product90 price: 18.0 diff --git a/test/test_helper.rb b/test/test_helper.rb index c1a3e496b8..371b450b6d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -39,7 +39,9 @@ class ActiveSupport::TestCase } OmniAuth.config.add_mock(:github, omniauth_hash) - + + + # Add more helper methods to be used by all tests here... def setup OmniAuth.config.test_mode = true