From d0645bf00dafa66f29c53f74eff7697d24ac6a59 Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Fri, 13 Jun 2008 22:11:31 +0000 Subject: [PATCH] ActionItem439: finished the activation of enterprise by code --- app/controllers/public/account_controller.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++++--------- app/models/enterprise.rb | 17 +++++++++++++++++ app/models/profile.rb | 3 +++ app/views/account/already_activated.rhtml | 1 + app/views/account/blocked.rhtml | 2 ++ test/functional/account_controller_test.rb | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- test/unit/enterprise_test.rb | 20 ++++++++++++++++++++ 7 files changed, 149 insertions(+), 20 deletions(-) create mode 100644 app/views/account/already_activated.rhtml create mode 100644 app/views/account/blocked.rhtml diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index 901b164..1f1acb2 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -38,20 +38,22 @@ class AccountController < PublicController @user = User.new(params[:user]) @user.terms_of_use = environment.terms_of_use @terms_of_use = environment.terms_of_use - if request.post? + if request.post? && answer_correct @user.save! @user.person.environment = environment @user.person.save! self.current_user = @user owner_role = Role.find_by_name('owner') @user.person.affiliate(@user.person, [owner_role]) if owner_role + post_activate_enterprise if params[:enterprise_code] go_to_user_initial_page flash[:notice] = _("Thanks for signing up!") + else + activate_enterprise if params[:enterprise_code] end - activate_enterprise if params[:enterprise_code] rescue ActiveRecord::RecordInvalid if params[:enterprise_code] - render :action => 'activate_enteprise' + render :action => 'activate_enterprise' else render :action => 'signup' end @@ -126,24 +128,60 @@ class AccountController < PublicController protected def activate_enterprise - @enterprise = Enterprise.return_by_code(params[:enterprise_code]) + load_enterprise unless @enterprise render :action => 'invalid_enterprise_code' return end + if @enterprise.enabled + render :action => 'already_activated' + return + end + + # Reaches here only if answer is not correct + if request.post? && !answer_correct + @enterprise.block + end + + define_question + + if !@question || @enterprise.blocked? + render :action => 'blocked' + return + end + + render :action => 'activate_enterprise' + end + + def post_activate_enterprise + if @enterprise + @enterprise.enable(@user.person) + end + end + + def load_enterprise + @enterprise ||= Enterprise.return_by_code(params[:enterprise_code]) + end + + def define_question + return if @question if !@enterprise.foundation_year.blank? @question = :foundation_year elsif !@enterprise.cnpj.blank? @question = :cnpj end + end - unless @question - render :action => 'blocked' - return - end + def answer_correct + return true unless params[:enterprise_code] + + load_enterprise + define_question + return false unless @question + return false if @enterprise.enabled - render :action => 'activate_enterprise'; return + params[:answer] == @enterprise.send(@question).to_s end def go_to_user_initial_page diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 72dbc47..0eef2d4 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -29,6 +29,7 @@ class Enterprise < Organization end def self.return_by_code(code) + return unless code id = code[0..5].to_i md5 = code[6..11] return unless md5 == Digest::MD5.hexdigest(id.to_s)[0..5] @@ -36,4 +37,20 @@ class Enterprise < Organization Enterprise.find(id) end + def blocked? + data[:blocked] + end + + def block + data[:blocked] = true + save + end + + def enable(owner) + return if enabled + affiliate(owner, Profile::Roles.all_roles) + update_attribute(:enabled,true) + save + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index a60897a..e966a49 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -19,6 +19,9 @@ class Profile < ActiveRecord::Base def self.editor ::Role.find_by_key('profile_editor') end + def self.all_roles + [admin, member, moderator, owner, editor] + end end PERMISSIONS['Profile'] = { diff --git a/app/views/account/already_activated.rhtml b/app/views/account/already_activated.rhtml new file mode 100644 index 0000000..4eca7b1 --- /dev/null +++ b/app/views/account/already_activated.rhtml @@ -0,0 +1 @@ +<%= _('This enterprise is already active') %> diff --git a/app/views/account/blocked.rhtml b/app/views/account/blocked.rhtml new file mode 100644 index 0000000..456f513 --- /dev/null +++ b/app/views/account/blocked.rhtml @@ -0,0 +1,2 @@ +<%# FIXME: use environment blocked text %> +<%= _('Your enterprise has been blocked') %> diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 53dc779..0c9a441 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -281,6 +281,13 @@ class AccountControllerTest < Test::Unit::TestCase assert_template 'invalid_enterprise_code' end + should 'report enterprise already enabled' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => true) + get :signup, :enterprise_code => ent.code + + assert_template 'already_activated' + end + should 'load enterprise from code on signup' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') get :signup, :enterprise_code => ent.code @@ -288,28 +295,69 @@ class AccountControllerTest < Test::Unit::TestCase assert_equal ent, assigns(:enterprise) end - should 'block enterprises that do not have foundation_year or cnpj' + should 'block enterprises that do not have foundation_year or cnpj' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :enabled => false) + get :signup, :enterprise_code => ent.code + + assert_template 'blocked' + end - should 'show form to those enterprises that have foundation year' + should 'show form to those enterprises that have foundation year' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + get :signup, :enterprise_code => ent.code - should 'show form to those enterprises that have cnpj' + assert_template 'activate_enterprise' + end - should 'block those who failed to answer the question' + should 'show form to those enterprises that have cnpj' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => false) + get :signup, :enterprise_code => ent.code - should 'activate enterprise for those who answer the question right' + assert_template 'activate_enterprise' + end - should 'make new user admin of new enterprise' + should 'block those who are blocked' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => '1998', :enabled => false) + ent.block + get :signup, :enterprise_code => ent.code + + assert_template 'blocked' + end + + should 'block those who failed to answer the question' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + + create_user({}, :enterprise_code => ent.code, :answer => '1997') + ent.reload + + assert_nil User.find_by_login('test_user') + assert ent.blocked? + assert_template 'blocked' + end + + should 'activate enterprise for those who answer the question right and make them admin of the enterprise' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + create_user({}, :enterprise_code => ent.code, :answer => '1998') + ent.reload + + assert ent.enabled + assert_includes ent.members, assigns(:user).person + end protected - def create_user(options = {}) - post :signup, :user => { :login => 'quire', :email => 'quire@example.com', - :password => 'quire', :password_confirmation => 'quire' }.merge(options) + def create_user(options = {}, extra_options ={}) + post :signup, { :user => { :login => 'quire', + :email => 'quire@example.com', + :password => 'quire', + :password_confirmation => 'quire' + }.merge(options) + }.merge(extra_options) end - + def auth_token(token) CGI::Cookie.new('name' => 'auth_token', 'value' => token) end - + def cookie_for(user) auth_token users(user).remember_token end diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index 2054ebc..304cc6d 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -133,6 +133,10 @@ class EnterpriseTest < Test::Unit::TestCase assert_nil Enterprise.return_by_code(ent.code.next) end + should 'return nil when asked for an enterprise with code nil' do + assert_nil Enterprise.return_by_code(nil) + end + should 'have foudation_year' do ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') @@ -147,4 +151,20 @@ class EnterpriseTest < Test::Unit::TestCase assert_respond_to ent, 'cnpj=' end + should 'block' do + ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') + ent.block + assert Enterprise.find(ent.id).blocked? + end + + should 'enable and make user admin' do + ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) + p = create_user('test_user').person + + assert ent.enable(p) + ent.reload + assert ent.enabled + assert_includes ent.members, p + end + end -- libgit2 0.21.2