Skip to content

Commit

Permalink
Add tests for comments and maps
Browse files Browse the repository at this point in the history
  • Loading branch information
kaustubh-nair committed Apr 4, 2019
1 parent 784bfbc commit 1f69835
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 23 deletions.
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])
@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

0 comments on commit 1f69835

Please sign in to comment.