From 437142424cf2b546cbaa24411d8890e72552df8a Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Wed, 17 Apr 2013 17:33:34 +1000
Subject: [PATCH] speed up tests, fix notification of mentioned users in pms

---
 app/models/post_alert_observer.rb    |  2 ++
 spec/components/post_creator_spec.rb | 47 ++++++++++++----------------
 2 files changed, 22 insertions(+), 27 deletions(-)

diff --git a/app/models/post_alert_observer.rb b/app/models/post_alert_observer.rb
index 88c7bca90..b377230bf 100644
--- a/app/models/post_alert_observer.rb
+++ b/app/models/post_alert_observer.rb
@@ -15,6 +15,8 @@ class PostAlertObserver < ActiveRecord::Observer
 
   # We need to consider new people to mention / quote when a post is edited
   def after_save_post(post)
+    return if post.topic.private_message?
+
     mentioned_users = extract_mentioned_users(post)
     quoted_users = extract_quoted_users(post)
 
diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb
index 633270aa8..9a38e29cc 100644
--- a/spec/components/post_creator_spec.rb
+++ b/spec/components/post_creator_spec.rb
@@ -146,6 +146,7 @@ describe PostCreator do
 
   end
 
+  # more integration testing ... maximise our testing
   context 'existing topic' do
     let!(:topic) { Fabricate(:topic, user: user) }
     let(:creator) { PostCreator.new(user, raw: 'test reply', topic_id: topic.id, reply_to_post_number: 4) }
@@ -156,44 +157,36 @@ describe PostCreator do
     end
 
     context 'success' do
-      it 'should create the post' do
-        lambda { creator.create }.should change(Post, :count).by(1)
+      it 'create correctly' do
+        post = creator.create
+        Post.count.should == 1
+        Topic.count.should == 1
+        post.reply_to_post_number.should == 4
       end
 
-      it "doesn't create a topic" do
-        lambda { creator.create }.should_not change(Topic, :count)
-      end
-
-      it "passes through the reply_to_post_number" do
-        creator.create.reply_to_post_number.should == 4
-      end
     end
 
   end
 
+  # integration test ... minimise db work
   context 'private message' do
     let(:target_user1) { Fabricate(:coding_horror) }
     let(:target_user2) { Fabricate(:moderator) }
+    let(:unrelated) { Fabricate(:user) }
+    let(:post) do
+      PostCreator.create(user, title: 'hi there welcome to my topic',
+                               raw: "this is my awesome message @#{unrelated.username_lower}",
+                               archetype: Archetype.private_message,
+                               target_usernames: [target_user1.username, target_user2.username].join(','))
+    end
 
-    describe 'regular user to user' do
-      let(:post) do
-        PostCreator.create(user, title: 'hi there welcome to my topic',
-                                 raw: 'this is my awesome message',
-                                 archetype: Archetype.private_message,
-                                 target_usernames: [target_user1.username, target_user2.username].join(','))
-      end
+    it 'acts correctly' do
+      post.topic.archetype.should == Archetype.private_message
+      post.topic.topic_allowed_users.count.should == 3
 
-      it 'has the right archetype' do
-        post.topic.archetype.should == Archetype.private_message
-      end
-
-      it 'has the right count (me and 2 other users)' do
-        post.topic.topic_allowed_users.count.should == 3
-      end
-
-      it 'has the right subtype' do
-        post.topic.subtype.should == TopicSubtype.user_to_user
-      end
+      # does not notify an unrelated user
+      unrelated.notifications.count.should == 0
+      post.topic.subtype.should == TopicSubtype.user_to_user
     end
   end