@@ -1,131 +1,133 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 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. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
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 | class WatchersController < ApplicationController |
|
18 | class WatchersController < ApplicationController | |
19 | before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] |
|
19 | before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] | |
20 |
|
20 | |||
21 | def watch |
|
21 | def watch | |
22 | set_watcher(@watchables, User.current, true) |
|
22 | set_watcher(@watchables, User.current, true) | |
23 | end |
|
23 | end | |
24 |
|
24 | |||
25 | def unwatch |
|
25 | def unwatch | |
26 | set_watcher(@watchables, User.current, false) |
|
26 | set_watcher(@watchables, User.current, false) | |
27 | end |
|
27 | end | |
28 |
|
28 | |||
29 | before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user] |
|
29 | before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user] | |
30 | accept_api_auth :create, :destroy |
|
30 | accept_api_auth :create, :destroy | |
31 |
|
31 | |||
32 | def new |
|
32 | def new | |
33 | @users = users_for_new_watcher |
|
33 | @users = users_for_new_watcher | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 | def create |
|
36 | def create | |
37 | user_ids = [] |
|
37 | user_ids = [] | |
38 | if params[:watcher].is_a?(Hash) |
|
38 | if params[:watcher].is_a?(Hash) | |
39 | user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id]) |
|
39 | user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id]) | |
40 | else |
|
40 | else | |
41 | user_ids << params[:user_id] |
|
41 | user_ids << params[:user_id] | |
42 | end |
|
42 | end | |
43 | users = User.active.visible.where(:id => user_ids.flatten.compact.uniq) |
|
43 | users = User.active.visible.where(:id => user_ids.flatten.compact.uniq) | |
44 | users.each do |user| |
|
44 | users.each do |user| | |
45 | Watcher.create(:watchable => @watched, :user => user) |
|
45 | Watcher.create(:watchable => @watched, :user => user) | |
46 | end |
|
46 | end | |
47 | respond_to do |format| |
|
47 | respond_to do |format| | |
48 | format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} |
|
48 | format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} | |
49 | format.js { @users = users_for_new_watcher } |
|
49 | format.js { @users = users_for_new_watcher } | |
50 | format.api { render_api_ok } |
|
50 | format.api { render_api_ok } | |
51 | end |
|
51 | end | |
52 | end |
|
52 | end | |
53 |
|
53 | |||
54 | def append |
|
54 | def append | |
55 | if params[:watcher].is_a?(Hash) |
|
55 | if params[:watcher].is_a?(Hash) | |
56 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] |
|
56 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] | |
57 | @users = User.active.visible.where(:id => user_ids).to_a |
|
57 | @users = User.active.visible.where(:id => user_ids).to_a | |
58 | end |
|
58 | end | |
59 | if @users.blank? |
|
59 | if @users.blank? | |
60 | render :nothing => true |
|
60 | render :nothing => true | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def destroy |
|
64 | def destroy | |
65 |
@watched.set_watcher(User |
|
65 | @watched.set_watcher(User.find(params[:user_id]), false) | |
66 | respond_to do |format| |
|
66 | respond_to do |format| | |
67 | format.html { redirect_to :back } |
|
67 | format.html { redirect_to :back } | |
68 | format.js |
|
68 | format.js | |
69 | format.api { render_api_ok } |
|
69 | format.api { render_api_ok } | |
70 | end |
|
70 | end | |
|
71 | rescue ActiveRecord::RecordNotFound | |||
|
72 | render_404 | |||
71 | end |
|
73 | end | |
72 |
|
74 | |||
73 | def autocomplete_for_user |
|
75 | def autocomplete_for_user | |
74 | @users = users_for_new_watcher |
|
76 | @users = users_for_new_watcher | |
75 | render :layout => false |
|
77 | render :layout => false | |
76 | end |
|
78 | end | |
77 |
|
79 | |||
78 | private |
|
80 | private | |
79 |
|
81 | |||
80 | def find_project |
|
82 | def find_project | |
81 | if params[:object_type] && params[:object_id] |
|
83 | if params[:object_type] && params[:object_id] | |
82 | klass = Object.const_get(params[:object_type].camelcase) |
|
84 | klass = Object.const_get(params[:object_type].camelcase) | |
83 | return false unless klass.respond_to?('watched_by') |
|
85 | return false unless klass.respond_to?('watched_by') | |
84 | @watched = klass.find(params[:object_id]) |
|
86 | @watched = klass.find(params[:object_id]) | |
85 | @project = @watched.project |
|
87 | @project = @watched.project | |
86 | elsif params[:project_id] |
|
88 | elsif params[:project_id] | |
87 | @project = Project.visible.find_by_param(params[:project_id]) |
|
89 | @project = Project.visible.find_by_param(params[:project_id]) | |
88 | end |
|
90 | end | |
89 | rescue |
|
91 | rescue | |
90 | render_404 |
|
92 | render_404 | |
91 | end |
|
93 | end | |
92 |
|
94 | |||
93 | def find_watchables |
|
95 | def find_watchables | |
94 | klass = Object.const_get(params[:object_type].camelcase) rescue nil |
|
96 | klass = Object.const_get(params[:object_type].camelcase) rescue nil | |
95 | if klass && klass.respond_to?('watched_by') |
|
97 | if klass && klass.respond_to?('watched_by') | |
96 | @watchables = klass.where(:id => Array.wrap(params[:object_id])).to_a |
|
98 | @watchables = klass.where(:id => Array.wrap(params[:object_id])).to_a | |
97 | raise Unauthorized if @watchables.any? {|w| |
|
99 | raise Unauthorized if @watchables.any? {|w| | |
98 | if w.respond_to?(:visible?) |
|
100 | if w.respond_to?(:visible?) | |
99 | !w.visible? |
|
101 | !w.visible? | |
100 | elsif w.respond_to?(:project) && w.project |
|
102 | elsif w.respond_to?(:project) && w.project | |
101 | !w.project.visible? |
|
103 | !w.project.visible? | |
102 | end |
|
104 | end | |
103 | } |
|
105 | } | |
104 | end |
|
106 | end | |
105 | render_404 unless @watchables.present? |
|
107 | render_404 unless @watchables.present? | |
106 | end |
|
108 | end | |
107 |
|
109 | |||
108 | def set_watcher(watchables, user, watching) |
|
110 | def set_watcher(watchables, user, watching) | |
109 | watchables.each do |watchable| |
|
111 | watchables.each do |watchable| | |
110 | watchable.set_watcher(user, watching) |
|
112 | watchable.set_watcher(user, watching) | |
111 | end |
|
113 | end | |
112 | respond_to do |format| |
|
114 | respond_to do |format| | |
113 | format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} |
|
115 | format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} | |
114 | format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} } |
|
116 | format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} } | |
115 | end |
|
117 | end | |
116 | end |
|
118 | end | |
117 |
|
119 | |||
118 | def users_for_new_watcher |
|
120 | def users_for_new_watcher | |
119 | scope = nil |
|
121 | scope = nil | |
120 | if params[:q].blank? && @project.present? |
|
122 | if params[:q].blank? && @project.present? | |
121 | scope = @project.users |
|
123 | scope = @project.users | |
122 | else |
|
124 | else | |
123 | scope = User.all.limit(100) |
|
125 | scope = User.all.limit(100) | |
124 | end |
|
126 | end | |
125 | users = scope.active.visible.sorted.like(params[:q]).to_a |
|
127 | users = scope.active.visible.sorted.like(params[:q]).to_a | |
126 | if @watched |
|
128 | if @watched | |
127 | users -= @watched.watcher_users |
|
129 | users -= @watched.watcher_users | |
128 | end |
|
130 | end | |
129 | users |
|
131 | users | |
130 | end |
|
132 | end | |
131 | end |
|
133 | end |
@@ -1,271 +1,293 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 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. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
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 |
|
19 | |||
20 | class WatchersControllerTest < ActionController::TestCase |
|
20 | class WatchersControllerTest < ActionController::TestCase | |
21 | fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, |
|
21 | fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, | |
22 | :issues, :trackers, :projects_trackers, :issue_statuses, :enumerations, :watchers |
|
22 | :issues, :trackers, :projects_trackers, :issue_statuses, :enumerations, :watchers | |
23 |
|
23 | |||
24 | def setup |
|
24 | def setup | |
25 | User.current = nil |
|
25 | User.current = nil | |
26 | end |
|
26 | end | |
27 |
|
27 | |||
28 | def test_watch_a_single_object |
|
28 | def test_watch_a_single_object | |
29 | @request.session[:user_id] = 3 |
|
29 | @request.session[:user_id] = 3 | |
30 | assert_difference('Watcher.count') do |
|
30 | assert_difference('Watcher.count') do | |
31 | xhr :post, :watch, :object_type => 'issue', :object_id => '1' |
|
31 | xhr :post, :watch, :object_type => 'issue', :object_id => '1' | |
32 | assert_response :success |
|
32 | assert_response :success | |
33 | assert_include '$(".issue-1-watcher")', response.body |
|
33 | assert_include '$(".issue-1-watcher")', response.body | |
34 | end |
|
34 | end | |
35 | assert Issue.find(1).watched_by?(User.find(3)) |
|
35 | assert Issue.find(1).watched_by?(User.find(3)) | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | def test_watch_a_collection_with_a_single_object |
|
38 | def test_watch_a_collection_with_a_single_object | |
39 | @request.session[:user_id] = 3 |
|
39 | @request.session[:user_id] = 3 | |
40 | assert_difference('Watcher.count') do |
|
40 | assert_difference('Watcher.count') do | |
41 | xhr :post, :watch, :object_type => 'issue', :object_id => ['1'] |
|
41 | xhr :post, :watch, :object_type => 'issue', :object_id => ['1'] | |
42 | assert_response :success |
|
42 | assert_response :success | |
43 | assert_include '$(".issue-1-watcher")', response.body |
|
43 | assert_include '$(".issue-1-watcher")', response.body | |
44 | end |
|
44 | end | |
45 | assert Issue.find(1).watched_by?(User.find(3)) |
|
45 | assert Issue.find(1).watched_by?(User.find(3)) | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | def test_watch_a_collection_with_multiple_objects |
|
48 | def test_watch_a_collection_with_multiple_objects | |
49 | @request.session[:user_id] = 3 |
|
49 | @request.session[:user_id] = 3 | |
50 | assert_difference('Watcher.count', 2) do |
|
50 | assert_difference('Watcher.count', 2) do | |
51 | xhr :post, :watch, :object_type => 'issue', :object_id => ['1', '3'] |
|
51 | xhr :post, :watch, :object_type => 'issue', :object_id => ['1', '3'] | |
52 | assert_response :success |
|
52 | assert_response :success | |
53 | assert_include '$(".issue-bulk-watcher")', response.body |
|
53 | assert_include '$(".issue-bulk-watcher")', response.body | |
54 | end |
|
54 | end | |
55 | assert Issue.find(1).watched_by?(User.find(3)) |
|
55 | assert Issue.find(1).watched_by?(User.find(3)) | |
56 | assert Issue.find(3).watched_by?(User.find(3)) |
|
56 | assert Issue.find(3).watched_by?(User.find(3)) | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | def test_watch_a_news_module_should_add_watcher |
|
59 | def test_watch_a_news_module_should_add_watcher | |
60 | @request.session[:user_id] = 7 |
|
60 | @request.session[:user_id] = 7 | |
61 | assert_not_nil m = Project.find(1).enabled_module('news') |
|
61 | assert_not_nil m = Project.find(1).enabled_module('news') | |
62 |
|
62 | |||
63 | assert_difference 'Watcher.count' do |
|
63 | assert_difference 'Watcher.count' do | |
64 | xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s |
|
64 | xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s | |
65 | assert_response :success |
|
65 | assert_response :success | |
66 | end |
|
66 | end | |
67 | assert m.reload.watched_by?(User.find(7)) |
|
67 | assert m.reload.watched_by?(User.find(7)) | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | def test_watch_a_private_news_module_without_permission_should_fail |
|
70 | def test_watch_a_private_news_module_without_permission_should_fail | |
71 | @request.session[:user_id] = 7 |
|
71 | @request.session[:user_id] = 7 | |
72 | assert_not_nil m = Project.find(2).enabled_module('news') |
|
72 | assert_not_nil m = Project.find(2).enabled_module('news') | |
73 |
|
73 | |||
74 | assert_no_difference 'Watcher.count' do |
|
74 | assert_no_difference 'Watcher.count' do | |
75 | xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s |
|
75 | xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s | |
76 | assert_response 403 |
|
76 | assert_response 403 | |
77 | end |
|
77 | end | |
78 | end |
|
78 | end | |
79 |
|
79 | |||
80 | def test_watch_should_be_denied_without_permission |
|
80 | def test_watch_should_be_denied_without_permission | |
81 | Role.find(2).remove_permission! :view_issues |
|
81 | Role.find(2).remove_permission! :view_issues | |
82 | @request.session[:user_id] = 3 |
|
82 | @request.session[:user_id] = 3 | |
83 | assert_no_difference('Watcher.count') do |
|
83 | assert_no_difference('Watcher.count') do | |
84 | xhr :post, :watch, :object_type => 'issue', :object_id => '1' |
|
84 | xhr :post, :watch, :object_type => 'issue', :object_id => '1' | |
85 | assert_response 403 |
|
85 | assert_response 403 | |
86 | end |
|
86 | end | |
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | def test_watch_invalid_class_should_respond_with_404 |
|
89 | def test_watch_invalid_class_should_respond_with_404 | |
90 | @request.session[:user_id] = 3 |
|
90 | @request.session[:user_id] = 3 | |
91 | assert_no_difference('Watcher.count') do |
|
91 | assert_no_difference('Watcher.count') do | |
92 | xhr :post, :watch, :object_type => 'foo', :object_id => '1' |
|
92 | xhr :post, :watch, :object_type => 'foo', :object_id => '1' | |
93 | assert_response 404 |
|
93 | assert_response 404 | |
94 | end |
|
94 | end | |
95 | end |
|
95 | end | |
96 |
|
96 | |||
97 | def test_watch_invalid_object_should_respond_with_404 |
|
97 | def test_watch_invalid_object_should_respond_with_404 | |
98 | @request.session[:user_id] = 3 |
|
98 | @request.session[:user_id] = 3 | |
99 | assert_no_difference('Watcher.count') do |
|
99 | assert_no_difference('Watcher.count') do | |
100 | xhr :post, :watch, :object_type => 'issue', :object_id => '999' |
|
100 | xhr :post, :watch, :object_type => 'issue', :object_id => '999' | |
101 | assert_response 404 |
|
101 | assert_response 404 | |
102 | end |
|
102 | end | |
103 | end |
|
103 | end | |
104 |
|
104 | |||
105 | def test_unwatch |
|
105 | def test_unwatch | |
106 | @request.session[:user_id] = 3 |
|
106 | @request.session[:user_id] = 3 | |
107 | assert_difference('Watcher.count', -1) do |
|
107 | assert_difference('Watcher.count', -1) do | |
108 | xhr :delete, :unwatch, :object_type => 'issue', :object_id => '2' |
|
108 | xhr :delete, :unwatch, :object_type => 'issue', :object_id => '2' | |
109 | assert_response :success |
|
109 | assert_response :success | |
110 | assert_include '$(".issue-2-watcher")', response.body |
|
110 | assert_include '$(".issue-2-watcher")', response.body | |
111 | end |
|
111 | end | |
112 | assert !Issue.find(1).watched_by?(User.find(3)) |
|
112 | assert !Issue.find(1).watched_by?(User.find(3)) | |
113 | end |
|
113 | end | |
114 |
|
114 | |||
115 | def test_unwatch_a_collection_with_multiple_objects |
|
115 | def test_unwatch_a_collection_with_multiple_objects | |
116 | @request.session[:user_id] = 3 |
|
116 | @request.session[:user_id] = 3 | |
117 | Watcher.create!(:user_id => 3, :watchable => Issue.find(1)) |
|
117 | Watcher.create!(:user_id => 3, :watchable => Issue.find(1)) | |
118 | Watcher.create!(:user_id => 3, :watchable => Issue.find(3)) |
|
118 | Watcher.create!(:user_id => 3, :watchable => Issue.find(3)) | |
119 |
|
119 | |||
120 | assert_difference('Watcher.count', -2) do |
|
120 | assert_difference('Watcher.count', -2) do | |
121 | xhr :delete, :unwatch, :object_type => 'issue', :object_id => ['1', '3'] |
|
121 | xhr :delete, :unwatch, :object_type => 'issue', :object_id => ['1', '3'] | |
122 | assert_response :success |
|
122 | assert_response :success | |
123 | assert_include '$(".issue-bulk-watcher")', response.body |
|
123 | assert_include '$(".issue-bulk-watcher")', response.body | |
124 | end |
|
124 | end | |
125 | assert !Issue.find(1).watched_by?(User.find(3)) |
|
125 | assert !Issue.find(1).watched_by?(User.find(3)) | |
126 | assert !Issue.find(3).watched_by?(User.find(3)) |
|
126 | assert !Issue.find(3).watched_by?(User.find(3)) | |
127 | end |
|
127 | end | |
128 |
|
128 | |||
129 | def test_new |
|
129 | def test_new | |
130 | @request.session[:user_id] = 2 |
|
130 | @request.session[:user_id] = 2 | |
131 | xhr :get, :new, :object_type => 'issue', :object_id => '2' |
|
131 | xhr :get, :new, :object_type => 'issue', :object_id => '2' | |
132 | assert_response :success |
|
132 | assert_response :success | |
133 | assert_match /ajax-modal/, response.body |
|
133 | assert_match /ajax-modal/, response.body | |
134 | end |
|
134 | end | |
135 |
|
135 | |||
136 | def test_new_for_new_record_with_project_id |
|
136 | def test_new_for_new_record_with_project_id | |
137 | @request.session[:user_id] = 2 |
|
137 | @request.session[:user_id] = 2 | |
138 | xhr :get, :new, :project_id => 1 |
|
138 | xhr :get, :new, :project_id => 1 | |
139 | assert_response :success |
|
139 | assert_response :success | |
140 | assert_equal Project.find(1), assigns(:project) |
|
140 | assert_equal Project.find(1), assigns(:project) | |
141 | assert_match /ajax-modal/, response.body |
|
141 | assert_match /ajax-modal/, response.body | |
142 | end |
|
142 | end | |
143 |
|
143 | |||
144 | def test_new_for_new_record_with_project_identifier |
|
144 | def test_new_for_new_record_with_project_identifier | |
145 | @request.session[:user_id] = 2 |
|
145 | @request.session[:user_id] = 2 | |
146 | xhr :get, :new, :project_id => 'ecookbook' |
|
146 | xhr :get, :new, :project_id => 'ecookbook' | |
147 | assert_response :success |
|
147 | assert_response :success | |
148 | assert_equal Project.find(1), assigns(:project) |
|
148 | assert_equal Project.find(1), assigns(:project) | |
149 | assert_match /ajax-modal/, response.body |
|
149 | assert_match /ajax-modal/, response.body | |
150 | end |
|
150 | end | |
151 |
|
151 | |||
152 | def test_create |
|
152 | def test_create | |
153 | @request.session[:user_id] = 2 |
|
153 | @request.session[:user_id] = 2 | |
154 | assert_difference('Watcher.count') do |
|
154 | assert_difference('Watcher.count') do | |
155 | xhr :post, :create, :object_type => 'issue', :object_id => '2', |
|
155 | xhr :post, :create, :object_type => 'issue', :object_id => '2', | |
156 | :watcher => {:user_id => '4'} |
|
156 | :watcher => {:user_id => '4'} | |
157 | assert_response :success |
|
157 | assert_response :success | |
158 | assert_match /watchers/, response.body |
|
158 | assert_match /watchers/, response.body | |
159 | assert_match /ajax-modal/, response.body |
|
159 | assert_match /ajax-modal/, response.body | |
160 | end |
|
160 | end | |
161 | assert Issue.find(2).watched_by?(User.find(4)) |
|
161 | assert Issue.find(2).watched_by?(User.find(4)) | |
162 | end |
|
162 | end | |
163 |
|
163 | |||
164 | def test_create_multiple |
|
164 | def test_create_multiple | |
165 | @request.session[:user_id] = 2 |
|
165 | @request.session[:user_id] = 2 | |
166 | assert_difference('Watcher.count', 2) do |
|
166 | assert_difference('Watcher.count', 2) do | |
167 | xhr :post, :create, :object_type => 'issue', :object_id => '2', |
|
167 | xhr :post, :create, :object_type => 'issue', :object_id => '2', | |
168 | :watcher => {:user_ids => ['4', '7']} |
|
168 | :watcher => {:user_ids => ['4', '7']} | |
169 | assert_response :success |
|
169 | assert_response :success | |
170 | assert_match /watchers/, response.body |
|
170 | assert_match /watchers/, response.body | |
171 | assert_match /ajax-modal/, response.body |
|
171 | assert_match /ajax-modal/, response.body | |
172 | end |
|
172 | end | |
173 | assert Issue.find(2).watched_by?(User.find(4)) |
|
173 | assert Issue.find(2).watched_by?(User.find(4)) | |
174 | assert Issue.find(2).watched_by?(User.find(7)) |
|
174 | assert Issue.find(2).watched_by?(User.find(7)) | |
175 | end |
|
175 | end | |
176 |
|
176 | |||
177 | def test_autocomplete_on_watchable_creation |
|
177 | def test_autocomplete_on_watchable_creation | |
178 | @request.session[:user_id] = 2 |
|
178 | @request.session[:user_id] = 2 | |
179 | xhr :get, :autocomplete_for_user, :q => 'mi', :project_id => 'ecookbook' |
|
179 | xhr :get, :autocomplete_for_user, :q => 'mi', :project_id => 'ecookbook' | |
180 | assert_response :success |
|
180 | assert_response :success | |
181 | assert_select 'input', :count => 4 |
|
181 | assert_select 'input', :count => 4 | |
182 | assert_select 'input[name=?][value="1"]', 'watcher[user_ids][]' |
|
182 | assert_select 'input[name=?][value="1"]', 'watcher[user_ids][]' | |
183 | assert_select 'input[name=?][value="2"]', 'watcher[user_ids][]' |
|
183 | assert_select 'input[name=?][value="2"]', 'watcher[user_ids][]' | |
184 | assert_select 'input[name=?][value="8"]', 'watcher[user_ids][]' |
|
184 | assert_select 'input[name=?][value="8"]', 'watcher[user_ids][]' | |
185 | assert_select 'input[name=?][value="9"]', 'watcher[user_ids][]' |
|
185 | assert_select 'input[name=?][value="9"]', 'watcher[user_ids][]' | |
186 | end |
|
186 | end | |
187 |
|
187 | |||
188 | def test_search_non_member_on_create |
|
188 | def test_search_non_member_on_create | |
189 | @request.session[:user_id] = 2 |
|
189 | @request.session[:user_id] = 2 | |
190 | project = Project.find_by_name("ecookbook") |
|
190 | project = Project.find_by_name("ecookbook") | |
191 | user = User.generate!(:firstname => 'issue15622') |
|
191 | user = User.generate!(:firstname => 'issue15622') | |
192 | membership = user.membership(project) |
|
192 | membership = user.membership(project) | |
193 | assert_nil membership |
|
193 | assert_nil membership | |
194 | xhr :get, :autocomplete_for_user, :q => 'issue15622', :project_id => 'ecookbook' |
|
194 | xhr :get, :autocomplete_for_user, :q => 'issue15622', :project_id => 'ecookbook' | |
195 | assert_response :success |
|
195 | assert_response :success | |
196 | assert_select 'input', :count => 1 |
|
196 | assert_select 'input', :count => 1 | |
197 | end |
|
197 | end | |
198 |
|
198 | |||
199 | def test_autocomplete_on_watchable_update |
|
199 | def test_autocomplete_on_watchable_update | |
200 | @request.session[:user_id] = 2 |
|
200 | @request.session[:user_id] = 2 | |
201 | xhr :get, :autocomplete_for_user, :q => 'mi', :object_id => '2', |
|
201 | xhr :get, :autocomplete_for_user, :q => 'mi', :object_id => '2', | |
202 | :object_type => 'issue', :project_id => 'ecookbook' |
|
202 | :object_type => 'issue', :project_id => 'ecookbook' | |
203 | assert_response :success |
|
203 | assert_response :success | |
204 | assert_select 'input', :count => 3 |
|
204 | assert_select 'input', :count => 3 | |
205 | assert_select 'input[name=?][value="2"]', 'watcher[user_ids][]' |
|
205 | assert_select 'input[name=?][value="2"]', 'watcher[user_ids][]' | |
206 | assert_select 'input[name=?][value="8"]', 'watcher[user_ids][]' |
|
206 | assert_select 'input[name=?][value="8"]', 'watcher[user_ids][]' | |
207 | assert_select 'input[name=?][value="9"]', 'watcher[user_ids][]' |
|
207 | assert_select 'input[name=?][value="9"]', 'watcher[user_ids][]' | |
208 | end |
|
208 | end | |
209 |
|
209 | |||
210 | def test_search_and_add_non_member_on_update |
|
210 | def test_search_and_add_non_member_on_update | |
211 | @request.session[:user_id] = 2 |
|
211 | @request.session[:user_id] = 2 | |
212 | project = Project.find_by_name("ecookbook") |
|
212 | project = Project.find_by_name("ecookbook") | |
213 | user = User.generate!(:firstname => 'issue15622') |
|
213 | user = User.generate!(:firstname => 'issue15622') | |
214 | membership = user.membership(project) |
|
214 | membership = user.membership(project) | |
215 | assert_nil membership |
|
215 | assert_nil membership | |
216 | xhr :get, :autocomplete_for_user, :q => 'issue15622', :object_id => '2', |
|
216 | xhr :get, :autocomplete_for_user, :q => 'issue15622', :object_id => '2', | |
217 | :object_type => 'issue', :project_id => 'ecookbook' |
|
217 | :object_type => 'issue', :project_id => 'ecookbook' | |
218 | assert_response :success |
|
218 | assert_response :success | |
219 | assert_select 'input', :count => 1 |
|
219 | assert_select 'input', :count => 1 | |
220 | assert_difference('Watcher.count', 1) do |
|
220 | assert_difference('Watcher.count', 1) do | |
221 | xhr :post, :create, :object_type => 'issue', :object_id => '2', |
|
221 | xhr :post, :create, :object_type => 'issue', :object_id => '2', | |
222 | :watcher => {:user_ids => ["#{user.id}"]} |
|
222 | :watcher => {:user_ids => ["#{user.id}"]} | |
223 | assert_response :success |
|
223 | assert_response :success | |
224 | assert_match /watchers/, response.body |
|
224 | assert_match /watchers/, response.body | |
225 | assert_match /ajax-modal/, response.body |
|
225 | assert_match /ajax-modal/, response.body | |
226 | end |
|
226 | end | |
227 | assert Issue.find(2).watched_by?(user) |
|
227 | assert Issue.find(2).watched_by?(user) | |
228 | end |
|
228 | end | |
229 |
|
229 | |||
230 | def test_autocomplete_for_user_should_return_visible_users |
|
230 | def test_autocomplete_for_user_should_return_visible_users | |
231 | Role.update_all :users_visibility => 'members_of_visible_projects' |
|
231 | Role.update_all :users_visibility => 'members_of_visible_projects' | |
232 |
|
232 | |||
233 | hidden = User.generate!(:lastname => 'autocomplete') |
|
233 | hidden = User.generate!(:lastname => 'autocomplete') | |
234 | visible = User.generate!(:lastname => 'autocomplete') |
|
234 | visible = User.generate!(:lastname => 'autocomplete') | |
235 | User.add_to_project(visible, Project.find(1)) |
|
235 | User.add_to_project(visible, Project.find(1)) | |
236 |
|
236 | |||
237 | @request.session[:user_id] = 2 |
|
237 | @request.session[:user_id] = 2 | |
238 | xhr :get, :autocomplete_for_user, :q => 'autocomp', :project_id => 'ecookbook' |
|
238 | xhr :get, :autocomplete_for_user, :q => 'autocomp', :project_id => 'ecookbook' | |
239 | assert_response :success |
|
239 | assert_response :success | |
240 |
|
240 | |||
241 | assert_include visible, assigns(:users) |
|
241 | assert_include visible, assigns(:users) | |
242 | assert_not_include hidden, assigns(:users) |
|
242 | assert_not_include hidden, assigns(:users) | |
243 | end |
|
243 | end | |
244 |
|
244 | |||
245 | def test_append |
|
245 | def test_append | |
246 | @request.session[:user_id] = 2 |
|
246 | @request.session[:user_id] = 2 | |
247 | assert_no_difference 'Watcher.count' do |
|
247 | assert_no_difference 'Watcher.count' do | |
248 | xhr :post, :append, :watcher => {:user_ids => ['4', '7']}, :project_id => 'ecookbook' |
|
248 | xhr :post, :append, :watcher => {:user_ids => ['4', '7']}, :project_id => 'ecookbook' | |
249 | assert_response :success |
|
249 | assert_response :success | |
250 | assert_include 'watchers_inputs', response.body |
|
250 | assert_include 'watchers_inputs', response.body | |
251 | assert_include 'issue[watcher_user_ids][]', response.body |
|
251 | assert_include 'issue[watcher_user_ids][]', response.body | |
252 | end |
|
252 | end | |
253 | end |
|
253 | end | |
254 |
|
254 | |||
255 | def test_append_without_user_should_render_nothing |
|
255 | def test_append_without_user_should_render_nothing | |
256 | @request.session[:user_id] = 2 |
|
256 | @request.session[:user_id] = 2 | |
257 | xhr :post, :append, :project_id => 'ecookbook' |
|
257 | xhr :post, :append, :project_id => 'ecookbook' | |
258 | assert_response :success |
|
258 | assert_response :success | |
259 | assert response.body.blank? |
|
259 | assert response.body.blank? | |
260 | end |
|
260 | end | |
261 |
|
261 | |||
262 |
def test_ |
|
262 | def test_destroy | |
263 | @request.session[:user_id] = 2 |
|
263 | @request.session[:user_id] = 2 | |
264 | assert_difference('Watcher.count', -1) do |
|
264 | assert_difference('Watcher.count', -1) do | |
265 | xhr :delete, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3' |
|
265 | xhr :delete, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3' | |
266 | assert_response :success |
|
266 | assert_response :success | |
267 | assert_match /watchers/, response.body |
|
267 | assert_match /watchers/, response.body | |
268 | end |
|
268 | end | |
269 | assert !Issue.find(2).watched_by?(User.find(3)) |
|
269 | assert !Issue.find(2).watched_by?(User.find(3)) | |
270 | end |
|
270 | end | |
|
271 | ||||
|
272 | def test_destroy_locked_user | |||
|
273 | user = User.find(3) | |||
|
274 | user.lock! | |||
|
275 | assert user.reload.locked? | |||
|
276 | ||||
|
277 | @request.session[:user_id] = 2 | |||
|
278 | assert_difference('Watcher.count', -1) do | |||
|
279 | xhr :delete, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3' | |||
|
280 | assert_response :success | |||
|
281 | assert_match /watchers/, response.body | |||
|
282 | end | |||
|
283 | assert !Issue.find(2).watched_by?(User.find(3)) | |||
|
284 | end | |||
|
285 | ||||
|
286 | def test_destroy_invalid_user_should_respond_with_404 | |||
|
287 | @request.session[:user_id] = 2 | |||
|
288 | assert_no_difference('Watcher.count') do | |||
|
289 | delete :destroy, :object_type => 'issue', :object_id => '2', :user_id => '999' | |||
|
290 | assert_response 404 | |||
|
291 | end | |||
|
292 | end | |||
271 | end |
|
293 | end |
General Comments 0
You need to be logged in to leave comments.
Login now