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

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