]> git.openstreetmap.org Git - rails.git/blob - test/controllers/reports_controller_test.rb
Move controller tests for new reports into seperate file, and adapt to new form struc...
[rails.git] / test / controllers / reports_controller_test.rb
1 require "test_helper"
2
3 class ReportsControllerTest < ActionController::TestCase
4   def test_new_report_without_login
5     target_user = create(:user)
6     get :new, :params => { :reportable_id => target_user.id, :reportable_type => "User" }
7     assert_response :redirect
8     assert_redirected_to login_path(:referer => new_report_path(:reportable_id => target_user.id, :reportable_type => "User"))
9   end
10
11   def test_new_report_after_login
12     target_user = create(:user)
13
14     session[:user] = create(:user).id
15
16     assert_equal Issue.count, 0
17
18     # Create an Issue and a report
19     get :new, :params => { :reportable_id => target_user.id, :reportable_type => "User" }
20     assert_response :success
21     assert_difference "Issue.count", 1 do
22       details = "Details of a report"
23       post :create,
24            :params => {
25              :report => {
26                :details => details,
27                :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
28              }
29            }
30     end
31     assert_equal Issue.count, 1
32     assert_response :redirect
33     assert_redirected_to root_path
34   end
35
36   def test_new_report_with_incomplete_details
37     # Test creation of a new issue and a new report
38     target_user = create(:user)
39
40     # Login
41     session[:user] = create(:user).id
42
43     assert_equal Issue.count, 0
44
45     # Create an Issue and a report
46     get :new, :params => { :reportable_id => target_user.id, :reportable_type => "User" }
47     assert_response :success
48     assert_difference "Issue.count", 1 do
49       details = "Details of a report"
50       post :create,
51            :params => {
52              :report => {
53                :details => details,
54                :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
55              }
56            }
57     end
58     assert_equal Issue.count, 1
59     assert_response :redirect
60     assert_redirected_to root_path
61
62     get :new, :params => { :reportable_id => target_user.id, :reportable_type => "User" }
63     assert_response :success
64
65     # Report without report_type
66     assert_no_difference "Issue.count" do
67       details = "Details of another report under the same issue"
68       post :create,
69            :params => {
70              :report => {
71                :details => details,
72                :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
73              }
74            }
75     end
76     assert_response :redirect
77     assert_equal Issue.find_by(:reportable_id => target_user.id, :reportable_type => "User").reports.count, 1
78
79     # Report without details
80     assert_no_difference "Issue.count" do
81       post :create,
82            :params => {
83              :report => {
84                :issue => { :reportable_id => 1, :reportable_type => "User" }
85              }
86            }
87     end
88     assert_response :redirect
89     assert_equal Issue.find_by(:reportable_id => target_user.id, :reportable_type => "User").reports.count, 1
90   end
91
92   def test_new_report_with_complete_details
93     # Test creation of a new issue and a new report
94     target_user = create(:user)
95
96     # Login
97     session[:user] = create(:user).id
98
99     assert_equal Issue.count, 0
100
101     # Create an Issue and a report
102     get :new, :params => { :reportable_id => target_user.id, :reportable_type => "User" }
103     assert_response :success
104     assert_difference "Issue.count", 1 do
105       details = "Details of a report"
106       post :create,
107            :params => {
108              :report => {
109                :details => details,
110                :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
111              }
112            }
113     end
114     assert_equal Issue.count, 1
115     assert_response :redirect
116     assert_redirected_to root_path
117
118     # Create a report for an existing Issue
119     get :new, :params => { :reportable_id => target_user.id, :reportable_type => "User" }
120     assert_response :success
121     assert_no_difference "Issue.count" do
122       details = "Details of another report under the same issue"
123       post :create,
124            :params => {
125              :report => {
126                :details => details,
127                :issue => { :reportable_id => target_user.id, :reportable_type => "User" }
128              }
129            }
130     end
131     assert_response :redirect
132     report_count = Issue.find_by(:reportable_id => target_user.id, :reportable_type => "User").reports.count
133     assert_equal report_count, 2
134   end
135 end