profile_test.rb
7.19 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
require File.dirname(__FILE__) + '/../test_helper'
class ProfileTest < Test::Unit::TestCase
fixtures :profiles, :environments, :users
def test_identifier_validation
p = Profile.new
p.valid?
assert p.errors.invalid?(:identifier)
p.identifier = 'with space'
p.valid?
assert p.errors.invalid?(:identifier)
p.identifier = 'áéíóú'
p.valid?
assert p.errors.invalid?(:identifier)
p.identifier = 'rightformat2007'
p.valid?
assert ! p.errors.invalid?(:identifier)
p.identifier = 'rightformat'
p.valid?
assert ! p.errors.invalid?(:identifier)
p.identifier = 'right_format'
p.valid?
assert ! p.errors.invalid?(:identifier)
end
def test_has_domains
p = Profile.new
assert_kind_of Array, p.domains
end
def test_belongs_to_environment_and_has_default
p = Profile.new
assert_kind_of Environment, p.environment
end
def test_cannot_rename
assert_valid p = Profile.create(:name => 'new_profile', :identifier => 'new_profile')
assert_raise ArgumentError do
p.identifier = 'other_profile'
end
end
should 'provide access to home page' do
profile = Profile.create!(:identifier => 'newprofile', :name => 'New Profile')
assert_nil profile.home_page
page = profile.articles.build(:name => "My custom home page")
page.save!
profile.home_page = page
profile.save!
assert_equal page, profile.home_page
end
def test_name_should_be_mandatory
p = Profile.new
p.valid?
assert p.errors.invalid?(:name)
p.name = 'a very unprobable name'
p.valid?
assert !p.errors.invalid?(:name)
end
def test_can_have_affiliated_people
pr = Profile.create(:name => 'composite_profile', :identifier => 'composite')
pe = User.create(:login => 'aff', :email => 'aff@pr.coop', :password => 'blih', :password_confirmation => 'blih').person
member_role = Role.new(:name => 'new_member_role')
assert member_role.save
assert pr.affiliate(pe, member_role)
assert pe.memberships.include?(pr)
end
def test_find_by_contents
p = Profile.create(:name => 'wanted', :identifier => 'wanted')
assert Profile.find_by_contents('wanted').include?(p)
assert ! Profile.find_by_contents('not_wanted').include?(p)
end
should 'remove pages when removing profile' do
profile = Profile.create!(:name => 'testing profile', :identifier => 'testingprofile')
first = profile.articles.build(:name => 'first'); first.save!
second = profile.articles.build(:name => 'second'); second.save!
third = profile.articles.build(:name => 'third'); third.save!
n = Article.count
profile.destroy
assert_equal n - 3, Article.count
end
def test_should_define_info
assert_nil Profile.new.info
end
def test_should_avoid_reserved_identifiers
assert_invalid_identifier 'admin'
assert_invalid_identifier 'system'
assert_invalid_identifier 'myprofile'
assert_invalid_identifier 'profile'
assert_invalid_identifier 'cms'
assert_invalid_identifier 'community'
assert_invalid_identifier 'test'
assert_invalid_identifier 'tag'
assert_invalid_identifier 'cat'
end
should 'provide recent documents' do
profile = Profile.create!(:name => 'testing profile', :identifier => 'testingprofile')
first = profile.articles.build(:name => 'first'); first.save!
second = profile.articles.build(:name => 'second'); second.save!
third = profile.articles.build(:name => 'third'); third.save!
assert_equal [first,second], profile.recent_documents(2)
assert_equal [first,second,third], profile.recent_documents
end
should 'affiliate and provide a list of the affiliated users' do
profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting')
person = create_user('test_user').person
role = Role.create!(:name => 'just_another_test_role')
assert profile.affiliate(person, role)
assert profile.members.map(&:id).include?(person.id)
end
should 'authorize users that have permission on the environment' do
env = Environment.create!(:name => 'test_env')
profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env)
person = create_user('test_user').person
role = Role.create!(:name => 'just_another_test_role', :permissions => ['edit_profile'])
assert env.affiliate(person, role)
assert person.has_permission?('edit_profile', profile)
end
should 'have articles' do
env = Environment.create!(:name => 'test_env')
profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env)
assert_raise ActiveRecord::AssociationTypeMismatch do
profile.articles << 1
end
assert_nothing_raised do
profile.articles << Article.new(:name => 'testing article')
end
end
should 'list top-level articles' do
env = Environment.create!(:name => 'test_env')
profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env)
p1 = profile.articles.build(:name => 'parent1')
p1.save!
p2 = profile.articles.build(:name => 'parent2')
p2.save!
child = profile.articles.build(:name => 'parent2', :parent_id => p1.id)
child.save!
top = profile.top_level_articles
assert top.include?(p1)
assert top.include?(p2)
assert !top.include?(child)
end
should 'be able to optionally reload the list of top level articles' do
env = Environment.create!(:name => 'test_env')
profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env)
list = profile.top_level_articles
same_list = profile.top_level_articles
assert_same list, same_list
other_list = profile.top_level_articles(true)
assert_not_same list, other_list
end
should 'be able to find profiles by their names with ferret' do
small = Profile.create!(:name => 'A small profile for testing ', :identifier => 'smallprofilefortesting')
big = Profile.create!(:name => 'A big profile for testing', :identifier => 'bigprofilefortesting')
assert Profile.find_by_contents('small').include?(small)
assert Profile.find_by_contents('big').include?(big)
both = Profile.find_by_contents('profile testing')
assert both.include?(small)
assert both.include?(big)
end
should 'provide a shortcut for picking a profile by its identifier' do
profile = Profile.create!(:name => 'bla', :identifier => 'testprofile')
assert_equal profile, Profile['testprofile']
end
should 'remove boxes and blocks when removing profile' do
profile = Profile.create!(:name => 'test environment', :identifier => 'testenv')
profile_boxes = profile.boxes.size
profile_blocks = profile.blocks.size
assert profile_boxes > 0
assert profile_blocks > 0
boxes = Design::Box.count
blocks = Design::Block.count
profile.destroy
assert_equal boxes - profile_boxes, Design::Box.count
assert_equal blocks - profile_blocks, Design::Block.count
end
private
def assert_invalid_identifier(id)
profile = Profile.new(:identifier => id)
assert !profile.valid?
assert profile.errors.invalid?(:identifier)
end
end