]> git.openstreetmap.org Git - rails.git/blob - test/controllers/reports_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / reports_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class ReportsControllerTest < ActionDispatch::IntegrationTest
6   def test_new_missing_parameters
7     session_for(create(:user))
8     get new_report_path
9
10     assert_response :bad_request
11   end
12
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"))
17   end
18
19   def test_new_report_after_login
20     target_user = create(:user)
21
22     session_for(create(:user))
23
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"
29       category = "other"
30       post reports_path(:report => {
31                           :details => details,
32                           :category => category,
33                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
34                         })
35     end
36     assert_redirected_to user_path(target_user)
37   end
38
39   def test_new_report_with_incomplete_details
40     # Test creation of a new issue and a new report
41     target_user = create(:user)
42
43     # Login
44     session_for(create(:user))
45
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"
51       category = "other"
52       post reports_path(:report => {
53                           :details => details,
54                           :category => category,
55                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
56                         })
57     end
58     assert_redirected_to user_path(target_user)
59
60     issue = Issue.last
61
62     assert_equal 1, issue.reports.count
63
64     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
65     assert_response :success
66
67     # Report without details
68     assert_no_difference "Issue.count" do
69       category = "other"
70       post reports_path(:report => {
71                           :category => category,
72                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
73                         })
74     end
75     assert_response :success
76     assert_template :new
77     assert_match(/Please provide the required details/, flash[:warning])
78
79     assert_equal 1, issue.reports.count
80   end
81
82   def test_new_report_with_complete_details
83     # Test creation of a new issue and a new report
84     target_user = create(:user)
85
86     # Login
87     session_for(create(:user))
88
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"
94       category = "other"
95       post reports_path(:report => {
96                           :details => details,
97                           :category => category,
98                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
99                         })
100     end
101     assert_redirected_to user_path(target_user)
102
103     issue = Issue.last
104
105     assert_equal 1, issue.reports.count
106
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"
112       category = "other"
113       post reports_path(:report => {
114                           :details => details,
115                           :category => category,
116                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
117                         })
118     end
119     assert_response :redirect
120
121     assert_equal 2, issue.reports.count
122   end
123
124   def test_spam_reports_can_suspend
125     target_user = create(:user)
126
127     session_for(create(:user))
128
129     post reports_path(:report => {
130                         :details => "Spammer",
131                         :category => "spam",
132                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
133                       })
134     assert_equal "active", target_user.reload.status
135
136     session_for(create(:user))
137
138     post reports_path(:report => {
139                         :details => "Spammer",
140                         :category => "spam",
141                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
142                       })
143     assert_equal "active", target_user.reload.status
144
145     post reports_path(:report => {
146                         :details => "Spammer",
147                         :category => "spam",
148                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
149                       })
150     assert_equal "active", target_user.reload.status
151
152     session_for(create(:user))
153
154     post reports_path(:report => {
155                         :details => "Spammer",
156                         :category => "spam",
157                         :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
158                       })
159     assert_equal "suspended", target_user.reload.status
160   end
161 end