organization_test.rb
13.8 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
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
require File.dirname(__FILE__) + '/../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,
    }
    CreateEnterprise.create!(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?
    assert !org.errors.invalid?(:contact_email)
    org.contact_email = ''
    org.valid?
    assert !org.errors.invalid?(:contact_email)
    org.contact_email = 'invalid-email'
    org.valid?
    assert org.errors.invalid?(:contact_email)
    org.contact_email = 'someone@somedomain.com'
    org.valid?
    assert !org.errors.invalid?(:contact_email)
  end
  should 'list contact_email plus admin emails as "notification emails"' do
    o = Organization.new(: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 = Organization.new(: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 = Organization.new(: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 'return empty array if contact_email is a blank string and it has no admin' do
    o = Organization.new(:contact_email => '', :environment => Environment.default)
    assert_equal [], o.notification_emails
  end
  should 'list pending enterprise validations' do
    org = Organization.new
    assert_kind_of Array, org.pending_validations
  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_kind_of Array, org.processed_validations
  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 = Organization.create!(: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.invalid?(:foundation_year)
    org.foundation_year = 20.07
    org.valid?
    assert org.errors.invalid?(:foundation_year)
    
    org.foundation_year = 2007
    org.valid?
    assert ! org.errors.invalid?(:foundation_year)
  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)
    assert !organization.closed
    organization.public_profile = false
    organization.save!
    assert organization.closed
  end
  should 'escape malformed html tags' do
    organization = Organization.new
    organization.acronym = "<h1 Malformed >> html >< tag"
    organization.contact_person = "<h1 Malformed >,<<<asfdf> html >< tag"
    organization.contact_email = "<h1<malformed@html.com>>"
    organization.description = "<h1 Malformed /h1>>><<> html ><>h1< tag"
    organization.legal_form = "<h1 Malformed /h1>>><<> html ><>h1< tag"
    organization.economic_activity = "<h1 Malformed /h1>>><<> html ><>h1< tag"
    organization.management_information = "<h1 Malformed /h1>>><<> html ><>h1< tag"
    organization.valid?
    assert_no_match /[<>]/, organization.acronym
    assert_no_match /[<>]/, organization.contact_person
    assert_no_match /[<>]/, organization.contact_email
    assert_no_match /[<>]/, organization.legal_form
    assert_no_match /[<>]/, organization.economic_activity
    assert_no_match /[<>]/, organization.management_information
  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)
    assert !p1.is_member_of?(o)
    o.add_member(p1)
    assert p1.is_member_of?(o)
    assert !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
    Organization.delete_all
    o1 = fast_create(Organization)
    o2 = fast_create(Organization)
    p1 = fast_create(Person)
    p2 = fast_create(Person)
    o1.add_member(p1)
    assert_equal [o1,o2] , Organization.more_popular
    o2.add_member(p1)
    o2.add_member(p2)
    assert_equal [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)
    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)
    assert_equal "2 members", organization.more_popular_label
    person3 = fast_create(Person)
    organization.add_member(person3)
    assert_equal "3 members", organization.more_popular_label
  end
  should 'find more active organizations' do
    person = fast_create(Person)
    Organization.destroy_all
    p1 = fast_create(Organization)
    p2 = fast_create(Organization)
    p3 = fast_create(Organization)
    ActionTracker::Record.destroy_all
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => Time.now, :target_id => p1.id)
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => Time.now, :target_id => p2.id)
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => Time.now, :target_id => p2.id)
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => Time.now, :target_id => p3.id)
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => Time.now, :target_id => p3.id)
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => Time.now, :target_id => p3.id)
    assert_equal [p3,p2,p1] , Organization.more_active
  end
  should 'more active profile take in consideration only actions created only in the recent delay interval' do
    ActionTracker::Record.destroy_all
    recent_delay = ActionTracker::Record::RECENT_DELAY.days.ago
    person = fast_create(Person)
    Organization.destroy_all
    p1 = fast_create(Organization)
    p2 = fast_create(Organization)
    ActionTracker::Record.destroy_all
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => recent_delay, :target_id => p1.id)
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => recent_delay, :target_id => p1.id)
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => recent_delay, :target_id => p2.id)
    fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person, :created_at => recent_delay - 1.day, :target_id => p2.id)
    assert_equal [p1,p2] , 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 = Organization.new(:cnpj => '239-234.234')
    organization.valid?
    assert organization.errors.invalid?(:cnpj)
    organization.cnpj = '94.132.024/0001-48'
    organization.valid?
    assert !organization.errors.invalid?(:cnpj)
  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
    assert !organization.visible
  end
end