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"
49 post :create, { :report => { :details => details},
50 :report_type => "[OFFENSIVE]",
51 :issue => { reportable_id: 1, reportable_type: "User", reported_user_id: 2} }
53 assert_equal Issue.count,1
54 assert_response :redirect
55 assert_redirected_to root_path
58 def test_new_report_with_incomplete_details
59 # Test creation of a new issue and a new report
62 session[:user] = users(:normal_user).id
64 assert_equal Issue.count,0
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} }
75 assert_equal Issue.count,1
76 assert_response :redirect
77 assert_redirected_to root_path
79 get :new, {reportable_id: 1, reportable_type: "User", reported_user_id: 2}
80 assert_response :success
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} }
88 assert_response :redirect
89 assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"User").reports.count,1
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} }
96 assert_response :redirect
97 assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"User").reports.count,1
100 def test_new_report_with_complete_details
101 # Test creation of a new issue and a new report
104 session[:user] = users(:normal_user).id
106 assert_equal Issue.count,0
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} }
117 assert_equal Issue.count,1
118 assert_response :redirect
119 assert_redirected_to root_path
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} }
130 assert_response :redirect
131 assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"User").reports.count,2
134 def test_change_status_by_normal_user
135 # Login as normal user
136 session[:user] = users(:normal_user).id
139 test_new_issue_after_login
140 assert_equal Issue.count,1
142 get :resolve, id: Issue.find_by_reportable_id_and_reportable_type(1,"User").id
144 assert_response :redirect
145 assert_redirected_to root_path
148 def test_change_status_by_admin
149 # Login as normal user
150 session[:user] = users(:normal_user).id
153 test_new_issue_after_login
154 assert_equal Issue.count,1
155 assert_response :redirect
157 # Login as administrator
158 session[:user] = users(:administrator_user).id
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
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
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
176 def test_search_issues
177 # Login as administrator
178 session[:user] = users(:administrator_user).id
180 # No issues against the user
181 get :index, search_by_user: "test1"
182 assert_response :redirect
183 assert_redirected_to issues_path
186 get :index, search_by_user: "test1000"
187 assert_response :redirect
188 assert_redirected_to issues_path
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
195 session[:user] = users(:administrator_user).id
197 # Find Issue against user_id:2
198 get :index, search_by_user: "test2"
199 assert_response :success
202 def test_comment_by_normal_user
204 test_new_issue_after_login
205 assert_equal Issue.count,1
208 assert_response :redirect
209 assert_redirected_to root_path
214 test_new_issue_after_login
215 assert_equal Issue.count,1
216 @issue = Issue.all.first
218 # Login as administrator
219 session[:user] = users(:administrator_user).id
221 get :comment, id: @issue.id, :issue_comment => { body: "test comment" }
222 assert_response :redirect
223 assert_redirected_to @issue