1 # frozen_string_literal: true
5 class ReportsControllerTest < ActionDispatch::IntegrationTest
6 def test_new_missing_parameters
7 session_for(create(:user))
10 assert_response :bad_request
13 def test_new_report_without_login
14 target_user = create(:user)
15 get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
16 assert_redirected_to login_path(:referer => new_report_path(:reportable_id => target_user.id, :reportable_type => "User"))
19 def test_new_report_after_login
20 target_user = create(:user)
22 session_for(create(:user))
24 # Create an Issue and a report
25 get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
26 assert_response :success
27 assert_difference "Issue.count", 1 do
28 details = "Details of a report"
30 post reports_path(:report => {
32 :category => category,
33 :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
36 assert_redirected_to user_path(target_user)
39 def test_new_report_with_incomplete_details
40 # Test creation of a new issue and a new report
41 target_user = create(:user)
44 session_for(create(:user))
46 # Create an Issue and a report
47 get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
48 assert_response :success
49 assert_difference "Issue.count", 1 do
50 details = "Details of a report"
52 post reports_path(:report => {
54 :category => category,
55 :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
58 assert_redirected_to user_path(target_user)
62 assert_equal 1, issue.reports.count
64 get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
65 assert_response :success
67 # Report without details
68 assert_no_difference "Issue.count" do
70 post reports_path(:report => {
71 :category => category,
72 :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
75 assert_response :success
77 assert_match(/Please provide the required details/, flash[:warning])
79 assert_equal 1, issue.reports.count
82 def test_new_report_with_complete_details
83 # Test creation of a new issue and a new report
84 target_user = create(:user)
87 session_for(create(:user))
89 # Create an Issue and a report
90 get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
91 assert_response :success
92 assert_difference "Issue.count", 1 do
93 details = "Details of a report"
95 post reports_path(:report => {
97 :category => category,
98 :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
101 assert_redirected_to user_path(target_user)
105 assert_equal 1, issue.reports.count
107 # Create a report for an existing Issue
108 get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
109 assert_response :success
110 assert_no_difference "Issue.count" do
111 details = "Details of another report under the same issue"
113 post reports_path(:report => {
115 :category => category,
116 :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
119 assert_response :redirect
121 assert_equal 2, issue.reports.count
124 def test_spam_reports_can_suspend
125 target_user = create(:user)
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 session_for(create(:user))
138 post reports_path(:report => {
139 :details => "Spammer",
141 :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
143 assert_equal "active", target_user.reload.status
145 post reports_path(:report => {
146 :details => "Spammer",
148 :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
150 assert_equal "active", target_user.reload.status
152 session_for(create(:user))
154 post reports_path(:report => {
155 :details => "Spammer",
157 :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
159 assert_equal "suspended", target_user.reload.status