Chris@0
|
1 # Redmine - project management software
|
Chris@1115
|
2 # Copyright (C) 2006-2012 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 require 'watchers_controller'
|
Chris@0
|
20
|
Chris@0
|
21 # Re-raise errors caught by the controller.
|
Chris@0
|
22 class WatchersController; def rescue_action(e) raise e end; end
|
Chris@0
|
23
|
Chris@0
|
24 class WatchersControllerTest < ActionController::TestCase
|
Chris@0
|
25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules,
|
Chris@0
|
26 :issues, :trackers, :projects_trackers, :issue_statuses, :enumerations, :watchers
|
Chris@909
|
27
|
Chris@0
|
28 def setup
|
Chris@0
|
29 @controller = WatchersController.new
|
Chris@0
|
30 @request = ActionController::TestRequest.new
|
Chris@0
|
31 @response = ActionController::TestResponse.new
|
Chris@0
|
32 User.current = nil
|
Chris@0
|
33 end
|
Chris@909
|
34
|
Chris@0
|
35 def test_watch
|
Chris@0
|
36 @request.session[:user_id] = 3
|
Chris@0
|
37 assert_difference('Watcher.count') do
|
Chris@0
|
38 xhr :post, :watch, :object_type => 'issue', :object_id => '1'
|
Chris@0
|
39 assert_response :success
|
Chris@1115
|
40 assert_include '$(".issue-1-watcher")', response.body
|
Chris@0
|
41 end
|
Chris@0
|
42 assert Issue.find(1).watched_by?(User.find(3))
|
Chris@0
|
43 end
|
Chris@909
|
44
|
Chris@0
|
45 def test_watch_should_be_denied_without_permission
|
Chris@0
|
46 Role.find(2).remove_permission! :view_issues
|
Chris@0
|
47 @request.session[:user_id] = 3
|
Chris@0
|
48 assert_no_difference('Watcher.count') do
|
Chris@0
|
49 xhr :post, :watch, :object_type => 'issue', :object_id => '1'
|
Chris@0
|
50 assert_response 403
|
Chris@0
|
51 end
|
Chris@0
|
52 end
|
Chris@909
|
53
|
Chris@1115
|
54 def test_watch_invalid_class_should_respond_with_404
|
Chris@1115
|
55 @request.session[:user_id] = 3
|
Chris@1115
|
56 assert_no_difference('Watcher.count') do
|
Chris@1115
|
57 xhr :post, :watch, :object_type => 'foo', :object_id => '1'
|
Chris@1115
|
58 assert_response 404
|
Chris@1115
|
59 end
|
Chris@1115
|
60 end
|
Chris@1115
|
61
|
Chris@1115
|
62 def test_watch_invalid_object_should_respond_with_404
|
Chris@1115
|
63 @request.session[:user_id] = 3
|
Chris@1115
|
64 assert_no_difference('Watcher.count') do
|
Chris@1115
|
65 xhr :post, :watch, :object_type => 'issue', :object_id => '999'
|
Chris@1115
|
66 assert_response 404
|
Chris@1115
|
67 end
|
Chris@1115
|
68 end
|
Chris@1115
|
69
|
Chris@0
|
70 def test_unwatch
|
Chris@0
|
71 @request.session[:user_id] = 3
|
Chris@0
|
72 assert_difference('Watcher.count', -1) do
|
Chris@0
|
73 xhr :post, :unwatch, :object_type => 'issue', :object_id => '2'
|
Chris@0
|
74 assert_response :success
|
Chris@1115
|
75 assert_include '$(".issue-2-watcher")', response.body
|
Chris@0
|
76 end
|
Chris@0
|
77 assert !Issue.find(1).watched_by?(User.find(3))
|
Chris@0
|
78 end
|
Chris@0
|
79
|
Chris@1115
|
80 def test_new
|
Chris@1115
|
81 @request.session[:user_id] = 2
|
Chris@1115
|
82 xhr :get, :new, :object_type => 'issue', :object_id => '2'
|
Chris@1115
|
83 assert_response :success
|
Chris@1115
|
84 assert_match /ajax-modal/, response.body
|
Chris@1115
|
85 end
|
Chris@1115
|
86
|
Chris@1115
|
87 def test_new_for_new_record_with_id
|
Chris@1115
|
88 @request.session[:user_id] = 2
|
Chris@1115
|
89 xhr :get, :new, :project_id => 1
|
Chris@1115
|
90 assert_response :success
|
Chris@1115
|
91 assert_equal Project.find(1), assigns(:project)
|
Chris@1115
|
92 assert_match /ajax-modal/, response.body
|
Chris@1115
|
93 end
|
Chris@1115
|
94
|
Chris@1115
|
95 def test_new_for_new_record_with_identifier
|
Chris@1115
|
96 @request.session[:user_id] = 2
|
Chris@1115
|
97 xhr :get, :new, :project_id => 'ecookbook'
|
Chris@1115
|
98 assert_response :success
|
Chris@1115
|
99 assert_equal Project.find(1), assigns(:project)
|
Chris@1115
|
100 assert_match /ajax-modal/, response.body
|
Chris@1115
|
101 end
|
Chris@1115
|
102
|
Chris@1115
|
103 def test_create
|
Chris@0
|
104 @request.session[:user_id] = 2
|
Chris@0
|
105 assert_difference('Watcher.count') do
|
Chris@1115
|
106 xhr :post, :create, :object_type => 'issue', :object_id => '2', :watcher => {:user_id => '4'}
|
Chris@0
|
107 assert_response :success
|
Chris@1115
|
108 assert_match /watchers/, response.body
|
Chris@1115
|
109 assert_match /ajax-modal/, response.body
|
Chris@0
|
110 end
|
Chris@0
|
111 assert Issue.find(2).watched_by?(User.find(4))
|
Chris@0
|
112 end
|
Chris@909
|
113
|
Chris@1115
|
114 def test_create_multiple
|
Chris@1115
|
115 @request.session[:user_id] = 2
|
Chris@1115
|
116 assert_difference('Watcher.count', 2) do
|
Chris@1115
|
117 xhr :post, :create, :object_type => 'issue', :object_id => '2', :watcher => {:user_ids => ['4', '7']}
|
Chris@1115
|
118 assert_response :success
|
Chris@1115
|
119 assert_match /watchers/, response.body
|
Chris@1115
|
120 assert_match /ajax-modal/, response.body
|
Chris@1115
|
121 end
|
Chris@1115
|
122 assert Issue.find(2).watched_by?(User.find(4))
|
Chris@1115
|
123 assert Issue.find(2).watched_by?(User.find(7))
|
Chris@1115
|
124 end
|
Chris@1115
|
125
|
Chris@1115
|
126 def test_autocomplete_on_watchable_creation
|
Chris@1115
|
127 xhr :get, :autocomplete_for_user, :q => 'mi'
|
Chris@1115
|
128 assert_response :success
|
Chris@1115
|
129 assert_select 'input', :count => 4
|
Chris@1115
|
130 assert_select 'input[name=?][value=1]', 'watcher[user_ids][]'
|
Chris@1115
|
131 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
|
Chris@1115
|
132 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
|
Chris@1115
|
133 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
|
Chris@1115
|
134 end
|
Chris@1115
|
135
|
Chris@1115
|
136 def test_autocomplete_on_watchable_update
|
Chris@1115
|
137 xhr :get, :autocomplete_for_user, :q => 'mi', :object_id => '2' , :object_type => 'issue'
|
Chris@1115
|
138 assert_response :success
|
Chris@1115
|
139 assert_select 'input', :count => 3
|
Chris@1115
|
140 assert_select 'input[name=?][value=2]', 'watcher[user_ids][]'
|
Chris@1115
|
141 assert_select 'input[name=?][value=8]', 'watcher[user_ids][]'
|
Chris@1115
|
142 assert_select 'input[name=?][value=9]', 'watcher[user_ids][]'
|
Chris@1115
|
143
|
Chris@1115
|
144 end
|
Chris@1115
|
145
|
Chris@1115
|
146 def test_append
|
Chris@1115
|
147 @request.session[:user_id] = 2
|
Chris@1115
|
148 assert_no_difference 'Watcher.count' do
|
Chris@1115
|
149 xhr :post, :append, :watcher => {:user_ids => ['4', '7']}
|
Chris@1115
|
150 assert_response :success
|
Chris@1115
|
151 assert_include 'watchers_inputs', response.body
|
Chris@1115
|
152 assert_include 'issue[watcher_user_ids][]', response.body
|
Chris@1115
|
153 end
|
Chris@1115
|
154 end
|
Chris@1115
|
155
|
Chris@0
|
156 def test_remove_watcher
|
Chris@0
|
157 @request.session[:user_id] = 2
|
Chris@0
|
158 assert_difference('Watcher.count', -1) do
|
Chris@0
|
159 xhr :post, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3'
|
Chris@0
|
160 assert_response :success
|
Chris@1115
|
161 assert_match /watchers/, response.body
|
Chris@0
|
162 end
|
Chris@0
|
163 assert !Issue.find(2).watched_by?(User.find(3))
|
Chris@0
|
164 end
|
Chris@0
|
165 end
|