From a76b1b3fee641847ac48db2181c9b0d2f11ccf3b Mon Sep 17 00:00:00 2001 From: Suyash Choudhary <57896905+sssash18@users.noreply.github.com> Date: Sun, 12 Jan 2020 20:47:44 +0530 Subject: [PATCH] Unapproved users (#7203) * Added the test * Added 759-819 in if statement * Fixed indentation * Updated line 596 * Modified the statements * Deleted modified the statements commit * Restored the node.rb to commit before "Modified the statements" * Refactored the line 810 - 812 * Changed '==' to '=' in line 811 --- app/models/node.rb | 96 +++++++++++++------------ test/functional/wiki_controller_test.rb | 2 +- test/unit/node_test.rb | 7 ++ 3 files changed, 58 insertions(+), 47 deletions(-) diff --git a/app/models/node.rb b/app/models/node.rb index 50e3874420..1cfe69e06e 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -756,66 +756,70 @@ def add_barnstar(tagname, giver) end def add_tag(tagname, user) - tagname = tagname.downcase - unless has_tag_without_aliasing(tagname) - saved = false - table_updated = false - tag = Tag.find_by(name: tagname) || Tag.new(vid: 3, # vocabulary id; 1 + if user.status == 1 + tagname = tagname.downcase + unless has_tag_without_aliasing(tagname) + saved = false + table_updated = false + tag = Tag.find_by(name: tagname) || Tag.new(vid: 3, # vocabulary id; 1 name: tagname, description: '', weight: 0) - ActiveRecord::Base.transaction do - if tag.valid? - key = tag.name.split(':')[0] - value = tag.name.split(':')[1] - # add base tags: - if ['question', 'upgrade', 'activity'].include?(key) - add_tag(value, user) - end - # add sub-tags: - subtags = {} - subtags['pm'] = 'particulate-matter' - if subtags.include?(key) - add_tag(subtags[key], user) - end - # parse date tags: - if key == 'date' - begin - DateTime.strptime(value, '%m-%d-%Y').to_date.to_s(:long) - rescue StandardError - return [false, tag.destroy] + ActiveRecord::Base.transaction do + if tag.valid? + key = tag.name.split(':')[0] + value = tag.name.split(':')[1] + # add base tags: + if ['question', 'upgrade', 'activity'].include?(key) + add_tag(value, user) end - end - tag.save! - node_tag = NodeTag.new(tid: tag.id, + # add sub-tags: + subtags = {} + subtags['pm'] = 'particulate-matter' + if subtags.include?(key) + add_tag(subtags[key], user) + end + # parse date tags: + if key == 'date' + begin + DateTime.strptime(value, '%m-%d-%Y').to_date.to_s(:long) + rescue StandardError + return [false, tag.destroy] + end + end + tag.save! + node_tag = NodeTag.new(tid: tag.id, uid: user.uid, date: DateTime.now.to_i, nid: id) - # Adding lat/lon values into node table - if key == 'lat' - tagvalue = value - table_updated = update_attributes(latitude: tagvalue, precision: decimals(tagvalue).to_s) - elsif key == 'lon' - tagvalue = value - table_updated = update_attributes(longitude: tagvalue) - end + # Adding lat/lon values into node table + if key == 'lat' + tagvalue = value + table_updated = update_attributes(latitude: tagvalue, precision: decimals(tagvalue).to_s) + elsif key == 'lon' + tagvalue = value + table_updated = update_attributes(longitude: tagvalue) + end - if node_tag.save - saved = true - tag.run_count # update count of tag usage - # send email notification if there are subscribers, status is OK, and less than 1 month old - unless tag.subscriptions.empty? || status == 3 || status == 4 || created < (DateTime.now - 1.month).to_i - SubscriptionMailer.notify_tag_added(self, tag, user).deliver_now + if node_tag.save + saved = true + tag.run_count # update count of tag usage + # send email notification if there are subscribers, status is OK, and less than 1 month old + isStatusValid = status == 3 || status == 4 + isMonthOld = created < (DateTime.now - 1.month).to_i + unless tag.subscriptions.empty? || isStatusValid || !isMonthOld + SubscriptionMailer.notify_tag_added(self, tag, user).deliver_now + end + else + saved = false + tag.destroy end - else - saved = false - tag.destroy end end + return [saved, tag, table_updated] end - return [saved, tag, table_updated] end end diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index a3a6515aec..8064e5d547 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -593,7 +593,7 @@ def teardown @user = UserSession.create(users(:jeff)) @node = nodes(:wiki_page) slug = @node.path.gsub('/wiki/', '') - @node.add_tag('date:bad', @user) + @node.add_tag('date:bad', users(:jeff)) assert_equal false, @node.has_power_tag('date') # assert_equal "anything goes", DateTime.strptime(@node.power_tag('date'),'%m- %d-%Y').to_date.to_s(:long) diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index f8a6ea17c6..2416cf5542 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -529,4 +529,11 @@ def setup assert_equal 'Canon A1200 IR conversion at PLOTS Barnraising at LUMCON', Node.find_by_tag_and_author('awesome', 2, 'notes').first.title assert_equal 'Question by a moderated user', Node.find_by_tag_and_author('question:spectrometer', 9, 'questions').first.title end + test 'non-approved users should not be able to add tags' do + node = nodes(:one) + assert_difference 'node.tags.count', 0 do + node.add_tag('myspamtag', users(:spammer)) + end + assert_not node.has_tag('myspamtag') + end end