Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

262 lines
7.0 KiB
Ruby
Raw Permalink Normal View History

2025-05-05 09:29:55 +02:00
# frozen_string_literal: true
2014-02-26 17:13:34 +01:00
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2014-02-26 17:13:34 +01:00
#
# 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:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2014-02-26 17:13:34 +01:00
# 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 COPYRIGHT and LICENSE files for more details.
2014-02-26 17:13:34 +01:00
#++
2016-11-17 16:11:40 +01:00
require "spec_helper"
2023-07-18 13:15:25 +02:00
RSpec.describe Watcher do
2016-11-17 16:11:40 +01:00
let(:project) { watchable.project }
let(:user) { build(:user, admin: true) }
2016-11-17 16:11:40 +01:00
let(:watcher) do
build(:watcher,
watchable:,
user:)
2016-11-17 16:11:40 +01:00
end
let(:watchable) { build(:news) }
2016-11-17 16:11:40 +01:00
let(:other_watcher) do
build(:watcher,
watchable:,
user: other_user)
2016-11-17 16:11:40 +01:00
end
2022-01-24 19:22:35 +01:00
let(:other_project) { create(:project) }
let(:other_user) { create(:user, admin: true) }
2021-06-24 14:03:29 +02:00
let(:notification_settings) { [] }
2019-09-20 10:15:17 +02:00
let(:saved_user) do
create(:user,
member_with_permissions: { saved_watchable.project => [] },
notification_settings:)
2019-09-20 10:15:17 +02:00
end
let(:saved_watchable) { create(:news) }
2016-11-17 16:11:40 +01:00
2017-05-18 07:24:58 +02:00
describe "#valid" do
it "is valid for an active user" do
expect(watcher).to be_valid
end
it "is valid for an invited user" do
2021-02-04 09:52:56 +01:00
user.status = Principal.statuses[:invited]
2017-05-18 07:24:58 +02:00
expect(watcher).to be_valid
end
it "is valid for a registered user" do
2021-02-04 09:52:56 +01:00
user.status = Principal.statuses[:registered]
2017-05-18 07:24:58 +02:00
expect(watcher).to be_valid
end
end
2016-11-17 16:11:40 +01:00
describe ".prune" do
shared_examples_for "a pruned watchable" do
before do
watcher.save!
other_watcher.save!
user.update_attribute(:admin, false)
user.reload
end
context "with a matching user scope" do
it "removes the watcher" do
Watcher.prune(user:)
expect(Watcher.find_by(id: watcher.id)).to be_nil
end
it "leaves the other watcher" do
Watcher.prune(user:)
expect(Watcher.find_by(id: other_watcher.id)).to eql other_watcher
end
end
context "without a scope" do
it "removes the watcher" do
Watcher.prune
expect(Watcher.find_by(id: watcher.id)).to be_nil
end
it "leaves the other watcher" do
Watcher.prune
expect(Watcher.find_by(id: other_watcher.id)).to eql other_watcher
end
end
context "with a non matching user scope" do
2022-01-24 19:22:35 +01:00
let(:other_other_user) { create(:user) }
2016-11-17 16:11:40 +01:00
it "leaves the watcher" do
Watcher.prune(user: other_other_user)
expect(Watcher.find_by(id: watcher.id)).to eql watcher
end
it "leaves the other watcher" do
Watcher.prune(user: other_other_user)
expect(Watcher.find_by(id: other_watcher.id)).to eql other_watcher
end
end
context "with a matching user and project_id scope" do
it "removes the watcher" do
Watcher.prune(user:, project_id: project.id)
expect(Watcher.find_by(id: watcher.id)).to be_nil
end
it "leaves the other watcher" do
Watcher.prune(user:, project_id: project.id)
expect(Watcher.find_by(id: other_watcher.id)).to eql other_watcher
end
end
context "with a matching project_id scope" do
it "removes the watcher" do
Watcher.prune(project_id: project.id)
expect(Watcher.find_by(id: watcher.id)).to be_nil
end
it "leaves the other watcher" do
Watcher.prune(project_id: project.id)
expect(Watcher.find_by(id: other_watcher.id)).to eql other_watcher
end
end
context "with a non matching project_id scope" do
it "leaves the watcher" do
Watcher.prune(project_id: other_project.id)
expect(Watcher.find_by(id: watcher.id)).to eql watcher
end
it "leaves the other watcher" do
Watcher.prune(project_id: other_project.id)
expect(Watcher.find_by(id: other_watcher.id)).to eql other_watcher
end
end
end
shared_examples_for "no watcher exists" do
before do
watchable.save!
end
it "is robust" do
expect { Watcher.prune }.not_to raise_error
end
end
context "for a work package" do
it_behaves_like "a pruned watchable"
it_behaves_like "no watcher exists"
end
context "for a message" do
2022-01-24 19:22:35 +01:00
let(:forum) { build(:forum) }
2016-11-17 16:11:40 +01:00
let(:watchable) do
2019-03-12 10:34:45 +01:00
forum.save!
2022-01-24 19:22:35 +01:00
build(:message, forum:)
2016-11-17 16:11:40 +01:00
end
2019-03-12 10:34:45 +01:00
let(:project) { forum.project }
2016-11-17 16:11:40 +01:00
it_behaves_like "a pruned watchable"
it_behaves_like "no watcher exists"
end
end
2019-09-20 10:15:17 +02:00
describe "#add_watcher" do
it "returns true when the watcher is added" do
expect(saved_watchable.add_watcher(saved_user))
.to be_truthy
end
2019-09-20 10:15:17 +02:00
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
2024-01-04 17:01:17 +01:00
it "does not add the same user when called twice" do
2019-09-20 10:15:17 +02:00
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))
2024-01-04 17:01:17 +01:00
.to contain_exactly(saved_user)
2019-09-20 10:15:17 +02:00
end
end
2016-11-17 16:11:40 +01:00
end