From 430b32dfc7d743efbecdbd6083a4f2cc7e4dc675 Mon Sep 17 00:00:00 2001 From: "Butler, Alexis (UG - Computer Science)" <ab02259@surrey.ac.uk> Date: Thu, 26 Nov 2020 16:11:12 +0000 Subject: [PATCH] Wrote and passed controller tests for users, sections, sessions and reviews --- test/controllers/reviews_controller_test.rb | 39 +++++++++++++++++--- test/controllers/sections_controller_test.rb | 6 ++- test/controllers/sessions_controller_test.rb | 35 +++++++++++++++--- test/controllers/users_controller_test.rb | 1 + test/fixtures/reviews.yml | 6 +++ test/fixtures/users.yml | 13 +++++-- 6 files changed, 84 insertions(+), 16 deletions(-) diff --git a/test/controllers/reviews_controller_test.rb b/test/controllers/reviews_controller_test.rb index 42a2bca..85afcc0 100644 --- a/test/controllers/reviews_controller_test.rb +++ b/test/controllers/reviews_controller_test.rb @@ -1,12 +1,41 @@ require 'test_helper' class ReviewsControllerTest < ActionDispatch::IntegrationTest - test "should create review on a product" do - + test 'should create a review on a product' do + sign_in_as_tester + assert_difference('Review.count', +1) do + post product_reviews_url(products(:one)), params: {review: {rating: 3, content: 'A review content'}} + end + assert_redirected_to(product_url(products(:one))) + assert_equal 'Review Posted', flash[:success] end - - test "should view a review on a product" do - + test 'should reject a mallformed review on a product' do + sign_in_as_tester + assert_no_difference('Review.count') do + post product_reviews_url(products(:one)), params: {review: {content: 'A review content'}} + end + assert_redirected_to(product_url(products(:one))) + assert_equal 'Failed to post Try again', flash[:danger] + end + test 'reviews should be shown on products' do + get product_url(products(:one)) + assert_select('strong', "#{reviews(:two).rating} / 5") + end + test 'should delete a review from product when logged in as owner of review' do + sign_in_as_tester + post product_reviews_url(Product.last), params: {review: {rating: 3, content: 'A review content'}} + assert_difference('Review.count', -1) do + delete product_review_url(product_id: Product.last, id:Product.last.reviews.first.id) + end + assert_redirected_to(product_url(Product.last)) + assert_equal 'Review deleted', flash[:success] + end + test 'should not delete a review when not the review owner' do + sign_in_as_tester + assert_no_difference('Review.count') do + delete product_review_url(product_id: products(:one), id:reviews(:three)) + end + assert_redirected_to(product_url(Product.last)) end end diff --git a/test/controllers/sections_controller_test.rb b/test/controllers/sections_controller_test.rb index b12732c..181ac94 100644 --- a/test/controllers/sections_controller_test.rb +++ b/test/controllers/sections_controller_test.rb @@ -1,8 +1,10 @@ require 'test_helper' class SectionsControllerTest < ActionDispatch::IntegrationTest - test "should show a specific section" do - + test 'should show a specific section' do + get section_url(sections(:one)) + assert_response(:success) + assert_select('h1', sections(:one).name) end end diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index 4fac6d8..f541c7d 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -1,12 +1,35 @@ require 'test_helper' class SessionsControllerTest < ActionDispatch::IntegrationTest - test "should get login page" do - + test 'should get login page if not logged in' do + get login_url + assert_response(:success) end - - test "should get logout page" do - + test 'should not get login page if logged in' do + sign_in_as_tester + get login_url + assert_redirected_to(root_url) + assert_equal 'Already have an existing account', flash[:warning] + end + test 'should login when correct params given' do + post login_url, params: {email: users(:one).email, password: "t00l33t4u"} + assert_redirected_to(root_url) + assert_equal("Successful login - #{users(:one).firstName}", flash[:success]) + end + test 'should not login when incorrect params given' do + post login_url, params: {email: users(:one).email, password: "aaaaaaa"} + assert_redirected_to(root_url) + assert_equal 'Login Failed', flash[:danger] + end + test 'should logout if logged in' do + sign_in_as_tester + get logout_url + assert_redirected_to(root_url) + assert_equal 'Logged out successfully', flash[:warning] + end + test 'should not logout if not logged in' do + get logout_url + assert_redirected_to(root_url) + assert_equal 'Not logged in', flash[:warning] end - end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 830ab48..ab4763b 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -45,3 +45,4 @@ class UsersControllerTest < ActionDispatch::IntegrationTest assert_equal 'Sorry, something went wrong', flash[:danger] end end +#Not sure if tests should be commented. The names should be enough? \ No newline at end of file diff --git a/test/fixtures/reviews.yml b/test/fixtures/reviews.yml index 435da14..5020c36 100644 --- a/test/fixtures/reviews.yml +++ b/test/fixtures/reviews.yml @@ -11,3 +11,9 @@ two: user: one content: "Content" rating: 4 + +three: + product: one + user: two + content: "Content" + rating: 4 \ No newline at end of file diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index d137476..c9007f8 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -5,11 +5,18 @@ one: lastName: "testerson" email: "user1@testfix.com" password_digest: "$2a$10$z4tBMW3RFGkQm/mGEKgAP..POVOETJn/Ybne1nIaunXi.DL.uT/Ra" #t00l33t4u - admin: true + admin: false two: firstName: "User2" lastName: "testerson" email: "user2@testfix.com" - password_digest: "$2a$10$z4tBMW3RFGkQm/mGEKgAP..POVOETJn/Ybne1nIaunXi.DL.uT/Ra" - admin: false \ No newline at end of file + password_digest: "$2a$10$z4tBMW3RFGkQm/mGEKgAP..POVOETJn/Ybne1nIaunXi.DL.uT/Ra" #t00l33t4u + admin: false + +admin: + firstName: "Alexis" + lastName: "Butler" + email: "admin@testfix.com" + password_digest: "$2a$10$HUzGp9sJIDlsW50KCbAVfuFPkZjZZ2EjxgkRH/rSJ828IGG0GMmMq" #superSecret + admin: true -- GitLab