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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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. require 'watchers_controller'
  19. # Re-raise errors caught by the controller.
  20. class WatchersController; def rescue_action(e) raise e end; end
  21. class WatchersControllerTest < ActionController::TestCase
  22. fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules,
  23. :issues, :trackers, :projects_trackers, :issue_statuses, :enumerations, :watchers
  24. def setup
  25. @controller = WatchersController.new
  26. @request = ActionController::TestRequest.new
  27. @response = ActionController::TestResponse.new
  28. User.current = nil
  29. end
  30. def test_watch
  31. @request.session[:user_id] = 3
  32. assert_difference('Watcher.count') do
  33. xhr :post, :watch, :object_type => 'issue', :object_id => '1'
  34. assert_response :success
  35. assert @response.body.include?('$$(".issue-1-watcher")')
  36. end
  37. assert Issue.find(1).watched_by?(User.find(3))
  38. end
  39. def test_watch_should_be_denied_without_permission
  40. Role.find(2).remove_permission! :view_issues
  41. @request.session[:user_id] = 3
  42. assert_no_difference('Watcher.count') do
  43. xhr :post, :watch, :object_type => 'issue', :object_id => '1'
  44. assert_response 403
  45. end
  46. end
  47. def test_unwatch
  48. @request.session[:user_id] = 3
  49. assert_difference('Watcher.count', -1) do
  50. xhr :post, :unwatch, :object_type => 'issue', :object_id => '2'
  51. assert_response :success
  52. assert @response.body.include?('$$(".issue-2-watcher")')
  53. end
  54. assert !Issue.find(1).watched_by?(User.find(3))
  55. end
  56. def test_new
  57. @request.session[:user_id] = 2
  58. xhr :get, :new, :object_type => 'issue', :object_id => '2'
  59. assert_response :success
  60. assert_select_rjs :replace_html, 'ajax-modal'
  61. end
  62. def test_new_for_new_record
  63. @request.session[:user_id] = 2
  64. xhr :get, :new, :project_id => 1
  65. assert_response :success
  66. assert_select_rjs :replace_html, 'ajax-modal'
  67. end
  68. def test_create
  69. @request.session[:user_id] = 2
  70. assert_difference('Watcher.count') do
  71. xhr :post, :create, :object_type => 'issue', :object_id => '2', :watcher => {:user_id => '4'}
  72. assert_response :success
  73. assert_select_rjs :replace_html, 'watchers'
  74. assert_select_rjs :replace_html, 'ajax-modal'
  75. end
  76. assert Issue.find(2).watched_by?(User.find(4))
  77. end
  78. def test_create_multiple
  79. @request.session[:user_id] = 2
  80. assert_difference('Watcher.count', 2) do
  81. xhr :post, :create, :object_type => 'issue', :object_id => '2', :watcher => {:user_ids => ['4', '7']}
  82. assert_response :success
  83. assert_select_rjs :replace_html, 'watchers'
  84. assert_select_rjs :replace_html, 'ajax-modal'
  85. end
  86. assert Issue.find(2).watched_by?(User.find(4))
  87. assert Issue.find(2).watched_by?(User.find(7))
  88. end
  89. def test_autocomplete_on_watchable_creation
  90. xhr :get, :autocomplete_for_user, :q => 'mi'
  91. assert_response :success
  92. assert_select 'input', :count => 4
  93. assert_select 'input[name=?][value=1]', 'watcher[user_ids][]'
  94. assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
  95. assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
  96. assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
  97. end
  98. def test_autocomplete_on_watchable_update
  99. xhr :get, :autocomplete_for_user, :q => 'mi', :object_id => '2' , :object_type => 'issue'
  100. assert_response :success
  101. assert_select 'input', :count => 3
  102. assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
  103. assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
  104. assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
  105. end
  106. def test_append
  107. @request.session[:user_id] = 2
  108. assert_no_difference 'Watcher.count' do
  109. xhr :post, :append, :watcher => {:user_ids => ['4', '7']}
  110. assert_response :success
  111. assert_select_rjs :insert_html, 'watchers_inputs' do
  112. assert_select 'input[name=?][value=4]', 'issue[watcher_user_ids][]'
  113. assert_select 'input[name=?][value=7]', 'issue[watcher_user_ids][]'
  114. end
  115. end
  116. end
  117. def test_remove_watcher
  118. @request.session[:user_id] = 2
  119. assert_difference('Watcher.count', -1) do
  120. xhr :post, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3'
  121. assert_response :success
  122. assert_select_rjs :replace_html, 'watchers'
  123. end
  124. assert !Issue.find(2).watched_by?(User.find(3))
  125. end
  126. end