]> git.openstreetmap.org Git - rails.git/commitdiff
Assign vandalism reports for users to moderators
authorTom Hughes <tom@compton.nu>
Sun, 17 Jun 2018 10:14:19 +0000 (11:14 +0100)
committerTom Hughes <tom@compton.nu>
Sun, 17 Jun 2018 10:14:19 +0000 (11:14 +0100)
app/controllers/reports_controller.rb
app/models/issue.rb
app/views/reports/new.html.erb
test/factories/issues.rb
test/models/issue_test.rb
test/system/issues_test.rb
test/system/report_diary_comment_test.rb
test/system/report_diary_entry_test.rb
test/system/report_note_test.rb
test/system/report_user_test.rb

index 08092c073c7de0b14938edafc6c28f445f457c18..ef87a8699fe807884ec29d09d65a1064bf49a2fa 100644 (file)
@@ -16,11 +16,15 @@ class ReportsController < ApplicationController
 
   def create
     @report = current_user.reports.new(report_params)
-    @report.issue = Issue.find_or_initialize_by(:reportable_id => params[:report][:issue][:reportable_id], :reportable_type => params[:report][:issue][:reportable_type])
+    @report.issue = Issue
+                    .create_with(:assigned_role => default_assigned_role)
+                    .find_or_initialize_by(issue_params)
 
     if @report.save
-      @report.issue.save
-      @report.issue.reopen! unless @report.issue.open?
+      @report.issue.assigned_role = "administrator" if default_assigned_role == "administrator"
+      @report.issue.reopen unless @report.issue.open?
+      @report.issue.save!
+
       redirect_to helpers.reportable_url(@report.issue.reportable), :notice => t(".successful_report")
     else
       redirect_to new_report_path(:reportable_type => @report.issue.reportable_type, :reportable_id => @report.issue.reportable_id), :notice => t(".provide_details")
@@ -38,6 +42,21 @@ class ReportsController < ApplicationController
   end
 
   def report_params
-    params[:report].permit(:details, :category)
+    params.require(:report).permit(:details, :category)
+  end
+
+  def issue_params
+    params.require(:report).require(:issue).permit(:reportable_id, :reportable_type)
+  end
+
+  def default_assigned_role
+    case issue_params[:reportable_type]
+    when "Note" then "moderator"
+    when "User" then case report_params[:category]
+                     when "vandal" then "moderator"
+                     else "administrator"
+                     end
+    else "administrator"
+    end
   end
 end
index d893a6e3a67378296f5b1c70b192dfcaca3e68eb..f18581b5f059670f01a52836aca272b2d77265fb 100644 (file)
@@ -44,7 +44,6 @@ class Issue < ActiveRecord::Base
   ASSIGNED_ROLES = %w[administrator moderator].freeze
   validates :assigned_role, :presence => true, :inclusion => ASSIGNED_ROLES
 
-  before_validation :set_default_assigned_role
   before_validation :set_reported_user
 
   scope :with_status, ->(issue_status) { where(:status => statuses[issue_status]) }
@@ -93,13 +92,4 @@ class Issue < ActiveRecord::Base
                            reportable.user
                          end
   end
-
-  def set_default_assigned_role
-    if assigned_role.blank?
-      self.assigned_role = case reportable
-                           when Note then "moderator"
-                           else "administrator"
-                           end
-    end
-  end
 end
index 98d1358903fe2911c21233984a3aad9208aced68..fb338b4979c2bd46376e2f96b7d5c315930ceb58 100644 (file)
@@ -32,7 +32,7 @@
     </div>
 
     <div class='form-row'>
-      <%= text_area :report, :details, :cols => 20, :rows => 5, placeholder: t('.details'), :required => true %>
+      <%= text_area :report, :details, :cols => 20, :rows => 5, placeholder: t('.details') %>
     </div>
 
     <div class='buttons'>
index 913c47f2af941ff6955d92769ac48f619ffa0c46..e209bd25dab26f2827fc737afb5fb1dc3273e7d4 100644 (file)
@@ -3,5 +3,8 @@ FactoryBot.define do
     # Default to reporting users
     association :reportable, :factory => :user
     association :reported_user, :factory => :user
+
+    # Default to assigning to an administrator
+    assigned_role "administrator"
   end
 end
index 4203d02c1a84d17092da3796644eb064971cf41e..073dc26447d921fb81214a828557d5af9b36745f 100644 (file)
@@ -17,72 +17,24 @@ class IssueTest < ActiveSupport::TestCase
     diary_entry = create(:diary_entry)
     diary_comment = create(:diary_comment, :diary_entry => diary_entry)
 
-    issue = Issue.new(:reportable => user)
+    issue = Issue.new(:reportable => user, :assigned_role => "administrator")
     issue.save!
     assert_equal issue.reported_user, user
 
-    issue = Issue.new(:reportable => note)
+    issue = Issue.new(:reportable => note, :assigned_role => "administrator")
     issue.save!
     assert_equal issue.reported_user, note.author
 
-    issue = Issue.new(:reportable => anonymous_note)
+    issue = Issue.new(:reportable => anonymous_note, :assigned_role => "administrator")
     issue.save!
     assert_nil issue.reported_user
 
-    issue = Issue.new(:reportable => diary_entry)
+    issue = Issue.new(:reportable => diary_entry, :assigned_role => "administrator")
     issue.save!
     assert_equal issue.reported_user, diary_entry.user
 
-    issue = Issue.new(:reportable => diary_comment)
+    issue = Issue.new(:reportable => diary_comment, :assigned_role => "administrator")
     issue.save!
     assert_equal issue.reported_user, diary_comment.user
   end
-
-  def test_default_assigned_role
-    create(:language, :code => "en")
-    user = create(:user)
-    note = create(:note_with_comments)
-    diary_entry = create(:diary_entry)
-    diary_comment = create(:diary_comment, :diary_entry => diary_entry)
-
-    issue = Issue.new(:reportable => user)
-    issue.save!
-    assert_equal "administrator", issue.assigned_role
-
-    issue = Issue.new(:reportable => note)
-    issue.save!
-    assert_equal "moderator", issue.assigned_role
-
-    issue = Issue.new(:reportable => diary_entry)
-    issue.save!
-    assert_equal "administrator", issue.assigned_role
-
-    issue = Issue.new(:reportable => diary_comment)
-    issue.save!
-    assert_equal "administrator", issue.assigned_role
-  end
-
-  def test_no_default_explicit_role
-    create(:language, :code => "en")
-    user = create(:user)
-    note = create(:note_with_comments)
-    diary_entry = create(:diary_entry)
-    diary_comment = create(:diary_comment, :diary_entry => diary_entry)
-
-    issue = Issue.new(:reportable => user, :assigned_role => "moderator")
-    issue.save!
-    assert_equal "moderator", issue.reload.assigned_role
-
-    issue = Issue.new(:reportable => note, :assigned_role => "administrator")
-    issue.save!
-    assert_equal "administrator", issue.reload.assigned_role
-
-    issue = Issue.new(:reportable => diary_entry, :assigned_role => "moderator")
-    issue.save!
-    assert_equal "moderator", issue.reload.assigned_role
-
-    issue = Issue.new(:reportable => diary_comment, :assigned_role => "moderator")
-    issue.save!
-    assert_equal "moderator", issue.reload.assigned_role
-  end
 end
index 3cecf58d99d07dd938301292bb043dc70ac63a77..9aee867a9c498353b9b868236f3be363feb54265 100644 (file)
@@ -33,7 +33,7 @@ class IssuesTest < ApplicationSystemTestCase
   def test_view_issues_with_no_reported_user
     sign_in_as(create(:moderator_user))
     anonymous_note = create(:note_with_comments)
-    issue = create(:issue, :reportable => anonymous_note)
+    issue = create(:issue, :reportable => anonymous_note, :assigned_role => "moderator")
 
     visit issues_path
     assert page.has_content?(reportable_title(anonymous_note))
index 5b66bcee71f6422ce93b027c233689a130f86054..18be7e649103743a2ffa540b3e72d6db6e573455 100644 (file)
@@ -32,5 +32,6 @@ class ReportDiaryCommentTest < ApplicationSystemTestCase
     assert page.has_content? "Your report has been registered sucessfully"
 
     assert_equal @comment, Issue.last.reportable
+    assert_equal "administrator", Issue.last.assigned_role
   end
 end
index 1aac8ba70c57bd158f6ab8accf59ca3cab87cfe6..1db733ab7ca510e0bc38a63ed86efcca5349696a 100644 (file)
@@ -31,6 +31,7 @@ class ReportDiaryEntryTest < ApplicationSystemTestCase
     assert page.has_content? "Your report has been registered sucessfully"
 
     assert_equal @diary_entry, Issue.last.reportable
+    assert_equal "administrator", Issue.last.assigned_role
   end
 
   def test_it_reopens_issue
index 24deac2cb96de20dc32e6fad8a5ce24f919e05f7..93e73cf5aaf7885f01063a8cd48ec91859748588 100644 (file)
@@ -27,6 +27,7 @@ class ReportNoteTest < ApplicationSystemTestCase
     assert page.has_content? "Your report has been registered sucessfully"
 
     assert_equal note, Issue.last.reportable
+    assert_equal "moderator", Issue.last.assigned_role
   end
 
   def test_can_report_notes_with_author
@@ -47,5 +48,6 @@ class ReportNoteTest < ApplicationSystemTestCase
     assert page.has_content? "Your report has been registered sucessfully"
 
     assert_equal note, Issue.last.reportable
+    assert_equal "moderator", Issue.last.assigned_role
   end
 end
index 8a30c80e05f80585e00d48d194a2a2c25c610609..99533353b1252e095f1c7628a8249a778af99301 100644 (file)
@@ -27,5 +27,44 @@ class ReportUserTest < ApplicationSystemTestCase
     assert page.has_content? "Your report has been registered sucessfully"
 
     assert_equal user, Issue.last.reportable
+    assert_equal "moderator", Issue.last.assigned_role
+  end
+
+  def test_it_promotes_issues
+    user = create(:user)
+    sign_in_as(create(:user))
+    visit user_path(user.display_name)
+
+    click_on I18n.t("user.view.report")
+    assert page.has_content? "Report"
+    assert page.has_content? I18n.t("reports.new.disclaimer.intro")
+
+    choose I18n.t("reports.new.categories.user.vandal_label")
+    fill_in "report_details", :with => "This user is a vandal"
+    assert_difference "Issue.count", 1 do
+      click_on "Create Report"
+    end
+
+    assert page.has_content? "Your report has been registered sucessfully"
+
+    assert_equal user, Issue.last.reportable
+    assert_equal "moderator", Issue.last.assigned_role
+
+    visit user_path(user.display_name)
+
+    click_on I18n.t("user.view.report")
+    assert page.has_content? "Report"
+    assert page.has_content? I18n.t("reports.new.disclaimer.intro")
+
+    choose I18n.t("reports.new.categories.user.spam_label")
+    fill_in "report_details", :with => "This user is a spammer"
+    assert_no_difference "Issue.count" do
+      click_on "Create Report"
+    end
+
+    assert page.has_content? "Your report has been registered sucessfully"
+
+    assert_equal user, Issue.last.reportable
+    assert_equal "administrator", Issue.last.assigned_role
   end
 end