cirandas.net

ref: master

test/unit/create_enterprise_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
require_relative "../test_helper"

class CreateEnterpriseTest < ActiveSupport::TestCase

  def setup
    @person = fast_create(Person)
  end
  attr_reader :person

  should 'provide needed data' do
    task = CreateEnterprise.new

    %w[ name identifier address contact_phone contact_person acronym foundation_year economic_activity ].each do |field|
      assert task.respond_to?(field)
      assert task.respond_to?("#{field}=")
    end
  end

  should 'accept only numbers as foundation year' do
    task = CreateEnterprise.new
    task.stubs(:environment).returns(Environment.default)

    task.foundation_year = "test"
    task.valid?
    assert task.errors[:foundation_year].present?

    task.foundation_year = 2006
    task.valid?
    refute task.errors[:foundation_year].present?
  end

  should 'require a requestor' do
    task = CreateEnterprise.new
    task.stubs(:environment).returns(Environment.default)
    task.valid?

    assert task.errors[:requestor_id.to_s].present?
    task.requestor = create_user('testuser').person
    task.valid?
    refute task.errors[:requestor_id.to_s].present?
  end

  should 'require a target (validator organization)' do
    task = CreateEnterprise.new
    task.stubs(:environment).returns(Environment.default)
    task.valid?

    assert task.errors[:target_id.to_s].present?
    task.target = Organization.create!(:name => "My organization", :identifier => 'validator_organization')

    task.valid?
    refute task.errors[:target_id.to_s].present?
  end

  should 'require that the informed target (validator organization) actually validates for the chosen region' do
    environment = fast_create(Environment)
    region = fast_create(Region, :name => 'My region', :environment_id => environment.id)
    validator = fast_create(Organization, :name => "My organization", :identifier => 'myorg', :environment_id => environment.id)

    task = CreateEnterprise.new
    task.stubs(:environment).returns(Environment.default)

    task.region = region
    task.target = validator

    task.valid?
    assert task.errors[:target.to_s].present?

    region.validators << validator

    task.valid?
    refute task.errors[:target.to_s].present?
  end

  should 'cancel task when rejected ' do
    task = CreateEnterprise.new
    task.expects(:cancel)
    task.reject
  end

  should 'require an explanation for rejecting enterprise creation' do
    task = CreateEnterprise.new
    task.stubs(:environment).returns(Environment.default)
    task.reject_explanation = nil

    task.valid?
    refute task.errors[:reject_explanation.to_s].present?

    task.status = Task::Status::CANCELLED
    task.valid?
    assert task.errors[:reject_explanation.to_s].present?

    task.reject_explanation = 'bla bla bla'
    task.valid?
    refute task.errors[:reject_explanation.to_s].present?
  end

  should 'finish task when approved' do
    task = CreateEnterprise.new
    task.expects(:finish)
    task.approve
  end

  should 'actually create an enterprise when finishing the task and associate the task requestor as its owner through the "user" association' do

    environment = fast_create(Environment)
    environment.create_roles
    region = fast_create(Region, :name => 'My region', :environment_id => environment.id)
    validator = fast_create(Organization, :name => "My organization", :identifier => 'myorg', :environment_id => environment.id)
    region.validators << validator
    person = create_user('testuser').person
    person.environment = environment
    person.save

    task = CreateEnterprise.create!({
      :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 => person,
      :target => validator,
    })

    enterprise = Enterprise.new
    Enterprise.expects(:new).returns(enterprise)

    task.finish

    refute enterprise.new_record?
    assert_equal person.user, enterprise.user
    assert_equal environment, enterprise.environment

    # the data is not erased
    assert_equal task.name, enterprise.name
  end

  should 'actually create an enterprise when finishing the task and associate the task requestor as its owner through the "user" association even when environment is not default' do

    environment = fast_create(Environment)
    environment.create_roles
    region = fast_create(Region, :name => 'My region', :environment_id => environment.id)
    validator = fast_create(Organization, :name => "My organization", :identifier => 'myorg', :environment_id => environment.id)
    region.validators << validator
    person = create_user('testuser').person
    person.environment = environment
    person.save

    task = CreateEnterprise.create!({
      :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 => person,
      :target => validator,
    })

    enterprise = Enterprise.new
    Enterprise.expects(:new).returns(enterprise)

    task.finish

    refute enterprise.new_record?
    assert_equal person.user, enterprise.user
    assert_equal environment, enterprise.environment

    # the data is not erased
    assert_equal task.name, enterprise.name
  end

  should 'override message methods from Task' do
    specific = CreateEnterprise.new
    specific.stubs(:environment).returns(Environment.default)
    %w[ task_created_message task_finished_message task_cancelled_message ].each do |method|
      assert_nothing_raised NotImplementedError do
        specific.send(method)
      end
    end
  end

  should 'validate that eveything is ok but the validator (target)' do
    environment = fast_create(Environment)
    region = fast_create(Region, :name => 'My region', :environment_id => environment.id)
    validator = fast_create(Organization, :name => "My organization", :identifier => 'myorg', :environment_id => environment.id)
    region.validators << validator
    person = create_user('testuser').person
    task = CreateEnterprise.new({
      :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_id => person.id,
    })

    refute task.valid? && task.valid_before_selecting_target?

    task.target = validator
    assert task.valid?
  end

  should 'provide a message to be sent to the target' do
    task = CreateEnterprise.new
    task.stubs(:environment).returns(Environment.default)
    assert_not_nil task.target_notification_message
  end

  should 'report as approved when approved' do
    request = CreateEnterprise.new
    request.stubs(:status).returns(Task::Status::FINISHED)
    assert request.approved?
  end

  should 'report as rejected when rejected' do
    request = CreateEnterprise.new
    request.stubs(:status).returns(Task::Status::CANCELLED)
    assert request.rejected?
  end

  should 'refuse to create an enterprise creation request with an identifier already used by another profile' do
    request = CreateEnterprise.new
    request.stubs(:environment).returns(Environment.default)
    request.identifier = 'testid'
    request.valid?
    assert request.errors[:identifier].blank?

    Organization.create!(:name => 'test', :identifier => 'testid')
    request.valid?
    assert request.errors[:identifier].present?
  end

  should 'require the same fields as an enterprise does' do
    environment = mock
    request = CreateEnterprise.new
    request.stubs(:environment).returns(environment)
    environment.stubs(:organization_approval_method).returns(:region)

    environment.stubs(:required_enterprise_fields).returns([])
    request.valid?
    assert request.errors[:contact_person].blank?, 'should not require contact_person unless Enterprise requires it'

    environment.stubs(:required_enterprise_fields).returns(['contact_person'])
    request.valid?
    assert request.errors[:contact_person].present?, 'should require contact_person when Enterprise requires it'
  end

  should 'has permission to validate enterprise' do
    t = CreateEnterprise.new
    assert_equal :validate_enterprise, t.permission
  end

  should 'have target notification message' do
    task = CreateEnterprise.new(:name => 'My enterprise', :requestor => person, :target => Environment.default)

    assert_match(/#{task.name}.*requested to enter #{person.environment}.*approve or reject/, task.target_notification_message)
  end

  should 'have target notification description' do
    task = CreateEnterprise.new(:name => 'My enterprise', :requestor => person, :target => Environment.default)

    assert_match(/#{task.requestor.name} wants to create enterprise #{task.subject}/, task.target_notification_description)
  end

  should 'deliver target notification message' do
    task = CreateEnterprise.new(:name => 'My enterprise', :requestor => person, :target => Environment.default)

    email = TaskMailer.target_notification(task, task.target_notification_message).deliver

    assert_match(/#{task.requestor.name} wants to create enterprise #{task.subject}/, email.subject)
  end

end