cirandas.net

ref: master

test/unit/scrap_test.rb


  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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
require_relative "../test_helper"

class ScrapTest < ActiveSupport::TestCase

  def setup
    Person.destroy_all
    Scrap.destroy_all
    ActionTracker::Record.destroy_all
    Delayed::Job.destroy_all
  end

  should "have the content" do
    s = Scrap.new
    s.valid?
    assert s.errors[:content.to_s].present?

    s.content = ''
    s.valid?
    assert s.errors[:content.to_s].present?

    s.content = 'some content'
    s.valid?
    refute s.errors[:content.to_s].present?
  end

  should "have the sender" do
    s = Scrap.new
    s.valid?
    assert s.errors[:sender_id.to_s].present?

    s.sender_id = 1
    s.valid?
    refute s.errors[:sender_id.to_s].present?
  end

  should "have the receiver" do
    s = Scrap.new
    s.valid?
    assert s.errors[:receiver_id.to_s].present?

    s.receiver_id = 1
    s.valid?
    refute s.errors[:receiver_id.to_s].present?
  end

  should "be associated to Person as sender" do
    person = create_user.person
    s = Scrap.new
    assert_nothing_raised do
      s.sender = person
    end
  end

  should "be associated to Person as receiver" do
    person = create_user.person
    s = Scrap.new
    assert_nothing_raised do
      s.receiver = person
    end
  end

  should "be associated to Community as receiver" do
    community = fast_create(Community)
    s = Scrap.new
    assert_nothing_raised do
      s.receiver = community
    end
  end

  should "collect all scraps sent and received of a person" do
    person = create_user.person
    s1 = fast_create(Scrap, :sender_id => person.id)
    assert_equal [s1], Scrap.all_scraps(person)
    s2 = fast_create(Scrap, :sender_id => person.id)
    assert_equal [s1,s2], Scrap.all_scraps(person)
    s3 = fast_create(Scrap, :receiver_id => person.id)
    assert_equal [s1,s2,s3], Scrap.all_scraps(person)
  end

  should "collect all scraps sent and received of a community" do
    community = fast_create(Community)
    person = create_user.person
    s1 = fast_create(Scrap, :sender_id => person.id)
    assert_equal [], Scrap.all_scraps(community)
    s2 = fast_create(Scrap, :receiver_id => community.id, :sender_id => person.id)
    assert_equal [s2], Scrap.all_scraps(community)
    s3 = fast_create(Scrap, :receiver_id => community.id)
    assert_equal [s2,s3], Scrap.all_scraps(community)
  end

  should "create the leave_scrap action tracker verb on scrap creation of one user to another" do
    User.current = create_user
    p1 = User.current.person
    p2 = create_user.person
    s = Scrap.new
    s.sender= p1
    s.receiver= p2
    s.content = 'some content'
    s.save!
    ta = ActionTracker::Record.last
    assert_equal s.content, ta.params['content']
    assert_equal s.sender.name, ta.params['sender_name']
    assert_equal s.receiver.name, ta.params['receiver_name']
    assert_equal s.receiver.url, ta.params['receiver_url']
    assert_equal 'leave_scrap', ta.verb
    assert_equal p1, ta.user
  end

  should "create the leave_scrap action tracker verb on scrap creation of one user to community" do
    User.current = create_user
    p = User.current.person
    c = fast_create(Community)
    s = Scrap.new
    s.sender= p
    s.receiver= c
    s.content = 'some content'
    s.save!
    ta = ActionTracker::Record.last
    assert_equal s.content, ta.params['content']
    assert_equal s.sender.name, ta.params['sender_name']
    assert_equal s.receiver.name, ta.params['receiver_name']
    assert_equal s.receiver.url, ta.params['receiver_url']
    assert_equal 'leave_scrap', ta.verb
    assert_equal p, ta.user
    assert_equal c, ta.target
  end

  should "notify leave_scrap action tracker verb to followers and itself" do
    User.current = create_user
    p1 = User.current.person
    p2 = create_user.person
    p2.add_friend(p1)
    process_delayed_job_queue
    s = Scrap.new
    s.sender= p1
    s.receiver= p2
    s.content = 'some content'
    s.save!
    assert_difference 'ActionTrackerNotification.count', 2 do
      process_delayed_job_queue
    end
    ActionTrackerNotification.all.map(&:profile).map do |profile|
      assert [p1,p2].include?(profile)
    end
  end

  should "notify leave_scrap action tracker verb to members of the communities and the community itself" do
    User.current = create_user
    p = User.current.person
    c = fast_create(Community)
    c.add_member(p)
    ActionTrackerNotification.delete_all
    Delayed::Job.delete_all
    s = Scrap.new
    s.sender= p
    s.receiver= c
    s.content = 'some content'
    s.save!
    assert_difference 'ActionTrackerNotification.count', 2 do
      process_delayed_job_queue
    end
    assert_equal 2, ActionTrackerNotification.count
    ActionTrackerNotification.all.map{|a|a.profile}.map do |profile|
      assert [p,c].include?(profile)
    end
  end

  should "create the leave_scrap_to_self action tracker verb on scrap creation of one user to itself" do
    User.current = create_user
    p = User.current.person
    s = Scrap.new
    s.sender= p
    s.receiver= p
    s.content = 'some content'
    s.save!
    ta = ActionTracker::Record.last
    assert_equal s.content, ta.params['content']
    assert_equal s.sender.name, ta.params['sender_name']
    assert_equal 'leave_scrap_to_self', ta.verb
    assert_equal p, ta.user
  end

  should "notify leave_scrap_to_self action tracker verb to followers and itself" do
    User.current = create_user
    p1 = User.current.person
    p2 = create_user.person
    p2.add_friend(p1)
    ActionTrackerNotification.delete_all
    Delayed::Job.delete_all
    s = Scrap.new
    s.sender= p1
    s.receiver= p1
    s.content = 'some content'
    s.save!
    assert_difference 'ActionTrackerNotification.count', 2 do
      process_delayed_job_queue
    end
    ActionTrackerNotification.all.map{|a|a.profile}.map do |profile|
      assert [p1,p2].include?(profile)
    end
  end

  should "get replies of a scrap" do
    s = fast_create(Scrap)
    s1 = fast_create(Scrap, :scrap_id => s.id)
    s2 = fast_create(Scrap)
    s3 = fast_create(Scrap, :scrap_id => s.id)
    assert_equal [s1,s3], s.replies
  end

  should "get only replies scrap" do
    s0 = fast_create(Scrap)
    s1 = fast_create(Scrap, :scrap_id => s0.id)
    s2 = fast_create(Scrap)
    s3 = fast_create(Scrap, :scrap_id => s0.id)
    assert_equal [s0,s2], Scrap.not_replies
  end

  should "remove the replies is the root is removed" do
    s = fast_create(Scrap)
    s1 = fast_create(Scrap, :scrap_id => s.id)
    s2 = fast_create(Scrap, :scrap_id => s.id)
    assert_equal [s1,s2], s.replies
    assert_equal 3, Scrap.count
    s.destroy
    assert_equal 0, Scrap.count
  end

  should "update the scrap on reply creation" do
    person = create_user.person
    s = create(Scrap, sender: person, receiver: person, updated_at: DateTime.parse('2010-01-01'))
    assert_equal DateTime.parse('2010-01-01'), s.updated_at.strftime('%Y-%m-%d')
    DateTime.stubs(:now).returns(DateTime.parse('2010-09-07'))
    s1 = create(Scrap, :content => 'some content', :sender => person, :receiver => person, :scrap_id => s.id)
    s.reload
    assert_not_equal DateTime.parse('2010-01-01'), s.updated_at.strftime('%Y-%m-%d')
  end

  should "have the root defined" do
    s = fast_create(Scrap)
    s1 = fast_create(Scrap, :scrap_id => s.id)
    s2 = fast_create(Scrap, :scrap_id => s.id)
    assert_equal s, s1.root
    assert_equal s, s2.root
  end

  should "have the top_root defined" do
    s = fast_create(Scrap)
    s1 = fast_create(Scrap, :scrap_id => s.id)
    s2 = fast_create(Scrap, :scrap_id => s1.id)
    assert_equal s, s1.top_root
    assert_equal s, s2.top_root
  end

  should 'strip all html tags' do
    s, r = create_user.person, create_user.person
    s = build Scrap, :sender => s, :receiver => r, :content => "<p>Test <b>Rails</b></p>"
    assert_equal "Test Rails", s.strip_all_html_tags
  end

  should 'strip html before save' do
    s, r = create_user.person, create_user.person
    s = build Scrap, :sender => s, :receiver => r, :content => "<p>Test <b>Rails</b></p>"
    s.save!
    assert_equal "Test Rails", s.reload.content
  end

  should 'strip html before validate' do
    s, r = create_user.person, create_user.person
    s = build Scrap, :sender => s, :receiver => r, :content => "<p><b></b></p>"
    refute s.valid?
    s.content = "<p>Test</p>"
    assert s.valid?
  end

  should 'the action_tracker_target be the community when the scraps has the community as receiver' do
    scrap = Scrap.new
    assert_equal scrap, scrap.action_tracker_target

    community = fast_create(Community)
    scrap.receiver = community
    assert_equal community, scrap.action_tracker_target
  end

  should 'scrap wall url be the root scrap receiver url if it is a reply' do
    p1, p2 = create_user.person, create_user.person
    r = create Scrap, :sender => p1, :receiver => p2, :content => "Hello!"
    s = build Scrap, :sender => p2, :receiver => p1, :content => "Hi!"
    r.replies << s; s.reload
    assert_equal s.scrap_wall_url, s.root.receiver.wall_url
  end

  should 'scrap wall url be the scrap receiver url if it is not a reply' do
    p1, p2 = create_user.person, create_user.person
    s = create Scrap, :sender => p1, :receiver => p2, :content => "Hello!"
    assert_equal s.scrap_wall_url, s.receiver.wall_url
  end

  should 'create activity with reply_scrap_on_self when top_root scrap receiver is the same as sender' do
    User.current = create_user
    s, r = User.current.person, create_user.person
    root = create(Scrap, :sender_id => s.id, :receiver_id => r.id)
    assert_difference 'ActionTracker::Record.count', 1 do
      reply = create(Scrap, :sender => r, :receiver => s, :scrap_id => root.id, :content => 'sample')
    end

    activity = ActionTracker::Record.last
    assert_equal 'reply_scrap_on_self', activity.verb.to_s
    assert_equal s.name, activity.get_receiver_name
    assert_equal s.url, activity.get_receiver_url
  end

  should 'create profile activity' do
    p1, p2 = create_user.person, create_user.person
    s = create Scrap, :sender => p1, :receiver => p2, :content => "Hello!"
    assert_equal s, p2.activities.first.activity
  end

  should 'display to anyone if nobody marked' do
    assert Scrap.new.display_to?(fast_create(Person))
  end

  should 'display only to marked people' do
    scrap = Scrap.new
    u1 = mock
    u2 = mock
    u3 = mock
    scrap.stubs(:marked_people).returns([u1, u3])

    assert scrap.display_to?(u1)
    refute scrap.display_to?(u2)
    assert scrap.display_to?(u3)
  end

end