comparison test/functional/watchers_controller_test.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children e248c7af89ec
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
14 # You should have received a copy of the GNU General Public License 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 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. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 require File.expand_path('../../test_helper', __FILE__) 18 require File.expand_path('../../test_helper', __FILE__)
19 require 'watchers_controller'
20
21 # Re-raise errors caught by the controller.
22 class WatchersController; def rescue_action(e) raise e end; end
23 19
24 class WatchersControllerTest < ActionController::TestCase 20 class WatchersControllerTest < ActionController::TestCase
25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, 21 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules,
26 :issues, :trackers, :projects_trackers, :issue_statuses, :enumerations, :watchers 22 :issues, :trackers, :projects_trackers, :issue_statuses, :enumerations, :watchers
27 23
28 def setup 24 def setup
29 @controller = WatchersController.new
30 @request = ActionController::TestRequest.new
31 @response = ActionController::TestResponse.new
32 User.current = nil 25 User.current = nil
33 end 26 end
34 27
35 def test_watch 28 def test_watch_a_single_object
36 @request.session[:user_id] = 3 29 @request.session[:user_id] = 3
37 assert_difference('Watcher.count') do 30 assert_difference('Watcher.count') do
38 xhr :post, :watch, :object_type => 'issue', :object_id => '1' 31 xhr :post, :watch, :object_type => 'issue', :object_id => '1'
39 assert_response :success 32 assert_response :success
40 assert_include '$(".issue-1-watcher")', response.body 33 assert_include '$(".issue-1-watcher")', response.body
41 end 34 end
42 assert Issue.find(1).watched_by?(User.find(3)) 35 assert Issue.find(1).watched_by?(User.find(3))
36 end
37
38 def test_watch_a_collection_with_a_single_object
39 @request.session[:user_id] = 3
40 assert_difference('Watcher.count') do
41 xhr :post, :watch, :object_type => 'issue', :object_id => ['1']
42 assert_response :success
43 assert_include '$(".issue-1-watcher")', response.body
44 end
45 assert Issue.find(1).watched_by?(User.find(3))
46 end
47
48 def test_watch_a_collection_with_multiple_objects
49 @request.session[:user_id] = 3
50 assert_difference('Watcher.count', 2) do
51 xhr :post, :watch, :object_type => 'issue', :object_id => ['1', '3']
52 assert_response :success
53 assert_include '$(".issue-bulk-watcher")', response.body
54 end
55 assert Issue.find(1).watched_by?(User.find(3))
56 assert Issue.find(3).watched_by?(User.find(3))
43 end 57 end
44 58
45 def test_watch_should_be_denied_without_permission 59 def test_watch_should_be_denied_without_permission
46 Role.find(2).remove_permission! :view_issues 60 Role.find(2).remove_permission! :view_issues
47 @request.session[:user_id] = 3 61 @request.session[:user_id] = 3
68 end 82 end
69 83
70 def test_unwatch 84 def test_unwatch
71 @request.session[:user_id] = 3 85 @request.session[:user_id] = 3
72 assert_difference('Watcher.count', -1) do 86 assert_difference('Watcher.count', -1) do
73 xhr :post, :unwatch, :object_type => 'issue', :object_id => '2' 87 xhr :delete, :unwatch, :object_type => 'issue', :object_id => '2'
74 assert_response :success 88 assert_response :success
75 assert_include '$(".issue-2-watcher")', response.body 89 assert_include '$(".issue-2-watcher")', response.body
76 end 90 end
77 assert !Issue.find(1).watched_by?(User.find(3)) 91 assert !Issue.find(1).watched_by?(User.find(3))
92 end
93
94 def test_unwatch_a_collection_with_multiple_objects
95 @request.session[:user_id] = 3
96 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
97 Watcher.create!(:user_id => 3, :watchable => Issue.find(3))
98
99 assert_difference('Watcher.count', -2) do
100 xhr :delete, :unwatch, :object_type => 'issue', :object_id => ['1', '3']
101 assert_response :success
102 assert_include '$(".issue-bulk-watcher")', response.body
103 end
104 assert !Issue.find(1).watched_by?(User.find(3))
105 assert !Issue.find(3).watched_by?(User.find(3))
78 end 106 end
79 107
80 def test_new 108 def test_new
81 @request.session[:user_id] = 2 109 @request.session[:user_id] = 2
82 xhr :get, :new, :object_type => 'issue', :object_id => '2' 110 xhr :get, :new, :object_type => 'issue', :object_id => '2'
83 assert_response :success 111 assert_response :success
84 assert_match /ajax-modal/, response.body 112 assert_match /ajax-modal/, response.body
85 end 113 end
86 114
87 def test_new_for_new_record_with_id 115 def test_new_for_new_record_with_project_id
88 @request.session[:user_id] = 2 116 @request.session[:user_id] = 2
89 xhr :get, :new, :project_id => 1 117 xhr :get, :new, :project_id => 1
90 assert_response :success 118 assert_response :success
91 assert_equal Project.find(1), assigns(:project) 119 assert_equal Project.find(1), assigns(:project)
92 assert_match /ajax-modal/, response.body 120 assert_match /ajax-modal/, response.body
93 end 121 end
94 122
95 def test_new_for_new_record_with_identifier 123 def test_new_for_new_record_with_project_identifier
96 @request.session[:user_id] = 2 124 @request.session[:user_id] = 2
97 xhr :get, :new, :project_id => 'ecookbook' 125 xhr :get, :new, :project_id => 'ecookbook'
98 assert_response :success 126 assert_response :success
99 assert_equal Project.find(1), assigns(:project) 127 assert_equal Project.find(1), assigns(:project)
100 assert_match /ajax-modal/, response.body 128 assert_match /ajax-modal/, response.body
122 assert Issue.find(2).watched_by?(User.find(4)) 150 assert Issue.find(2).watched_by?(User.find(4))
123 assert Issue.find(2).watched_by?(User.find(7)) 151 assert Issue.find(2).watched_by?(User.find(7))
124 end 152 end
125 153
126 def test_autocomplete_on_watchable_creation 154 def test_autocomplete_on_watchable_creation
127 xhr :get, :autocomplete_for_user, :q => 'mi' 155 @request.session[:user_id] = 2
156 xhr :get, :autocomplete_for_user, :q => 'mi', :project_id => 'ecookbook'
128 assert_response :success 157 assert_response :success
129 assert_select 'input', :count => 4 158 assert_select 'input', :count => 4
130 assert_select 'input[name=?][value=1]', 'watcher[user_ids][]' 159 assert_select 'input[name=?][value=1]', 'watcher[user_ids][]'
131 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]' 160 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
132 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]' 161 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
133 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]' 162 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
134 end 163 end
135 164
136 def test_autocomplete_on_watchable_update 165 def test_autocomplete_on_watchable_update
137 xhr :get, :autocomplete_for_user, :q => 'mi', :object_id => '2' , :object_type => 'issue' 166 @request.session[:user_id] = 2
167 xhr :get, :autocomplete_for_user, :q => 'mi', :object_id => '2' , :object_type => 'issue', :project_id => 'ecookbook'
138 assert_response :success 168 assert_response :success
139 assert_select 'input', :count => 3 169 assert_select 'input', :count => 3
140 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]' 170 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
141 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]' 171 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
142 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]' 172 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
144 end 174 end
145 175
146 def test_append 176 def test_append
147 @request.session[:user_id] = 2 177 @request.session[:user_id] = 2
148 assert_no_difference 'Watcher.count' do 178 assert_no_difference 'Watcher.count' do
149 xhr :post, :append, :watcher => {:user_ids => ['4', '7']} 179 xhr :post, :append, :watcher => {:user_ids => ['4', '7']}, :project_id => 'ecookbook'
150 assert_response :success 180 assert_response :success
151 assert_include 'watchers_inputs', response.body 181 assert_include 'watchers_inputs', response.body
152 assert_include 'issue[watcher_user_ids][]', response.body 182 assert_include 'issue[watcher_user_ids][]', response.body
153 end 183 end
154 end 184 end
155 185
156 def test_remove_watcher 186 def test_remove_watcher
157 @request.session[:user_id] = 2 187 @request.session[:user_id] = 2
158 assert_difference('Watcher.count', -1) do 188 assert_difference('Watcher.count', -1) do
159 xhr :post, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3' 189 xhr :delete, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3'
160 assert_response :success 190 assert_response :success
161 assert_match /watchers/, response.body 191 assert_match /watchers/, response.body
162 end 192 end
163 assert !Issue.find(2).watched_by?(User.find(3)) 193 assert !Issue.find(2).watched_by?(User.find(3))
164 end 194 end