enterprises_block_test.rb
6.67 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
require File.dirname(__FILE__) + '/../test_helper'
class EnterprisesBlockTest < ActiveSupport::TestCase
should 'inherit from ProfileListBlock' do
assert_kind_of ProfileListBlock, EnterprisesBlock.new
end
should 'declare its default title' do
EnterprisesBlock.any_instance.stubs(:profile_count).returns(0)
assert_not_equal ProfileListBlock.new.default_title, EnterprisesBlock.new.default_title
end
should 'describe itself' do
assert_not_equal ProfileListBlock.description, EnterprisesBlock.description
end
should 'use its own finder' do
assert_not_equal EnterprisesBlock::Finder, ProfileListBlock::Finder
assert_kind_of EnterprisesBlock::Finder, EnterprisesBlock.new.profile_finder
end
should 'list owner enterprises' do
block = EnterprisesBlock.new
block.limit = 2
owner = mock
block.expects(:owner).at_least_once.returns(owner)
member1 = stub(:id => 1, :visible => true )
member2 = stub(:id => 2, :visible => true )
member3 = stub(:id => 3, :visible => true )
owner.expects(:enterprises).returns([member1, member2, member3])
block.profile_finder.expects(:pick_random).with(3).returns(2)
block.profile_finder.expects(:pick_random).with(2).returns(0)
Profile.expects(:find).with(3).returns(member3)
Profile.expects(:find).with(1).returns(member1)
assert_equal [member3, member1], block.profiles
end
should 'list private enterprises in environment' do
env = Environment.create!(:name => 'test_env')
enterprise1 = fast_create(Enterprise, :environment_id => env.id, :public_profile => true)
enterprise2 = fast_create(Enterprise, :environment_id => env.id, :public_profile => false) #private profile
block = EnterprisesBlock.new
env.boxes.first.blocks << block
block.save!
ids = block.profile_finder.ids
assert_includes ids, enterprise1.id
assert_includes ids, enterprise2.id
end
should 'not list invisible enterprises in environment' do
env = Environment.create!(:name => 'test_env')
enterprise1 = fast_create(Enterprise, :environment_id => env.id, :visible => true)
enterprise2 = fast_create(Enterprise, :environment_id => env.id, :visible => false) #invisible profile
block = EnterprisesBlock.new
env.boxes.first.blocks << block
block.save!
ids = block.profile_finder.ids
assert_includes ids, enterprise1.id
assert_not_includes ids, enterprise2.id
end
should 'list private enterprises in profile' do
person = create_user('testuser').person
enterprise1 = fast_create(Enterprise, :public_profile => true)
role = Profile::Roles.member(enterprise1.environment.id)
enterprise1.affiliate(person, role)
enterprise2 = fast_create(Enterprise, :public_profile => false)
enterprise2.affiliate(person, role)
block = EnterprisesBlock.new
person.boxes.first.blocks << block
block.save!
ids = block.profile_finder.ids
assert_includes ids, enterprise1.id
assert_includes ids, enterprise2.id
end
should 'not list invisible enterprises in profile' do
person = create_user('testuser').person
enterprise1 = fast_create(Enterprise, :visible => true)
role = Profile::Roles.member(enterprise1.environment.id)
enterprise1.affiliate(person, role)
enterprise2 = fast_create(Enterprise, :visible => false)
enterprise2.affiliate(person, role)
block = EnterprisesBlock.new
person.boxes.first.blocks << block
block.save!
ids = block.profile_finder.ids
assert_includes ids, enterprise1.id
assert_not_includes ids, enterprise2.id
end
should 'link to all enterprises for profile' do
profile = Profile.new
profile.expects(:identifier).returns('theprofile')
block = EnterprisesBlock.new
block.expects(:owner).returns(profile)
expects(:link_to).with('View all', :controller => 'profile', :profile => 'theprofile', :action => 'enterprises')
instance_eval(&block.footer)
end
should 'link to all enterprises for environment' do
env = Environment.default
block = EnterprisesBlock.new
block.expects(:owner).returns(env)
expects(:link_to).with('View all', :controller => 'search', :action => 'assets', :asset => 'enterprises')
instance_eval(&block.footer)
end
should 'give empty footer for unsupported owner type' do
block = EnterprisesBlock.new
block.expects(:owner).returns(1)
assert_equal '', block.footer
end
should 'count number of owner enterprises' do
user = create_user('testuser').person
ent1 = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'ent1')
ent1.expects(:closed?).returns(false)
ent1.add_member(user)
ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'ent2')
ent2.expects(:closed?).returns(false)
ent2.add_member(user)
block = EnterprisesBlock.new
block.expects(:owner).at_least_once.returns(user)
assert_equal 2, block.profile_count
end
should 'count non-public person enterprises' do
user = fast_create(Person)
ent1 = fast_create(Enterprise, :public_profile => true)
ent1.expects(:closed?).returns(false)
ent1.add_member(user)
ent2 = fast_create(Enterprise, :public_profile => false)
ent2.expects(:closed?).returns(false)
ent2.add_member(user)
block = EnterprisesBlock.new
block.expects(:owner).at_least_once.returns(user)
assert_equal 2, block.profile_count
end
should 'not count non-visible person enterprises' do
user = fast_create(Person)
ent1 = fast_create(Enterprise, :visible => true)
ent1.expects(:closed?).returns(false)
ent1.add_member(user)
ent2 = fast_create(Enterprise, :visible => false)
ent2.expects(:closed?).returns(false)
ent2.add_member(user)
block = EnterprisesBlock.new
block.expects(:owner).at_least_once.returns(user)
assert_equal 1, block.profile_count
end
should 'count non-public environment enterprises' do
env = fast_create(Environment)
ent1 = fast_create(Enterprise, :environment_id => env.id, :public_profile => true)
ent2 = fast_create(Enterprise, :environment_id => env.id, :public_profile => false)
block = EnterprisesBlock.new
block.expects(:owner).at_least_once.returns(env)
assert_equal 2, block.profile_count
end
should 'not count non-visible environment enterprises' do
env = fast_create(Environment)
ent1 = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'ent1', :environment_id => env.id, :visible => true)
ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'ent2', :environment_id => env.id, :visible => false)
block = EnterprisesBlock.new
block.expects(:owner).at_least_once.returns(env)
assert_equal 1, block.profile_count
end
end