create_enterprise_test.rb
3.78 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
require File.dirname(__FILE__) + '/../test_helper'
class CreateEnterpriseTest < Test::Unit::TestCase
should 'provide needed data' do
task = CreateEnterprise.new
%w[ name identifier address contact_phone contact_person acronym foundation_year legal_form economic_activity management_information ].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.foundation_year = "test"
task.valid?
assert task.errors.invalid?(:foundation_year)
task.foundation_year = 2006
task.valid?
assert !task.errors.invalid?(:foundation_year)
end
should 'require a requestor' do
task = CreateEnterprise.new
task.valid?
assert task.errors.invalid?(:requestor_id)
task.requestor = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person
task.valid?
assert !task.errors.invalid?(:requestor_id)
end
should 'require a target (validator organization)' do
task = CreateEnterprise.new
task.valid?
assert task.errors.invalid?(:target_id)
task.target = Organization.create!(:name => "My organization", :identifier => 'validator_organization')
task.valid?
assert !task.errors.invalid?(:target_id)
end
should 'require that the informed target (validator organization) actually validates for the chosen region' do
environment = Environment.create!(:name => "My environment")
region = Region.create!(:name => 'My region', :environment_id => environment.id)
validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id)
task = CreateEnterprise.new
task.region = region
task.target = validator
task.valid?
assert task.errors.invalid?(:target)
region.validators << validator
task.valid?
assert !task.errors.invalid?(:target)
end
should 'cancel task when rejected ' do
task = CreateEnterprise.new
task.expects(:cancel)
task.reject
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.destroy_all
environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain', :is_default => true)
region = Region.create!(:name => 'My region', :environment_id => environment.id)
validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id)
region.validators << validator
person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person
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_id => person.id,
:target_id => validator.id,
})
enterprise = Enterprise.new
Enterprise.expects(:new).returns(enterprise)
task.finish
assert !enterprise.new_record?
assert_equal person.user, enterprise.user
end
should 'override message methods from Task' do
generic = Task.new
specific = CreateEnterprise.new
Task.instance_methods.select { |m| m =~ /_message$/ }.each do |method|
assert_not_equal generic.send(method), specific.send(method)
end
end
end