3 class ReportsControllerTest < ActionDispatch::IntegrationTest
 
   4   def test_new_missing_parameters
 
   5     session_for(create(:user))
 
   8     assert_response :bad_request
 
  11   def test_new_report_without_login
 
  12     target_user = create(:user)
 
  13     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
  14     assert_redirected_to login_path(:referer => new_report_path(:reportable_id => target_user.id, :reportable_type => "User"))
 
  17   def test_new_report_after_login
 
  18     target_user = create(:user)
 
  20     session_for(create(:user))
 
  22     # Create an Issue and a report
 
  23     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
  24     assert_response :success
 
  25     assert_difference "Issue.count", 1 do
 
  26       details = "Details of a report"
 
  28       post reports_path(:report => {
 
  30                           :category => category,
 
  31                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
  34     assert_redirected_to user_path(target_user)
 
  37   def test_new_report_with_incomplete_details
 
  38     # Test creation of a new issue and a new report
 
  39     target_user = create(:user)
 
  42     session_for(create(:user))
 
  44     # Create an Issue and a report
 
  45     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
  46     assert_response :success
 
  47     assert_difference "Issue.count", 1 do
 
  48       details = "Details of a report"
 
  50       post reports_path(:report => {
 
  52                           :category => category,
 
  53                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
  56     assert_redirected_to user_path(target_user)
 
  60     assert_equal 1, issue.reports.count
 
  62     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
  63     assert_response :success
 
  65     # Report without details
 
  66     assert_no_difference "Issue.count" do
 
  68       post reports_path(:report => {
 
  69                           :category => category,
 
  70                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
  73     assert_response :success
 
  75     assert_match(/Please provide the required details/, flash[:warning])
 
  77     assert_equal 1, issue.reports.count
 
  80   def test_new_report_with_complete_details
 
  81     # Test creation of a new issue and a new report
 
  82     target_user = create(:user)
 
  85     session_for(create(:user))
 
  87     # Create an Issue and a report
 
  88     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
  89     assert_response :success
 
  90     assert_difference "Issue.count", 1 do
 
  91       details = "Details of a report"
 
  93       post reports_path(:report => {
 
  95                           :category => category,
 
  96                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
  99     assert_redirected_to user_path(target_user)
 
 103     assert_equal 1, issue.reports.count
 
 105     # Create a report for an existing Issue
 
 106     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
 
 107     assert_response :success
 
 108     assert_no_difference "Issue.count" do
 
 109       details = "Details of another report under the same issue"
 
 111       post reports_path(:report => {
 
 113                           :category => category,
 
 114                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
 117     assert_response :redirect
 
 119     assert_equal 2, issue.reports.count
 
 122   def test_spam_reports_can_suspend
 
 123     target_user = create(:user)
 
 125     session_for(create(:user))
 
 127     post reports_path(:report => {
 
 128                         :details => "Spammer",
 
 130                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
 132     assert_equal "active", target_user.reload.status
 
 134     session_for(create(:user))
 
 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     post reports_path(:report => {
 
 144                         :details => "Spammer",
 
 146                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
 148     assert_equal "active", target_user.reload.status
 
 150     session_for(create(:user))
 
 152     post reports_path(:report => {
 
 153                         :details => "Spammer",
 
 155                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
 
 157     assert_equal "suspended", target_user.reload.status