diff --git a/test/controllers/reviews_controller_test.rb b/test/controllers/reviews_controller_test.rb index 42a2bca10be3ef48dd5de4ebb573fddbcd1e6dc0..85afcc027e4f07fcd79537d540bb40788caf1af5 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 b12732c5272fa7fab55ba9dfb9b048d51361570c..181ac944802baca9de38adf0834bbee804966b89 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 4fac6d89a20f8da5b229de57d2f44badba6ab7dc..f541c7d15734fc9b9fd4287f50515f12a87d6cb1 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 830ab48fc7a1005022f1db75d962d2bc25334421..ab4763b698acc9a73575d073a334ff436a5bbf38 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 435da145c21361ee3060614043d04ed9537409c8..5020c361097296fc1cd2d081d9ca91da1d97b6a9 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 d1374767df5b3ee45929b9ee6f7c7ea1aa7caaef..c9007f85eff8ecd9f456c94a723e119b98938411 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