public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Michael Hartl (author)
Wed May 14 10:33:00 -0700 2008
commit  f073e77861aa482fa316ee646b0ff81ef5889b28
tree    dc12c97bed9c0a7c7594390877bd5867ddf07030
parent  2f611925ded407bc89926611103888091405d702
insoshi / spec / models / comment_spec.rb
100644 209 lines (171 sloc) 6.317 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
require File.dirname(__FILE__) + '/../spec_helper'
 
describe Comment do
  describe "blog post comments" do
    
    before(:each) do
      @post = posts(:blog_post)
      @comment = @post.comments.new(:body => "Hey there",
                                    :commenter => people(:aaron))
    end
  
    it "should be valid" do
      @comment.should be_valid
    end
  
    it "should require a body" do
      comment = @post.comments.new
      comment.should_not be_valid
      comment.errors.on(:body).should_not be_empty
    end
  
    it "should have a maximum body length" do
      @comment.should have_maximum(:body, MAX_TEXT_LENGTH)
    end
  
    it "should increase the comment count" do
      old_count = @post.comments.count
      @comment.save!
      @post.comments.count.should == old_count + 1
    end
  
    describe "associations" do
    
      before(:each) do
        @comment.save!
        @activity = Activity.find_by_item_id(@comment)
      end
 
      it "should have an activity" do
        @activity.should_not be_nil
      end
    
      it "should add an activity to the poster" do
        @comment.commentable.blog.person.activities.
          should include_the(@activity)
      end
 
      it "should add an activity to the commenter" do
        @comment.commenter.recent_activity.should include_the(@activity)
      end
    end
    
    describe "feed items for contacts" do
      it %(should not have duplicate items when a contact comments
on a blog) do
        @person = @post.blog.person
        @commenter = @comment.commenter
        Connection.connect(@person, @commenter)
        @comment.save!
        @person.activities.should have_distinct_elements
      end
    end
    
    describe "email notifications" do
      
      before(:each) do
        @emails = ActionMailer::Base.deliveries
        @emails.clear
        @global_prefs = Preference.find(:first)
        @recipient = @comment.commented_person
      end
      
      it "should send an email when global/recipient notifications are on" do
        # Both notifications are on by default.
        lambda do
          @comment.save
        end.should change(@emails, :length).by(1)
      end
      
      it "should not send an email when recipient's notifications are off" do
        @recipient.toggle!(:blog_comment_notifications)
        @recipient.blog_comment_notifications.should == false
        lambda do
          @comment.save
        end.should_not change(@emails, :length)
      end
      
      it "should not send an email when global notifications are off" do
        @global_prefs.update_attributes(:email_notifications => false)
        lambda do
          @comment.save
        end.should_not change(@emails, :length)
      end
      
      it "should not send an email for an own-comment" do
        lambda do
          @post.comments.create(:body => "Hey there",
                                :commenter => @post.blog.person)
        end.should_not change(@emails, :length)
      end
    end
  end
 
  describe "wall comments" do
  
    before(:each) do
      @person = people(:quentin)
      @comment = @person.comments.new(:body => "Hey there",
                                      :commenter => people(:aaron))
    end
  
    it "should be valid" do
      @comment.should be_valid
    end
  
    it "should require a body" do
      comment = @person.comments.new
      comment.should_not be_valid
      comment.errors.on(:body).should_not be_empty
    end
  
    it "should have a maximum body length" do
      @comment.should have_maximum(:body, SMALL_TEXT_LENGTH)
    end
  
    it "should increase the comment count" do
      old_count = @person.comments.count
      @comment.save!
      @person.comments.count.should == old_count + 1
    end
  
    describe "associations" do
    
      before(:each) do
        @comment.save!
      end
 
      it "should have an activity" do
        Activity.find_by_item_id(@comment).should_not be_nil
      end
    end
    
    describe "email notifications" do
    
      before(:each) do
        @emails = ActionMailer::Base.deliveries
        @emails.clear
        @global_prefs = Preference.find(:first)
        @recipient = @comment.commented_person
      end
    
      it "should send an email when global/recipient notifications are on" do
        # Both notifications are on by default.
        lambda do
          @comment.save
        end.should change(@emails, :length).by(1)
      end
    
      it "should not send an email when recipient's notifications are off" do
        @recipient.toggle!(:wall_comment_notifications)
        @recipient.wall_comment_notifications.should == false
        lambda do
          @comment.save
        end.should_not change(@emails, :length)
      end
    
      it "should not send an email when global notifications are off" do
        @global_prefs.update_attributes(:email_notifications => false)
        lambda do
          @comment.save
        end.should_not change(@emails, :length)
      end
      
      it "should not send an email for an own-comment" do
        lambda do
          @person.comments.create(:body => "Hey there",
                                  :commenter => @person)
        end.should_not change(@emails, :length)
      end
    end
  end
  
  describe "feed items for contacts" do
 
    before(:each) do
      @person = people(:quentin)
      @contact = people(:aaron)
      @second_contact = people(:kelly)
      Connection.connect(@person, @contact)
      Connection.connect(@person, @second_contact)
    end
    
    # When one of your contacts comments on his own wall,
    # an activity might get created for both the commenter and the commented.
    # Here they are the same person, so only one item should be craeted.
    it "should not have duplicate feed items when commenting on own wall" do
      Connection.connected?(@person, @contact).should be_true
      @contact.comments.create(:body => "Hey there", :commenter => @contact)
      @person.activities.should have_distinct_elements
    end
    
    it %(should not have duplicate feed items for Quentin
when Kelly comments on Aaron's wall) do
      @contact.comments.create(:body => "bar", :commenter => @second_contact)
      @person.activities.should have_distinct_elements
    end
  end
end