Skip to content
Snippets Groups Projects
Commit 430b32df authored by Butler, Alexis (UG - Computer Science)'s avatar Butler, Alexis (UG - Computer Science)
Browse files

Wrote and passed controller tests for users, sections, sessions and reviews

parent 6c557ac6
No related branches found
No related tags found
No related merge requests found
require 'test_helper' require 'test_helper'
class ReviewsControllerTest < ActionDispatch::IntegrationTest 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 end
test 'should reject a mallformed review on a product' do
test "should view a 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
end end
require 'test_helper' require 'test_helper'
class SectionsControllerTest < ActionDispatch::IntegrationTest 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
end end
require 'test_helper' require 'test_helper'
class SessionsControllerTest < ActionDispatch::IntegrationTest 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 end
test 'should not get login page if logged in' do
test "should get logout page" 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
end end
...@@ -45,3 +45,4 @@ class UsersControllerTest < ActionDispatch::IntegrationTest ...@@ -45,3 +45,4 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
assert_equal 'Sorry, something went wrong', flash[:danger] assert_equal 'Sorry, something went wrong', flash[:danger]
end end
end end
#Not sure if tests should be commented. The names should be enough?
\ No newline at end of file
...@@ -11,3 +11,9 @@ two: ...@@ -11,3 +11,9 @@ two:
user: one user: one
content: "Content" content: "Content"
rating: 4 rating: 4
three:
product: one
user: two
content: "Content"
rating: 4
\ No newline at end of file
...@@ -5,11 +5,18 @@ one: ...@@ -5,11 +5,18 @@ one:
lastName: "testerson" lastName: "testerson"
email: "user1@testfix.com" email: "user1@testfix.com"
password_digest: "$2a$10$z4tBMW3RFGkQm/mGEKgAP..POVOETJn/Ybne1nIaunXi.DL.uT/Ra" #t00l33t4u password_digest: "$2a$10$z4tBMW3RFGkQm/mGEKgAP..POVOETJn/Ybne1nIaunXi.DL.uT/Ra" #t00l33t4u
admin: true admin: false
two: two:
firstName: "User2" firstName: "User2"
lastName: "testerson" lastName: "testerson"
email: "user2@testfix.com" email: "user2@testfix.com"
password_digest: "$2a$10$z4tBMW3RFGkQm/mGEKgAP..POVOETJn/Ybne1nIaunXi.DL.uT/Ra" password_digest: "$2a$10$z4tBMW3RFGkQm/mGEKgAP..POVOETJn/Ybne1nIaunXi.DL.uT/Ra" #t00l33t4u
admin: false admin: false
\ No newline at end of file
admin:
firstName: "Alexis"
lastName: "Butler"
email: "admin@testfix.com"
password_digest: "$2a$10$HUzGp9sJIDlsW50KCbAVfuFPkZjZZ2EjxgkRH/rSJ828IGG0GMmMq" #superSecret
admin: true
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment