]> git.openstreetmap.org Git - rails.git/blob - test/controllers/issues_controller_test.rb
9cf68c8691f2f5df5651d64602a7c21173646bb1
[rails.git] / test / controllers / issues_controller_test.rb
1 require "test_helper"
2
3 class IssuesControllerTest < ActionController::TestCase
4   fixtures :users, :user_roles, :issues
5
6   teardown do
7     # cleanup any emails set off by the test
8     ActionMailer::Base.deliveries.clear
9   end
10
11   def test_view_dashboard_without_auth
12     # Access issues_path without login
13     get :index
14     assert_response :redirect
15     assert_redirected_to login_path(:referer => issues_path)
16
17     # Access issues_path as normal user
18     session[:user] = users(:normal_user).id
19     get :index
20     assert_response :redirect
21     assert_redirected_to root_path
22
23     # Access issues_path by admin
24     session[:user] = users(:administrator_user).id
25     get :index
26     # this is redirected because there are no issues?!
27     assert_response :redirect
28     assert_redirected_to issues_path
29
30     # Access issues_path by moderator
31     session[:user] = users(:moderator_user).id
32     get :index
33     # this is redirected because there are no issues?!
34     assert_response :redirect
35     assert_redirected_to issues_path
36
37     # clear session
38     session.delete(:user)
39   end
40
41   def test_new_issue_without_login
42     # Test creation of a new issue and a new report without logging in
43     get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 1
44     assert_response :redirect
45     assert_redirected_to login_path(:referer => new_issue_path(:reportable_id => 1, :reportable_type => "User", :reported_user_id => 1))
46   end
47
48   def test_new_issue_after_login
49     # Test creation of a new issue and a new report
50
51     # Login
52     session[:user] = users(:normal_user).id
53
54     assert_equal Issue.count, 0
55
56     # Create an Issue and a report
57     get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2
58     assert_response :success
59     assert_difference "Issue.count", 1 do
60       details = "Details of a report"
61       post :create,
62            :report => { :details => details },
63            :report_type => "[OFFENSIVE]",
64            :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
65     end
66     assert_equal Issue.count, 1
67     assert_response :redirect
68     assert_redirected_to root_path
69
70     # clear session
71     session.delete(:user)
72   end
73
74   def test_new_report_with_incomplete_details
75     # Test creation of a new issue and a new report
76
77     # Login
78     session[:user] = users(:normal_user).id
79
80     assert_equal Issue.count, 0
81
82     # Create an Issue and a report
83     get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2
84     assert_response :success
85     assert_difference "Issue.count", 1 do
86       details = "Details of a report"
87       post :create,
88            :report => { :details => details },
89            :report_type => "[OFFENSIVE]",
90            :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
91     end
92     assert_equal Issue.count, 1
93     assert_response :redirect
94     assert_redirected_to root_path
95
96     get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2
97     assert_response :success
98
99     # Report without report_type
100     assert_no_difference "Issue.count" do
101       details = "Details of another report under the same issue"
102       post :create,
103            :report => { :details => details },
104            :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
105     end
106     assert_response :redirect
107     assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").reports.count, 1
108
109     # Report without details
110     assert_no_difference "Issue.count" do
111       post :create,
112            :report_type => "[OFFENSIVE]",
113            :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
114     end
115     assert_response :redirect
116     assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").reports.count, 1
117
118     # clear session
119     session.delete(:user)
120   end
121
122   def test_new_report_with_complete_details
123     # Test creation of a new issue and a new report
124
125     # Login
126     session[:user] = users(:normal_user).id
127
128     assert_equal Issue.count, 0
129
130     # Create an Issue and a report
131     get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2
132     assert_response :success
133     assert_difference "Issue.count", 1 do
134       details = "Details of a report"
135       post :create,
136            :report => { :details => details },
137            :report_type => "[OFFENSIVE]",
138            :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
139     end
140     assert_equal Issue.count, 1
141     assert_response :redirect
142     assert_redirected_to root_path
143
144     # Create a report for an existing Issue
145     get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2
146     assert_response :success
147     assert_no_difference "Issue.count" do
148       details = "Details of another report under the same issue"
149       post :create,
150            :report => { :details => details },
151            :report_type => "[OFFENSIVE]",
152            :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
153     end
154     assert_response :redirect
155     report_count = Issue.find_by_reportable_id_and_reportable_type(1, "User").reports.count
156     assert_equal report_count, 2
157
158     # clear session
159     session.delete(:user)
160   end
161
162   def test_change_status_by_normal_user
163     # Login as normal user
164     session[:user] = users(:normal_user).id
165
166     # Create Issue
167     test_new_issue_after_login
168     assert_equal Issue.count, 1
169
170     get :resolve, :id => Issue.find_by_reportable_id_and_reportable_type(1, "User").id
171
172     assert_response :redirect
173     assert_redirected_to root_path
174
175     # clear session
176     session.delete(:user)
177   end
178
179   def test_change_status_by_admin
180     # Login as normal user
181     session[:user] = users(:normal_user).id
182
183     # Create Issue
184     test_new_issue_after_login
185     assert_equal Issue.count, 1
186     assert_response :redirect
187
188     # Login as administrator
189     session[:user] = users(:administrator_user).id
190
191     # Test 'Resolved'
192     get :resolve, :id => Issue.find_by_reportable_id_and_reportable_type(1, "User").id
193     assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").resolved?, true
194     assert_response :redirect
195
196     # Test 'Reopen'
197     get :reopen, :id => Issue.find_by_reportable_id_and_reportable_type(1, "User").id
198     assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").open?, true
199     assert_response :redirect
200
201     # Test 'Ignored'
202     get :ignore, :id => Issue.find_by_reportable_id_and_reportable_type(1, "User").id
203     assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").ignored?, true
204     assert_response :redirect
205
206     # clear session
207     session.delete(:user)
208   end
209
210   def test_search_issues
211     # Login as administrator
212     session[:user] = users(:administrator_user).id
213
214     # No issues against the user
215     get :index, :search_by_user => "test1"
216     assert_response :redirect
217     assert_redirected_to issues_path
218
219     # User doesn't exist
220     get :index, :search_by_user => "test1000"
221     assert_response :redirect
222     assert_redirected_to issues_path
223
224     # Create Issue against user_id:2
225     test_new_issue_after_login
226     assert_equal Issue.count, 1
227     assert_equal Issue.first.reported_user_id, 2
228
229     session[:user] = users(:administrator_user).id
230
231     # Find Issue against user_id:2
232     get :index, :search_by_user => "test2"
233     assert_response :success
234
235     # clear session
236     session.delete(:user)
237   end
238
239   def test_comment_by_normal_user
240     # Create Issue
241     test_new_issue_after_login
242     assert_equal Issue.count, 1
243
244     get :comment, :id => 1
245     assert_response :redirect
246     assert_redirected_to root_path
247   end
248
249   def test_comment
250     # Create Issue
251     test_new_issue_after_login
252     assert_equal Issue.count, 1
253     @issue = Issue.all.first
254
255     # Login as administrator
256     session[:user] = users(:administrator_user).id
257
258     get :comment, :id => @issue.id, :issue_comment => { :body => "test comment" }
259     assert_response :redirect
260     assert_redirected_to @issue
261
262     # clear session
263     session.delete(:user)
264   end
265 end