Commit 5fad1d5f04ba445a87fc9a4feab3c18fdb031dc2
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
Showing
6 changed files
with
123 additions
and
18 deletions
Show diff stats
app/controllers/public/account_controller.rb
@@ -128,6 +128,28 @@ class AccountController < PublicController | @@ -128,6 +128,28 @@ class AccountController < PublicController | ||
128 | end | 128 | end |
129 | end | 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 | protected | 153 | protected |
132 | 154 | ||
133 | def activate_enterprise | 155 | def activate_enterprise |
app/models/environment.rb
@@ -136,6 +136,23 @@ class Environment < ActiveRecord::Base | @@ -136,6 +136,23 @@ class Environment < ActiveRecord::Base | ||
136 | ! self.settings['terms_of_use'].nil? | 136 | ! self.settings['terms_of_use'].nil? |
137 | end | 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 | def message_for_disabled_enterprise | 156 | def message_for_disabled_enterprise |
140 | self.settings['message_for_disabled_enterprise'] | 157 | self.settings['message_for_disabled_enterprise'] |
141 | end | 158 | end |
@@ -0,0 +1,6 @@ | @@ -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 %> |
@@ -0,0 +1,10 @@ | @@ -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 < Test::Unit::TestCase | @@ -277,10 +277,16 @@ class AccountControllerTest < Test::Unit::TestCase | ||
277 | assert_redirected_to :controller => 'profile_editor' | 277 | assert_redirected_to :controller => 'profile_editor' |
278 | end | 278 | end |
279 | 279 | ||
280 | +################################ | ||
281 | +# # | ||
282 | +# Enterprise activation tests # | ||
283 | +# # | ||
284 | +################################ | ||
285 | + | ||
280 | should 'report invalid enterprise code on signup' do | 286 | should 'report invalid enterprise code on signup' do |
281 | EnterpriseActivation.expects(:find_by_code).with('some_invalid_code').returns(nil).at_least_once | 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 | assert_template 'invalid_enterprise_code' | 291 | assert_template 'invalid_enterprise_code' |
286 | end | 292 | end |
@@ -291,19 +297,19 @@ class AccountControllerTest < Test::Unit::TestCase | @@ -291,19 +297,19 @@ class AccountControllerTest < Test::Unit::TestCase | ||
291 | task.expects(:enterprise).returns(ent).at_least_once | 297 | task.expects(:enterprise).returns(ent).at_least_once |
292 | EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | 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 | assert_template 'already_activated' | 302 | assert_template 'already_activated' |
297 | end | 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 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') | 306 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') |
301 | 307 | ||
302 | task = mock | 308 | task = mock |
303 | task.expects(:enterprise).returns(ent).at_least_once | 309 | task.expects(:enterprise).returns(ent).at_least_once |
304 | EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | 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 | assert_equal ent, assigns(:enterprise) | 314 | assert_equal ent, assigns(:enterprise) |
309 | end | 315 | end |
@@ -315,7 +321,7 @@ class AccountControllerTest < Test::Unit::TestCase | @@ -315,7 +321,7 @@ class AccountControllerTest < Test::Unit::TestCase | ||
315 | task.expects(:enterprise).returns(ent).at_least_once | 321 | task.expects(:enterprise).returns(ent).at_least_once |
316 | EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | 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 | assert_template 'blocked' | 326 | assert_template 'blocked' |
321 | end | 327 | end |
@@ -327,34 +333,32 @@ class AccountControllerTest < Test::Unit::TestCase | @@ -327,34 +333,32 @@ class AccountControllerTest < Test::Unit::TestCase | ||
327 | task.expects(:enterprise).returns(ent).at_least_once | 333 | task.expects(:enterprise).returns(ent).at_least_once |
328 | EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | 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 | end | 339 | end |
334 | 340 | ||
335 | should 'show form to those enterprises that have cnpj' do | 341 | should 'show form to those enterprises that have cnpj' do |
336 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => false) | 342 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => false) |
337 | 343 | ||
338 | - | ||
339 | task = mock | 344 | task = mock |
340 | task.expects(:enterprise).returns(ent).at_least_once | 345 | task.expects(:enterprise).returns(ent).at_least_once |
341 | EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | 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 | end | 351 | end |
347 | 352 | ||
348 | should 'block those who are blocked' do | 353 | should 'block those who are blocked' do |
349 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => '1998', :enabled => false) | 354 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => '1998', :enabled => false) |
350 | ent.block | 355 | ent.block |
351 | 356 | ||
352 | - | ||
353 | task = mock | 357 | task = mock |
354 | task.expects(:enterprise).returns(ent).at_least_once | 358 | task.expects(:enterprise).returns(ent).at_least_once |
355 | EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | 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 | assert_template 'blocked' | 363 | assert_template 'blocked' |
360 | end | 364 | end |
@@ -366,7 +370,8 @@ class AccountControllerTest < Test::Unit::TestCase | @@ -366,7 +370,8 @@ class AccountControllerTest < Test::Unit::TestCase | ||
366 | task.expects(:enterprise).returns(ent).at_least_once | 370 | task.expects(:enterprise).returns(ent).at_least_once |
367 | EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | 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 | ent.reload | 375 | ent.reload |
371 | 376 | ||
372 | assert_nil User.find_by_login('test_user') | 377 | assert_nil User.find_by_login('test_user') |
@@ -374,17 +379,45 @@ class AccountControllerTest < Test::Unit::TestCase | @@ -374,17 +379,45 @@ class AccountControllerTest < Test::Unit::TestCase | ||
374 | assert_template 'blocked' | 379 | assert_template 'blocked' |
375 | end | 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 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) | 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 | task = EnterpriseActivation.create!(:enterprise => ent) | 398 | task = EnterpriseActivation.create!(:enterprise => ent) |
381 | EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | 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 | ent.reload | 417 | ent.reload |
385 | 418 | ||
386 | assert ent.enabled | 419 | assert ent.enabled |
387 | - assert_includes ent.members, assigns(:user).person | 420 | + assert_includes ent.members, p |
388 | end | 421 | end |
389 | 422 | ||
390 | should 'put hidden field with enterprise code for answering question' do | 423 | should 'put hidden field with enterprise code for answering question' do |
@@ -394,12 +427,13 @@ class AccountControllerTest < Test::Unit::TestCase | @@ -394,12 +427,13 @@ class AccountControllerTest < Test::Unit::TestCase | ||
394 | task.expects(:enterprise).returns(ent).at_least_once | 427 | task.expects(:enterprise).returns(ent).at_least_once |
395 | EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | 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 | assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'enterprise_code', :value => '0123456789'} | 432 | assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'enterprise_code', :value => '0123456789'} |
400 | - | ||
401 | end | 433 | end |
402 | 434 | ||
435 | +# end of enterprise activation tests | ||
436 | + | ||
403 | should 'not be able to signup while inverse captcha field filled' do | 437 | should 'not be able to signup while inverse captcha field filled' do |
404 | assert_no_difference User, :count do | 438 | assert_no_difference User, :count do |
405 | create_user({}, @controller.icaptcha_field => 'bli@bla.email.foo') | 439 | create_user({}, @controller.icaptcha_field => 'bli@bla.email.foo') |
test/unit/environment_test.rb
@@ -78,6 +78,22 @@ class EnvironmentTest < Test::Unit::TestCase | @@ -78,6 +78,22 @@ class EnvironmentTest < Test::Unit::TestCase | ||
78 | assert v.has_terms_of_use? | 78 | assert v.has_terms_of_use? |
79 | end | 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 | def test_should_list_top_level_categories | 97 | def test_should_list_top_level_categories |
82 | env = Environment.create!(:name => 'a test environment') | 98 | env = Environment.create!(:name => 'a test environment') |
83 | cat1 = Category.create!(:name => 'first category', :environment_id => env.id) | 99 | cat1 = Category.create!(:name => 'first category', :environment_id => env.id) |