3 class ReportsControllerTest < ActionDispatch::IntegrationTest
 
   4   def test_new_report_without_login
 
   5     target_user = create(:user)
 
   6     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
   7     assert_redirected_to login_path(:referer => new_report_path(:reportable_id => target_user.id, :reportable_type => "User"))
 
  10   def test_new_report_after_login
 
  11     target_user = create(:user)
 
  13     session_for(create(:user))
 
  15     # Create an Issue and a report
 
  16     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
  17     assert_response :success
 
  18     assert_difference "Issue.count", 1 do
 
  19       details = "Details of a report"
 
  21       post reports_path(:report => {
 
  23                           :category => category,
 
  24                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
  27     assert_redirected_to user_path(target_user)
 
  30   def test_new_report_with_incomplete_details
 
  31     # Test creation of a new issue and a new report
 
  32     target_user = create(:user)
 
  35     session_for(create(:user))
 
  37     # Create an Issue and a report
 
  38     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
  39     assert_response :success
 
  40     assert_difference "Issue.count", 1 do
 
  41       details = "Details of a report"
 
  43       post reports_path(:report => {
 
  45                           :category => category,
 
  46                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
  49     assert_redirected_to user_path(target_user)
 
  53     assert_equal 1, issue.reports.count
 
  55     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
  56     assert_response :success
 
  58     # Report without details
 
  59     assert_no_difference "Issue.count" do
 
  61       post reports_path(:report => {
 
  62                           :category => category,
 
  63                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
  66     assert_response :success
 
  68     assert_match(/Please provide the required details/, flash[:notice])
 
  70     assert_equal 1, issue.reports.count
 
  73   def test_new_report_with_complete_details
 
  74     # Test creation of a new issue and a new report
 
  75     target_user = create(:user)
 
  78     session_for(create(:user))
 
  80     # Create an Issue and a report
 
  81     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
  82     assert_response :success
 
  83     assert_difference "Issue.count", 1 do
 
  84       details = "Details of a report"
 
  86       post reports_path(:report => {
 
  88                           :category => category,
 
  89                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
  92     assert_redirected_to user_path(target_user)
 
  96     assert_equal 1, issue.reports.count
 
  98     # Create a report for an existing Issue
 
  99     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
 100     assert_response :success
 
 101     assert_no_difference "Issue.count" do
 
 102       details = "Details of another report under the same issue"
 
 104       post reports_path(:report => {
 
 106                           :category => category,
 
 107                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
 110     assert_response :redirect
 
 112     assert_equal 2, issue.reports.count
 
 115   def test_spam_reports_can_suspend
 
 116     target_user = create(:user)
 
 118     session_for(create(:user))
 
 120     post reports_path(:report => {
 
 121                         :details => "Spammer",
 
 123                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
 125     assert_equal "active", target_user.reload.status
 
 127     session_for(create(:user))
 
 129     post reports_path(:report => {
 
 130                         :details => "Spammer",
 
 132                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
 134     assert_equal "active", target_user.reload.status
 
 136     post reports_path(:report => {
 
 137                         :details => "Spammer",
 
 139                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
 141     assert_equal "active", target_user.reload.status
 
 143     session_for(create(:user))
 
 145     post reports_path(:report => {
 
 146                         :details => "Spammer",
 
 148                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
 150     assert_equal "suspended", target_user.reload.status