Commit d0645bf00dafa66f29c53f74eff7697d24ac6a59
1 parent
da8eda3a
Exists in
master
and in
29 other branches
ActionItem439: finished the activation of enterprise by code
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2029 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
7 changed files
with
149 additions
and
20 deletions
Show diff stats
app/controllers/public/account_controller.rb
| ... | ... | @@ -38,20 +38,22 @@ class AccountController < PublicController |
| 38 | 38 | @user = User.new(params[:user]) |
| 39 | 39 | @user.terms_of_use = environment.terms_of_use |
| 40 | 40 | @terms_of_use = environment.terms_of_use |
| 41 | - if request.post? | |
| 41 | + if request.post? && answer_correct | |
| 42 | 42 | @user.save! |
| 43 | 43 | @user.person.environment = environment |
| 44 | 44 | @user.person.save! |
| 45 | 45 | self.current_user = @user |
| 46 | 46 | owner_role = Role.find_by_name('owner') |
| 47 | 47 | @user.person.affiliate(@user.person, [owner_role]) if owner_role |
| 48 | + post_activate_enterprise if params[:enterprise_code] | |
| 48 | 49 | go_to_user_initial_page |
| 49 | 50 | flash[:notice] = _("Thanks for signing up!") |
| 51 | + else | |
| 52 | + activate_enterprise if params[:enterprise_code] | |
| 50 | 53 | end |
| 51 | - activate_enterprise if params[:enterprise_code] | |
| 52 | 54 | rescue ActiveRecord::RecordInvalid |
| 53 | 55 | if params[:enterprise_code] |
| 54 | - render :action => 'activate_enteprise' | |
| 56 | + render :action => 'activate_enterprise' | |
| 55 | 57 | else |
| 56 | 58 | render :action => 'signup' |
| 57 | 59 | end |
| ... | ... | @@ -126,24 +128,60 @@ class AccountController < PublicController |
| 126 | 128 | protected |
| 127 | 129 | |
| 128 | 130 | def activate_enterprise |
| 129 | - @enterprise = Enterprise.return_by_code(params[:enterprise_code]) | |
| 131 | + load_enterprise | |
| 130 | 132 | unless @enterprise |
| 131 | 133 | render :action => 'invalid_enterprise_code' |
| 132 | 134 | return |
| 133 | 135 | end |
| 134 | 136 | |
| 137 | + if @enterprise.enabled | |
| 138 | + render :action => 'already_activated' | |
| 139 | + return | |
| 140 | + end | |
| 141 | + | |
| 142 | + # Reaches here only if answer is not correct | |
| 143 | + if request.post? && !answer_correct | |
| 144 | + @enterprise.block | |
| 145 | + end | |
| 146 | + | |
| 147 | + define_question | |
| 148 | + | |
| 149 | + if !@question || @enterprise.blocked? | |
| 150 | + render :action => 'blocked' | |
| 151 | + return | |
| 152 | + end | |
| 153 | + | |
| 154 | + render :action => 'activate_enterprise' | |
| 155 | + end | |
| 156 | + | |
| 157 | + def post_activate_enterprise | |
| 158 | + if @enterprise | |
| 159 | + @enterprise.enable(@user.person) | |
| 160 | + end | |
| 161 | + end | |
| 162 | + | |
| 163 | + def load_enterprise | |
| 164 | + @enterprise ||= Enterprise.return_by_code(params[:enterprise_code]) | |
| 165 | + end | |
| 166 | + | |
| 167 | + def define_question | |
| 168 | + return if @question | |
| 135 | 169 | if !@enterprise.foundation_year.blank? |
| 136 | 170 | @question = :foundation_year |
| 137 | 171 | elsif !@enterprise.cnpj.blank? |
| 138 | 172 | @question = :cnpj |
| 139 | 173 | end |
| 174 | + end | |
| 140 | 175 | |
| 141 | - unless @question | |
| 142 | - render :action => 'blocked' | |
| 143 | - return | |
| 144 | - end | |
| 176 | + def answer_correct | |
| 177 | + return true unless params[:enterprise_code] | |
| 178 | + | |
| 179 | + load_enterprise | |
| 180 | + define_question | |
| 181 | + return false unless @question | |
| 182 | + return false if @enterprise.enabled | |
| 145 | 183 | |
| 146 | - render :action => 'activate_enterprise'; return | |
| 184 | + params[:answer] == @enterprise.send(@question).to_s | |
| 147 | 185 | end |
| 148 | 186 | |
| 149 | 187 | def go_to_user_initial_page | ... | ... |
app/models/enterprise.rb
| ... | ... | @@ -29,6 +29,7 @@ class Enterprise < Organization |
| 29 | 29 | end |
| 30 | 30 | |
| 31 | 31 | def self.return_by_code(code) |
| 32 | + return unless code | |
| 32 | 33 | id = code[0..5].to_i |
| 33 | 34 | md5 = code[6..11] |
| 34 | 35 | return unless md5 == Digest::MD5.hexdigest(id.to_s)[0..5] |
| ... | ... | @@ -36,4 +37,20 @@ class Enterprise < Organization |
| 36 | 37 | Enterprise.find(id) |
| 37 | 38 | end |
| 38 | 39 | |
| 40 | + def blocked? | |
| 41 | + data[:blocked] | |
| 42 | + end | |
| 43 | + | |
| 44 | + def block | |
| 45 | + data[:blocked] = true | |
| 46 | + save | |
| 47 | + end | |
| 48 | + | |
| 49 | + def enable(owner) | |
| 50 | + return if enabled | |
| 51 | + affiliate(owner, Profile::Roles.all_roles) | |
| 52 | + update_attribute(:enabled,true) | |
| 53 | + save | |
| 54 | + end | |
| 55 | + | |
| 39 | 56 | end | ... | ... |
app/models/profile.rb
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +<%= _('This enterprise is already active') %> | ... | ... |
test/functional/account_controller_test.rb
| ... | ... | @@ -281,6 +281,13 @@ class AccountControllerTest < Test::Unit::TestCase |
| 281 | 281 | assert_template 'invalid_enterprise_code' |
| 282 | 282 | end |
| 283 | 283 | |
| 284 | + should 'report enterprise already enabled' do | |
| 285 | + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => true) | |
| 286 | + get :signup, :enterprise_code => ent.code | |
| 287 | + | |
| 288 | + assert_template 'already_activated' | |
| 289 | + end | |
| 290 | + | |
| 284 | 291 | should 'load enterprise from code on signup' do |
| 285 | 292 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') |
| 286 | 293 | get :signup, :enterprise_code => ent.code |
| ... | ... | @@ -288,28 +295,69 @@ class AccountControllerTest < Test::Unit::TestCase |
| 288 | 295 | assert_equal ent, assigns(:enterprise) |
| 289 | 296 | end |
| 290 | 297 | |
| 291 | - should 'block enterprises that do not have foundation_year or cnpj' | |
| 298 | + should 'block enterprises that do not have foundation_year or cnpj' do | |
| 299 | + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :enabled => false) | |
| 300 | + get :signup, :enterprise_code => ent.code | |
| 301 | + | |
| 302 | + assert_template 'blocked' | |
| 303 | + end | |
| 292 | 304 | |
| 293 | - should 'show form to those enterprises that have foundation year' | |
| 305 | + should 'show form to those enterprises that have foundation year' do | |
| 306 | + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) | |
| 307 | + get :signup, :enterprise_code => ent.code | |
| 294 | 308 | |
| 295 | - should 'show form to those enterprises that have cnpj' | |
| 309 | + assert_template 'activate_enterprise' | |
| 310 | + end | |
| 296 | 311 | |
| 297 | - should 'block those who failed to answer the question' | |
| 312 | + should 'show form to those enterprises that have cnpj' do | |
| 313 | + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => false) | |
| 314 | + get :signup, :enterprise_code => ent.code | |
| 298 | 315 | |
| 299 | - should 'activate enterprise for those who answer the question right' | |
| 316 | + assert_template 'activate_enterprise' | |
| 317 | + end | |
| 300 | 318 | |
| 301 | - should 'make new user admin of new enterprise' | |
| 319 | + should 'block those who are blocked' do | |
| 320 | + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => '1998', :enabled => false) | |
| 321 | + ent.block | |
| 322 | + get :signup, :enterprise_code => ent.code | |
| 323 | + | |
| 324 | + assert_template 'blocked' | |
| 325 | + end | |
| 326 | + | |
| 327 | + should 'block those who failed to answer the question' do | |
| 328 | + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) | |
| 329 | + | |
| 330 | + create_user({}, :enterprise_code => ent.code, :answer => '1997') | |
| 331 | + ent.reload | |
| 332 | + | |
| 333 | + assert_nil User.find_by_login('test_user') | |
| 334 | + assert ent.blocked? | |
| 335 | + assert_template 'blocked' | |
| 336 | + end | |
| 337 | + | |
| 338 | + should 'activate enterprise for those who answer the question right and make them admin of the enterprise' do | |
| 339 | + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) | |
| 340 | + create_user({}, :enterprise_code => ent.code, :answer => '1998') | |
| 341 | + ent.reload | |
| 342 | + | |
| 343 | + assert ent.enabled | |
| 344 | + assert_includes ent.members, assigns(:user).person | |
| 345 | + end | |
| 302 | 346 | |
| 303 | 347 | protected |
| 304 | - def create_user(options = {}) | |
| 305 | - post :signup, :user => { :login => 'quire', :email => 'quire@example.com', | |
| 306 | - :password => 'quire', :password_confirmation => 'quire' }.merge(options) | |
| 348 | + def create_user(options = {}, extra_options ={}) | |
| 349 | + post :signup, { :user => { :login => 'quire', | |
| 350 | + :email => 'quire@example.com', | |
| 351 | + :password => 'quire', | |
| 352 | + :password_confirmation => 'quire' | |
| 353 | + }.merge(options) | |
| 354 | + }.merge(extra_options) | |
| 307 | 355 | end |
| 308 | - | |
| 356 | + | |
| 309 | 357 | def auth_token(token) |
| 310 | 358 | CGI::Cookie.new('name' => 'auth_token', 'value' => token) |
| 311 | 359 | end |
| 312 | - | |
| 360 | + | |
| 313 | 361 | def cookie_for(user) |
| 314 | 362 | auth_token users(user).remember_token |
| 315 | 363 | end | ... | ... |
test/unit/enterprise_test.rb
| ... | ... | @@ -133,6 +133,10 @@ class EnterpriseTest < Test::Unit::TestCase |
| 133 | 133 | assert_nil Enterprise.return_by_code(ent.code.next) |
| 134 | 134 | end |
| 135 | 135 | |
| 136 | + should 'return nil when asked for an enterprise with code nil' do | |
| 137 | + assert_nil Enterprise.return_by_code(nil) | |
| 138 | + end | |
| 139 | + | |
| 136 | 140 | should 'have foudation_year' do |
| 137 | 141 | ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') |
| 138 | 142 | |
| ... | ... | @@ -147,4 +151,20 @@ class EnterpriseTest < Test::Unit::TestCase |
| 147 | 151 | assert_respond_to ent, 'cnpj=' |
| 148 | 152 | end |
| 149 | 153 | |
| 154 | + should 'block' do | |
| 155 | + ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') | |
| 156 | + ent.block | |
| 157 | + assert Enterprise.find(ent.id).blocked? | |
| 158 | + end | |
| 159 | + | |
| 160 | + should 'enable and make user admin' do | |
| 161 | + ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) | |
| 162 | + p = create_user('test_user').person | |
| 163 | + | |
| 164 | + assert ent.enable(p) | |
| 165 | + ent.reload | |
| 166 | + assert ent.enabled | |
| 167 | + assert_includes ent.members, p | |
| 168 | + end | |
| 169 | + | |
| 150 | 170 | end | ... | ... |