diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index 926b4d5..aaae5b0 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -128,6 +128,28 @@ class AccountController < PublicController end end + def activation_question + @enterprise = load_enterprise + unless @enterprise + render :action => 'invalid_enterprise_code' + return + end + if @enterprise.enabled + render :action => 'already_activated' + return + end + + @question = @enterprise.question + if !@question || @enterprise.blocked? + render :action => 'blocked' + return + end + end + + def accept_terms + @terms_of_enterprise_use = environment.terms_of_enterprise_use + end + protected def activate_enterprise diff --git a/app/models/environment.rb b/app/models/environment.rb index eecefc6..eb44c0b 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -136,6 +136,23 @@ class Environment < ActiveRecord::Base ! self.settings['terms_of_use'].nil? end + # the environment's terms of enterprise use: every enterprise member must accept them before + # registering or activating enterprises. + def terms_of_enterprise_use + self.settings['terms_of_enterprise_use'] + end + + # sets the environment's terms of enterprise use. + def terms_of_enterprise_use=(value) + self.settings['terms_of_enterprise_use'] = value + end + + # returns true if this Environment has terms of enterprise use to be + # accepted by users before registration or activation of enterprises. + def has_terms_of_enterprise_use? + ! self.settings['terms_of_enterprise_use'].blank? + end + def message_for_disabled_enterprise self.settings['message_for_disabled_enterprise'] end diff --git a/app/views/account/accept_terms.rhtml b/app/views/account/accept_terms.rhtml new file mode 100644 index 0000000..1c8cec3 --- /dev/null +++ b/app/views/account/accept_terms.rhtml @@ -0,0 +1,6 @@ +
<%= @terms_of_enterprise_use %>
+ +<% button_bar do %> + <%= button_to(_('I accept the terms'), {:action => 'activate_enterprise', :terms_accepted => true, :enterprise_code => params[:enterprise_code], :answer => params[:answer]} ) %> + <%= button_to(_('I do NOT accept the terms'), {:controller => 'home', :action => 'index'} ) %> +<% end %> diff --git a/app/views/account/activation_question.rhtml b/app/views/account/activation_question.rhtml new file mode 100644 index 0000000..3d4ca22 --- /dev/null +++ b/app/views/account/activation_question.rhtml @@ -0,0 +1,10 @@ +<% form_tag :action => 'accept_terms' do %> + + <%= 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'))) %> + + <%= hidden_field_tag :enterprise_code, params[:enterprise_code] %> + + <% button_bar do %> + <%= submit_button('answer', _('Answer'), :cancel => {:action => 'index'} ) %> + <% end %> +<% end %> diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index ac6c075..5d92077 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -277,10 +277,16 @@ class AccountControllerTest < Test::Unit::TestCase assert_redirected_to :controller => 'profile_editor' end +################################ +# # +# Enterprise activation tests # +# # +################################ + should 'report invalid enterprise code on signup' do EnterpriseActivation.expects(:find_by_code).with('some_invalid_code').returns(nil).at_least_once - get :signup, :enterprise_code => 'some_invalid_code' + get :activation_question, :enterprise_code => 'some_invalid_code' assert_template 'invalid_enterprise_code' end @@ -291,19 +297,19 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_template 'already_activated' end - should 'load enterprise from code on signup' do + should 'load enterprise from code on for validation question' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') task = mock task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_equal ent, assigns(:enterprise) end @@ -315,7 +321,7 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_template 'blocked' end @@ -327,34 +333,32 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' - assert_template 'activate_enterprise' + assert_template 'activation_question' end should 'show form to those enterprises that have cnpj' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => false) - task = mock task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' - assert_template 'activate_enterprise' + assert_template 'activation_question' end should 'block those who are blocked' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => '1998', :enabled => false) ent.block - task = mock task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_template 'blocked' end @@ -366,7 +370,8 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - create_user({}, :enterprise_code => '0123456789', :answer => '1997') + post :accept_terms, :enterprise_code => '0123456789', :answer => '1997' + ent.reload assert_nil User.find_by_login('test_user') @@ -374,17 +379,45 @@ class AccountControllerTest < Test::Unit::TestCase assert_template 'blocked' end - should 'activate enterprise for those who answer the question right and make them admin of the enterprise' do + should 'show term of use for enterprise owners' do + env = Environment.default + env.terms_of_enterprise_use = 'Some terms' + env.save! + + post :accept_terms, :enterprise_code => '0123456789', :answer => '1998' + + assert_template 'accept_terms' + assert_tag :tag => 'div', :content => 'Some terms' + end + + should 'not activate if user does not accept terms' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person + login_as(p.identifier) task = EnterpriseActivation.create!(:enterprise => ent) EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - create_user({}, :enterprise_code => '0123456789', :answer => '1998') + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => false + ent.reload + + assert !ent.enabled + assert_not_includes ent.members, p + end + + should 'activate enterprise and make user admin' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person + login_as(p.identifier) + + task = EnterpriseActivation.create!(:enterprise => ent) + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true ent.reload assert ent.enabled - assert_includes ent.members, assigns(:user).person + assert_includes ent.members, p end should 'put hidden field with enterprise code for answering question' do @@ -394,12 +427,13 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'enterprise_code', :value => '0123456789'} - end +# end of enterprise activation tests + should 'not be able to signup while inverse captcha field filled' do assert_no_difference User, :count do create_user({}, @controller.icaptcha_field => 'bli@bla.email.foo') diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 29a54eb..606ea48 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -78,6 +78,22 @@ class EnvironmentTest < Test::Unit::TestCase assert v.has_terms_of_use? end + def test_terms_of_enterprise_use + v = Environment.new(:name => 'My test environment') + assert_nil v.terms_of_enterprise_use + v.terms_of_enterprise_use = 'To be owner of an enterprise in this environment, you must accept the following terms: ...' + assert v.save + id = v.id + assert_equal 'To be owner of an enterprise in this environment, you must accept the following terms: ...', Environment.find(id).terms_of_enterprise_use + end + + def test_has_terms_of_enterprise_use + v = Environment.new + assert !v.has_terms_of_enterprise_use? + v.terms_of_enterprise_use = 'some terms of enterprise use' + assert v.has_terms_of_enterprise_use? + end + def test_should_list_top_level_categories env = Environment.create!(:name => 'a test environment') cat1 = Category.create!(:name => 'first category', :environment_id => env.id) -- libgit2 0.21.2