diff --git a/app/controllers/contact_controller.rb b/app/controllers/contact_controller.rb index f4ff96a716f0949801a48f1cde17246ae940149c..63c2d532bf1a5dcf17b4a892e731a4a219d4c01d 100644 --- a/app/controllers/contact_controller.rb +++ b/app/controllers/contact_controller.rb @@ -1,6 +1,10 @@ class ContactController < ApplicationController def contact - @email = User.find(session[:user_id]).email + if session[:user_id].nil? + redirect_to homepage_path + else + @email = User.find(session[:user_id]).email + end end def sendmessage diff --git a/app/controllers/listview_controller.rb b/app/controllers/listview_controller.rb index 9cafd2f6a30c45e7b17c81af3d6b778800e6e0d0..e00ea17c2e25e0933980706d0af3dc84709e6c04 100644 --- a/app/controllers/listview_controller.rb +++ b/app/controllers/listview_controller.rb @@ -5,7 +5,7 @@ class ListviewController < ApplicationController (0..params[:product_id].length - 1).each do |i| change = Hash[] - change[:quantity] = params[:quantity][i] unless params[:quantity].nil? || params[:quantity][i].blank? + change[:quantity] = params[:quantity][i] unless params[:quantity].nil? || params[:quantity][i].blank? || Integer(params[:quantity][i]) < 1 change[:acquired] = params[:acquired][i] unless params[:acquired].nil? || params[:acquired][i].blank? change[:name] = params[:name][i] unless params[:name].nil? || params[:name][i].blank? @@ -22,26 +22,29 @@ class ListviewController < ApplicationController end def listview - session[:list_id] = params[:list_id] if params[:list_id] + if session[:list_id].nil? + redirect_to homepage_path + else + session[:list_id] = params[:list_id] if params[:list_id] - @name = List.find(session[:list_id]).name - @list_id = session[:list_id] + @name = List.find(session[:list_id]).name + @list_id = session[:list_id] + end end def new_prod - if Product.where(list_id: session[:list_id], name: params[:name]).exists? - prod = Product.find_by(list_id: session[:list_id], name: params[:name]) - - prod.quantity = 0 if prod.quantity.nil? - - quant = params[:quantity].number? ? Integer(params[:quantity]) : 1 - - Product.update(prod.id, quantity: [1, prod.quantity].max + quant) - - else - @prod = Product.create(name: params[:name], quantity: params[:quantity], acquired: 0, - list: List.find(session[:list_id])) - + valid_quantity = !params[:quantity].nil? && Integer(params[:quantity]) >= 1 + valid_name = !params[:name].blank? + + if valid_quantity && valid_name + if Product.where(list_id: params[:list_id], name: params[:name]).exists? + prod = Product.find_by(list_id: params[:list_id], name: params[:name]) + quant = !params[:quantity].nil? && params[:quantity].number? ? Integer(params[:quantity]) : 1 + Product.update(prod.id, quantity: [1, prod.quantity].max + quant) + else + @prod = Product.create(name: params[:name], quantity: !params[:quantity].blank? ? params[:quantity] : 1, acquired: 0, + list: List.find(params[:list_id])) + end end redirect_to listview_path end diff --git a/test/controllers/contact_controller_test.rb b/test/controllers/contact_controller_test.rb index 7902450b7402d34e752d12623ce16e18def93ed5..3c80e85f0524ba5085e514a39fd872dd3fcf4593 100644 --- a/test/controllers/contact_controller_test.rb +++ b/test/controllers/contact_controller_test.rb @@ -1,7 +1,29 @@ require 'test_helper' class ContactControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end + test 'should get contact' do + get contact_path + assert_response :redirect + end + + test 'email should not be blank and should be in a valid format' do + post sendmessage_path, params: {name: 'Ben', email: 'email@address.com', message: 'boop'} + assert_redirected_to homepage_path + + post sendmessage_path, params: {name: 'Ben', email: 'bad_email', message: 'boop'} + assert_redirected_to contact_path + + post sendmessage_path, params: {name: 'Ben', email: 'bad_email', message: 'boop'} + assert_redirected_to contact_path + end + + test 'message should not be blank' do + post sendmessage_path, params: {name: 'Ben', email: 'email@address.com', message: 'boop'} + assert_redirected_to homepage_path + + post sendmessage_path, params: {name: 'Ben', email: 'email@address.com', message: ''} + assert_redirected_to contact_path + + end + end diff --git a/test/controllers/home_controller_test.rb b/test/controllers/home_controller_test.rb index a69cfd88de200b6a40491d42438f41735a7b6e7e..7a5c4df8fe32385ae022a0ed7aa6d6aa86122b4f 100644 --- a/test/controllers/home_controller_test.rb +++ b/test/controllers/home_controller_test.rb @@ -1,9 +1,41 @@ require 'test_helper' class HomeControllerTest < ActionDispatch::IntegrationTest - test "should get home" do + test 'should get home' do get homepage_path assert_response :redirect end + test 'should get login/root' do + get root_path + assert_response :success + end + + test 'login email should not be blank' do + post login_path, params: {email: '', password: 'bing'} + assert_redirected_to root_path + end + + test 'login password should not be blank' do + post login_path, params: {email: 'email@address.com', password: ''} + assert_redirected_to root_path + end + + test 'new account name should not be blank' do + post new_acc_path, params: {name: '', email: 'email@address.com', password: 'bing'} + assert_redirected_to root_path + end + + test 'new account email should not be blank and should be in correct format' do + post new_acc_path, params: {name: 'Ben', email: '', password: 'bing'} + assert_redirected_to root_path + + post new_acc_path, params: {name: 'Ben', email: 'bad_email', password: 'bing'} + assert_redirected_to root_path + end + + test 'new account password should not be blank' do + post new_acc_path, params: {name: 'Ben', email: 'email@address.com', password: ''} + assert_redirected_to root_path + end end diff --git a/test/controllers/listview_controller_test.rb b/test/controllers/listview_controller_test.rb index 6868be69cffec935eeaa58bd1631195243baeeca..696fcf110522a17080b006e61a4125d0f7eb830a 100644 --- a/test/controllers/listview_controller_test.rb +++ b/test/controllers/listview_controller_test.rb @@ -1,7 +1,63 @@ require 'test_helper' class ListviewControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end + test 'should get listview' do + get listview_path + assert_response :redirect + end + + test 'new product quantity should be empty or greater than 0' do + assert_difference('Product.count', 0) do + post new_prod_path, params: {name: 'food', quantity: -4, list_id: List.first.id} + assert_redirected_to listview_path + end + + assert_difference('Product.count') do + post new_prod_path, params: {name: 'food', quantity: 3, list_id: List.first.id} + assert_redirected_to listview_path + end + + assert_difference('Product.count', 0) do + post new_prod_path, params: {name: 'food', list_id: List.first.id} + assert_redirected_to listview_path + end + end + + test 'new product name should not be empty' do + + assert_difference('Product.count') do + post new_prod_path, params: {name: 'food', quantity: 3, list_id: List.first.id} + assert_redirected_to listview_path + end + + assert_difference('Product.count', 0) do + post new_prod_path, params: {quantity: 3, list_id: List.first.id} + assert_redirected_to listview_path + end + end + + test 'product quantity should be empty or greater than 0' do + get change_prods_path, params: { product_id: [Product.first.id], quantity: [69] } + assert Product.first.quantity = 69 + assert_redirected_to listview_path + + get change_prods_path, params: { product_id: [Product.first.id], quantity: [''] } + assert Product.first.quantity = 69 + assert_redirected_to listview_path + + get change_prods_path, params: { product_id: [Product.first.id], quantity: [-4] } + assert Product.first.quantity = 69 + assert_redirected_to listview_path + end + + test 'product name should not be empty' do + + get change_prods_path, params: { product_id: [Product.first.id], name: ['nothing'] } + assert Product.first.name = 'nothing' + assert_redirected_to listview_path + + get change_prods_path, params: { product_id: [Product.first.id], name: [''] } + assert Product.first.name = 'nothing' + assert_redirected_to listview_path + end end