Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

replace legacy watcher spec

ulferts18c7e426338c
diff --git a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
index 606f81256b4..7cd8cb61d37 100644
--- a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
+++ b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
@@ -130,7 +130,7 @@ module Redmine
# Adds user as a watcher
def add_watcher(user)
- watchers << Watcher.new(user: user, watchable: self)
+ watchers << Watcher.new(user: user, watchable: self) unless watchers.map(&:user_id).include?(user.id)
end
# Removes user from the watchers list
diff --git a/spec/models/watcher_spec.rb b/spec/models/watcher_spec.rb
index 8c2533fb04c..c27975b76eb 100644
--- a/spec/models/watcher_spec.rb
+++ b/spec/models/watcher_spec.rb
@@ -28,22 +28,30 @@
require 'spec_helper'
-describe Watcher, type: :model do
+describe Watcher, type: :model, with_mail: false do
let(:project) { watchable.project }
let(:user) { FactoryBot.build :user, admin: true }
let(:watcher) do
FactoryBot.build :watcher,
- watchable: watchable,
- user: user
+ watchable: watchable,
+ user: user
end
- let(:watchable) { FactoryBot.build :work_package }
+ let(:watchable) { FactoryBot.build :news }
let(:other_watcher) do
FactoryBot.build :watcher,
- watchable: watchable,
- user: other_user
+ watchable: watchable,
+ user: other_user
end
let(:other_project) { FactoryBot.create(:project) }
let(:other_user) { FactoryBot.create(:user, admin: true) }
+ let(:mail_notification) { 'all' }
+ let(:saved_user) do
+ FactoryBot.create :user,
+ member_in_project: saved_watchable.project,
+ member_with_permissions: [],
+ mail_notification: mail_notification
+ end
+ let(:saved_watchable) { FactoryBot.create :news }
describe '#valid' do
it 'is valid for an active user' do
@@ -184,4 +192,90 @@ describe Watcher, type: :model do
it_behaves_like 'no watcher exists'
end
end
+
+ describe '#add_watcher' do
+ it 'returns true when the watcher is added' do
+ expect(saved_watchable.add_watcher(saved_user))
+ .to be_truthy
+ end
+ it 'adds the user to watchers' do
+ saved_watchable.add_watcher(saved_user)
+
+ expect(saved_watchable.watchers.map(&:user))
+ .to match_array(saved_user)
+ end
+
+ it 'will not add the same user when called twice' do
+ saved_watchable.add_watcher(saved_user)
+ saved_watchable.add_watcher(saved_user)
+
+ expect(saved_watchable.watchers.map(&:user))
+ .to match_array(saved_user)
+ end
+ end
+
+ describe '#remove_watcher' do
+ before do
+ saved_watchable.watchers.create(user: saved_user)
+ end
+
+ it 'removes the watcher' do
+ saved_watchable.remove_watcher(saved_user)
+
+ expect(saved_watchable.watchers)
+ .to be_empty
+ end
+ end
+
+ describe '#watched_by' do
+ context 'for a watcher user' do
+ before do
+ saved_watchable.watchers.create!(user: saved_user)
+ end
+
+ it 'is truthy' do
+ expect(saved_watchable.watched_by?(saved_user))
+ .to be_truthy
+ end
+ end
+
+ context 'for a non watcher user' do
+ it 'is falsey' do
+ expect(saved_watchable.watched_by?(saved_user))
+ .to be_falsey
+ end
+ end
+ end
+
+ describe '#watcher_user_ids' do
+ it 'only adds unique users' do
+ saved_watchable.watcher_user_ids = [saved_user.id, saved_user.id]
+ expect(saved_watchable)
+ .to be_valid
+ expect(saved_watchable.watchers.map(&:user))
+ .to match_array([saved_user])
+ end
+ end
+
+ describe '#watcher_recipients' do
+ before do
+ saved_watchable.watchers.create(user: saved_user)
+ end
+
+ context 'for a user with `all` notification' do
+ it 'returns the user' do
+ expect(saved_watchable.watcher_recipients)
+ .to match_array([saved_user])
+ end
+ end
+
+ context 'for a user with `none` notification' do
+ let(:mail_notification) { 'none' }
+
+ it 'is empty' do
+ expect(saved_watchable.watcher_recipients)
+ .to be_empty
+ end
+ end
+ end
end
diff --git a/spec_legacy/unit/watcher_spec.rb b/spec_legacy/unit/watcher_spec.rb
deleted file mode 100644
index 2e3b8145ee9..00000000000
--- a/spec_legacy/unit/watcher_spec.rb
+++ /dev/null
@@ -1,117 +0,0 @@
-#-- encoding: UTF-8
-#-- copyright
-# OpenProject is a project management system.
-# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2017 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See docs/COPYRIGHT.rdoc for more details.
-#++
-require_relative '../legacy_spec_helper'
-
-describe Watcher do
- before do
- @user = FactoryBot.create :user
- @issue = FactoryBot.create :work_package
- @role = FactoryBot.create :role, permissions: [:view_work_packages]
- @issue.project.add_member! @user, @role
- end
-
- it 'should add_watcher' do
- @issue.add_watcher(@user)
- assert_contains @issue.watchers.map(&:user), @user
- end
-
- it 'should add_watcher_will_not_add_same_user_twice' do
- assert @issue.add_watcher(@user)
- refute @issue.add_watcher(@user)
- end
-
- it 'should watched_by' do
- @issue.add_watcher(@user)
- assert @issue.watched_by?(@user)
- assert_contains WorkPackage.watched_by(@user), @issue
- end
-
- it 'watcher_users contains correct_classes' do
- @issue.add_watcher(@user)
- expect(@issue.watcher_users).to all be_kind_of(User)
- end
-
-
- it 'should watcher_users_should_not_validate_user' do
- skip 'Is a known issue in 5.0.0 has_many: https://github.com/rails/rails/issues/25718'
- allow(@user).to receive(:valid?).and_return(false)
- @issue.watcher_users << @user
- assert @issue.watched_by?(@user)
- end
-
- it 'should watcher_user_ids' do
- @issue.add_watcher(@user)
- assert_contains @issue.watcher_user_ids, @user.id
- end
-
- it 'should watcher_user_ids' do
- @issue.watcher_user_ids = [@user.id]
- assert @issue.watched_by?(@user)
- end
-
- it 'should watcher_user_ids_should_make_ids_uniq' do
- @issue.watcher_user_ids = [@user.id, @user.id]
- assert @issue.valid?
- assert_equal 1, @issue.watchers.count
- end
-
- it 'should recipients' do
- @user.update_attribute :mail_notification, 'all'
-
- assert @issue.watcher_recipients.empty?
- assert @issue.add_watcher(@user)
- assert_contains @issue.watcher_recipients, @user
-
- @user.update_attribute :mail_notification, 'none'
- assert_does_not_contain @issue.watcher_recipients, @user
- end
-
- it 'should unwatch' do
- assert @issue.add_watcher(@user)
- @issue.save
- assert_equal 1, @issue.remove_watcher(@user)
- @issue.save
- @issue.reload
- refute @issue.watched_by?(@user)
- end
-
- it 'should prune_removes_watchers_that_dont_have_permission' do
- @issue.add_watcher(@user)
-
- assert_no_difference 'Watcher.count' do
- Watcher.prune(user: @user)
- end
- assert @issue.watched_by?(@user)
-
- Member.destroy_all
- @issue.reload
-
- refute @issue.watched_by?(@user)
- end
-end