cirandas.net

ref: master

test/unit/organization_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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
require_relative "../test_helper"

class OrganizationTest < ActiveSupport::TestCase
  fixtures :profiles

  def create_create_enterprise(org)
    region = fast_create(Region, :name => 'some region')
    region.validators << org

    requestor = create_user('testreq').person

    data = {
      :name => 'My new enterprise',
      :identifier => 'mynewenterprise',
      :address => 'satan street, 666',
      :contact_phone => '1298372198',
      :contact_person => 'random joe',
      :legal_form => 'cooperative',
      :economic_activity => 'free software',
      :region_id => region.id,
      :requestor => requestor,
      :target => org,
    }
    create(CreateEnterprise, data)
  end


  should 'not reference organization info' do
    org = Organization.new
    assert_raise NoMethodError do
      org.organization_info
    end
  end

  should 'reference region' do
    org = Organization.new
    assert_raise ActiveRecord::AssociationTypeMismatch do
      org.region = 1
    end
    assert_nothing_raised do
      org.region = Region.new
    end
  end

  should 'reference validation info' do
    org = Organization.new
    assert_raise ActiveRecord::AssociationTypeMismatch do
      org.validation_info = 1
    end
    assert_nothing_raised do
      org.validation_info = ValidationInfo.new
    end
  end

  should 'provide validation methodology' do
    org = Organization.new
    assert_nil org.validation_methodology

    info = ValidationInfo.new
    info.expects(:validation_methodology).returns('something')
    org.validation_info = info
    assert_equal 'something', org.validation_methodology
  end

  should 'provide validation restrictions' do
    org = Organization.new
    assert_nil org.validation_restrictions

    info = ValidationInfo.new
    info.expects(:restrictions).returns('something')
    org.validation_info = info
    assert_equal 'something', org.validation_restrictions
  end

  should 'have contact_email' do
    org = Organization.new
    assert_respond_to org, :contact_email
  end

  should 'validate contact_email if filled' do
    org = Organization.new
    org.valid?
    refute org.errors[:contact_email.to_s].present?

    org.contact_email = ''
    org.valid?
    refute org.errors[:contact_email.to_s].present?


    org.contact_email = 'invalid-email'
    org.valid?
    assert org.errors[:contact_email.to_s].present?

    org.contact_email = 'someone@somedomain.com'
    org.valid?
    refute org.errors[:contact_email.to_s].present?
  end

  should 'list contact_email plus admin emails as "notification emails"' do
    o = build(Organization, :contact_email => 'org@email.com')
    admin1 = mock; admin1.stubs(:email).returns('admin1@email.com')
    admin2 = mock; admin2.stubs(:email).returns('admin2@email.com')
    o.stubs(:admins).returns([admin1, admin2])

    assert_equal ['org@email.com', 'admin1@email.com', 'admin2@email.com'], o.notification_emails
  end

  should 'list only admins if contact_email is nil' do
    o = build(Organization, :contact_email => nil)
    admin1 = mock; admin1.stubs(:email).returns('admin1@email.com')
    admin2 = mock; admin2.stubs(:email).returns('admin2@email.com')
    o.stubs(:admins).returns([admin1, admin2])

    assert_equal ['admin1@email.com', 'admin2@email.com'], o.notification_emails
  end

  should 'list only admins if contact_email is a blank string' do
    o = build(Organization, :contact_email => '')
    admin1 = mock; admin1.stubs(:email).returns('admin1@email.com')
    admin2 = mock; admin2.stubs(:email).returns('admin2@email.com')
    o.stubs(:admins).returns([admin1, admin2])

    assert_equal ['admin1@email.com', 'admin2@email.com'], o.notification_emails
  end

  should 'use the environment contact email if no emails are listed here' do
    o = build(Organization, :contact_email => '', :environment => Environment.default)
    assert_equal [o.environment.contact_email], o.notification_emails
  end

  should 'list pending enterprise validations' do
    org = Organization.new
    assert org.pending_validations.empty?
  end

  should 'be able to find a pending validation by its code' do
    org = fast_create(Organization)

    validation = create_create_enterprise(org)

    ok('should find pending validation by code') { validation == org.find_pending_validation(validation.code) }
  end

  should 'return nil when finding for an unexisting pending validation' do
    org = Organization.new
    assert_nil org.find_pending_validation('xxxxxxxxxxxxxxxxxxx')
  end

  should 'be able to find already processed validations' do
    org = Organization.new
    assert org.processed_validations.empty?
  end

  should 'be able to find an already processed validation by its code' do
    org = fast_create(Organization)
    validation = create_create_enterprise(org)
    validation.finish

    ok('should find processed validation by code') { validation == org.find_processed_validation(validation.code) }
  end

  should 'have boxes and blocks upon creation' do
    profile = create(Organization, :name => 'test org', :identifier => 'testorg')

    assert profile.boxes.size > 0
    assert profile.blocks.size > 0
  end

  should 'have members' do
    assert_equal true, Organization.new.has_members?
  end

  should 'update contact_person' do
    org = fast_create(Organization)
    assert_nil org.contact_person
    org.contact_person = 'person'
    assert_not_nil org.contact_person
  end

  should 'numericality year' do
    count = Organization.count

    org = Organization.new
    org.foundation_year = 'xxxx'
    org.valid?
    assert org.errors[:foundation_year.to_s].present?

    org.foundation_year = 20.07
    org.valid?
    assert org.errors[:foundation_year.to_s].present?

    org.foundation_year = 2007
    org.valid?
    refute  org.errors[:foundation_year.to_s].present?
  end

  should 'has closed' do
    org = Organization.new
    assert_respond_to org, :closed
    assert_respond_to org, :closed?
  end

  should 'allow to add new member' do
    o = fast_create(Organization)
    p = create_user('mytestuser').person

    o.add_member(p)

    assert o.members.include?(p), "Organization should add the new member"
  end

  should 'allow to remove members' do
    c = fast_create(Organization)
    p = create_user('myothertestuser').person

    c.add_member(p)
    assert_includes c.members, p
    c.remove_member(p)
    c.reload
    assert_not_includes c.members, p
  end

  should 'allow to add new moderator' do
    o = fast_create(Organization)
    p = create_user('myanothertestuser').person

    o.add_moderator(p)

    assert o.members.include?(p), "Organization should add the new moderator"
  end

  should 'moderator has moderate_comments permission' do
    o = fast_create(Organization)
    p = create_user('myanothertestuser').person
    o.add_moderator(p)
    assert p.has_permission?(:moderate_comments, o)
  end

  should 'be able to change identifier' do
    o = fast_create(Organization)
    assert_nothing_raised do
      o.identifier = 'test_org_new_url'
    end
  end

  should 'be closed if organization is not public' do
    organization = fast_create(Organization)
    refute organization.closed

    organization.public_profile = false
    organization.save!

    assert organization.closed
  end

  should "the followed_by? be true only to members" do
    o = fast_create(Organization)
    p1 = fast_create(Person)
    p2 = fast_create(Person)
    p3 = fast_create(Person)

    refute p1.is_member_of?(o)
    o.add_member(p1)
    assert p1.is_member_of?(o)

    refute p3.is_member_of?(o)
    o.add_member(p3)
    assert p3.is_member_of?(o)

    assert_equal true, o.send(:followed_by?,p1)
    assert_equal true, o.send(:followed_by?,p3)
    assert_equal false, o.send(:followed_by?,p2)
  end

  should "compose bare jabber id by identifier plus 'conference' and default hostname" do
    org = fast_create(Organization, :identifier => 'online_community')
    assert_equal "online_community@conference.#{org.environment.default_hostname}", org.jid
  end

  should "compose full jabber id by identifier plus 'conference.default_hostname' and short_name as resource" do
    org = fast_create(Organization, :identifier => 'online_community')
    assert_equal "online_community@conference.#{org.environment.default_hostname}/#{org.short_name}", org.full_jid
  end

  should 'find more popular organizations' do
    o1 = fast_create(Organization)
    o2 = fast_create(Organization)

    p1 = fast_create(Person)
    p2 = fast_create(Person)
    o1.add_member(p1)
    assert_order [o1,o2] , Organization.more_popular

    o2.add_member(p1)
    o2.add_member(p2)
    assert_order [o2,o1] , Organization.more_popular
  end

  should 'list organizations that have no members in more popular list' do
    organization = fast_create(Organization)
    assert_includes Organization.more_popular, organization
  end

  should "return no members on label if the organization has no members" do
    organization = fast_create(Organization)
    assert_equal 0, organization.members_count
    assert_equal "no members", organization.more_popular_label
  end

  should "return one member on label if the organization has one member" do
    person = fast_create(Person)
    organization = fast_create(Organization)
    organization.add_member(person)
    organization.reload

    assert_equal "one member", organization.more_popular_label
  end

  should "return the number of members on label if the organization has more than one member" do
    person1 = fast_create(Person)
    person2 = fast_create(Person)
    organization = fast_create(Organization)

    organization.add_member(person1)
    organization.add_member(person2)
    organization.reload
    assert_equal "2 members", organization.more_popular_label

    person3 = fast_create(Person)
    organization.add_member(person3)
    organization.reload
    assert_equal "3 members", organization.more_popular_label
  end

  should 'find more active organizations' do
    person = fast_create(Person)
    p1 = fast_create(Organization)
    p2 = fast_create(Organization)
    p3 = fast_create(Organization)

    ActionTracker::Record.destroy_all
    ActionTracker::Record.create!(:user => person, :target => p1, :verb => 'leave_scrap')
    ActionTracker::Record.create!(:user => person, :target => p2, :verb => 'leave_scrap')
    ActionTracker::Record.create!(:user => person, :target => p2, :verb => 'leave_scrap')
    ActionTracker::Record.create!(:user => person, :target => p3, :verb => 'leave_scrap')
    ActionTracker::Record.create!(:user => person, :target => p3, :verb => 'leave_scrap')
    ActionTracker::Record.create!(:user => person, :target => p3, :verb => 'leave_scrap')

    assert_order [p3,p2,p1] , Organization.more_active
  end

  should 'list profiles that have no actions in more active list' do
    profile = fast_create(Organization)
    assert_includes Organization.more_active, profile
  end

  should 'validates format of cnpj' do
    organization = build(Organization, :cnpj => '239-234.234')
    organization.valid?
    assert organization.errors[:cnpj.to_s].present?

    organization.cnpj = '94.132.024/0001-48'
    organization.valid?
    refute organization.errors[:cnpj.to_s].present?
  end

  should 'get members by role' do
    community = fast_create(Community)
    role1 = Role.create!(:name => 'role1')
    person1 = fast_create(Person)
    community.affiliate(person1, role1)
    role2 = Role.create!(:name => 'role2')
    person2 = fast_create(Person)
    community.affiliate(person2, role2)

    assert_equal [person1], community.members_by_role([role1])
  end

  should 'get members by more than one role' do
    community = fast_create(Community)
    role1 = Role.create!(:name => 'role1')
    person1 = fast_create(Person)
    community.affiliate(person1, role1)
    role2 = Role.create!(:name => 'role2')
    person2 = fast_create(Person)
    community.affiliate(person2, role2)
    role3 = Role.create!(:name => 'role3')
    person3 = fast_create(Person)
    community.affiliate(person3, role3)

    assert_equivalent [person2, person3], community.members_by_role([role2, role3])
  end

  should 'return members by role in a json format' do
    organization = fast_create(Organization)
    p1 = create_user('person-1').person
    p2 = create_user('person-2').person
    role = Profile::Roles.organization_member_roles(organization.environment.id).last

    organization.affiliate(p1, role)
    organization.affiliate(p2, role)
    json = organization.members_by_role_to_json(role)

    assert_match ({:id => p1.id, :name => p1.name}).to_json, json
    assert_match ({:id => p2.id, :name => p2.name}).to_json, json
  end

  should 'disable organization' do
    organization = fast_create(Organization, :visible => true)
    assert organization.visible

    organization.disable
    refute organization.visible
  end

  should 'increase members_count on new membership' do
    member = fast_create(Person)
    organization = fast_create(Organization)
    assert_difference 'organization.members_count', 1 do
      organization.add_member(member)
      organization.reload
    end
  end

  should 'decrease members_count on membership removal' do
    member = fast_create(Person)
    organization = fast_create(Organization)
    organization.add_member(member)
    organization.reload
    assert_difference 'organization.members_count', -1 do
      organization.remove_member(member)
      organization.reload
    end
  end

  should 'check if a community admin user is really a community admin' do
    c = fast_create(Organization, :name => 'my test profile', :identifier => 'mytestprofile')
    admin = create_user('adminuser').person
    c.add_admin(admin)

    assert c.is_admin?(admin)
  end

  should 'a member user not be a community admin' do
    c = fast_create(Organization, :name => 'my test profile', :identifier => 'mytestprofile')
    admin = create_user('adminuser').person
    c.add_admin(admin)

    member = create_user('memberuser').person
    c.add_member(member)
    refute c.is_admin?(member)
  end

  should 'a moderator user not be a community admin' do
    c = fast_create(Organization, :name => 'my test profile', :identifier => 'mytestprofile')
    moderator = create_user('moderatoruser').person
    c.add_moderator(moderator)
    refute c.is_admin?(moderator)
  end

  should 'fetch organizations that are visible for users' do
    person = create_user('some-person').person
    admin = create_user('some-admin').person
    env_admin = create_user('env-admin').person

    o1 = fast_create(Organization, :public_profile => true , :visible => true )
    o1.add_admin(admin)
    o1.add_member(person)

    o2 = fast_create(Organization, :public_profile => true , :visible => true )
    o3 = fast_create(Organization, :public_profile => false, :visible => true )

    o4 = fast_create(Organization, :public_profile => false, :visible => true)
    o4.add_admin(admin)
    o4.add_member(person)

    o5 = fast_create(Organization, :public_profile => true , :visible => false)
    o5.add_admin(admin)
    o5.add_member(person)

    o6 = fast_create(Enterprise, :enabled => false, :visible => true)
    o6.add_admin(admin)

    o7 = fast_create(Organization, :public_profile => false, :visible => false)

    Environment.default.add_admin(env_admin)

    person_orgs    = Organization.visible_for_person(person)
    admin_orgs     = Organization.visible_for_person(admin)
    env_admin_orgs = Organization.visible_for_person(env_admin)

    assert_includes     person_orgs,    o1
    assert_includes     admin_orgs,     o1
    assert_includes     env_admin_orgs, o1

    assert_includes     person_orgs,    o2
    assert_includes     env_admin_orgs, o2
    assert_not_includes person_orgs,    o3
    assert_includes     env_admin_orgs, o3

    assert_includes     person_orgs,    o4
    assert_includes     admin_orgs,     o4
    assert_includes     env_admin_orgs, o4

    assert_not_includes person_orgs,    o5
    assert_includes     admin_orgs,     o5
    assert_includes     env_admin_orgs, o5

    assert_not_includes person_orgs,    o6
    assert_includes     admin_orgs,     o6

    assert_not_includes person_orgs,    o7
    assert_includes     env_admin_orgs, o7
  end

  should 'fetch organizations that are listed for users' do
    person = create_user('some-person').person
    admin = create_user('some-admin').person
    env_admin = create_user('env-admin').person

    o1 = fast_create(Organization, :public_profile => true , :visible => true )
    o1.add_admin(admin)
    o1.add_member(person)

    o2 = fast_create(Organization, :public_profile => true , :visible => true )
    o3 = fast_create(Organization, :public_profile => false, :visible => true )

    o4 = fast_create(Organization, :public_profile => false, :visible => true)
    o4.add_admin(admin)
    o4.add_member(person)

    o5 = fast_create(Organization, :public_profile => true , :visible => false)
    o5.add_admin(admin)
    o5.add_member(person)

    o6 = fast_create(Enterprise, :enabled => false, :visible => true)
    o6.add_admin(admin)

    o7 = fast_create(Organization, :public_profile => false, :visible => false)

    Environment.default.add_admin(env_admin)

    person_orgs    = Organization.listed_for_person(person)
    admin_orgs     = Organization.listed_for_person(admin)
    env_admin_orgs = Organization.listed_for_person(env_admin)

    assert_includes     person_orgs,    o1
    assert_includes     admin_orgs,     o1
    assert_includes     env_admin_orgs, o1

    assert_includes     person_orgs,    o2
    assert_includes     env_admin_orgs, o2
    assert_includes     person_orgs,    o3
    assert_includes     env_admin_orgs, o3

    assert_includes     person_orgs,    o4
    assert_includes     admin_orgs,     o4
    assert_includes     env_admin_orgs, o4

    assert_not_includes person_orgs,    o5
    assert_includes     admin_orgs,     o5
    assert_includes     env_admin_orgs, o5

    assert_not_includes person_orgs,    o6
    assert_includes     admin_orgs,     o6

    assert_not_includes person_orgs,    o7
    assert_includes     env_admin_orgs, o7
  end

  should 'return true at display_private_info_to? when profile is public and user is nil' do
    organization = fast_create(Organization, public_profile: true)
    assert organization.display_private_info_to?(nil)
  end

  should 'return false at display_private_info_to? when profile is public and secret' do
    organization = fast_create(Organization, public_profile: true, secret: true)
    assert !organization.display_private_info_to?(nil)
  end

  should 'return false at display_private_info_to? when profile is public and not visible' do
    organization = fast_create(Organization, public_profile: true, visible: false)
    assert !organization.display_private_info_to?(nil)
  end

  should 'return false at display_private_info_to? when profile is private and user is nil' do
    organization = fast_create(Organization, public_profile: false)
    assert !organization.display_private_info_to?(nil)
  end
end