Commit 5fad1d5f04ba445a87fc9a4feab3c18fdb031dc2

Authored by MoisesMachado
1 parent 3b10449d

ActionItem603: changed tests & start implementing

  Changed the tests for activating enterprises so it uses the new tree
actions: activation_question, accept_terms and activate_enterprise
instead of using the one big action signup for every thing even if it
leads to some code duplication


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/branches/0.10.x@2362 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/account_controller.rb
... ... @@ -128,6 +128,28 @@ class AccountController < PublicController
128 128 end
129 129 end
130 130  
  131 + def activation_question
  132 + @enterprise = load_enterprise
  133 + unless @enterprise
  134 + render :action => 'invalid_enterprise_code'
  135 + return
  136 + end
  137 + if @enterprise.enabled
  138 + render :action => 'already_activated'
  139 + return
  140 + end
  141 +
  142 + @question = @enterprise.question
  143 + if !@question || @enterprise.blocked?
  144 + render :action => 'blocked'
  145 + return
  146 + end
  147 + end
  148 +
  149 + def accept_terms
  150 + @terms_of_enterprise_use = environment.terms_of_enterprise_use
  151 + end
  152 +
131 153 protected
132 154  
133 155 def activate_enterprise
... ...
app/models/environment.rb
... ... @@ -136,6 +136,23 @@ class Environment < ActiveRecord::Base
136 136 ! self.settings['terms_of_use'].nil?
137 137 end
138 138  
  139 + # the environment's terms of enterprise use: every enterprise member must accept them before
  140 + # registering or activating enterprises.
  141 + def terms_of_enterprise_use
  142 + self.settings['terms_of_enterprise_use']
  143 + end
  144 +
  145 + # sets the environment's terms of enterprise use.
  146 + def terms_of_enterprise_use=(value)
  147 + self.settings['terms_of_enterprise_use'] = value
  148 + end
  149 +
  150 + # returns <tt>true</tt> if this Environment has terms of enterprise use to be
  151 + # accepted by users before registration or activation of enterprises.
  152 + def has_terms_of_enterprise_use?
  153 + ! self.settings['terms_of_enterprise_use'].blank?
  154 + end
  155 +
139 156 def message_for_disabled_enterprise
140 157 self.settings['message_for_disabled_enterprise']
141 158 end
... ...
app/views/account/accept_terms.rhtml 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +<div id='terms-of-enterprise-use'><%= @terms_of_enterprise_use %></div>
  2 +
  3 +<% button_bar do %>
  4 + <%= button_to(_('I accept the terms'), {:action => 'activate_enterprise', :terms_accepted => true, :enterprise_code => params[:enterprise_code], :answer => params[:answer]} ) %>
  5 + <%= button_to(_('I do NOT accept the terms'), {:controller => 'home', :action => 'index'} ) %>
  6 +<% end %>
... ...
app/views/account/activation_question.rhtml 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +<% form_tag :action => 'accept_terms' do %>
  2 +
  3 + <%= ApplicationHelper::NoosferoFormBuilder::output_field(@question == :foundation_year ? _('What year your enterprise was founded?') : _('What is the CNPJ of your enterprise?'), text_field_tag(:answer, nil,:help => help=_('We need to be sure that this is your enterprise'))) %>
  4 +
  5 + <%= hidden_field_tag :enterprise_code, params[:enterprise_code] %>
  6 +
  7 + <% button_bar do %>
  8 + <%= submit_button('answer', _('Answer'), :cancel => {:action => 'index'} ) %>
  9 + <% end %>
  10 +<% end %>
... ...
test/functional/account_controller_test.rb
... ... @@ -277,10 +277,16 @@ class AccountControllerTest &lt; Test::Unit::TestCase
277 277 assert_redirected_to :controller => 'profile_editor'
278 278 end
279 279  
  280 +################################
  281 +# #
  282 +# Enterprise activation tests #
  283 +# #
  284 +################################
  285 +
280 286 should 'report invalid enterprise code on signup' do
281 287 EnterpriseActivation.expects(:find_by_code).with('some_invalid_code').returns(nil).at_least_once
282 288  
283   - get :signup, :enterprise_code => 'some_invalid_code'
  289 + get :activation_question, :enterprise_code => 'some_invalid_code'
284 290  
285 291 assert_template 'invalid_enterprise_code'
286 292 end
... ... @@ -291,19 +297,19 @@ class AccountControllerTest &lt; Test::Unit::TestCase
291 297 task.expects(:enterprise).returns(ent).at_least_once
292 298 EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
293 299  
294   - get :signup, :enterprise_code => '0123456789'
  300 + get :activation_question, :enterprise_code => '0123456789'
295 301  
296 302 assert_template 'already_activated'
297 303 end
298 304  
299   - should 'load enterprise from code on signup' do
  305 + should 'load enterprise from code on for validation question' do
300 306 ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent')
301 307  
302 308 task = mock
303 309 task.expects(:enterprise).returns(ent).at_least_once
304 310 EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
305 311  
306   - get :signup, :enterprise_code => '0123456789'
  312 + get :activation_question, :enterprise_code => '0123456789'
307 313  
308 314 assert_equal ent, assigns(:enterprise)
309 315 end
... ... @@ -315,7 +321,7 @@ class AccountControllerTest &lt; Test::Unit::TestCase
315 321 task.expects(:enterprise).returns(ent).at_least_once
316 322 EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
317 323  
318   - get :signup, :enterprise_code => '0123456789'
  324 + get :activation_question, :enterprise_code => '0123456789'
