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