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

Add missing uniqueness validation to Favorite

Alexander Brandon Colesefb998b7ce22
diff --git a/app/models/favorite.rb b/app/models/favorite.rb
index a0c449f5d47..cceb04ab6f5 100644
--- a/app/models/favorite.rb
+++ b/app/models/favorite.rb
@@ -31,4 +31,6 @@
class Favorite < ApplicationRecord
belongs_to :user
belongs_to :favorited, polymorphic: true
+
+ validates :favorited_id, uniqueness: { scope: %i[favorited_type user_id], message: :already_favorited }
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 488b6d5442d..4c628e95935 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1092,6 +1092,8 @@ en:
active_user_count_restriction: "Active users"
enterprise_trial:
company: "Company"
+ favorite:
+ favorited: "Item"
grids/grid:
page: "Page"
row_count: "Number of rows"
@@ -1453,6 +1455,8 @@ en:
only_one_trial: "Only one trial token can be active. Please delete the previous trial token before adding another."
unreadable: "can't be read. Are you sure it is a support token?"
already_added: "This token has already been added."
+ favorite:
+ already_favorited: "has already been favorited."
grids/grid:
overlaps: "overlap."
outside: "is outside of the grid."
diff --git a/spec/factories/favorite_factory.rb b/spec/factories/favorite_factory.rb
new file mode 100644
index 00000000000..70fb5b5ae2b
--- /dev/null
+++ b/spec/factories/favorite_factory.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# 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-2013 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+FactoryBot.define do
+ factory :favorite do
+ user
+ end
+end
diff --git a/spec/models/favorite_spec.rb b/spec/models/favorite_spec.rb
new file mode 100644
index 00000000000..98de0703018
--- /dev/null
+++ b/spec/models/favorite_spec.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# 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-2013 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require "rails_helper"
+
+RSpec.describe Favorite do
+ let(:user) { create(:user) }
+ let(:favorited) { create(:project) }
+
+ subject(:favorite) { create(:favorite, user:, favorited:) }
+
+ describe "validations" do
+ before do
+ favorite
+ end
+
+ it "validates uniqueness of favorited_id, scoped to favorited_type and user_id" do
+ expect { create(:favorite, user:, favorited:) }
+ .to raise_error(ActiveRecord::RecordInvalid, /Item has already been favorited/)
+ end
+ end
+end