You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watchers_controller_test.rb 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class WatchersControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules,
  21. :issues, :trackers, :projects_trackers, :issue_statuses, :enumerations, :watchers
  22. def setup
  23. User.current = nil
  24. end
  25. def test_watch_a_single_object_as_html
  26. @request.session[:user_id] = 3
  27. assert_difference('Watcher.count') do
  28. post :watch, :params => {:object_type => 'issue', :object_id => '1'}
  29. assert_response :success
  30. assert_include 'Watcher added', response.body
  31. end
  32. assert Issue.find(1).watched_by?(User.find(3))
  33. end
  34. def test_watch_a_single_object
  35. @request.session[:user_id] = 3
  36. assert_difference('Watcher.count') do
  37. post :watch, :params => {:object_type => 'issue', :object_id => '1'}, :xhr => true
  38. assert_response :success
  39. assert_include '$(".issue-1-watcher")', response.body
  40. end
  41. assert Issue.find(1).watched_by?(User.find(3))
  42. end
  43. def test_watch_a_collection_with_a_single_object
  44. @request.session[:user_id] = 3
  45. assert_difference('Watcher.count') do
  46. post :watch, :params => {:object_type => 'issue', :object_id => ['1']}, :xhr => true
  47. assert_response :success
  48. assert_include '$(".issue-1-watcher")', response.body
  49. end
  50. assert Issue.find(1).watched_by?(User.find(3))
  51. end
  52. def test_watch_a_collection_with_multiple_objects
  53. @request.session[:user_id] = 3
  54. assert_difference('Watcher.count', 2) do
  55. post :watch, :params => {:object_type => 'issue', :object_id => ['1', '3']}, :xhr => true
  56. assert_response :success
  57. assert_include '$(".issue-bulk-watcher")', response.body
  58. end
  59. assert Issue.find(1).watched_by?(User.find(3))
  60. assert Issue.find(3).watched_by?(User.find(3))
  61. end
  62. def test_watch_a_news_module_should_add_watcher
  63. @request.session[:user_id] = 7
  64. assert_not_nil m = Project.find(1).enabled_module('news')
  65. assert_difference 'Watcher.count' do
  66. post :watch, :params => {:object_type => 'enabled_module', :object_id => m.id.to_s}, :xhr => true
  67. assert_response :success
  68. end
  69. assert m.reload.watched_by?(User.find(7))
  70. end
  71. def test_watch_a_private_news_module_without_permission_should_fail
  72. @request.session[:user_id] = 7
  73. assert_not_nil m = Project.find(2).enabled_module('news')
  74. assert_no_difference 'Watcher.count' do
  75. post :watch, :params => {:object_type => 'enabled_module', :object_id => m.id.to_s}, :xhr => true
  76. assert_response 403
  77. end
  78. end
  79. def test_watch_should_be_denied_without_permission
  80. Role.find(2).remove_permission! :view_issues
  81. @request.session[:user_id] = 3
  82. assert_no_difference('Watcher.count') do
  83. post :watch, :params => {:object_type => 'issue', :object_id => '1'}, :xhr => true
  84. assert_response 403
  85. end
  86. end
  87. def test_watch_invalid_class_should_respond_with_404
  88. @request.session[:user_id] = 3
  89. assert_no_difference('Watcher.count') do
  90. post :watch, :params => {:object_type => 'foo', :object_id => '1'}, :xhr => true
  91. assert_response 404
  92. end
  93. end
  94. def test_watch_invalid_object_should_respond_with_404
  95. @request.session[:user_id] = 3
  96. assert_no_difference('Watcher.count') do
  97. post :watch, :params => {:object_type => 'issue', :object_id => '999'}, :xhr => true
  98. assert_response 404
  99. end
  100. end
  101. def test_unwatch_as_html
  102. @request.session[:user_id] = 3
  103. assert_difference('Watcher.count', -1) do
  104. delete :unwatch, :params => {:object_type => 'issue', :object_id => '2'}
  105. assert_response :success
  106. assert_include 'Watcher removed', response.body
  107. end
  108. assert !Issue.find(1).watched_by?(User.find(3))
  109. end
  110. def test_unwatch
  111. @request.session[:user_id] = 3
  112. assert_difference('Watcher.count', -1) do
  113. delete :unwatch, :params => {:object_type => 'issue', :object_id => '2'}, :xhr => true
  114. assert_response :success
  115. assert_include '$(".issue-2-watcher")', response.body
  116. end
  117. assert !Issue.find(1).watched_by?(User.find(3))
  118. end
  119. def test_unwatch_a_collection_with_multiple_objects
  120. @request.session[:user_id] = 3
  121. Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
  122. Watcher.create!(:user_id => 3, :watchable => Issue.find(3))
  123. assert_difference('Watcher.count', -2) do
  124. delete :unwatch, :params => {:object_type => 'issue', :object_id => ['1', '3']}, :xhr => true
  125. assert_response :success
  126. assert_include '$(".issue-bulk-watcher")', response.body
  127. end
  128. assert !Issue.find(1).watched_by?(User.find(3))
  129. assert !Issue.find(3).watched_by?(User.find(3))
  130. end
  131. def test_new
  132. @request.session[:user_id] = 2
  133. get :new, :params => {:object_type => 'issue', :object_id => '2'}, :xhr => true
  134. assert_response :success
  135. assert_match /ajax-modal/, response.body
  136. end
  137. def test_new_with_multiple_objects
  138. @request.session[:user_id] = 2
  139. get :new, :params => {:object_type => 'issue', :object_id => ['1', '2']}, :xhr => true
  140. assert_response :success
  141. assert_match /ajax-modal/, response.body
  142. end
  143. def test_new_for_new_record_with_project_id
  144. @request.session[:user_id] = 2
  145. get :new, :params => {:project_id => 1}, :xhr => true
  146. assert_response :success
  147. assert_match /ajax-modal/, response.body
  148. end
  149. def test_new_for_new_record_with_project_identifier
  150. @request.session[:user_id] = 2
  151. get :new, :params => {:project_id => 'ecookbook'}, :xhr => true
  152. assert_response :success
  153. assert_match /ajax-modal/, response.body
  154. end
  155. def test_create_as_html
  156. @request.session[:user_id] = 2
  157. assert_difference('Watcher.count') do
  158. post :create, :params => {
  159. :object_type => 'issue', :object_id => '2',
  160. :watcher => {:user_id => '4'}
  161. }
  162. assert_response :success
  163. assert_include 'Watcher added', response.body
  164. end
  165. assert Issue.find(2).watched_by?(User.find(4))
  166. end
  167. def test_create
  168. @request.session[:user_id] = 2
  169. assert_difference('Watcher.count') do
  170. post :create, :params => {
  171. :object_type => 'issue', :object_id => '2',
  172. :watcher => {:user_id => '4'}
  173. }, :xhr => true
  174. assert_response :success
  175. assert_match /watchers/, response.body
  176. assert_match /ajax-modal/, response.body
  177. end
  178. assert Issue.find(2).watched_by?(User.find(4))
  179. end
  180. def test_create_with_mutiple_users
  181. @request.session[:user_id] = 2
  182. assert_difference('Watcher.count', 2) do
  183. post :create, :params => {
  184. :object_type => 'issue', :object_id => '2',
  185. :watcher => {:user_ids => ['4', '7']}
  186. }, :xhr => true
  187. assert_response :success
  188. assert_match /watchers/, response.body
  189. assert_match /ajax-modal/, response.body
  190. end
  191. assert Issue.find(2).watched_by?(User.find(4))
  192. assert Issue.find(2).watched_by?(User.find(7))
  193. end
  194. def test_create_with_mutiple_objects
  195. @request.session[:user_id] = 2
  196. assert_difference('Watcher.count', 4) do
  197. post :create, :params => {
  198. :object_type => 'issue', :object_id => ['1', '2'],
  199. :watcher => {:user_ids => ['4', '7']}
  200. }, :xhr => true
  201. assert_response :success
  202. assert_match /watchers/, response.body
  203. assert_match /ajax-modal/, response.body
  204. end
  205. assert Issue.find(1).watched_by?(User.find(4))
  206. assert Issue.find(2).watched_by?(User.find(4))
  207. assert Issue.find(1).watched_by?(User.find(7))
  208. assert Issue.find(2).watched_by?(User.find(7))
  209. end
  210. def test_autocomplete_on_watchable_creation
  211. @request.session[:user_id] = 2
  212. get :autocomplete_for_user, :params => {:q => 'mi', :project_id => 'ecookbook'}, :xhr => true
  213. assert_response :success
  214. assert_select 'input', :count => 4
  215. assert_select 'input[name=?][value="1"]', 'watcher[user_ids][]'
  216. assert_select 'input[name=?][value="2"]', 'watcher[user_ids][]'
  217. assert_select 'input[name=?][value="8"]', 'watcher[user_ids][]'
  218. assert_select 'input[name=?][value="9"]', 'watcher[user_ids][]'
  219. end
  220. def test_search_non_member_on_create
  221. @request.session[:user_id] = 2
  222. project = Project.find_by_name("ecookbook")
  223. user = User.generate!(:firstname => 'issue15622')
  224. membership = user.membership(project)
  225. assert_nil membership
  226. get :autocomplete_for_user, :params => {:q => 'issue15622', :project_id => 'ecookbook'}, :xhr => true
  227. assert_response :success
  228. assert_select 'input', :count => 1
  229. end
  230. def test_autocomplete_on_watchable_update
  231. @request.session[:user_id] = 2
  232. get :autocomplete_for_user, :params => {
  233. :object_type => 'issue', :object_id => '2',
  234. :project_id => 'ecookbook', :q => 'mi'
  235. }, :xhr => true
  236. assert_response :success
  237. assert_select 'input', :count => 3
  238. assert_select 'input[name=?][value="2"]', 'watcher[user_ids][]'
  239. assert_select 'input[name=?][value="8"]', 'watcher[user_ids][]'
  240. assert_select 'input[name=?][value="9"]', 'watcher[user_ids][]'
  241. end
  242. def test_search_and_add_non_member_on_update
  243. @request.session[:user_id] = 2
  244. project = Project.find_by_name("ecookbook")
  245. user = User.generate!(:firstname => 'issue15622')
  246. membership = user.membership(project)
  247. assert_nil membership
  248. get :autocomplete_for_user, :params => {
  249. :object_type => 'issue', :object_id => '2',
  250. :project_id => 'ecookbook', :q => 'issue15622'
  251. }, :xhr => true
  252. assert_response :success
  253. assert_select 'input', :count => 1
  254. assert_difference('Watcher.count', 1) do
  255. post :create, :params => {
  256. :object_type => 'issue', :object_id => '2',
  257. :watcher => {:user_ids => ["#{user.id}"]}
  258. }, :xhr => true
  259. assert_response :success
  260. assert_match /watchers/, response.body
  261. assert_match /ajax-modal/, response.body
  262. end
  263. assert Issue.find(2).watched_by?(user)
  264. end
  265. def test_autocomplete_for_user_should_return_visible_users
  266. Role.update_all :users_visibility => 'members_of_visible_projects'
  267. hidden = User.generate!(:lastname => 'autocomplete_hidden')
  268. visible = User.generate!(:lastname => 'autocomplete_visible')
  269. User.add_to_project(visible, Project.find(1))
  270. @request.session[:user_id] = 2
  271. get :autocomplete_for_user, :params => {:q => 'autocomp', :project_id => 'ecookbook'}, :xhr => true
  272. assert_response :success
  273. assert_include visible.name, response.body
  274. assert_not_include hidden.name, response.body
  275. end
  276. def test_append
  277. @request.session[:user_id] = 2
  278. assert_no_difference 'Watcher.count' do
  279. post :append, :params => {
  280. :watcher => {:user_ids => ['4', '7']}, :project_id => 'ecookbook'
  281. }, :xhr => true
  282. assert_response :success
  283. assert_include 'watchers_inputs', response.body
  284. assert_include 'issue[watcher_user_ids][]', response.body
  285. end
  286. end
  287. def test_append_without_user_should_render_nothing
  288. @request.session[:user_id] = 2
  289. post :append, :params => {:project_id => 'ecookbook'}, :xhr => true
  290. assert_response :success
  291. assert response.body.blank?
  292. end
  293. def test_destroy_as_html
  294. @request.session[:user_id] = 2
  295. assert_difference('Watcher.count', -1) do
  296. delete :destroy, :params => {
  297. :object_type => 'issue', :object_id => '2', :user_id => '3'
  298. }
  299. assert_response :success
  300. assert_include 'Watcher removed', response.body
  301. end
  302. assert !Issue.find(2).watched_by?(User.find(3))
  303. end
  304. def test_destroy
  305. @request.session[:user_id] = 2
  306. assert_difference('Watcher.count', -1) do
  307. delete :destroy, :params => {
  308. :object_type => 'issue', :object_id => '2', :user_id => '3'
  309. }, :xhr => true
  310. assert_response :success
  311. assert_match /watchers/, response.body
  312. end
  313. assert !Issue.find(2).watched_by?(User.find(3))
  314. end
  315. def test_destroy_locked_user
  316. user = User.find(3)
  317. user.lock!
  318. assert user.reload.locked?
  319. @request.session[:user_id] = 2
  320. assert_difference('Watcher.count', -1) do
  321. delete :destroy, :params => {
  322. :object_type => 'issue', :object_id => '2', :user_id => '3'
  323. }, :xhr => true
  324. assert_response :success
  325. assert_match /watchers/, response.body
  326. end
  327. assert !Issue.find(2).watched_by?(User.find(3))
  328. end
  329. def test_destroy_invalid_user_should_respond_with_404
  330. @request.session[:user_id] = 2
  331. assert_no_difference('Watcher.count') do
  332. delete :destroy, :params => {
  333. :object_type => 'issue', :object_id => '2', :user_id => '999'
  334. }
  335. assert_response 404
  336. end
  337. end
  338. end