319 325  
320 326 assert_template 'blocked'
321 327 end
... ... @@ -327,34 +333,32 @@ class AccountControllerTest &lt; Test::Unit::TestCase
327 333 task.expects(:enterprise).returns(ent).at_least_once
328 334 EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
329 335  
330   - get :signup, :enterprise_code => '0123456789'
  336 + get :activation_question, :enterprise_code => '0123456789'
331 337  
332   - assert_template 'activate_enterprise'
  338 + assert_template 'activation_question'
333 339 end
334 340  
335 341 should 'show form to those enterprises that have cnpj' do
336 342 ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => false)
337 343  
338   -
339 344 task = mock
340 345 task.expects(:enterprise).returns(ent).at_least_once
341 346 EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
342 347  
343   - get :signup, :enterprise_code => '0123456789'
  348 + get :activation_question, :enterprise_code => '0123456789'
344 349  
345   - assert_template 'activate_enterprise'
  350 + assert_template 'activation_question'
346 351 end
347 352  
348 353 should 'block those who are blocked' do
349 354 ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => '1998', :enabled => false)
350 355 ent.block
351 356  
352   -
353 357 task = mock
354 358 task.expects(:enterprise).returns(ent).at_least_once
355 359 EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
356 360  
357   - get :signup, :enterprise_code => '0123456789'
  361 + get :activation_question, :enterprise_code => '0123456789'
358 362  
359 363 assert_template 'blocked'
360 364 end
... ... @@ -366,7 +370,8 @@ class AccountControllerTest &lt; Test::Unit::TestCase
366 370 task.expects(:enterprise).returns(ent).at_least_once
367 371 EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
368 372  
369   - create_user({}, :enterprise_code => '0123456789', :answer => '1997')
  373 + post :accept_terms, :enterprise_code => '0123456789', :answer => '1997'
  374 +
370 375 ent.reload
371 376  
372 377 assert_nil User.find_by_login('test_user')
... ... @@ -374,17 +379,45 @@ class AccountControllerTest &lt; Test::Unit::TestCase
374 379 assert_template 'blocked'
375 380 end
376 381  
377   - should 'activate enterprise for those who answer the question right and make them admin of the enterprise' do
  382 + should 'show term of use for enterprise owners' do
  383 + env = Environment.default
  384 + env.terms_of_enterprise_use = 'Some terms'
  385 + env.save!
  386 +
  387 + post :accept_terms, :enterprise_code => '0123456789', :answer => '1998'
  388 +
  389 + assert_template 'accept_terms'
  390 + assert_tag :tag => 'div', :content => 'Some terms'
  391 + end
  392 +
  393 + should 'not activate if user does not accept terms' do
378 394 ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false)
  395 + p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person
  396 + login_as(p.identifier)
379 397  
380 398 task = EnterpriseActivation.create!(:enterprise => ent)
381 399 EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
382 400  
383   - create_user({}, :enterprise_code => '0123456789', :answer => '1998')
  401 + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => false
  402 + ent.reload
  403 +
  404 + assert !ent.enabled
  405 + assert_not_includes ent.members, p
  406 + end
  407 +
  408 + should 'activate enterprise and make user admin' do
  409 + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false)
  410 + p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person
  411 + login_as(p.identifier)
  412 +
  413 + task = EnterpriseActivation.create!(:enterprise => ent)
  414 + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
  415 +
  416 + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true
384 417 ent.reload
385 418  
386 419 assert ent.enabled
387   - assert_includes ent.members, assigns(:user).person
  420 + assert_includes ent.members, p
388 421 end
389 422  
390 423 should 'put hidden field with enterprise code for answering question' do
... ... @@ -394,12 +427,13 @@ class AccountControllerTest &lt; Test::Unit::TestCase
394 427 task.expects(:enterprise).returns(ent).at_least_once
395 428 EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once
396 429  
397   - get :signup, :enterprise_code => '0123456789'
  430 + get :activation_question, :enterprise_code => '0123456789'
398 431  
399 432 assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'enterprise_code', :value => '0123456789'}
400   -
401 433 end
402 434  
  435 +# end of enterprise activation tests
  436 +
403 437 should 'not be able to signup while inverse captcha field filled' do
404 438 assert_no_difference User, :count do
405 439 create_user({}, @controller.icaptcha_field => 'bli@bla.email.foo')
... ...
test/unit/environment_test.rb
... ... @@ -78,6 +78,22 @@ class EnvironmentTest &lt; Test::Unit::TestCase
78 78 assert v.has_terms_of_use?
79 79 end
80 80  
  81 + def test_terms_of_enterprise_use
  82 + v = Environment.new(:name => 'My test environment')
  83 + assert_nil v.terms_of_enterprise_use
  84 + v.terms_of_enterprise_use = 'To be owner of an enterprise in this environment, you must accept the following terms: ...'
  85 + assert v.save
  86 + id = v.id
  87 + assert_equal 'To be owner of an enterprise in this environment, you must accept the following terms: ...', Environment.find(id).terms_of_enterprise_use
  88 + end
  89 +
  90 + def test_has_terms_of_enterprise_use
  91 + v = Environment.new
  92 + assert !v.has_terms_of_enterprise_use?
  93 + v.terms_of_enterprise_use = 'some terms of enterprise use'
  94 + assert v.has_terms_of_enterprise_use?
  95 + end
  96 +
81 97 def test_should_list_top_level_categories
82 98 env = Environment.create!(:name => 'a test environment')
83 99 cat1 = Category.create!(:name => 'first category', :environment_id => env.id)
... ...