organization_test.rb
6.86 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
require File.dirname(__FILE__) + '/../test_helper'
class OrganizationTest < Test::Unit::TestCase
  fixtures :profiles
  def create_create_enterprise(org)
    region = Region.create!(:name => 'some region', :environment => Environment.default)
    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 '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 = Organization.create!(:name => 'test org', :identifier => 'testorg')
    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 = Organization.create!(:name => 'test org', :identifier => 'testorg')
    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 = Organization.create!(:name => 'test org', :identifier => 'testorg')
    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 = Organization.create!(:name => 'my test profile', :identifier => 'mytestprofile')
    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 = Organization.create!(:name => 'my other test profile', :identifier => 'myothertestprofile')
    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 = Organization.create!(:name => 'my test profile', :identifier => 'mytestprofile')
    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 = Organization.create!(:name => 'my test profile', :identifier => 'mytestprofile')
    p = create_user('myanothertestuser').person
    o.add_moderator(p)
    assert p.has_permission?(:moderate_comments, o)
  end
end