From 703663c4a2f1db3b0a0dd070e653bbccd7d36c1e Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 3 Feb 2013 15:50:09 +0000 Subject: [PATCH 1/1] Add a test for commenting on diary entries --- app/controllers/diary_entry_controller.rb | 2 + .../functional/diary_entry_controller_test.rb | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/app/controllers/diary_entry_controller.rb b/app/controllers/diary_entry_controller.rb index f218ea6bb..12f3fdcc8 100644 --- a/app/controllers/diary_entry_controller.rb +++ b/app/controllers/diary_entry_controller.rb @@ -70,6 +70,8 @@ class DiaryEntryController < ApplicationController else render :action => 'view' end + rescue ActiveRecord::RecordNotFound + render :action => "no_such_entry", :status => :not_found end def list diff --git a/test/functional/diary_entry_controller_test.rb b/test/functional/diary_entry_controller_test.rb index 4d4a324ed..5d0cb93ac 100644 --- a/test/functional/diary_entry_controller_test.rb +++ b/test/functional/diary_entry_controller_test.rb @@ -320,7 +320,54 @@ class DiaryEntryControllerTest < ActionController::TestCase end def test_creating_diary_comment + @request.cookies["_osm_username"] = users(:public_user).display_name + entry = diary_entries(:normal_user_entry_1) + + # Make sure that you are denied when you are not logged in + post :comment, :display_name => entry.user.display_name, :id => entry.id + assert_response :forbidden + # Verify that you get a not found error, when you pass a bogus id + post :comment, {:display_name => entry.user.display_name, :id => 9999}, {:user => users(:public_user).id} + assert_response :not_found + assert_select "html", :count => 1 do + assert_select "body", :count => 1 do + assert_select "div.wrapper", :count => 1 do + assert_select "div.content-heading", :count => 1 do + assert_select "h2", :text => "No entry with the id: 9999", :count => 1 + end + end + end + end + + # Now try again with the right id + assert_difference "ActionMailer::Base.deliveries.size", 1 do + assert_difference "DiaryComment.count", 1 do + post :comment, {:display_name => entry.user.display_name, :id => entry.id, :diary_comment => {:body => "New comment"}}, {:user => users(:public_user).id} + end + end + assert_response :redirect + assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id + email = ActionMailer::Base.deliveries.first + assert_equal [ users(:normal_user).email ], email.to + assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on your diary entry", email.subject + assert_match /New comment/, email.text_part.decoded + assert_match /New comment/, email.html_part.decoded + ActionMailer::Base.deliveries.clear + comment = DiaryComment.find(4) + assert_equal entry.id, comment.diary_entry_id + assert_equal users(:public_user).id, comment.user_id + assert_equal "New comment", comment.body + + # Now view the diary entry, and check the new comment is present + get :view, :display_name => entry.user.display_name, :id => entry.id + assert_response :success + assert_select ".diary-comment", :count => 1 do + assert_select "#comment4", :count => 1 do + assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1 + end + assert_select ".richtext", :text => /New comment/, :count => 1 + end end # Check that you can get the expected response and template for all available languages -- 2.43.2