]> git.openstreetmap.org Git - rails.git/commitdiff
Add a test for commenting on diary entries
authorTom Hughes <tom@compton.nu>
Sun, 3 Feb 2013 15:50:09 +0000 (15:50 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 3 Feb 2013 15:50:09 +0000 (15:50 +0000)
app/controllers/diary_entry_controller.rb
test/functional/diary_entry_controller_test.rb

index f218ea6bb1ea89d4f1c634a6da4e8995d5267655..12f3fdcc83435187e52dd6abfd9d1ffa2ad68b11 100644 (file)
@@ -70,6 +70,8 @@ class DiaryEntryController < ApplicationController
     else
       render :action => 'view'
     end
     else
       render :action => 'view'
     end
+  rescue ActiveRecord::RecordNotFound
+    render :action => "no_such_entry", :status => :not_found
   end
 
   def list
   end
 
   def list
index 4d4a324ed68b0ebbd138fb2eea5b1aecc33bcb16..5d0cb93acfe3df6bf4bfbb0f0f504f52fb3c8738 100644 (file)
@@ -320,7 +320,54 @@ class DiaryEntryControllerTest < ActionController::TestCase
   end
   
   def test_creating_diary_comment
   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
   end
   
   # Check that you can get the expected response and template for all available languages