]> git.openstreetmap.org Git - rails.git/blob - test/controllers/issues_controller_test.rb
87eda15069df7c9c9340b10ef1d42d62ab3be7de
[rails.git] / test / controllers / issues_controller_test.rb
1 require 'test_helper'
2
3 class IssuesControllerTest < ActionController::TestCase
4   fixtures :users,:user_roles
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     assert_response :success
22
23     # Access issues_path by moderator
24     session[:user]= users(:moderator_user).id
25     get :index
26     assert_response :success
27   end
28
29   def test_new_issue_without_login
30     # Test creation of a new issue and a new report without logging in
31     get :new, {reportable_id: 1, reportable_type: "User", reported_user_id: 1}
32     assert_response :redirect
33     assert_redirected_to login_path(:referer => new_issue_path(:reportable_id=>1, :reportable_type=>"User",:reported_user_id=> 1))
34   end
35
36   def test_new_issue_after_login
37     # Test creation of a new issue and a new report
38
39     # Login
40     session[:user] = users(:normal_user).id
41
42     assert_equal Issue.count,0
43     
44     # Create an Issue and a report  
45     get :new, {reportable_id: 1, reportable_type: "User", reported_user_id: 2}
46     assert_response :success
47     assert_difference "Issue.count",1 do
48       details = "Details of a report"
49       post :create, { :report => { :details => details},
50                     :report_type => "[OFFENSIVE]",
51                     :issue => { reportable_id: 1, reportable_type: "User", reported_user_id: 2} }
52     end
53     assert_equal Issue.count,1
54     assert_response :redirect
55     assert_redirected_to root_path
56   end
57
58   def test_new_report_with_incomplete_details
59     # Test creation of a new issue and a new report
60
61     # Login
62     session[:user] = users(:normal_user).id
63
64     assert_equal Issue.count,0
65
66     # Create an Issue and a report
67     get :new, {reportable_id: 1, reportable_type: "User", reported_user_id: 2}
68     assert_response :success
69     assert_difference "Issue.count",1 do
70       details = "Details of a report"
71       post :create, { :report => { :details => details},
72                       :report_type => "[OFFENSIVE]",
73                       :issue => { reportable_id: 1, reportable_type: "User", reported_user_id: 2} }
74     end 
75     assert_equal Issue.count,1
76     assert_response :redirect
77     assert_redirected_to root_path
78     
79     get :new, {reportable_id: 1, reportable_type: "User", reported_user_id: 2}
80     assert_response :success
81
82     # Report without report_type
83     assert_no_difference "Issue.count" do
84       details = "Details of another report under the same issue"
85       post :create, { :report => { :details => details},
86                       :issue => { reportable_id: 1, reportable_type: "User", reported_user_id: 2} }
87     end
88     assert_response :redirect
89     assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"User").reports.count,1
90
91     # Report without details
92     assert_no_difference "Issue.count" do
93       post :create, { :report_type => "[OFFENSIVE]", 
94                       :issue => { reportable_id: 1, reportable_type: "User", reported_user_id: 2} }
95     end
96     assert_response :redirect
97     assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"User").reports.count,1
98   end
99
100   def test_new_report_with_complete_details
101     # Test creation of a new issue and a new report
102
103     # Login
104     session[:user] = users(:normal_user).id
105
106     assert_equal Issue.count,0
107
108     # Create an Issue and a report
109     get :new, {reportable_id: 1, reportable_type: "User", reported_user_id: 2}
110     assert_response :success
111     assert_difference "Issue.count",1 do
112       details = "Details of a report"
113       post :create, { :report => { :details => details},
114                     :report_type => "[OFFENSIVE]",
115                     :issue => { reportable_id: 1, reportable_type: "User", reported_user_id: 2} }
116     end
117     assert_equal Issue.count,1
118     assert_response :redirect
119     assert_redirected_to root_path
120     
121     # Create a report for an existing Issue
122     get :new, {reportable_id: 1, reportable_type: "User", reported_user_id: 2}
123     assert_response :success
124     assert_no_difference "Issue.count" do
125       details = "Details of another report under the same issue"
126       post :create, { :report => { :details => details},
127                       :report_type => "[OFFENSIVE]",      
128                       :issue => { reportable_id: 1, reportable_type: "User", reported_user_id: 2} }
129     end
130     assert_response :redirect
131     assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"User").reports.count,2
132   end
133
134   def test_change_status_by_normal_user
135     # Login as normal user
136     session[:user] = users(:normal_user).id
137     
138     # Create Issue
139     test_new_issue_after_login    
140     assert_equal Issue.count,1
141     
142     get :resolve, id: Issue.find_by_reportable_id_and_reportable_type(1,"User").id
143
144     assert_response :redirect
145     assert_redirected_to root_path
146   end
147
148   def test_change_status_by_admin
149     # Login as normal user
150     session[:user] = users(:normal_user).id
151
152     # Create Issue
153     test_new_issue_after_login
154     assert_equal Issue.count,1
155     assert_response :redirect
156
157     # Login as administrator
158     session[:user] = users(:administrator_user).id
159    
160     # Test 'Resolved'
161     get :resolve, id: Issue.find_by_reportable_id_and_reportable_type(1,"User").id
162     assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"User").resolved?, true
163     assert_response :redirect
164
165     # Test 'Reopen'
166     get :reopen, id: Issue.find_by_reportable_id_and_reportable_type(1,"User").id
167     assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"User").open?, true
168     assert_response :redirect
169
170     # Test 'Ignored'
171     get :ignore, id: Issue.find_by_reportable_id_and_reportable_type(1,"User").id
172     assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"User").ignored?, true
173     assert_response :redirect
174   end
175
176   def test_search_issues
177     # Login as administrator
178     session[:user] = users(:administrator_user).id
179
180     # No issues against the user
181     get :index, search_by_user: "test1"
182     assert_response :redirect
183     assert_redirected_to issues_path
184
185     # User doesn't exist
186     get :index, search_by_user: "test1000"
187     assert_response :redirect
188     assert_redirected_to issues_path
189
190     # Create Issue against user_id:2
191     test_new_issue_after_login
192     assert_equal Issue.count,1
193     assert_equal Issue.first.reported_user_id,2
194
195     session[:user] = users(:administrator_user).id
196
197     # Find Issue against user_id:2
198     get :index, search_by_user: "test2"
199     assert_response :success
200   end
201
202 end