memberships_controller_test.rb
9.22 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
require File.dirname(__FILE__) + '/../test_helper'
require 'memberships_controller'
# Re-raise errors caught by the controller.
class MembershipsController; def rescue_action(e) raise e end; end
class MembershipsControllerTest < Test::Unit::TestCase
include ApplicationHelper
def setup
@controller = MembershipsController.new
@request = ActionController::TestRequest.new
@request.stubs(:ssl?).returns(true)
@response = ActionController::TestResponse.new
@profile = create_user('testuser').person
login_as('testuser')
end
attr_reader :profile
def test_local_files_reference
assert_local_files_reference :get, :index, :profile => profile.identifier
end
def test_valid_xhtml
assert_valid_xhtml
end
should 'list current memberships' do
get :index, :profile => profile.identifier
assert_kind_of Array, assigns(:memberships)
end
should 'present confirmation before joining a profile' do
community = Community.create!(:name => 'my test community')
get :join, :profile => profile.identifier, :id => community.id
assert_response :success
assert_template 'join'
end
should 'actually join profile' do
community = Community.create!(:name => 'my test community')
post :join, :profile => profile.identifier, :id => community.id, :confirmation => '1'
assert_response :redirect
assert_redirected_to community.url
profile = Profile.find(@profile.id)
assert profile.memberships.include?(community), 'profile should be actually added to the community'
end
should 'present new community form' do
get :new_community, :profile => profile.identifier
assert_response :success
assert_template 'new_community'
end
should 'be able to create a new community' do
assert_difference Community, :count do
post :new_community, :profile => profile.identifier, :community => { :name => 'My shiny new community', :description => 'This is a community devoted to anything interesting we find in the internet '}
assert_response :redirect
assert_redirected_to :action => 'index'
assert Community.find_by_identifier('my-shiny-new-community').members.include?(profile), "Creator user should be added as member of the community just created"
end
end
should 'link to new community creation in index' do
get :index, :profile => profile.identifier
assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/memberships/new_community" }
end
should 'filter html from name' do
login_as(profile.identifier)
post :new_community, :profile => profile.identifier, :community => { :name => '<b>new</b> community' }
assert_sanitized assigns(:community).name
end
should 'filter html from description' do
login_as(profile.identifier)
post :new_community, :profile => profile.identifier, :community => { :name => 'new community', :description => '<b>new</b> community' }
assert_sanitized assigns(:community).description
end
should 'show number of members on list' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
get :index, :profile => profile.identifier
assert_tag :tag => 'li', :content => /Members: 1/
end
should 'show created at on list' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
get :index, :profile => profile.identifier
assert_tag :tag => 'li', :content => /Created at: #{show_date(community.created_at)}/
end
should 'show description on list' do
community = Community.create!(:name => 'my test community', :description => 'description test')
community.add_member(profile)
get :index, :profile => profile.identifier
assert_tag :tag => 'li', :content => /Description: description test/
end
should 'not show description to enterprises on list' do
enterprise = Enterprise.create!(:identifier => 'enterprise-test', :name => 'my test enterprise')
enterprise.add_member(profile)
get :index, :profile => profile.identifier
assert_no_tag :tag => 'li', :content => /Description:/
end
should 'show link to leave from community' do
community = Community.create!(:name => 'my test community', :description => 'description test')
community.add_member(profile)
get :index, :profile => profile.identifier
assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/memberships/leave/#{community.id}" }, :content => 'Leave'
end
should 'present confirmation before leaving a profile' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
get :leave, :profile => profile.identifier, :id => community.id
assert_response :success
assert_template 'leave'
end
should 'actually leave profile' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
assert_includes profile.memberships, community
post :leave, :profile => profile.identifier, :id => community.id, :confirmation => '1'
assert_response :redirect
assert_redirected_to :action => 'index'
profile = Profile.find(@profile.id)
assert_not_includes profile.memberships, community
end
should 'create task when join to closed organization' do
community = Community.create!(:name => 'my test community', :closed => true)
assert_difference AddMember, :count do
post :join, :profile => profile.identifier, :id => community.id, :confirmation => '1'
end
end
should 'current user is added as admin after create new community' do
post :new_community, :profile => profile.identifier, :community => { :name => 'My shiny new community', :description => 'This is a community devoted to anything interesting we find in the internet '}
assert_equal Profile::Roles.admin, profile.find_roles(Community.find_by_identifier('my-shiny-new-community')).first.role
end
should 'display button to create community' do
get :index, :profile => 'testuser'
assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/new_community" }
end
should 'not display link to register new enterprise if there is no validators' do
get :index, :profile => 'testuser'
assert_no_tag :tag => 'a', :content => 'Register a new Enterprise'
end
should 'display link to register new enterprise' do
reg = Environment.default.regions.create!(:name => 'Region test')
reg.validators.create!(:name => 'Validator test', :identifier => 'validator-test')
get :index, :profile => 'testuser'
assert_tag :tag => 'a', :content => 'Register a new Enterprise'
end
should 'render destroy_community template' do
community = Community.create!(:name => 'A community to destroy')
get :destroy_community, :profile => 'testuser', :id => community.id
assert_template 'destroy_community'
end
should 'display destroy link only to communities' do
community = Community.create!(:name => 'A community to destroy')
enterprise = Enterprise.create!(:name => 'A enterprise test', :identifier => 'enterprise-test')
person = Person['testuser']
community.add_admin(person)
enterprise.add_admin(person)
get :index, :profile => 'testuser'
assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" }
assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{enterprise.id}" }
end
should 'be able to destroy communities' do
community = Community.create!(:name => 'A community to destroy')
person = Person['testuser']
community.add_admin(person)
assert_difference Community, :count, -1 do
post :destroy_community, :profile => 'testuser', :id => community.id
end
end
should 'not display destroy link to normal members' do
community = Community.create!(:name => 'A community to destroy')
person = Person['testuser']
community.add_member(person)
login_as('testuser')
get :index, :profile => 'testuser'
assert_template 'index'
assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" }
end
should 'use the current environment for the template of user' do
template = Community.create!(:identifier => 'test_template', :name => 'test@bli.com')
template.boxes.destroy_all
template.boxes << Box.new
template.boxes[0].blocks << Block.new
template.save!
env = Environment.create!(:name => 'test_env')
env.settings[:community_template_id] = template.id
env.save!
profile.environment = env
profile.save!
@controller.stubs(:environment).returns(env)
post :new_community, :profile => profile.identifier, :community => { :name => 'test community', :description => 'a test community'}
assert_equal 1, assigns(:community).boxes.size
assert_equal 1, assigns(:community).boxes[0].blocks.size
end
should 'leave community' do
community = Community.create!(:name =>'Boca do Siri', :identifier => 'boca_do_siri')
community.affiliate(profile, Profile::Roles.all_roles)
post :leave, :profile => profile.identifier, :id => community.id, :confirmation => true
profile = Profile.find(@profile.id)
assert_not_includes profile.memberships, community
end
end