Chris@0
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@119
|
18 require File.expand_path('../../test_helper', __FILE__)
|
Chris@0
|
19
|
Chris@0
|
20 class WatchersControllerTest < ActionController::TestCase
|
Chris@0
|
21 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules,
|
Chris@0
|
22 :issues, :trackers, :projects_trackers, :issue_statuses, :enumerations, :watchers
|
Chris@909
|
23
|
Chris@0
|
24 def setup
|
Chris@0
|
25 User.current = nil
|
Chris@0
|
26 end
|
Chris@909
|
27
|
Chris@1464
|
28 def test_watch_a_single_object
|
Chris@0
|
29 @request.session[:user_id] = 3
|
Chris@0
|
30 assert_difference('Watcher.count') do
|
Chris@0
|
31 xhr :post, :watch, :object_type => 'issue', :object_id => '1'
|
Chris@0
|
32 assert_response :success
|
Chris@1115
|
33 assert_include '$(".issue-1-watcher")', response.body
|
Chris@0
|
34 end
|
Chris@0
|
35 assert Issue.find(1).watched_by?(User.find(3))
|
Chris@0
|
36 end
|
Chris@909
|
37
|
Chris@1464
|
38 def test_watch_a_collection_with_a_single_object
|
Chris@1464
|
39 @request.session[:user_id] = 3
|
Chris@1464
|
40 assert_difference('Watcher.count') do
|
Chris@1464
|
41 xhr :post, :watch, :object_type => 'issue', :object_id => ['1']
|
Chris@1464
|
42 assert_response :success
|
Chris@1464
|
43 assert_include '$(".issue-1-watcher")', response.body
|
Chris@1464
|
44 end
|
Chris@1464
|
45 assert Issue.find(1).watched_by?(User.find(3))
|
Chris@1464
|
46 end
|
Chris@1464
|
47
|
Chris@1464
|
48 def test_watch_a_collection_with_multiple_objects
|
Chris@1464
|
49 @request.session[:user_id] = 3
|
Chris@1464
|
50 assert_difference('Watcher.count', 2) do
|
Chris@1464
|
51 xhr :post, :watch, :object_type => 'issue', :object_id => ['1', '3']
|
Chris@1464
|
52 assert_response :success
|
Chris@1464
|
53 assert_include '$(".issue-bulk-watcher")', response.body
|
Chris@1464
|
54 end
|
Chris@1464
|
55 assert Issue.find(1).watched_by?(User.find(3))
|
Chris@1464
|
56 assert Issue.find(3).watched_by?(User.find(3))
|
Chris@1464
|
57 end
|
Chris@1464
|
58
|
Chris@1517
|
59 def test_watch_a_news_module_should_add_watcher
|
Chris@1517
|
60 @request.session[:user_id] = 7
|
Chris@1517
|
61 assert_not_nil m = Project.find(1).enabled_module('news')
|
Chris@1517
|
62
|
Chris@1517
|
63 assert_difference 'Watcher.count' do
|
Chris@1517
|
64 xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s
|
Chris@1517
|
65 assert_response :success
|
Chris@1517
|
66 end
|
Chris@1517
|
67 assert m.reload.watched_by?(User.find(7))
|
Chris@1517
|
68 end
|
Chris@1517
|
69
|
Chris@1517
|
70 def test_watch_a_private_news_module_without_permission_should_fail
|
Chris@1517
|
71 @request.session[:user_id] = 7
|
Chris@1517
|
72 assert_not_nil m = Project.find(2).enabled_module('news')
|
Chris@1517
|
73
|
Chris@1517
|
74 assert_no_difference 'Watcher.count' do
|
Chris@1517
|
75 xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s
|
Chris@1517
|
76 assert_response 403
|
Chris@1517
|
77 end
|
Chris@1517
|
78 end
|
Chris@1517
|
79
|
Chris@0
|
80 def test_watch_should_be_denied_without_permission
|
Chris@0
|
81 Role.find(2).remove_permission! :view_issues
|
Chris@0
|
82 @request.session[:user_id] = 3
|
Chris@0
|
83 assert_no_difference('Watcher.count') do
|
Chris@0
|
84 xhr :post, :watch, :object_type => 'issue', :object_id => '1'
|
Chris@0
|
85 assert_response 403
|
Chris@0
|
86 end
|
Chris@0
|
87 end
|
Chris@909
|
88
|
Chris@1115
|
89 def test_watch_invalid_class_should_respond_with_404
|
Chris@1115
|
90 @request.session[:user_id] = 3
|
Chris@1115
|
91 assert_no_difference('Watcher.count') do
|
Chris@1115
|
92 xhr :post, :watch, :object_type => 'foo', :object_id => '1'
|
Chris@1115
|
93 assert_response 404
|
Chris@1115
|
94 end
|
Chris@1115
|
95 end
|
Chris@1115
|
96
|
Chris@1115
|
97 def test_watch_invalid_object_should_respond_with_404
|
Chris@1115
|
98 @request.session[:user_id] = 3
|
Chris@1115
|
99 assert_no_difference('Watcher.count') do
|
Chris@1115
|
100 xhr :post, :watch, :object_type => 'issue', :object_id => '999'
|
Chris@1115
|
101 assert_response 404
|
Chris@1115
|
102 end
|
Chris@1115
|
103 end
|
Chris@1115
|
104
|
Chris@0
|
105 def test_unwatch
|
Chris@0
|
106 @request.session[:user_id] = 3
|
Chris@0
|
107 assert_difference('Watcher.count', -1) do
|
Chris@1464
|
108 xhr :delete, :unwatch, :object_type => 'issue', :object_id => '2'
|
Chris@0
|
109 assert_response :success
|
Chris@1115
|
110 assert_include '$(".issue-2-watcher")', response.body
|
Chris@0
|
111 end
|
Chris@0
|
112 assert !Issue.find(1).watched_by?(User.find(3))
|
Chris@0
|
113 end
|
Chris@0
|
114
|
Chris@1464
|
115 def test_unwatch_a_collection_with_multiple_objects
|
Chris@1464
|
116 @request.session[:user_id] = 3
|
Chris@1464
|
117 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
|
Chris@1464
|
118 Watcher.create!(:user_id => 3, :watchable => Issue.find(3))
|
Chris@1464
|
119
|
Chris@1464
|
120 assert_difference('Watcher.count', -2) do
|
Chris@1464
|
121 xhr :delete, :unwatch, :object_type => 'issue', :object_id => ['1', '3']
|
Chris@1464
|
122 assert_response :success
|
Chris@1464
|
123 assert_include '$(".issue-bulk-watcher")', response.body
|
Chris@1464
|
124 end
|
Chris@1464
|
125 assert !Issue.find(1).watched_by?(User.find(3))
|
Chris@1464
|
126 assert !Issue.find(3).watched_by?(User.find(3))
|
Chris@1464
|
127 end
|
Chris@1464
|
128
|
Chris@1115
|
129 def test_new
|
Chris@1115
|
130 @request.session[:user_id] = 2
|
Chris@1115
|
131 xhr :get, :new, :object_type => 'issue', :object_id => '2'
|
Chris@1115
|
132 assert_response :success
|
Chris@1115
|
133 assert_match /ajax-modal/, response.body
|
Chris@1115
|
134 end
|
Chris@1115
|
135
|
Chris@1464
|
136 def test_new_for_new_record_with_project_id
|
Chris@1115
|
137 @request.session[:user_id] = 2
|
Chris@1115
|
138 xhr :get, :new, :project_id => 1
|
Chris@1115
|
139 assert_response :success
|
Chris@1115
|
140 assert_equal Project.find(1), assigns(:project)
|
Chris@1115
|
141 assert_match /ajax-modal/, response.body
|
Chris@1115
|
142 end
|
Chris@1115
|
143
|
Chris@1464
|
144 def test_new_for_new_record_with_project_identifier
|
Chris@1115
|
145 @request.session[:user_id] = 2
|
Chris@1115
|
146 xhr :get, :new, :project_id => 'ecookbook'
|
Chris@1115
|
147 assert_response :success
|
Chris@1115
|
148 assert_equal Project.find(1), assigns(:project)
|
Chris@1115
|
149 assert_match /ajax-modal/, response.body
|
Chris@1115
|
150 end
|
Chris@1115
|
151
|
Chris@1115
|
152 def test_create
|
Chris@0
|
153 @request.session[:user_id] = 2
|
Chris@0
|
154 assert_difference('Watcher.count') do
|
Chris@1517
|
155 xhr :post, :create, :object_type => 'issue', :object_id => '2',
|
Chris@1517
|
156 :watcher => {:user_id => '4'}
|
Chris@0
|
157 assert_response :success
|
Chris@1115
|
158 assert_match /watchers/, response.body
|
Chris@1115
|
159 assert_match /ajax-modal/, response.body
|
Chris@0
|
160 end
|
Chris@0
|
161 assert Issue.find(2).watched_by?(User.find(4))
|
Chris@0
|
162 end
|
Chris@909
|
163
|
Chris@1115
|
164 def test_create_multiple
|
Chris@1115
|
165 @request.session[:user_id] = 2
|
Chris@1115
|
166 assert_difference('Watcher.count', 2) do
|
Chris@1517
|
167 xhr :post, :create, :object_type => 'issue', :object_id => '2',
|
Chris@1517
|
168 :watcher => {:user_ids => ['4', '7']}
|
Chris@1115
|
169 assert_response :success
|
Chris@1115
|
170 assert_match /watchers/, response.body
|
Chris@1115
|
171 assert_match /ajax-modal/, response.body
|
Chris@1115
|
172 end
|
Chris@1115
|
173 assert Issue.find(2).watched_by?(User.find(4))
|
Chris@1115
|
174 assert Issue.find(2).watched_by?(User.find(7))
|
Chris@1115
|
175 end
|
Chris@1115
|
176
|
Chris@1115
|
177 def test_autocomplete_on_watchable_creation
|
Chris@1464
|
178 @request.session[:user_id] = 2
|
Chris@1464
|
179 xhr :get, :autocomplete_for_user, :q => 'mi', :project_id => 'ecookbook'
|
Chris@1115
|
180 assert_response :success
|
Chris@1115
|
181 assert_select 'input', :count => 4
|
Chris@1115
|
182 assert_select 'input[name=?][value=1]', 'watcher[user_ids][]'
|
Chris@1115
|
183 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
|
Chris@1115
|
184 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
|
Chris@1115
|
185 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
|
Chris@1115
|
186 end
|
Chris@1115
|
187
|
Chris@1517
|
188 def test_search_non_member_on_create
|
Chris@1517
|
189 @request.session[:user_id] = 2
|
Chris@1517
|
190 project = Project.find_by_name("ecookbook")
|
Chris@1517
|
191 user = User.generate!(:firstname => 'issue15622')
|
Chris@1517
|
192 membership = user.membership(project)
|
Chris@1517
|
193 assert_nil membership
|
Chris@1517
|
194 xhr :get, :autocomplete_for_user, :q => 'issue15622', :project_id => 'ecookbook'
|
Chris@1517
|
195 assert_response :success
|
Chris@1517
|
196 assert_select 'input', :count => 1
|
Chris@1517
|
197 end
|
Chris@1517
|
198
|
Chris@1115
|
199 def test_autocomplete_on_watchable_update
|
Chris@1464
|
200 @request.session[:user_id] = 2
|
Chris@1517
|
201 xhr :get, :autocomplete_for_user, :q => 'mi', :object_id => '2',
|
Chris@1517
|
202 :object_type => 'issue', :project_id => 'ecookbook'
|
Chris@1115
|
203 assert_response :success
|
Chris@1115
|
204 assert_select 'input', :count => 3
|
Chris@1115
|
205 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
|
Chris@1115
|
206 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
|
Chris@1115
|
207 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
|
Chris@1517
|
208 end
|
Chris@1115
|
209
|
Chris@1517
|
210 def test_search_and_add_non_member_on_update
|
Chris@1517
|
211 @request.session[:user_id] = 2
|
Chris@1517
|
212 project = Project.find_by_name("ecookbook")
|
Chris@1517
|
213 user = User.generate!(:firstname => 'issue15622')
|
Chris@1517
|
214 membership = user.membership(project)
|
Chris@1517
|
215 assert_nil membership
|
Chris@1517
|
216 xhr :get, :autocomplete_for_user, :q => 'issue15622', :object_id => '2',
|
Chris@1517
|
217 :object_type => 'issue', :project_id => 'ecookbook'
|
Chris@1517
|
218 assert_response :success
|
Chris@1517
|
219 assert_select 'input', :count => 1
|
Chris@1517
|
220 assert_difference('Watcher.count', 1) do
|
Chris@1517
|
221 xhr :post, :create, :object_type => 'issue', :object_id => '2',
|
Chris@1517
|
222 :watcher => {:user_ids => ["#{user.id}"]}
|
Chris@1517
|
223 assert_response :success
|
Chris@1517
|
224 assert_match /watchers/, response.body
|
Chris@1517
|
225 assert_match /ajax-modal/, response.body
|
Chris@1517
|
226 end
|
Chris@1517
|
227 assert Issue.find(2).watched_by?(user)
|
Chris@1115
|
228 end
|
Chris@1115
|
229
|
Chris@1115
|
230 def test_append
|
Chris@1115
|
231 @request.session[:user_id] = 2
|
Chris@1115
|
232 assert_no_difference 'Watcher.count' do
|
Chris@1464
|
233 xhr :post, :append, :watcher => {:user_ids => ['4', '7']}, :project_id => 'ecookbook'
|
Chris@1115
|
234 assert_response :success
|
Chris@1115
|
235 assert_include 'watchers_inputs', response.body
|
Chris@1115
|
236 assert_include 'issue[watcher_user_ids][]', response.body
|
Chris@1115
|
237 end
|
Chris@1115
|
238 end
|
Chris@1115
|
239
|
Chris@1517
|
240 def test_append_without_user_should_render_nothing
|
Chris@1517
|
241 @request.session[:user_id] = 2
|
Chris@1517
|
242 xhr :post, :append, :project_id => 'ecookbook'
|
Chris@1517
|
243 assert_response :success
|
Chris@1517
|
244 assert response.body.blank?
|
Chris@1517
|
245 end
|
Chris@1517
|
246
|
Chris@0
|
247 def test_remove_watcher
|
Chris@0
|
248 @request.session[:user_id] = 2
|
Chris@0
|
249 assert_difference('Watcher.count', -1) do
|
Chris@1464
|
250 xhr :delete, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3'
|
Chris@0
|
251 assert_response :success
|
Chris@1115
|
252 assert_match /watchers/, response.body
|
Chris@0
|
253 end
|
Chris@0
|
254 assert !Issue.find(2).watched_by?(User.find(3))
|
Chris@0
|
255 end
|
Chris@0
|
256 end
|