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