Commit 2401de2461bde8cbfe43ff49c6cada509293129b
1 parent
1c4bc5e7
Exists in
master
and in
28 other branches
ActionItem439: finishing enterprise activation (or ... sort of)
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2050 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
11 changed files
with
232 additions
and
64 deletions
Show diff stats
app/controllers/my_profile/profile_editor_controller.rb
app/controllers/public/account_controller.rb
... | ... | @@ -128,25 +128,27 @@ class AccountController < PublicController |
128 | 128 | protected |
129 | 129 | |
130 | 130 | def activate_enterprise |
131 | - load_enterprise | |
132 | - unless @enterprise | |
131 | + enterprise = load_enterprise | |
132 | + @enterprise = enterprise | |
133 | + | |
134 | + unless enterprise | |
133 | 135 | render :action => 'invalid_enterprise_code' |
134 | 136 | return |
135 | 137 | end |
136 | 138 | |
137 | - if @enterprise.enabled | |
139 | + if enterprise.enabled | |
138 | 140 | render :action => 'already_activated' |
139 | 141 | return |
140 | 142 | end |
141 | 143 | |
142 | 144 | # Reaches here only if answer is not correct |
143 | 145 | if request.post? && !answer_correct |
144 | - @enterprise.block | |
146 | + enterprise.block | |
145 | 147 | end |
146 | 148 | |
147 | - define_question | |
149 | + @question = enterprise.question | |
148 | 150 | |
149 | - if !@question || @enterprise.blocked? | |
151 | + if !@question || enterprise.blocked? | |
150 | 152 | render :action => 'blocked' |
151 | 153 | return |
152 | 154 | end |
... | ... | @@ -155,33 +157,34 @@ class AccountController < PublicController |
155 | 157 | end |
156 | 158 | |
157 | 159 | def post_activate_enterprise |
158 | - if @enterprise | |
159 | - @enterprise.enable(@user.person) | |
160 | + activation = load_enterprise_activation | |
161 | + if activation | |
162 | + activation.requestor = user | |
163 | + activation.finish | |
160 | 164 | end |
161 | 165 | end |
162 | 166 | |
163 | - def load_enterprise | |
164 | - @enterprise ||= Enterprise.return_by_code(params[:enterprise_code]) | |
167 | + def load_enterprise_activation | |
168 | + EnterpriseActivation.find_by_code(params[:enterprise_code]) | |
165 | 169 | end |
166 | 170 | |
167 | - def define_question | |
168 | - return if @question | |
169 | - if !@enterprise.foundation_year.blank? | |
170 | - @question = :foundation_year | |
171 | - elsif !@enterprise.cnpj.blank? | |
172 | - @question = :cnpj | |
171 | + def load_enterprise | |
172 | + activation = load_enterprise_activation | |
173 | + if activation.nil? | |
174 | + nil | |
175 | + else | |
176 | + activation.enterprise | |
173 | 177 | end |
174 | 178 | end |
175 | 179 | |
176 | 180 | def answer_correct |
177 | 181 | return true unless params[:enterprise_code] |
178 | 182 | |
179 | - load_enterprise | |
180 | - define_question | |
181 | - return false unless @question | |
182 | - return false if @enterprise.enabled | |
183 | + enterprise = load_enterprise | |
184 | + return false unless enterprise.question | |
185 | + return false if enterprise.enabled | |
183 | 186 | |
184 | - params[:answer] == @enterprise.send(@question).to_s | |
187 | + params[:answer] == enterprise.send(enterprise.question).to_s | |
185 | 188 | end |
186 | 189 | |
187 | 190 | def go_to_user_initial_page | ... | ... |
app/models/enterprise.rb
... | ... | @@ -24,19 +24,6 @@ class Enterprise < Organization |
24 | 24 | true |
25 | 25 | end |
26 | 26 | |
27 | - def code | |
28 | - ("%06d" % id) + Digest::MD5.hexdigest(id.to_s)[0..5] | |
29 | - end | |
30 | - | |
31 | - def self.return_by_code(code) | |
32 | - return unless code | |
33 | - id = code[0..5].to_i | |
34 | - md5 = code[6..11] | |
35 | - return unless md5 == Digest::MD5.hexdigest(id.to_s)[0..5] | |
36 | - | |
37 | - Enterprise.find(id) | |
38 | - end | |
39 | - | |
40 | 27 | def blocked? |
41 | 28 | data[:blocked] |
42 | 29 | end |
... | ... | @@ -53,4 +40,21 @@ class Enterprise < Organization |
53 | 40 | save |
54 | 41 | end |
55 | 42 | |
43 | + def question | |
44 | + if !self.foundation_year.blank? | |
45 | + :foundation_year | |
46 | + elsif !self.cnpj.blank? | |
47 | + :cnpj | |
48 | + else | |
49 | + nil | |
50 | + end | |
51 | + end | |
52 | + | |
53 | + after_create :create_activation_task | |
54 | + def create_activation_task | |
55 | + if !self.enabled | |
56 | + EnterpriseActivation.create!(:enterprise => self, :code_length => 7) | |
57 | + end | |
58 | + end | |
59 | + | |
56 | 60 | end | ... | ... |
... | ... | @@ -0,0 +1,23 @@ |
1 | +class EnterpriseActivation < Task | |
2 | + | |
3 | + class RequestorRequired < Exception; end | |
4 | + | |
5 | + acts_as_having_settings :field => :data | |
6 | + settings_items :enterprise_id, :integer | |
7 | + | |
8 | + validates_presence_of :enterprise_id | |
9 | + | |
10 | + def enterprise | |
11 | + Enterprise.find(enterprise_id) | |
12 | + end | |
13 | + | |
14 | + def enterprise=(ent) | |
15 | + self.enterprise_id = ent.id | |
16 | + end | |
17 | + | |
18 | + def perform | |
19 | + raise EnterpriseActivation::RequestorRequired if requestor.nil? | |
20 | + self.enterprise.enable(requestor) | |
21 | + end | |
22 | + | |
23 | +end | ... | ... |
app/models/task.rb
... | ... | @@ -35,11 +35,12 @@ class Task < ActiveRecord::Base |
35 | 35 | self.status ||= Task::Status::ACTIVE |
36 | 36 | end |
37 | 37 | |
38 | + attr_accessor :code_length | |
38 | 39 | before_validation_on_create do |task| |
39 | 40 | if task.code.nil? |
40 | - task.code = Task.generate_code | |
41 | + task.code = Task.generate_code(task.code_length) | |
41 | 42 | while (Task.find_by_code(task.code)) |
42 | - task.code = Task.generate_code | |
43 | + task.code = Task.generate_code(task.code_length) | |
43 | 44 | end |
44 | 45 | end |
45 | 46 | end |
... | ... | @@ -162,12 +163,12 @@ class Task < ActiveRecord::Base |
162 | 163 | self.find(:all, :conditions => { :status => [Task::Status::CANCELLED, Task::Status::FINISHED]}) |
163 | 164 | end |
164 | 165 | |
165 | - # generates a random code string consisting of 36 characters in the ranges | |
166 | - # a-z and 0-9 | |
167 | - def generate_code | |
166 | + # generates a random code string consisting of length characters (or 36 by | |
167 | + # default) in the ranges a-z and 0-9 | |
168 | + def generate_code(length = nil) | |
168 | 169 | chars = ('a'..'z').to_a + ('0'..'9').to_a |
169 | 170 | code = "" |
170 | - chars.size.times do |n| | |
171 | + (length || chars.size).times do |n| | |
171 | 172 | code << chars[rand(chars.size)] |
172 | 173 | end |
173 | 174 | code | ... | ... |
app/views/account/activate_enterprise.rhtml
... | ... | @@ -26,7 +26,7 @@ |
26 | 26 | |
27 | 27 | <%= 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'))) %> |
28 | 28 | |
29 | -<%= hidden_field_tag :enterprise_code, @enterprise.code %> | |
29 | +<%= hidden_field_tag :enterprise_code, params[:enterprise_code] %> | |
30 | 30 | |
31 | 31 | <% if @terms_of_use %> |
32 | 32 | <p> | ... | ... |
db/schema.rb
... | ... | @@ -68,8 +68,8 @@ ActiveRecord::Schema.define(:version => 41) do |
68 | 68 | t.integer "category_id" |
69 | 69 | end |
70 | 70 | |
71 | - add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id" | |
72 | 71 | add_index "articles_categories", ["category_id"], :name => "index_articles_categories_on_category_id" |
72 | + add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id" | |
73 | 73 | |
74 | 74 | create_table "blocks", :force => true do |t| |
75 | 75 | t.string "title" |
... | ... | @@ -217,8 +217,8 @@ ActiveRecord::Schema.define(:version => 41) do |
217 | 217 | t.datetime "created_at" |
218 | 218 | end |
219 | 219 | |
220 | - add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" | |
221 | 220 | add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type" |
221 | + add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" | |
222 | 222 | |
223 | 223 | create_table "tags", :force => true do |t| |
224 | 224 | t.string "name" | ... | ... |
test/functional/account_controller_test.rb
... | ... | @@ -277,6 +277,8 @@ class AccountControllerTest < Test::Unit::TestCase |
277 | 277 | end |
278 | 278 | |
279 | 279 | should 'report invalid enterprise code on signup' do |
280 | + EnterpriseActivation.expects(:find_by_code).with('some_invalid_code').returns(nil).at_least_once | |
281 | + | |
280 | 282 | get :signup, :enterprise_code => 'some_invalid_code' |
281 | 283 | |
282 | 284 | assert_template 'invalid_enterprise_code' |
... | ... | @@ -284,35 +286,60 @@ class AccountControllerTest < Test::Unit::TestCase |
284 | 286 | |
285 | 287 | should 'report enterprise already enabled' do |
286 | 288 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => true) |
287 | - get :signup, :enterprise_code => ent.code | |
289 | + task = mock | |
290 | + task.expects(:enterprise).returns(ent).at_least_once | |
291 | + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
292 | + | |
293 | + get :signup, :enterprise_code => '0123456789' | |
288 | 294 | |
289 | 295 | assert_template 'already_activated' |
290 | 296 | end |
291 | 297 | |
292 | 298 | should 'load enterprise from code on signup' do |
293 | 299 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') |
294 | - get :signup, :enterprise_code => ent.code | |
300 | + | |
301 | + task = mock | |
302 | + task.expects(:enterprise).returns(ent).at_least_once | |
303 | + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
304 | + | |
305 | + get :signup, :enterprise_code => '0123456789' | |
295 | 306 | |
296 | 307 | assert_equal ent, assigns(:enterprise) |
297 | 308 | end |
298 | 309 | |
299 | 310 | should 'block enterprises that do not have foundation_year or cnpj' do |
300 | 311 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :enabled => false) |
301 | - get :signup, :enterprise_code => ent.code | |
312 | + | |
313 | + task = mock | |
314 | + task.expects(:enterprise).returns(ent).at_least_once | |
315 | + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
316 | + | |
317 | + get :signup, :enterprise_code => '0123456789' | |
302 | 318 | |
303 | 319 | assert_template 'blocked' |
304 | 320 | end |
305 | 321 | |
306 | 322 | should 'show form to those enterprises that have foundation year' do |
307 | 323 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) |
308 | - get :signup, :enterprise_code => ent.code | |
324 | + | |
325 | + task = mock | |
326 | + task.expects(:enterprise).returns(ent).at_least_once | |
327 | + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
328 | + | |
329 | + get :signup, :enterprise_code => '0123456789' | |
309 | 330 | |
310 | 331 | assert_template 'activate_enterprise' |
311 | 332 | end |
312 | 333 | |
313 | 334 | should 'show form to those enterprises that have cnpj' do |
314 | 335 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => false) |
315 | - get :signup, :enterprise_code => ent.code | |
336 | + | |
337 | + | |
338 | + task = mock | |
339 | + task.expects(:enterprise).returns(ent).at_least_once | |
340 | + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
341 | + | |
342 | + get :signup, :enterprise_code => '0123456789' | |
316 | 343 | |
317 | 344 | assert_template 'activate_enterprise' |
318 | 345 | end |
... | ... | @@ -320,7 +347,13 @@ class AccountControllerTest < Test::Unit::TestCase |
320 | 347 | should 'block those who are blocked' do |
321 | 348 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => '1998', :enabled => false) |
322 | 349 | ent.block |
323 | - get :signup, :enterprise_code => ent.code | |
350 | + | |
351 | + | |
352 | + task = mock | |
353 | + task.expects(:enterprise).returns(ent).at_least_once | |
354 | + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
355 | + | |
356 | + get :signup, :enterprise_code => '0123456789' | |
324 | 357 | |
325 | 358 | assert_template 'blocked' |
326 | 359 | end |
... | ... | @@ -328,7 +361,11 @@ class AccountControllerTest < Test::Unit::TestCase |
328 | 361 | should 'block those who failed to answer the question' do |
329 | 362 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) |
330 | 363 | |
331 | - create_user({}, :enterprise_code => ent.code, :answer => '1997') | |
364 | + task = mock | |
365 | + task.expects(:enterprise).returns(ent).at_least_once | |
366 | + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
367 | + | |
368 | + create_user({}, :enterprise_code => '0123456789', :answer => '1997') | |
332 | 369 | ent.reload |
333 | 370 | |
334 | 371 | assert_nil User.find_by_login('test_user') |
... | ... | @@ -338,13 +375,30 @@ class AccountControllerTest < Test::Unit::TestCase |
338 | 375 | |
339 | 376 | should 'activate enterprise for those who answer the question right and make them admin of the enterprise' do |
340 | 377 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) |
341 | - create_user({}, :enterprise_code => ent.code, :answer => '1998') | |
378 | + | |
379 | + task = EnterpriseActivation.create!(:enterprise => ent) | |
380 | + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
381 | + | |
382 | + create_user({}, :enterprise_code => '0123456789', :answer => '1998') | |
342 | 383 | ent.reload |
343 | 384 | |
344 | 385 | assert ent.enabled |
345 | 386 | assert_includes ent.members, assigns(:user).person |
346 | 387 | end |
347 | 388 | |
389 | + should 'put hidden field with enterprise code for answering question' do | |
390 | + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) | |
391 | + | |
392 | + task = mock | |
393 | + task.expects(:enterprise).returns(ent).at_least_once | |
394 | + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
395 | + | |
396 | + get :signup, :enterprise_code => '0123456789' | |
397 | + | |
398 | + assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'enterprise_code', :value => '0123456789'} | |
399 | + | |
400 | + end | |
401 | + | |
348 | 402 | protected |
349 | 403 | def create_user(options = {}, extra_options ={}) |
350 | 404 | post :signup, { :user => { :login => 'quire', | ... | ... |
... | ... | @@ -0,0 +1,68 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +class EnterpriseActivationTest < ActiveSupport::TestCase | |
4 | + | |
5 | + fixtures :users, :profiles | |
6 | + | |
7 | + should 'be a Task' do | |
8 | + assert_kind_of Task, EnterpriseActivation.new | |
9 | + end | |
10 | + | |
11 | + should 'keep enterprise_id' do | |
12 | + assert_nil EnterpriseActivation.new.enterprise_id | |
13 | + end | |
14 | + | |
15 | + should 'have an enteprise through enterprise_id' do | |
16 | + ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent') | |
17 | + | |
18 | + assert_equal ent, EnterpriseActivation.new(:enterprise_id => ent.id).enterprise | |
19 | + end | |
20 | + | |
21 | + should 'require an enterprise' do | |
22 | + t = EnterpriseActivation.new | |
23 | + t.valid? | |
24 | + assert t.errors.invalid?(:enterprise_id), "enterprise must be required" | |
25 | + | |
26 | + ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent') | |
27 | + t.enterprise = ent | |
28 | + t.valid? | |
29 | + assert !t.errors.invalid?(:enterprise_id), "must validate after enterprise is set" | |
30 | + end | |
31 | + | |
32 | + should 'activate enterprise when finished' do | |
33 | + ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent', :enabled => false) | |
34 | + t = EnterpriseActivation.create!(:enterprise => ent) | |
35 | + t.requestor = profiles(:ze) | |
36 | + | |
37 | + t.finish | |
38 | + ent.reload | |
39 | + | |
40 | + assert ent.enabled, "finishing task should left enterprise enabled" | |
41 | + end | |
42 | + | |
43 | + should 'require requestor to finish' do | |
44 | + ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent', :enabled => false) | |
45 | + t = EnterpriseActivation.create!(:enterprise => ent) | |
46 | + | |
47 | + assert_raise EnterpriseActivation::RequestorRequired do | |
48 | + t.finish | |
49 | + end | |
50 | + end | |
51 | + | |
52 | + should 'put requestor as enterprise owner when finishing' do | |
53 | + ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent', :enabled => false) | |
54 | + t = EnterpriseActivation.create!(:enterprise => ent) | |
55 | + | |
56 | + person = profiles(:ze) | |
57 | + t.requestor = person | |
58 | + | |
59 | + t.stubs(:enterprise).returns(ent) | |
60 | + | |
61 | + # must pass the requestor to the enterprise activation | |
62 | + ent.expects(:enable).with(person) | |
63 | + | |
64 | + t.finish | |
65 | + end | |
66 | + | |
67 | +end | |
68 | + | ... | ... |
test/unit/enterprise_test.rb
... | ... | @@ -125,18 +125,6 @@ class EnterpriseTest < Test::Unit::TestCase |
125 | 125 | assert_not_includes c.members, p |
126 | 126 | end |
127 | 127 | |
128 | - should 'return coherent code' do | |
129 | - ent = Enterprise.create!(:name => 'my test profile', :identifier => 'mytestprofile') | |
130 | - ent2 = Enterprise.create!(:name => 'my test profile 2', :identifier => 'mytestprofile2') | |
131 | - | |
132 | - assert_equal ent, Enterprise.return_by_code(ent.code) | |
133 | - assert_nil Enterprise.return_by_code(ent.code.next) | |
134 | - end | |
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 | - | |
140 | 128 | should 'have foudation_year' do |
141 | 129 | ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') |
142 | 130 | |
... | ... | @@ -167,4 +155,22 @@ class EnterpriseTest < Test::Unit::TestCase |
167 | 155 | assert_includes ent.members, p |
168 | 156 | end |
169 | 157 | |
158 | + should 'create EnterpriseActivation task when creating with enabled = false' do | |
159 | + EnterpriseActivation.delete_all | |
160 | + ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) | |
161 | + assert_equal [ent], EnterpriseActivation.find(:all).map(&:enterprise) | |
162 | + end | |
163 | + | |
164 | + should 'create EnterpriseActivation with 7-characters codes' do | |
165 | + EnterpriseActivation.delete_all | |
166 | + Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) | |
167 | + assert_equal 7, EnterpriseActivation.find(:first).code.size | |
168 | + end | |
169 | + | |
170 | + should 'not create activation task when enabled = true' do | |
171 | + assert_no_difference EnterpriseActivation, :count do | |
172 | + Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => true) | |
173 | + end | |
174 | + end | |
175 | + | |
170 | 176 | end | ... | ... |
test/unit/task_test.rb
... | ... | @@ -135,6 +135,15 @@ class TaskTest < Test::Unit::TestCase |
135 | 135 | assert_not_nil Task.find_by_code(task.code) |
136 | 136 | end |
137 | 137 | |
138 | + should 'use 36-chars codes by default' do | |
139 | + assert_equal 36, Task.create.code.size | |
140 | + end | |
141 | + | |
142 | + should 'be able to limit the length of the generated code' do | |
143 | + assert_equal 3, Task.create(:code_length => 3).code.size | |
144 | + assert_equal 7, Task.create(:code_length => 7).code.size | |
145 | + end | |
146 | + | |
138 | 147 | should 'not send notification to target when target_notification_message is nil (as in Task base class)' do |
139 | 148 | task = Task.new |
140 | 149 | TaskMailer.expects(:deliver_target_notification).never | ... | ... |