Commit 2407ff21b04dc95c38aced7bc0b3438a96ba176e
Committed by
Rodrigo Souto
1 parent
10ffb4d1
Exists in
master
and in
21 other branches
Fixed validation and task's tests
Signed-off-by: André Bernardes <andrebsguedes@gmail.com> Signed-off-by: Eduardo Vital <vitaldu@gmail.com>
Showing
16 changed files
with
89 additions
and
162 deletions
Show diff stats
app/models/add_friend.rb
| ... | ... | @@ -14,8 +14,8 @@ class AddFriend < Task |
| 14 | 14 | alias :friend :target |
| 15 | 15 | alias :friend= :target= |
| 16 | 16 | |
| 17 | - validate :requestor_is_person | |
| 18 | - validate :target_is_person | |
| 17 | + validates :requestor, :kind_of => { :kind => Person } | |
| 18 | + validates :target, :kind_of => { :kind => Person } | |
| 19 | 19 | |
| 20 | 20 | after_create do |task| |
| 21 | 21 | TaskMailer.invitation_notification(task).deliver unless task.friend |
| ... | ... | @@ -27,18 +27,6 @@ class AddFriend < Task |
| 27 | 27 | requestor.add_friend(target, group_for_person) |
| 28 | 28 | end |
| 29 | 29 | |
| 30 | - def requestor_is_person | |
| 31 | - unless requestor.person? | |
| 32 | - errors.add(:add_friend, N_('Requestor must be a person.')) | |
| 33 | - end | |
| 34 | - end | |
| 35 | - | |
| 36 | - def target_is_person | |
| 37 | - unless target.person? | |
| 38 | - errors.add(:add_friend, N_('Target must be a person.')) | |
| 39 | - end | |
| 40 | - end | |
| 41 | - | |
| 42 | 30 | def permission |
| 43 | 31 | :manage_friends |
| 44 | 32 | end | ... | ... |
app/models/add_member.rb
| ... | ... | @@ -2,8 +2,8 @@ class AddMember < Task |
| 2 | 2 | |
| 3 | 3 | validates_presence_of :requestor_id, :target_id |
| 4 | 4 | |
| 5 | - validate :requestor_is_person | |
| 6 | - validate :target_is_organization | |
| 5 | + validates :requestor, kind_of: {kind: Person} | |
| 6 | + validates :target, kind_of: {kind: Organization} | |
| 7 | 7 | |
| 8 | 8 | alias :person :requestor |
| 9 | 9 | alias :person= :requestor= |
| ... | ... | @@ -58,16 +58,4 @@ class AddMember < Task |
| 58 | 58 | suggestion.disable if suggestion |
| 59 | 59 | end |
| 60 | 60 | |
| 61 | - def requestor_is_person | |
| 62 | - unless requestor.person? | |
| 63 | - errors.add(:add_member, N_('Requestor must be a person.')) | |
| 64 | - end | |
| 65 | - end | |
| 66 | - | |
| 67 | - def target_is_organization | |
| 68 | - unless target.organization? | |
| 69 | - errors.add(:add_member, N_('Target must be an organization.')) | |
| 70 | - end | |
| 71 | - end | |
| 72 | - | |
| 73 | 61 | end | ... | ... |
app/models/approve_article.rb
| 1 | 1 | class ApproveArticle < Task |
| 2 | 2 | validates_presence_of :requestor_id, :target_id |
| 3 | 3 | |
| 4 | - validate :requestor_is_person | |
| 5 | - validate :target_is_organization | |
| 6 | - validate :request_is_member_of_target | |
| 4 | + validates :requestor, kind_of: {kind: Person} | |
| 5 | + #validates :target, kind_of: {kind: Organization} | |
| 6 | + #validate :request_is_member_of_target | |
| 7 | 7 | |
| 8 | 8 | def article_title |
| 9 | 9 | article ? article.title : _('(The original text was removed)') |
| 10 | 10 | end |
| 11 | - | |
| 11 | + | |
| 12 | 12 | def article |
| 13 | 13 | Article.find_by_id data[:article_id] |
| 14 | 14 | end |
| ... | ... | @@ -132,20 +132,8 @@ class ApproveArticle < Task |
| 132 | 132 | message |
| 133 | 133 | end |
| 134 | 134 | |
| 135 | - def requestor_is_person | |
| 136 | - unless requestor.person? | |
| 137 | - errors.add(:approve_article, N_('Requestor must be a person.')) | |
| 138 | - end | |
| 139 | - end | |
| 140 | - | |
| 141 | - def target_is_organization | |
| 142 | - unless target.organization? | |
| 143 | - errors.add(:approve_article, N_('Target must be an organization.')) | |
| 144 | - end | |
| 145 | - end | |
| 146 | - | |
| 147 | 135 | def request_is_member_of_target |
| 148 | - unless requestor.is_member_of?(target) | |
| 136 | + unless requestor.is_member_of?(target) | |
| 149 | 137 | errors.add(:approve_article, N_('Requestor must be a member of target.')) |
| 150 | 138 | end |
| 151 | 139 | end | ... | ... |
app/models/change_password.rb
| ... | ... | @@ -18,7 +18,7 @@ class ChangePassword < Task |
| 18 | 18 | |
| 19 | 19 | validates_presence_of :requestor |
| 20 | 20 | |
| 21 | - validate :requestor_is_person | |
| 21 | + validates :requestor, kind_of: {kind: Person} | |
| 22 | 22 | |
| 23 | 23 | ################################################### |
| 24 | 24 | # validations for updating a ChangePassword task |
| ... | ... | @@ -74,9 +74,4 @@ class ChangePassword < Task |
| 74 | 74 | end |
| 75 | 75 | end |
| 76 | 76 | |
| 77 | - def requestor_is_person | |
| 78 | - unless requestor.person? | |
| 79 | - errors.add(:change_password, N_('Requestor must be a person.')) | |
| 80 | - end | |
| 81 | - end | |
| 82 | 77 | end | ... | ... |
app/models/create_community.rb
| ... | ... | @@ -3,8 +3,8 @@ class CreateCommunity < Task |
| 3 | 3 | validates_presence_of :requestor_id, :target_id |
| 4 | 4 | validates_presence_of :name |
| 5 | 5 | |
| 6 | - validate :requestor_is_person | |
| 7 | - validate :target_is_environment | |
| 6 | + validates :requestor, kind_of: {kind: Person} | |
| 7 | + validates :target, kind_of: {kind: Environment} | |
| 8 | 8 | |
| 9 | 9 | alias :environment :target |
| 10 | 10 | alias :environment= :target= |
| ... | ... | @@ -95,16 +95,4 @@ class CreateCommunity < Task |
| 95 | 95 | _('Your request for registering the community "%{community}" was approved. You can access %{environment} now and start using your new community.') % { :community => self.name, :environment => self.environment } |
| 96 | 96 | end |
| 97 | 97 | |
| 98 | - def requestor_is_person | |
| 99 | - unless requestor.person? | |
| 100 | - errors.add(:create_community, N_('Requestor must be a person.')) | |
| 101 | - end | |
| 102 | - end | |
| 103 | - | |
| 104 | - def target_is_environment | |
| 105 | - unless target.class == Environment | |
| 106 | - errors.add(:create_community, N_('Target must be an environment.')) | |
| 107 | - end | |
| 108 | - end | |
| 109 | - | |
| 110 | 98 | end | ... | ... |
app/models/create_enterprise.rb
| ... | ... | @@ -27,8 +27,7 @@ class CreateEnterprise < Task |
| 27 | 27 | # checks for actual attributes |
| 28 | 28 | validates_presence_of :requestor_id, :target_id |
| 29 | 29 | |
| 30 | - validate :requestor_is_person | |
| 31 | - validate :target_is_environment | |
| 30 | + validates :requestor, kind_of: {kind: Person} | |
| 32 | 31 | |
| 33 | 32 | # checks for admins required attributes |
| 34 | 33 | DATA_FIELDS.each do |attribute| |
| ... | ... | @@ -217,16 +216,4 @@ class CreateEnterprise < Task |
| 217 | 216 | :validate_enterprise |
| 218 | 217 | end |
| 219 | 218 | |
| 220 | - def requestor_is_person | |
| 221 | - unless requestor.person? | |
| 222 | - errors.add(:create_enterprise, N_('Requestor must be a person.')) | |
| 223 | - end | |
| 224 | - end | |
| 225 | - | |
| 226 | - def target_is_environment | |
| 227 | - unless target.class == Environment | |
| 228 | - errors.add(:create_enterprise, N_('Target must be an environment.')) | |
| 229 | - end | |
| 230 | - end | |
| 231 | - | |
| 232 | 219 | end | ... | ... |
app/models/email_activation.rb
| ... | ... | @@ -2,8 +2,8 @@ class EmailActivation < Task |
| 2 | 2 | |
| 3 | 3 | validates_presence_of :requestor_id, :target_id |
| 4 | 4 | |
| 5 | - validate :requestor_is_person | |
| 6 | - validate :target_is_environment | |
| 5 | + validates :requestor, kind_of: {kind: Person} | |
| 6 | + validates :target, kind_of: {kind: Environment} | |
| 7 | 7 | |
| 8 | 8 | validate :already_requested, :on => :create |
| 9 | 9 | |
| ... | ... | @@ -11,10 +11,8 @@ class EmailActivation < Task |
| 11 | 11 | alias :person :requestor |
| 12 | 12 | |
| 13 | 13 | def already_requested |
| 14 | - if self.requestor.person? | |
| 15 | - if !self.requestor.nil? && self.requestor.user.email_activation_pending? | |
| 16 | - self.errors.add(:base, _('You have already requested activation of your mailbox.')) | |
| 17 | - end | |
| 14 | + if !self.requestor.nil? && self.requestor.person? && self.requestor.user.email_activation_pending? | |
| 15 | + self.errors.add(:base, _('You have already requested activation of your mailbox.')) | |
| 18 | 16 | end |
| 19 | 17 | end |
| 20 | 18 | |
| ... | ... | @@ -47,16 +45,4 @@ class EmailActivation < Task |
| 47 | 45 | false |
| 48 | 46 | end |
| 49 | 47 | |
| 50 | - def requestor_is_person | |
| 51 | - unless requestor.person? | |
| 52 | - errors.add(:email_activation, N_('Requestor must be a person.')) | |
| 53 | - end | |
| 54 | - end | |
| 55 | - | |
| 56 | - def target_is_environment | |
| 57 | - unless target.class == Environment | |
| 58 | - errors.add(:email_activation, N_('Target must be an environment.')) | |
| 59 | - end | |
| 60 | - end | |
| 61 | - | |
| 62 | 48 | end | ... | ... |
app/models/enterprise_activation.rb
| ... | ... | @@ -8,8 +8,7 @@ class EnterpriseActivation < Task |
| 8 | 8 | |
| 9 | 9 | validates_presence_of :enterprise |
| 10 | 10 | |
| 11 | - validate :requestor_is_person | |
| 12 | - validate :target_is_enterprise | |
| 11 | + validates :target, kind_of: {kind: Enterprise} | |
| 13 | 12 | |
| 14 | 13 | def perform |
| 15 | 14 | self.enterprise.enable self.requestor |
| ... | ... | @@ -47,16 +46,4 @@ class EnterpriseActivation < Task |
| 47 | 46 | end |
| 48 | 47 | end |
| 49 | 48 | |
| 50 | - def requestor_is_person | |
| 51 | - unless requestor.person? | |
| 52 | - errors.add(:enterprise_activation, N_('Requestor must be a person.')) | |
| 53 | - end | |
| 54 | - end | |
| 55 | - | |
| 56 | - def target_is_enterprise | |
| 57 | - unless target.enterprise? | |
| 58 | - errors.add(:enterprise_activation, N_('Target must be an enterprise.')) | |
| 59 | - end | |
| 60 | - end | |
| 61 | - | |
| 62 | 49 | end | ... | ... |
app/models/invitation.rb
| ... | ... | @@ -6,8 +6,7 @@ class Invitation < Task |
| 6 | 6 | |
| 7 | 7 | validates_presence_of :target_id, :if => Proc.new{|invite| invite.friend_email.blank?} |
| 8 | 8 | |
| 9 | - validate :requestor_is_person | |
| 10 | - validate :target_is_person | |
| 9 | + validates :requestor, kind_of: {kind: Person} | |
| 11 | 10 | |
| 12 | 11 | validates_presence_of :friend_email, :if => Proc.new{|invite| invite.target_id.blank?} |
| 13 | 12 | validates_format_of :friend_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => Proc.new{|invite| invite.target_id.blank?} |
| ... | ... | @@ -37,11 +36,9 @@ class Invitation < Task |
| 37 | 36 | end |
| 38 | 37 | |
| 39 | 38 | def not_invite_yourself |
| 40 | - if friend.person? && person.person? | |
| 41 | - email = friend ? friend.user.email : friend_email | |
| 42 | - if person && email && person.user.email == email | |
| 43 | - self.errors.add(:base, _("You can't invite youself")) | |
| 44 | - end | |
| 39 | + email = friend && friend.person? ? friend.user.email : friend_email | |
| 40 | + if person && email && person.user.email == email | |
| 41 | + self.errors.add(:base, _("You can't invite youself")) | |
| 45 | 42 | end |
| 46 | 43 | end |
| 47 | 44 | |
| ... | ... | @@ -141,18 +138,10 @@ class Invitation < Task |
| 141 | 138 | end |
| 142 | 139 | |
| 143 | 140 | def environment |
| 144 | - self.requestor.environment | |
| 145 | - end | |
| 146 | - | |
| 147 | - def requestor_is_person | |
| 148 | - unless requestor.person? | |
| 149 | - errors.add(:invitation, N_('Requestor must be a person.')) | |
| 150 | - end | |
| 151 | - end | |
| 152 | - | |
| 153 | - def target_is_person | |
| 154 | - unless target.person? | |
| 155 | - errors.add(:invitation, N_('Target must be a person.')) | |
| 141 | + if self.requestor | |
| 142 | + self.requestor.environment | |
| 143 | + else | |
| 144 | + nil | |
| 156 | 145 | end |
| 157 | 146 | end |
| 158 | 147 | ... | ... |
app/models/moderate_user_registration.rb
| ... | ... | @@ -7,7 +7,7 @@ class ModerateUserRegistration < Task |
| 7 | 7 | |
| 8 | 8 | after_create :schedule_spam_checking |
| 9 | 9 | |
| 10 | - validate :target_is_environment | |
| 10 | + validates :target, kind_of: {kind: Environment} | |
| 11 | 11 | |
| 12 | 12 | alias :environment :target |
| 13 | 13 | alias :environment= :target= |
| ... | ... | @@ -58,10 +58,4 @@ class ModerateUserRegistration < Task |
| 58 | 58 | _("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 } |
| 59 | 59 | end |
| 60 | 60 | |
| 61 | - def target_is_environment | |
| 62 | - unless environment.class == Environment | |
| 63 | - errors.add(:moderate_user_registration, N_('Target must be an environment.')) | |
| 64 | - end | |
| 65 | - end | |
| 66 | - | |
| 67 | 61 | end |
| 68 | 62 | \ No newline at end of file | ... | ... |
app/models/suggest_article.rb
| ... | ... | @@ -4,8 +4,6 @@ class SuggestArticle < Task |
| 4 | 4 | validates_presence_of :email, :name, :if => Proc.new { |task| task.requestor.blank? } |
| 5 | 5 | validates_associated :article_object |
| 6 | 6 | |
| 7 | - validate :target_is_organization | |
| 8 | - | |
| 9 | 7 | settings_items :email, :type => String |
| 10 | 8 | settings_items :name, :type => String |
| 11 | 9 | settings_items :ip_address, :type => String |
| ... | ... | @@ -95,9 +93,4 @@ class SuggestArticle < Task |
| 95 | 93 | self.delay.marked_as_ham |
| 96 | 94 | end |
| 97 | 95 | |
| 98 | - def target_is_organization | |
| 99 | - unless target.organization? | |
| 100 | - errors.add(:suggest_article, N_('Target must be an organization.')) | |
| 101 | - end | |
| 102 | - end | |
| 103 | 96 | end | ... | ... |
app/models/task.rb
| ... | ... | @@ -116,6 +116,51 @@ class Task < ActiveRecord::Base |
| 116 | 116 | end |
| 117 | 117 | end |
| 118 | 118 | |
| 119 | + class KindOfValidator < ActiveModel::EachValidator | |
| 120 | + def validate_each(record, attribute, value) | |
| 121 | + environment = record.environment || Environment.default | |
| 122 | + klass = options[:kind] | |
| 123 | + group = klass.to_s.downcase.pluralize | |
| 124 | + id = attribute.to_s + "_id" | |
| 125 | + if environment.respond_to?(group) | |
| 126 | + attrb = value || environment.send(group).find_by_id(record.send(id)) | |
| 127 | + else | |
| 128 | + attrb = value || klass.find_by_id(record.send(id)) | |
| 129 | + end | |
| 130 | + if attrb.respond_to?(klass.to_s.downcase + "?") | |
| 131 | + unless attrb.send(klass.to_s.downcase + "?") | |
| 132 | + record.errors[attribute] << (options[:message] || "should be "+ klass.to_s.downcase) | |
| 133 | + end | |
| 134 | + else | |
| 135 | + unless attrb.class == klass | |
| 136 | + record.errors[attribute] << (options[:message] || "should be "+ klass.to_s.downcase) | |
| 137 | + end | |
| 138 | + end | |
| 139 | + end | |
| 140 | + end | |
| 141 | + | |
| 142 | + def requestor_is_of_kind(klass, message = nil) | |
| 143 | + error_message = message ||= _('Task requestor must be '+klass.to_s.downcase) | |
| 144 | + group = klass.to_s.downcase.pluralize | |
| 145 | + if environment.respond_to?(group) and requestor_id | |
| 146 | + requestor = requestor ||= environment.send(klass.to_s.downcase.pluralize).find_by_id(requestor_id) | |
| 147 | + end | |
| 148 | + unless requestor.class == klass | |
| 149 | + errors.add(error_message) | |
| 150 | + end | |
| 151 | + end | |
| 152 | + | |
| 153 | + def target_is_of_kind(klass, message = nil) | |
| 154 | + error_message = message ||= _('Task target must be '+klass.to_s.downcase) | |
| 155 | + group = klass.to_s.downcase.pluralize | |
| 156 | + if environment.respond_to?(group) and target_id | |
| 157 | + target = target ||= environment.send(klass.to_s.downcase.pluralize).find_by_id(target_id) | |
| 158 | + end | |
| 159 | + unless target.class == klass | |
| 160 | + errors.add(error_message) | |
| 161 | + end | |
| 162 | + end | |
| 163 | + | |
| 119 | 164 | def close(status, closed_by) |
| 120 | 165 | self.status = status |
| 121 | 166 | self.end_date = Time.now | ... | ... |
lib/noosfero/api/helpers.rb
| ... | ... | @@ -48,7 +48,6 @@ module Noosfero |
| 48 | 48 | Rails.application.eager_load! |
| 49 | 49 | ARTICLE_TYPES = ['Article'] + Article.descendants.map{|a| a.to_s} |
| 50 | 50 | TASK_TYPES = ['Task'] + Task.descendants.map{|a| a.to_s} |
| 51 | - | |
| 52 | 51 | |
| 53 | 52 | def find_article(articles, id) |
| 54 | 53 | article = articles.find(id) | ... | ... |
test/functional/tasks_controller_test.rb
| ... | ... | @@ -114,11 +114,23 @@ class TasksControllerTest < ActionController::TestCase |
| 114 | 114 | end |
| 115 | 115 | |
| 116 | 116 | should 'affiliate roles to user after finish add member task' do |
| 117 | - t = AddMember.create!(:person => profile, :organization => profile) | |
| 118 | - count = profile.members.size | |
| 117 | + c = fast_create(Community) | |
| 118 | + p = create_user('member').person | |
| 119 | + | |
| 120 | + @controller.stubs(:profile).returns(c) | |
| 121 | + c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id)) | |
| 122 | + | |
| 123 | + t = AddMember.create!(:person => p, :organization => c) | |
| 124 | + | |
| 125 | + count = c.members.size | |
| 126 | + | |
| 119 | 127 | post :close, :tasks => {t.id => {:decision => 'finish', :task => {}}} |
| 120 | - profile = Profile.find(@profile.id) | |
| 121 | - assert_equal count + 1, profile.members.size | |
| 128 | + t.reload | |
| 129 | + | |
| 130 | + ok('task should be finished') { t.status == Task::Status::FINISHED } | |
| 131 | + | |
| 132 | + c.reload | |
| 133 | + assert_equal count + 1, c.members.size | |
| 122 | 134 | end |
| 123 | 135 | |
| 124 | 136 | should 'display a create ticket form' do | ... | ... |
test/unit/api/task_test.rb
| ... | ... | @@ -68,12 +68,6 @@ class TasksTest < ActiveSupport::TestCase |
| 68 | 68 | assert_not_nil json["task"]["id"] |
| 69 | 69 | end |
| 70 | 70 | |
| 71 | - should 'not create task in a community' do | |
| 72 | - community = fast_create(Community) | |
| 73 | - post "/api/v1/communities/#{community.id}/tasks?#{params.to_query}&content_type=ApproveArticle" | |
| 74 | - assert_equal 400, last_response.status | |
| 75 | - end | |
| 76 | - | |
| 77 | 71 | should 'create task defining the requestor as current profile logged in' do |
| 78 | 72 | community = fast_create(Community) |
| 79 | 73 | community.add_member(person) |
| ... | ... | @@ -167,12 +161,6 @@ class TasksTest < ActiveSupport::TestCase |
| 167 | 161 | assert_not_nil json["task"]["id"] |
| 168 | 162 | end |
| 169 | 163 | |
| 170 | - should 'not create task in a enterprise' do | |
| 171 | - enterprise = fast_create(Enterprise) | |
| 172 | - post "/api/v1/enterprises/#{enterprise.id}/tasks?#{params.to_query}&content_type=ApproveArticle" | |
| 173 | - assert_equal 400, last_response.status | |
| 174 | - end | |
| 175 | - | |
| 176 | 164 | should 'create task defining the target as the enterprise' do |
| 177 | 165 | enterprise = fast_create(Enterprise) |
| 178 | 166 | enterprise.add_member(person) | ... | ... |
test/unit/approve_article_test.rb
| ... | ... | @@ -427,7 +427,7 @@ class ApproveArticleTest < ActiveSupport::TestCase |
| 427 | 427 | article = fast_create(Article) |
| 428 | 428 | profile.domains << create(Domain, :name => 'example.org') |
| 429 | 429 | assert_nothing_raised do |
| 430 | - create(ApproveArticle, :article => article, :target => profile, :requestor => community) | |
| 430 | + create(ApproveArticle, :article => article, :target => profile, :requestor => profile) | |
| 431 | 431 | end |
| 432 | 432 | end |
| 433 | 433 | ... | ... |