cirandas.net

ref: master

test/unit/task_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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
require_relative "../test_helper"

class TaskTest < ActiveSupport::TestCase

  def setup
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
  end

  def test_relationship_with_requestor
    t = Task.create
    assert_raise ActiveRecord::AssociationTypeMismatch do
      t.requestor = 1
    end
    assert_nothing_raised do
      t.requestor = Profile.new
    end
  end

  should 'target be able to polymorphic relationship' do
    t = Task.create
    assert_nothing_raised do
      t.target = Environment.new
    end
    assert_nothing_raised do
      t.target = Profile.new
    end
  end

  def test_should_call_perform_in_finish
    TaskMailer.expects(:generic_message).with('task_finished', anything)
    t = Task.create
    t.requestor = sample_user
    t.expects(:perform)
    t.finish
    assert_equal Task::Status::FINISHED, t.status
  end

  def test_should_have_cancelled_status_after_cancel
    TaskMailer.expects(:generic_message).with('task_cancelled', anything)
    t = Task.create
    t.requestor = sample_user
    t.cancel
    assert_equal Task::Status::CANCELLED, t.status
  end

  def test_should_start_with_active_status
    t = Task.create
    assert_equal Task::Status::ACTIVE, t.status
  end

  def test_should_notify_finish
    t = Task.create
    t.requestor = sample_user

    TaskMailer.expects(:generic_message).with('task_finished', t)

    t.finish
  end

  def test_should_notify_cancel
    t = Task.create
    t.requestor = sample_user

    TaskMailer.expects(:generic_message).with('task_cancelled', t)

    t.cancel
  end

  def test_should_not_notify_when_perform_fails
    count = Task.count

    t = Task.create
    class << t
      def perform
        raise RuntimeError
      end
    end

    t.expects(:notify_requestor).never
    assert_raise RuntimeError do
      t.finish
    end
  end

  should 'provide a description method' do
    requestor = create_user('requestor').person
    assert_kind_of Hash, build(Task, :requestor => requestor).information
  end

  should 'notify just after the task is created' do
    task = Task.new
    task.requestor = sample_user

    TaskMailer.expects(:generic_message).with('task_created', task)
    task.save!
  end

  should 'not notify if the task is hidden' do
    task = build(Task, :status => Task::Status::HIDDEN)
    task.requestor = sample_user

    TaskMailer.expects(:generic_message).with('task_created', anything).never
    task.save!
  end

  should 'generate a random code before validation' do
    Task.expects(:generate_code)
    Task.new.valid?
  end

  should 'make sure that codes are unique' do
    task1 = Task.create!
    task2 = build(Task, :code => task1.code)

    refute task2.valid?
    assert task2.errors[:code.to_s].present?
  end

  should 'generate a code with chars from a-z and 0-9' do
    code = Task.generate_code
    assert(code =~ /^[a-z0-9]+$/)
    assert_equal 36, code.size
  end

  should 'find only in active tasks' do
    task = Task.new
    task.requestor = sample_user
    task.save!

    task.cancel

    assert_equal [], Task.from_code(task.code)
  end

  should 'be able to find active tasks' do
    task = Task.new
    task.requestor = sample_user
    task.save!

    assert_not_nil Task.from_code(task.code)
  end

  should 'use 36-chars codes by default' do
    assert_equal 36, Task.create.code.size
  end

  should 'be able to limit the length of the generated code' do
    assert_equal 3, Task.create!(:code_length => 3).code.size
    assert_equal 7, Task.create!(:code_length => 7).code.size
  end

  should 'throws exception when try to send target_notification_message in Task base class' do
    task = Task.new
    assert_raise NotImplementedError do
      task.target_notification_message
    end
  end

  should 'send notification to target just after task creation' do
    task = Task.new
    target = fast_create(Profile)
    target.stubs(:notification_emails).returns(['adm@example.com'])
    task.target = target
    task.stubs(:target_notification_message).returns('some non nil message to be sent to target')

    mailer = mock
    mailer.expects(:deliver).once
    TaskMailer.expects(:target_notification).returns(mailer).once
    task.save!
  end

  should 'not send notification to target if the task is hidden' do
    task = build(Task, :status => Task::Status::HIDDEN)
    target = fast_create(Profile)
    target.stubs(:notification_emails).returns(['adm@example.com'])
    task.target = target
    task.stubs(:target_notification_message).returns('some non nil message to be sent to target')

    TaskMailer.expects(:target_notification).never
    task.save!
  end

  should 'not send notification to target if notification is disabled in profile' do
    task = Task.new
    target = fast_create(Organization)
    target.stubs(:notification_emails).returns(['adm@example.com'])
    target.stubs(:profile_admin_mail_notification).returns(false)
    task.target = target
    task.stubs(:target_notification_message).returns('some non nil message to be sent to target')
    TaskMailer.expects(:target_notification).never
    task.save!
  end

  should 'send notification to target if notification is enabled in profile' do
    task = Task.new
    target = fast_create(Organization)
    target.stubs(:notification_emails).returns(['adm@example.com'])
    target.stubs(:profile_admin_mail_notification).returns(true)
    task.target = target
    task.stubs(:target_notification_message).returns('some non nil message to be sent to target')

    mailer = mock
    mailer.expects(:deliver).once
    TaskMailer.expects(:target_notification).returns(mailer).once
    task.save!
  end

  should 'be able to list pending tasks' do
    Task.delete_all
    t1 = Task.create!
    t2 = Task.create!
    t2.finish
    t3 = Task.create!

    assert_equivalent [t1,t3], Task.pending
  end

  should 'be able to list finished tasks' do
    Task.delete_all
    t1 = Task.create!
    t2 = Task.create!
    t2.finish
    t3 = Task.create!

    assert_equal [t2], Task.finished
  end

  should 'has perform task permission' do
    t = Task.new
    assert_equal :perform_task, t.permission
  end

  should 'be destroyed when requestor destroyed' do
    user = create_user('test_user').person
    assert_no_difference 'Task.count' do
      create(Task, :requestor => user)
      user.destroy
    end
  end

  should 'not deliver notification message to target' do
    task = Task.new
    assert_raise NotImplementedError do
      task.target_notification_message
    end
  end

  should 'not send message when created, finished or cancelled' do
    task = Task.new
    %w[ created finished cancelled ].each do |action|
      assert_raise NotImplementedError do
        task.send("task_#{action}_message")
      end
    end
  end

  should 'not notify target if message is nil' do
    task = Task.new
    task.stubs(:target_notification_message).returns(nil)
    TaskMailer.expects(:target_notification).never
    task.save!
  end

  should 'not notify target if notification emails is empty' do
    task = Task.new
    target = Profile.new
    target.stubs(:notification_emails).returns([])
    task.target = target
    task.stubs(:target_notification_message).returns('some non nil message to be sent to target')
    TaskMailer.expects(:target_notification).never
    task.save!
  end

  should 'the environment method be defined' do
    task = Task.new
    assert task.method_exists?('environment')
  end

  should 'the task environment method return the target environment' do
    task = Task.new
    target = build(Profile, :environment => Environment.new)
    task.target = target
    assert_equal task.environment, target.environment
  end

  should 'the task environment method return nil if the target task is nil' do
    task = Task.new
    assert_equal task.environment, nil
  end

  should 'have blank string on target_notification_description in Task base class' do
    task = Task.new
    assert_equal '', task.target_notification_description
  end

  should 'activate task' do
    task = build(Task, :status => Task::Status::HIDDEN)
    task.activate
    assert_equal Task::Status::ACTIVE, task.status
  end

  should 'notify just after the task is activated' do
    task = build(Task, :status => Task::Status::HIDDEN)
    task.requestor = sample_user
    task.save!

    TaskMailer.expects(:generic_message).with('task_activated', task)
    task.activate
  end

  should 'send notification message to target just after task activation' do
    task = build(Task, :status => Task::Status::HIDDEN)
    target = fast_create(Profile)
    target.stubs(:notification_emails).returns(['target@example.com'])
    task.target = target
    task.save!
    task.stubs(:target_notification_message).returns('some non nil message to be sent to target')

    mailer = mock
    mailer.expects(:deliver).once
    TaskMailer.expects(:target_notification).returns(mailer).once
    task.activate
  end

  should 'filter tasks by type with scope' do
    class CleanHouse < Task; end
    class FeedDog < Task; end
    requestor = fast_create(Person)
    target = fast_create(Person)
    t1 = create(CleanHouse, :requestor => requestor, :target => target)
    t2 = create(CleanHouse, :requestor => requestor, :target => target)
    t3 = create(FeedDog, :requestor => requestor, :target => target)
    type = t1.type

    assert_includes Task.of(type), t1
    assert_includes Task.of(type), t2
    assert_not_includes Task.of(type), t3
    assert_includes Task.of(nil), t3
  end

  should 'order tasks by some attribute correctly' do
    Task.destroy_all
    t1 = fast_create(Task, :status => 4, :created_at => Time.now.in_time_zone + 1.hour)
    t2 = fast_create(Task, :status => 3, :created_at => Time.now.in_time_zone + 2.hour)
    t3 = fast_create(Task, :status => 2, :created_at => Time.now.in_time_zone + 3.hour)
    t4 = fast_create(Task, :status => 1, :created_at => Time.now.in_time_zone + 4.hour)

    assert_equal [t1,t2,t3,t4], Task.order_by('created_at', 'asc')
    assert_equal [t4,t3,t2,t1], Task.order_by('created_at', 'desc')
    assert_equal [t1,t2,t3,t4], Task.order_by('status', 'desc')
    assert_equal [t4,t3,t2,t1], Task.order_by('status', 'asc')
  end

  should 'retrieve tasks by status' do
    pending = fast_create(Task, :status => Task::Status::ACTIVE)
    hidden = fast_create(Task, :status => Task::Status::HIDDEN)
    finished = fast_create(Task, :status => Task::Status::FINISHED)
    canceled = fast_create(Task, :status => Task::Status::CANCELLED)

    assert_includes Task.pending, pending
    assert_not_includes Task.pending, hidden
    assert_not_includes Task.pending, finished
    assert_not_includes Task.pending, canceled

    assert_not_includes Task.hidden, pending
    assert_includes Task.hidden, hidden
    assert_not_includes Task.hidden, finished
    assert_not_includes Task.hidden, canceled

    assert_not_includes Task.finished, pending
    assert_not_includes Task.finished, hidden
    assert_includes Task.finished, finished
    assert_not_includes Task.finished, canceled

    assert_not_includes Task.canceled, pending
    assert_not_includes Task.canceled, hidden
    assert_not_includes Task.canceled, finished
    assert_includes Task.canceled, canceled

    assert_includes Task.opened, pending
    assert_includes Task.opened, hidden
    assert_not_includes Task.opened, finished
    assert_not_includes Task.opened, canceled

    assert_not_includes Task.closed, pending
    assert_not_includes Task.closed, hidden
    assert_includes Task.closed, finished
    assert_includes Task.closed, canceled
  end

  should 'be ham by default' do # ham means not spam
    assert_equal false, Task.create.spam
  end

  should 'be able to mark tasks as spam/ham/unknown' do
    t = Task.new
    t.spam = true
    assert t.spam?
    refute t.ham?

    t.spam = false
    assert t.ham?
    refute t.spam?

    t.spam = nil
    refute t.spam?
    refute t.ham?
  end

  should 'be able to select non-spam tasks' do
    t1 = fast_create(Task)
    t2 = fast_create(Task, :spam => false)
    t3 = fast_create(Task, :spam => true)

    assert_equivalent [t1,t2], Task.without_spam
  end

  should 'be able to select spam tasks' do
    t1 = fast_create(Task)
    t2 = fast_create(Task, :spam => false)
    t3 = fast_create(Task, :spam => true)

    assert_equivalent [t3], Task.spam
  end

  should 'be able to mark as spam' do
    t1 = fast_create(Task)
    t1.spam!
    t1.reload
    assert t1.spam?
  end

  should 'be able to mark as ham' do
    t1 = fast_create(Task)
    t1.ham!
    t1.reload
    assert t1.ham?
  end

  should 'be able to assign a responsible to a task' do
    person = fast_create(Person)
    task = fast_create(Task)
    task.responsible = person
    task.save!
    assert_equal person, task.responsible
  end

  should 'store who finish the task' do
    t = Task.create
    person = fast_create(Person)
    t.finish(person)
    assert_equal person, t.reload.closed_by
  end

  should 'store who cancel the task' do
    t = Task.create
    person = fast_create(Person)
    t.cancel(person)
    assert_equal person, t.reload.closed_by
  end

  should 'named scope to get tasks of a profile' do
    requestor = fast_create(Person)
    person = fast_create(Person)
    another_person = fast_create(Person)
    t1 = create(Task, :requestor => requestor, :target => person)
    t2 = create(Task, :requestor => requestor, :target => another_person)
    t3 = create(Task, :requestor => requestor, :target => person)

    assert_equivalent [t1,t3], Task.to(person)
  end

  should 'named scope to get environment tasks if passed profile is environment admin' do
    requestor = fast_create(Person)
    person = fast_create(Person)
    another_person = fast_create(Person)
    environment = Environment.default
    environment.add_admin(person)
    t1 = create(Task, :requestor => requestor, :target => person)
    t2 = create(Task, :requestor => requestor, :target => another_person)
    t3 = create(Task, :requestor => requestor, :target => environment)

    assert_equivalent [t1,t3], Task.to(person)
  end

  should 'named scope to not get environment tasks if passed profile is not environment admin' do
    requestor = fast_create(Person)
    person = fast_create(Person)
    another_person = fast_create(Person)
    environment = Environment.default
    t1 = create(Task, :requestor => requestor, :target => person)
    t2 = create(Task, :requestor => requestor, :target => another_person)
    t3 = create(Task, :requestor => requestor, :target => environment)

    assert_equivalent [t1], Task.to(person)
  end

  should 'named scope to get communities tasks if passed profile has perform_task permission' do
    requestor = fast_create(Person)
    person = fast_create(Person)
    another_person = fast_create(Person)
    community = fast_create(Community)
    community.add_member(person)
    person.expects(:has_permission?).with(:perform_task, community).returns(true)
    t1 = create(Task, :requestor => requestor, :target => person)
    t2 = create(Task, :requestor => requestor, :target => another_person)
    t3 = create(Task, :requestor => requestor, :target => community)

    assert_equivalent [t1, t3], Task.to(person)
  end


  should 'named scope to not get communities tasks if passed profile has no perform_task permission in community' do
    requestor = fast_create(Person)
    person = fast_create(Person)
    another_person = fast_create(Person)
    community = fast_create(Community)
    community.add_member(person)
    person.expects(:has_permission?).with(:perform_task, community).returns(false)
    t1 = create(Task, :requestor => requestor, :target => person)
    t2 = create(Task, :requestor => requestor, :target => another_person)
    t3 = create(Task, :requestor => requestor, :target => community)

    assert_equivalent [t1], Task.to(person)
  end

  should 'named scope to return environment, person and communities tasks if user has permission' do
    requestor = fast_create(Person)
    person = fast_create(Person)
    another_person = fast_create(Person)
    community = fast_create(Community)
    community.add_member(person)
    person.expects(:has_permission?).with(:perform_task, community).returns(true)
    environment = Environment.default
    environment.add_admin(person)
    t1 = create(Task, :requestor => requestor, :target => person)
    t2 = create(Task, :requestor => requestor, :target => another_person)
    t3 = create(Task, :requestor => requestor, :target => community)
    t4 = create(Task, :requestor => requestor, :target => environment)
    assert_equivalent [t1,t3,t4], Task.to(person)
  end

  protected

  def sample_user
    user = build(User, :login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain')
    user.build_person(person_data)
    user.save
    user.person
  end

end