cirandas.net

ref: master

test/functional/memberships_controller_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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
require_relative '../test_helper'

class MembershipsControllerTest < ActionController::TestCase

  include ApplicationHelper

  def setup
    @controller = MembershipsController.new

    @profile = create_user('testuser').person
    login_as('testuser')
  end
  attr_reader :profile

  should 'list current memberships' do
    get :index, :profile => profile.identifier

    assert assigns(:memberships)
  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 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 'be able to create a new community with custom field' do
    assert_difference 'Community.count' do
      assert_difference 'CustomFieldValue.count' do
        CustomField.create!(:name => "zombies", :format=>"String", :default_value => "awrrr", :customized_type=>"Community", :active => true, :required => true, :signup => true, :environment => Environment.default)
        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 '}, "profile_data"=>{"custom_values"=>{"zombies"=>{"value"=>"BRAINSSS"}}}
        assert_response :redirect
        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
  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 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 = fast_create(Enterprise, :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 with reload' 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 => "/profile/#{community.identifier}/leave?reload=true" }, :content => 'Leave community'
  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.environment.id), 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 'display destroy link to communities and enterprise' do
    community = Community.create!(:name => 'A community to destroy')
    enterprise = fast_create(Enterprise, :name => 'A enterprise test')

    person = Person['testuser']
    community.add_admin(person)
    enterprise.add_admin(person)

    get :index, :profile => 'testuser'

    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{community.identifier}/profile_editor/destroy_profile" }
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{enterprise.identifier}/profile_editor/destroy_profile" }
  end

  should 'not display destroy link to normal members' do
    community = fast_create(Community)
    admin = fast_create(Person)
    community.add_member(admin)

    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/#{community.identifier}/profile_editor/destroy_profile" }
  end

  should 'use the current environment for the template of user' do
    template = Community.create!(:identifier => 'test_template', :name => 'test@bli.com', :is_template => true)
    template.boxes.destroy_all
    template.boxes << Box.new
    template.boxes[0].blocks << Block.new
    template.save!

    env = fast_create(Environment, :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 'display only templates of the current environment' do
    env2 = fast_create(Environment)

    template1 = fast_create(Community, :name => 'template1', :environment_id => Environment.default.id, :is_template => true)
    template2 = fast_create(Community, :name => 'template2', :environment_id => Environment.default.id, :is_template => true)
    template3 = fast_create(Community, :name => 'template3', :environment_id => env2.id, :is_template => true)

    get :new_community, :profile => profile.identifier

    assert_select '#template-options' do |elements|
      assert_match /template1/, elements[0].to_s
      assert_match /template2/, elements[0].to_s
      assert_no_match /template3/, elements[0].to_s
    end
  end

  should 'display only required fields when register new community' do
    env = Environment.default
    env.custom_community_fields = {
      'contact_email' => {'active' => 'true', 'required' => 'true'},
      'contact_phone' => {'active' => 'true', 'required' => 'false'}
    }
    env.save!

    get :new_community, :profile => profile.identifier

    assert_tag :tag => 'input', :attributes => { :name => 'community[contact_email]' }
    assert_no_tag :tag => 'input', :attributes => { :name => 'community[contact_phone]' }
  end

  should 'display all required fields when register new community' do
    env = Environment.default
    env.custom_community_fields = {
      'contact_email' => {'active' => 'true', 'required' => 'true'},
      'contact_phone' => {'active' => 'true', 'required' => 'true'}
    }
    env.save!

    get :new_community, :profile => profile.identifier

    env.required_community_fields.each do |field|
      assert_tag :tag => 'input', :attributes => { :name => "community[#{field}]" }
    end
  end

  should 'set environment when render new community form' do
    get :new_community, :profile => profile.identifier

    assert_not_nil assigns(:community).environment
   end

  should 'set environment' do
    @controller.stubs(:environment).returns(Environment.default).at_least_once
    post :new_community, :profile => profile.identifier, :community => {:name => 'test community'}

    assert_not_nil assigns(:community).environment
  end

  should 'not show description if isnt enabled when register new community' do
    env = Environment.default
    env.custom_community_fields = { :description => {:active => 'false', :required => 'false'} }
    env.save!

    get :new_community, :profile => profile.identifier

    assert_no_tag :tag => 'textarea', :attributes => {:name => 'community[description]'}
  end

  should 'include hidden fields supplied by plugins on new community' do
    class Plugin1 < Noosfero::Plugin
      def new_community_hidden_fields
        {'plugin1' => 'Plugin 1'}
      end
    end

    class Plugin2 < Noosfero::Plugin
      def new_community_hidden_fields
        {'plugin2' => 'Plugin 2'}
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])

    environment = Environment.default
    environment.enable_plugin(Plugin1.name)
    environment.enable_plugin(Plugin2.name)

    get :new_community, :profile => profile.identifier

    assert_tag :tag => 'input', :attributes => {:id => 'community_plugin1', :type => 'hidden', :value => 'Plugin 1'}
    assert_tag :tag => 'input', :attributes => {:id => 'community_plugin2', :type => 'hidden', :value => 'Plugin 2'}
  end

  should 'redirect to back_to parameter when community needs admin approval' do
    back_to = '/'
    environment = Environment.default
    environment.enable('admin_must_approve_new_communities')
    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 '}, :back_to => back_to
    assert_response :redirect
    assert_redirected_to back_to
  end

  should 'cancel button redirect to back_to parameter' do
    back_to = '/'
    get :new_community, :profile => profile.identifier, :back_to => back_to
    assert_tag :tag => 'a', :attributes => { :class => 'button icon-cancel with-text', :href => back_to }
  end

  should 'only display control panel link to members with permission' do
    c1 = fast_create(Community, :name => 'My own community')
    c2 = fast_create(Community, :name => 'Not my community')

    owner = fast_create(Person)
    c2.add_admin(owner)

    person = Person['testuser']
    c1.add_admin(person)
    c2.add_member(person)

    login_as('testuser')
    get :index, :profile => 'testuser'

    assert_template 'index'
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{c2.identifier}" }
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{c1.identifier}" }
  end

  should 'filter memberships by role' do
    c1 = fast_create(Community, :name => 'First community')
    c2 = fast_create(Community, :name => 'Second community')

    role = Role.create!(:name => 'special_role', :permissions => ['edit_profile'], :environment => c2.environment)

    person = Person['testuser']
    c1.add_member(person)
    c2.add_member(person)

    person.add_role(role, c2)

    login_as('testuser')
    get :index, :profile => 'testuser'

    assert_includes assigns(:memberships), c1
    assert_includes assigns(:memberships), c2

    get :index, :profile => 'testuser', :filter_type => role.id

    assert_not_includes assigns(:memberships), c1
    assert_includes assigns(:memberships), c2
  end

  should 'only show roles the user has' do
    c1 = fast_create(Community, :name => 'First community')

    role1 = Role.create!(:name => 'normal_role', :permissions => ['edit_profile'], :environment => c1.environment)
    role2 = Role.create!(:name => 'special_role', :permissions => ['edit_profile'], :environment => c1.environment)

    person = Person['testuser']
    c1.add_member(person)
    person.add_role(role1, c1)

    login_as('testuser')
    get :index, :profile => 'testuser'

    assert_includes assigns(:roles), role1
    assert_not_includes assigns(:roles), role2
  end

  should 'only show roles related to profiles' do
    c1 = fast_create(Community, :name => 'First community')
    role1 = Role.create!(:name => 'profile_role', :permissions => ['edit_profile'], :environment => c1.environment)
    role2 = Role.create!(:name => 'environment_role', :permissions => ['edit_profile'], :environment => c1.environment)

    person = Person['testuser']
    c1.add_member(person)
    person.add_role(role2, c1.environment)
    person.add_role(role1, c1)

    login_as('testuser')
    get :index, :profile => 'testuser'

    assert_includes assigns(:roles), role1
    assert_not_includes assigns(:roles), role2
  end

  should 'display list suggestions button' do
    community = fast_create(Community)
    profile.suggested_profiles.create(:suggestion => community)
    get :index, :profile => 'testuser'
    assert_tag :tag => 'a', :content => 'See some suggestions of communities...', :attributes => { :href => "/myprofile/testuser/memberships/suggest" }
  end

  should 'display communities suggestions' do
    community = fast_create(Community)
    profile.suggested_profiles.create(:suggestion => community)
    get :suggest, :profile => 'testuser'
    assert_tag :tag => 'a', :content => "+ #{community.name}", :attributes => { :href => "/profile/#{community.identifier}/join" }
  end

  should 'display button to join on community suggestion' do
    community = fast_create(Community)
    profile.suggested_profiles.create(:suggestion => community)
    get :suggest, :profile => 'testuser'
    assert_tag :tag => 'a', :attributes => { :href => "/profile/#{community.identifier}/join" }
  end

  should 'display button to remove community suggestion' do
    community = fast_create(Community)
    profile.suggested_profiles.create(:suggestion => community)
    get :suggest, :profile => 'testuser'
    assert_tag :tag => 'a', :attributes => { :href => /\/myprofile\/testuser\/memberships\/remove_suggestion\/#{community.identifier}/ }
  end

  should 'remove suggestion of community' do
    community = fast_create(Community)
    suggestion = profile.suggested_profiles.create(:suggestion => community)
    post :remove_suggestion, :profile => 'testuser', :id => community.identifier

    assert_response :success
    assert_equal false, ProfileSuggestion.find(suggestion.id).enabled
  end

end