Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase test coverage #467

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ def create

def update
@comment = Comment.find params[:id]

if logged_in? && current_user.can_edit?(@comment)
Comment.update(@comment.id, :body => params[:comment][:body])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line was not updating the comment and the tests were failing. That's why I had to change it

@comment.update_attribute(:body, params[:comment][:body])
redirect_to "/maps/" + params[:map_id]
else
flash[:error] = "You must be logged in to comment."
flash[:error] = "You do not have permissions to update that comment."
redirect_to "/login"
end
end
Expand Down
17 changes: 16 additions & 1 deletion test/fixtures/comments.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
body: I'll just leave a comment, why don't I.
body: I'll just leave a comment, why don't I.
user_id: 1
map_id: 1

two:
body: Here's an awesome comment.
user_id: 2
map_id: 1

three:
body: Testing controllers here.
user_id: 3
map_id: 1

four:
body: Another one bites the dust
user_id: 4
map_id: 1
12 changes: 12 additions & 0 deletions test/fixtures/maps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ saugus:
id: 1
name: Saugus Landfill Incinerator
slug: saugus-landfill-incinerator
location: MA
lat: 42.43823313018592
lon: -70.9849190711975
description: A landfill in Saugus, MA.
created_at: <%= Time.now %>
license: publicdomain
user_id: 1

cubbon:
id: 2
name: Cubbon Park
slug: cubbon-park
location: India
lat: 12.976347
lon: 77.592928
description: A park in bangalore
created_at: <%= Time.now %>
license: publicdomain
user_id: 1
7 changes: 7 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ chris:
remember_token_expires_at:
remember_token:

joshua:
id: 4
login: joshua
email: joshua@example.com
created_at: <%= 3.days.ago.to_s :db %>
remember_token_expires_at:
remember_token:
17 changes: 16 additions & 1 deletion test/fixtures/warpables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,20 @@ two:
nodes: "1,2,3,4"
locked: false
deleted: false
cm_per_pixel: 99.86133660017036>
cm_per_pixel: 99.86133660017036

three:
id: 3
parent_id: nil
image_content_type: "image/png"
image_file_name: "test.png"
thumbnail: nil
image_file_size: 574286
width: 756
height: 392
history: ''
map_id: 2
nodes: "1,2,3,4"
locked: false
deleted: false
cm_per_pixel: 99.86133660017036
171 changes: 163 additions & 8 deletions test/functional/comments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,187 @@ def teardown
end

test "should not create comment if not logged in" do
before_count = Comment.count

post(:create,
map_id: @map.slug,
comment: {
user_id: 1
})

assert_response :success
assert_empty @map.comments
assert_equal before_count, Comment.count
end

test "should create comment" do
session[:user_id] = 1
before_count = Comment.count

post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!",
user_id: 1
body: "I'm gonna troll you!"
})

assert_response :success
assert_not_empty @map.comments
assert_not_equal before_count, Comment.count
assert_equal "I'm gonna troll you!", @map.comments.last.body
end

test "should update comment if commenter" do
@comment = comments(:four)
session[:user_id] = 4

put(:update,
id: @comment.id,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

#refresh the object
@comment.reload

assert_redirected_to "/maps/" + @map.slug
assert_equal "I'm gonna troll you!", @comment.body
end

#test "update comment" do
#end
test "should not update comment if not commenter" do
@comment = comments(:one)
session[:user_id] = 3

#test "destroy comment" do
#end
put(:update,
id: @comment.id,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

@comment.reload

assert_redirected_to "/login"
assert_not_equal "I'm gonna troll you!", @comment.body
assert_equal "I'll just leave a comment, why don't I.", @comment.body
assert_equal "You do not have permissions to update that comment.", flash[:error]
end

test "should not update comment if not logged in" do
@comment = comments(:one)

put(:update,
id: @comment.id,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

@comment.reload

assert_redirected_to "/login"
assert_not_equal "I'm gonna troll you!", @comment.body
assert_equal "I'll just leave a comment, why don't I.", @comment.body
assert_equal "You do not have permissions to update that comment.", flash[:error]
end

test "should delete comment" do
@comment = comments(:one)
session[:user_id] = 1
before_count = Comment.count

delete(:destroy,
id: @comment.id,
map_id: @map.slug
)

assert_redirected_to "/maps/" + @map.slug
assert_not_equal before_count, Comment.count
assert_equal "Comment deleted.", flash[:notice]
end

test "should not delete comment if not commmenter" do
@comment = comments(:one)
session[:user_id] = 3
before_count = Comment.count

delete(:destroy,
id: @comment.id,
map_id: @map.slug
)

assert_redirected_to "/maps/" + @map.slug
assert_equal before_count, Comment.count
assert_equal "You do not have permission to delete that comment.", flash[:error]
end

test "should not delete comment if not logged in" do
@comment = comments(:one)
before_count = Comment.count

delete(:destroy,
id: @comment.id,
map_id: @map.slug
)

assert_redirected_to "/maps/" + @map.slug
assert_equal before_count, Comment.count
assert_equal "You do not have permission to delete that comment.", flash[:error]
end

test "should not send email to author" do
@user = users(:quentin)
session[:user_id] = @user.id

post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

assert_response :success
assert !ActionMailer::Base.deliveries.collect(&:to).include?([@user.email])
end

test "should send email to author if someone else comments" do
@user = users(:quentin)
session[:user_id] = 3

post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

email = ActionMailer::Base.deliveries.last

assert_response :success
assert ActionMailer::Base.deliveries.collect(&:to).include?([@user.email])
assert ActionMailer::Base.deliveries.collect(&:subject).include?("New comment on '#{@map.name}'")
end

test "should send email to all commenters on commenting" do
@joshua = users(:joshua)
@chris = users(:chris)

session[:user_id] = @chris.id

post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

session[:user_id] = @joshua.id

post(:create,
map_id: @map.slug,
comment: {
body: "Yeah we'll see!"
})

email = ActionMailer::Base.deliveries.last

assert_response :success
assert ActionMailer::Base.deliveries.collect(&:to).include?([@chris.email])
assert ActionMailer::Base.deliveries.collect(&:subject).include?("New comment on '#{@map.name}'")
end
end
9 changes: 9 additions & 0 deletions test/functional/images_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class ImagesControllerTest < ActionController::TestCase

# called before every single test
def setup
@map = maps(:saugus)
end
end
Loading