]> git.openstreetmap.org Git - rails.git/blob - test/controllers/reports_controller_test.rb
Add social sharing functionality
[rails.git] / test / controllers / reports_controller_test.rb
1 require "test_helper"
2
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"))
8   end
9
10   def test_new_report_after_login
11     target_user = create(:user)
12
13     session_for(create(:user))
14
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"
20       category = "other"
21       post reports_path(:report => {
22                           :details => details,
23                           :category => category,
24                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
25                         })
26     end
27     assert_redirected_to user_path(target_user)
28   end
29
30   def test_new_report_with_incomplete_details
31     # Test creation of a new issue and a new report
32     target_user = create(:user)
33
34     # Login
35     session_for(create(:user))
36
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"
42       category = "other"
43       post reports_path(:report => {
44                           :details => details,
45                           :category => category,
46                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
47                         })
48     end
49     assert_redirected_to user_path(target_user)
50
51     issue = Issue.last
52
53     assert_equal 1, issue.reports.count
54
55     get new_report_path(:reportable_id => target_user.id, :reportable_type => "User")
56     assert_response :success
57
58     # Report without details
59     assert_no_difference "Issue.count" do
60       category = "other"
61       post reports_path(:report => {
62                           :category => category,
63                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
64                         })
65     end
66     assert_response :success
67     assert_template :new
68     assert_match(/Please provide the required details/, flash[:notice])
69
70     assert_equal 1, issue.reports.count
71   end
72
73   def test_new_report_with_complete_details
74     # Test creation of a new issue and a new report
75     target_user = create(:user)
76
77     # Login
78     session_for(create(:user))
79
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"
85       category = "other"
86       post reports_path(:report => {
87                           :details => details,
88                           :category => category,
89                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
90                         })
91     end
92     assert_redirected_to user_path(target_user)
93
94     issue = Issue.last
95
96     assert_equal 1, issue.reports.count
97
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"
103       category = "other"
104       post reports_path(:report => {
105                           :details => details,
106                           :category => category,
107                           :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
108                         })
109     end
110     assert_response :redirect
111
112     assert_equal 2, issue.reports.count
113   end
114 end