3 class IssuesControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles
6 def test_view_dashboard_without_auth
7 # Access issues_path without login
9 assert_response :redirect
10 assert_redirected_to login_path(:referer => issues_path)
12 # Access issues_path as normal user
13 session[:user] = users(:normal_user).id
15 assert_response :redirect
16 assert_redirected_to root_path
18 # Access issues_path by admin
19 session[:user] = users(:administrator_user).id
21 assert_response :success
23 # Access issues_path by moderator
24 session[:user] = users(:moderator_user).id
26 assert_response :success
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))
36 def test_new_issue_after_login
37 # Test creation of a new issue and a new report
40 session[:user] = users(:normal_user).id
42 assert_equal Issue.count, 0
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"
50 :report => { :details => details },
51 :report_type => "[OFFENSIVE]",
52 :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
54 assert_equal Issue.count, 1
55 assert_response :redirect
56 assert_redirected_to root_path
59 def test_new_report_with_incomplete_details
60 # Test creation of a new issue and a new report
63 session[:user] = users(:normal_user).id
65 assert_equal Issue.count, 0
67 # Create an Issue and a report
68 get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2
69 assert_response :success
70 assert_difference "Issue.count", 1 do
71 details = "Details of a report"
73 :report => { :details => details },
74 :report_type => "[OFFENSIVE]",
75 :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
77 assert_equal Issue.count, 1
78 assert_response :redirect
79 assert_redirected_to root_path
81 get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2
82 assert_response :success
84 # Report without report_type
85 assert_no_difference "Issue.count" do
86 details = "Details of another report under the same issue"
88 :report => { :details => details },
89 :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
91 assert_response :redirect
92 assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").reports.count, 1
94 # Report without details
95 assert_no_difference "Issue.count" do
97 :report_type => "[OFFENSIVE]",
98 :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
100 assert_response :redirect
101 assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").reports.count, 1
104 def test_new_report_with_complete_details
105 # Test creation of a new issue and a new report
108 session[:user] = users(:normal_user).id
110 assert_equal Issue.count, 0
112 # Create an Issue and a report
113 get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2
114 assert_response :success
115 assert_difference "Issue.count", 1 do
116 details = "Details of a report"
118 :report => { :details => details },
119 :report_type => "[OFFENSIVE]",
120 :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
122 assert_equal Issue.count, 1
123 assert_response :redirect
124 assert_redirected_to root_path
126 # Create a report for an existing Issue
127 get :new, :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2
128 assert_response :success
129 assert_no_difference "Issue.count" do
130 details = "Details of another report under the same issue"
132 :report => { :details => details },
133 :report_type => "[OFFENSIVE]",
134 :issue => { :reportable_id => 1, :reportable_type => "User", :reported_user_id => 2 }
136 assert_response :redirect
137 report_count = Issue.find_by_reportable_id_and_reportable_type(1, "User").reports.count
138 assert_equal report_count, 2
141 def test_change_status_by_normal_user
142 # Login as normal user
143 session[:user] = users(:normal_user).id
146 test_new_issue_after_login
147 assert_equal Issue.count, 1
149 get :resolve, :id => Issue.find_by_reportable_id_and_reportable_type(1, "User").id
151 assert_response :redirect
152 assert_redirected_to root_path
155 def test_change_status_by_admin
156 # Login as normal user
157 session[:user] = users(:normal_user).id
160 test_new_issue_after_login
161 assert_equal Issue.count, 1
162 assert_response :redirect
164 # Login as administrator
165 session[:user] = users(:administrator_user).id
168 get :resolve, :id => Issue.find_by_reportable_id_and_reportable_type(1, "User").id
169 assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").resolved?, true
170 assert_response :redirect
173 get :reopen, :id => Issue.find_by_reportable_id_and_reportable_type(1, "User").id
174 assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").open?, true
175 assert_response :redirect
178 get :ignore, :id => Issue.find_by_reportable_id_and_reportable_type(1, "User").id
179 assert_equal Issue.find_by_reportable_id_and_reportable_type(1, "User").ignored?, true
180 assert_response :redirect
183 def test_search_issues
184 # Login as administrator
185 session[:user] = users(:administrator_user).id
187 # No issues against the user
188 get :index, :search_by_user => "test1"
189 assert_response :redirect
190 assert_redirected_to issues_path
193 get :index, :search_by_user => "test1000"
194 assert_response :redirect
195 assert_redirected_to issues_path
197 # Create Issue against user_id:2
198 test_new_issue_after_login
199 assert_equal Issue.count, 1
200 assert_equal Issue.first.reported_user_id, 2
202 session[:user] = users(:administrator_user).id
204 # Find Issue against user_id:2
205 get :index, :search_by_user => "test2"
206 assert_response :success
209 def test_comment_by_normal_user
211 test_new_issue_after_login
212 assert_equal Issue.count, 1
214 get :comment, :id => 1
215 assert_response :redirect
216 assert_redirected_to root_path
221 test_new_issue_after_login
222 assert_equal Issue.count, 1
223 @issue = Issue.all.first
225 # Login as administrator
226 session[:user] = users(:administrator_user).id
228 get :comment, :id => @issue.id, :issue_comment => { :body => "test comment" }
229 assert_response :redirect
230 assert_redirected_to @issue