Commit ee4b142a2dc4f2a5bbe90101a0e1f126bf898987
Committed by
Tallys Martins
1 parent
8af1ea81
Exists in
master
and in
22 other branches
AI3036 Code Refactoring
Modified conditional to be more logical on User model. Renamed moderation task to a more intuitive name. Added unit test for moderate_user_registration task. Signed-off-by: André Bernardes <andrebsguedes@gmail.com> Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
Showing
4 changed files
with
85 additions
and
63 deletions
Show diff stats
app/models/create_user.rb
| ... | ... | @@ -1,59 +0,0 @@ |
| 1 | -class CreateUser < Task | |
| 2 | - | |
| 3 | - settings_items :user_id, :type => String | |
| 4 | - settings_items :name, :type => String | |
| 5 | - settings_items :author_name, :type => String | |
| 6 | - settings_items :email, :type => String | |
| 7 | - | |
| 8 | - after_create :schedule_spam_checking | |
| 9 | - | |
| 10 | - alias :environment :target | |
| 11 | - alias :environment= :target= | |
| 12 | - | |
| 13 | - def schedule_spam_checking | |
| 14 | - self.delay.check_for_spam | |
| 15 | - end | |
| 16 | - | |
| 17 | - include Noosfero::Plugin::HotSpot | |
| 18 | - | |
| 19 | - def sender | |
| 20 | - "#{name} (#{email})" | |
| 21 | - end | |
| 22 | - | |
| 23 | - def perform | |
| 24 | - user=environment.users.find_by_id(user_id) | |
| 25 | - user.activate | |
| 26 | - end | |
| 27 | - | |
| 28 | - def title | |
| 29 | - _("New user") | |
| 30 | - end | |
| 31 | - | |
| 32 | - def subject | |
| 33 | - name | |
| 34 | - end | |
| 35 | - | |
| 36 | - def information | |
| 37 | - { :message => _('%{sender} wants to register.'), | |
| 38 | - :variables => {:sender => sender} } | |
| 39 | - end | |
| 40 | - | |
| 41 | - def icon | |
| 42 | - result = {:type => :defined_image, :src => '/images/icons-app/person-minor.png', :name => name} | |
| 43 | - end | |
| 44 | - | |
| 45 | - def target_notification_description | |
| 46 | - _('%{sender} tried to register.') % | |
| 47 | - {:sender => sender} | |
| 48 | - end | |
| 49 | - | |
| 50 | - def target_notification_message | |
| 51 | - target_notification_description + "\n\n" + | |
| 52 | - _('You need to login on %{system} in order to approve or reject this user.') % { :environment => self.environment } | |
| 53 | - end | |
| 54 | - | |
| 55 | - def target_notification_message | |
| 56 | - _("User \"%{user}\" just requested to register. You have to approve or reject it through the \"Pending Validations\" section in your control panel.\n") % { :user => self.name } | |
| 57 | - end | |
| 58 | - | |
| 59 | -end | |
| 60 | 0 | \ No newline at end of file |
| ... | ... | @@ -0,0 +1,59 @@ |
| 1 | +class ModerateUserRegistration < Task | |
| 2 | + | |
| 3 | + settings_items :user_id, :type => String | |
| 4 | + settings_items :name, :type => String | |
| 5 | + settings_items :author_name, :type => String | |
| 6 | + settings_items :email, :type => String | |
| 7 | + | |
| 8 | + after_create :schedule_spam_checking | |
| 9 | + | |
| 10 | + alias :environment :target | |
| 11 | + alias :environment= :target= | |
| 12 | + | |
| 13 | + def schedule_spam_checking | |
| 14 | + self.delay.check_for_spam | |
| 15 | + end | |
| 16 | + | |
| 17 | + include Noosfero::Plugin::HotSpot | |
| 18 | + | |
| 19 | + def sender | |
| 20 | + "#{name} (#{email})" | |
| 21 | + end | |
| 22 | + | |
| 23 | + def perform | |
| 24 | + user=environment.users.find_by_id(user_id) | |
| 25 | + user.activate | |
| 26 | + end | |
| 27 | + | |
| 28 | + def title | |
| 29 | + _("New user") | |
| 30 | + end | |
| 31 | + | |
| 32 | + def subject | |
| 33 | + name | |
| 34 | + end | |
| 35 | + | |
| 36 | + def information | |
| 37 | + { :message => _('%{sender} wants to register.'), | |
| 38 | + :variables => {:sender => sender} } | |
| 39 | + end | |
| 40 | + | |
| 41 | + def icon | |
| 42 | + result = {:type => :defined_image, :src => '/images/icons-app/person-minor.png', :name => name} | |
| 43 | + end | |
| 44 | + | |
| 45 | + def target_notification_description | |
| 46 | + _('%{sender} tried to register.') % | |
| 47 | + {:sender => sender} | |
| 48 | + end | |
| 49 | + | |
| 50 | + def target_notification_message | |
| 51 | + target_notification_description + "\n\n" + | |
| 52 | + _('You need to login on %{system} in order to approve or reject this user.') % { :environment => self.environment } | |
| 53 | + end | |
| 54 | + | |
| 55 | + def target_notification_message | |
| 56 | + _("User \"%{user}\" just requested to register. You have to approve or reject it through the \"Pending Validations\" section in your control panel.\n") % { :user => self.name } | |
| 57 | + end | |
| 58 | + | |
| 59 | +end | |
| 0 | 60 | \ No newline at end of file | ... | ... |
app/models/user.rb
| ... | ... | @@ -48,10 +48,10 @@ class User < ActiveRecord::Base |
| 48 | 48 | user.person = p |
| 49 | 49 | end |
| 50 | 50 | if user.environment.enabled?('skip_new_user_email_confirmation') |
| 51 | - unless user.environment.enabled?('admin_must_approve_new_users') | |
| 52 | - user.activate | |
| 53 | - else | |
| 51 | + if user.environment.enabled?('admin_must_approve_new_users') | |
| 54 | 52 | create_moderate_task |
| 53 | + else | |
| 54 | + user.activate | |
| 55 | 55 | end |
| 56 | 56 | end |
| 57 | 57 | end |
| ... | ... | @@ -142,7 +142,7 @@ class User < ActiveRecord::Base |
| 142 | 142 | end |
| 143 | 143 | |
| 144 | 144 | def create_moderate_task |
| 145 | - @task = CreateUser.new | |
| 145 | + @task = ModerateUserRegistration.new | |
| 146 | 146 | @task.user_id = self.id |
| 147 | 147 | @task.name = self.name |
| 148 | 148 | @task.email = self.email | ... | ... |
| ... | ... | @@ -0,0 +1,22 @@ |
| 1 | +# encoding: UTF-8 | |
| 2 | +require File.dirname(__FILE__) + '/../test_helper' | |
| 3 | + | |
| 4 | +class ModerateUserRegistrationTest < ActiveSupport::TestCase | |
| 5 | + fixtures :users, :environments | |
| 6 | + | |
| 7 | + def test_should_on_perform_activate_user | |
| 8 | + user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test') | |
| 9 | + user.save! | |
| 10 | + environment = Environment.default | |
| 11 | + t= ModerateUserRegistration.new | |
| 12 | + t.user_id = user.id | |
| 13 | + t.name = user.name | |
| 14 | + t.author_name = user.name | |
| 15 | + t.email = user.email | |
| 16 | + t.target= environment | |
| 17 | + t.save! | |
| 18 | + assert !user.activated? | |
| 19 | + t.perform | |
| 20 | + assert environment.users.find_by_id(user.id).activated? | |
| 21 | + end | |
| 22 | +end | ... | ... |