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@0
|
59 def test_watch_should_be_denied_without_permission
|
Chris@0
|
60 Role.find(2).remove_permission! :view_issues
|
Chris@0
|
61 @request.session[:user_id] = 3
|
Chris@0
|
62 assert_no_difference('Watcher.count') do
|
Chris@0
|
63 xhr :post, :watch, :object_type => 'issue', :object_id => '1'
|
Chris@0
|
64 assert_response 403
|
Chris@0
|
65 end
|
Chris@0
|
66 end
|
Chris@909
|
67
|
Chris@1115
|
68 def test_watch_invalid_class_should_respond_with_404
|
Chris@1115
|
69 @request.session[:user_id] = 3
|
Chris@1115
|
70 assert_no_difference('Watcher.count') do
|
Chris@1115
|
71 xhr :post, :watch, :object_type => 'foo', :object_id => '1'
|
Chris@1115
|
72 assert_response 404
|
Chris@1115
|
73 end
|
Chris@1115
|
74 end
|
Chris@1115
|
75
|
Chris@1115
|
76 def test_watch_invalid_object_should_respond_with_404
|
Chris@1115
|
77 @request.session[:user_id] = 3
|
Chris@1115
|
78 assert_no_difference('Watcher.count') do
|
Chris@1115
|
79 xhr :post, :watch, :object_type => 'issue', :object_id => '999'
|
Chris@1115
|
80 assert_response 404
|
Chris@1115
|
81 end
|
Chris@1115
|
82 end
|
Chris@1115
|
83
|
Chris@0
|
84 def test_unwatch
|
Chris@0
|
85 @request.session[:user_id] = 3
|
Chris@0
|
86 assert_difference('Watcher.count', -1) do
|
Chris@1464
|
87 xhr :delete, :unwatch, :object_type => 'issue', :object_id => '2'
|
Chris@0
|
88 assert_response :success
|
Chris@1115
|
89 assert_include '$(".issue-2-watcher")', response.body
|
Chris@0
|
90 end
|
Chris@0
|
91 assert !Issue.find(1).watched_by?(User.find(3))
|
Chris@0
|
92 end
|
Chris@0
|
93
|
Chris@1464
|
94 def test_unwatch_a_collection_with_multiple_objects
|
Chris@1464
|
95 @request.session[:user_id] = 3
|
Chris@1464
|
96 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
|
Chris@1464
|
97 Watcher.create!(:user_id => 3, :watchable => Issue.find(3))
|
Chris@1464
|
98
|
Chris@1464
|
99 assert_difference('Watcher.count', -2) do
|
Chris@1464
|
100 xhr :delete, :unwatch, :object_type => 'issue', :object_id => ['1', '3']
|
Chris@1464
|
101 assert_response :success
|
Chris@1464
|
102 assert_include '$(".issue-bulk-watcher")', response.body
|
Chris@1464
|
103 end
|
Chris@1464
|
104 assert !Issue.find(1).watched_by?(User.find(3))
|
Chris@1464
|
105 assert !Issue.find(3).watched_by?(User.find(3))
|
Chris@1464
|
106 end
|
Chris@1464
|
107
|
Chris@1115
|
108 def test_new
|
Chris@1115
|
109 @request.session[:user_id] = 2
|
Chris@1115
|
110 xhr :get, :new, :object_type => 'issue', :object_id => '2'
|
Chris@1115
|
111 assert_response :success
|
Chris@1115
|
112 assert_match /ajax-modal/, response.body
|
Chris@1115
|
113 end
|
Chris@1115
|
114
|
Chris@1464
|
115 def test_new_for_new_record_with_project_id
|
Chris@1115
|
116 @request.session[:user_id] = 2
|
Chris@1115
|
117 xhr :get, :new, :project_id => 1
|
Chris@1115
|
118 assert_response :success
|
Chris@1115
|
119 assert_equal Project.find(1), assigns(:project)
|
Chris@1115
|
120 assert_match /ajax-modal/, response.body
|
Chris@1115
|
121 end
|
Chris@1115
|
122
|
Chris@1464
|
123 def test_new_for_new_record_with_project_identifier
|
Chris@1115
|
124 @request.session[:user_id] = 2
|
Chris@1115
|
125 xhr :get, :new, :project_id => 'ecookbook'
|
Chris@1115
|
126 assert_response :success
|
Chris@1115
|
127 assert_equal Project.find(1), assigns(:project)
|
Chris@1115
|
128 assert_match /ajax-modal/, response.body
|
Chris@1115
|
129 end
|
Chris@1115
|
130
|
Chris@1115
|
131 def test_create
|
Chris@0
|
132 @request.session[:user_id] = 2
|
Chris@0
|
133 assert_difference('Watcher.count') do
|
Chris@1115
|
134 xhr :post, :create, :object_type => 'issue', :object_id => '2', :watcher => {:user_id => '4'}
|
Chris@0
|
135 assert_response :success
|
Chris@1115
|
136 assert_match /watchers/, response.body
|
Chris@1115
|
137 assert_match /ajax-modal/, response.body
|
Chris@0
|
138 end
|
Chris@0
|
139 assert Issue.find(2).watched_by?(User.find(4))
|
Chris@0
|
140 end
|
Chris@909
|
141
|
Chris@1115
|
142 def test_create_multiple
|
Chris@1115
|
143 @request.session[:user_id] = 2
|
Chris@1115
|
144 assert_difference('Watcher.count', 2) do
|
Chris@1115
|
145 xhr :post, :create, :object_type => 'issue', :object_id => '2', :watcher => {:user_ids => ['4', '7']}
|
Chris@1115
|
146 assert_response :success
|
Chris@1115
|
147 assert_match /watchers/, response.body
|
Chris@1115
|
148 assert_match /ajax-modal/, response.body
|
Chris@1115
|
149 end
|
Chris@1115
|
150 assert Issue.find(2).watched_by?(User.find(4))
|
Chris@1115
|
151 assert Issue.find(2).watched_by?(User.find(7))
|
Chris@1115
|
152 end
|
Chris@1115
|
153
|
Chris@1115
|
154 def test_autocomplete_on_watchable_creation
|
Chris@1464
|
155 @request.session[:user_id] = 2
|
Chris@1464
|
156 xhr :get, :autocomplete_for_user, :q => 'mi', :project_id => 'ecookbook'
|
Chris@1115
|
157 assert_response :success
|
Chris@1115
|
158 assert_select 'input', :count => 4
|
Chris@1115
|
159 assert_select 'input[name=?][value=1]', 'watcher[user_ids][]'
|
Chris@1115
|
160 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
|
Chris@1115
|
161 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
|
Chris@1115
|
162 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
|
Chris@1115
|
163 end
|
Chris@1115
|
164
|
Chris@1115
|
165 def test_autocomplete_on_watchable_update
|
Chris@1464
|
166 @request.session[:user_id] = 2
|
Chris@1464
|
167 xhr :get, :autocomplete_for_user, :q => 'mi', :object_id => '2' , :object_type => 'issue', :project_id => 'ecookbook'
|
Chris@1115
|
168 assert_response :success
|
Chris@1115
|
169 assert_select 'input', :count => 3
|
Chris@1115
|
170 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
|
Chris@1115
|
171 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
|
Chris@1115
|
172 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
|
Chris@1115
|
173
|
Chris@1115
|
174 end
|
Chris@1115
|
175
|
Chris@1115
|
176 def test_append
|
Chris@1115
|
177 @request.session[:user_id] = 2
|
Chris@1115
|
178 assert_no_difference 'Watcher.count' do
|
Chris@1464
|
179 xhr :post, :append, :watcher => {:user_ids => ['4', '7']}, :project_id => 'ecookbook'
|
Chris@1115
|
180 assert_response :success
|
Chris@1115
|
181 assert_include 'watchers_inputs', response.body
|
Chris@1115
|
182 assert_include 'issue[watcher_user_ids][]', response.body
|
Chris@1115
|
183 end
|
Chris@1115
|
184 end
|
Chris@1115
|
185
|
Chris@0
|
186 def test_remove_watcher
|
Chris@0
|
187 @request.session[:user_id] = 2
|
Chris@0
|
188 assert_difference('Watcher.count', -1) do
|
Chris@1464
|
189 xhr :delete, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3'
|
Chris@0
|
190 assert_response :success
|
Chris@1115
|
191 assert_match /watchers/, response.body
|
Chris@0
|
192 end
|
Chris@0
|
193 assert !Issue.find(2).watched_by?(User.find(3))
|
Chris@0
|
194 end
|
Chris@0
|
195 end
|