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 details to notification representer

ulferts3c8d0001f031
diff --git a/lib/api/decorators/date_property.rb b/lib/api/decorators/date_property.rb
index a2b5f716b90..cf2f88b0973 100644
--- a/lib/api/decorators/date_property.rb
+++ b/lib/api/decorators/date_property.rb
@@ -37,6 +37,10 @@ module API
base.extend ClassMethods
end
+ def datetime_formatter
+ ::API::V3::Utilities::DateTimeFormatter
+ end
+
module ClassMethods
def date_property(name,
getter: default_date_getter(name),
diff --git a/lib/api/decorators/self_link.rb b/lib/api/decorators/self_link.rb
new file mode 100644
index 00000000000..9a4166798a2
--- /dev/null
+++ b/lib/api/decorators/self_link.rb
@@ -0,0 +1,74 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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.
+# ++
+
+module API
+ module Decorators
+ module SelfLink
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ def self.prepended(base)
+ base.extend ClassMethods
+ end
+
+ def self_v3_path(path, id_attribute)
+ path ||= _type.underscore
+
+ id = if id_attribute.respond_to?(:call)
+ instance_eval(&id_attribute)
+ else
+ represented.send(id_attribute)
+ end
+
+ id = [nil] if id.nil?
+
+ api_v3_paths.send(path, *Array(id))
+ end
+
+ module ClassMethods
+ def self_link(path: nil,
+ id_attribute: :id,
+ title: true,
+ title_getter: ->(*) { represented.name })
+ link :self do
+ self_path = self_v3_path(path, id_attribute)
+
+ link_object = { href: self_path }
+ if title
+ title_string = instance_eval(&title_getter)
+ link_object[:title] = title_string if title_string
+ end
+
+ link_object
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/decorators/single.rb b/lib/api/decorators/single.rb
index a72b1b7d246..43dae9a5913 100644
--- a/lib/api/decorators/single.rb
+++ b/lib/api/decorators/single.rb
@@ -35,6 +35,7 @@ module API
class Single < ::Roar::Decorator
include ::Roar::JSON::HAL
include ::Roar::Hypermedia
+ include ::API::Decorators::SelfLink
include ::API::V3::Utilities::PathHelper
attr_reader :current_user, :embed_links
@@ -64,17 +65,6 @@ module API
render_nil: false,
writable: false
- def self.self_link(path: nil, id_attribute: :id, title_getter: ->(*) { represented.name })
- link :self do
- self_path = self_v3_path(path, id_attribute)
-
- link_object = { href: self_path }
- title = instance_eval(&title_getter)
- link_object[:title] = title if title
-
- link_object
- end
- end
class_attribute :to_eager_load
class_attribute :checked_permissions
@@ -102,30 +92,12 @@ module API
end
end
- def datetime_formatter
- ::API::V3::Utilities::DateTimeFormatter
- end
-
# If a subclass does not depend on a model being passed to this class, it can override
# this method and return false. Otherwise it will be enforced that the model of each
# representer is non-nil.
def model_required?
true
end
-
- def self_v3_path(path, id_attribute)
- path ||= _type.underscore
-
- id = if id_attribute.respond_to?(:call)
- instance_eval(&id_attribute)
- else
- represented.send(id_attribute)
- end
-
- id = [nil] if id.nil?
-
- api_v3_paths.send(path, *Array(id))
- end
end
end
end
diff --git a/lib/api/v3/notifications/details_factory.rb b/lib/api/v3/notifications/details_factory.rb
new file mode 100644
index 00000000000..c1f3d0daab4
--- /dev/null
+++ b/lib/api/v3/notifications/details_factory.rb
@@ -0,0 +1,52 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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.
+# ++
+
+module API::V3::Notifications
+ module DetailsFactory
+ extend ::API::V3::Utilities::PathHelper
+
+ module_function
+
+ def for(notification)
+ concrete_factory_for(notification.reason)
+ .for(notification)
+ end
+
+ def concrete_factory_for(reason)
+ @concrete_factory_for ||= Hash.new do |h, reason_key|
+ h[reason_key] = if API::V3::Notifications::DetailsFactory.const_defined?(reason_key.camelcase)
+ "API::V3::Notifications::DetailsFactory::#{reason_key.camelcase}".constantize
+ else
+ API::V3::Notifications::DetailsFactory::Default
+ end
+ end
+
+ @concrete_factory_for[reason]
+ end
+ end
+end
diff --git a/lib/api/v3/notifications/details_factory/date_alert_due_date.rb b/lib/api/v3/notifications/details_factory/date_alert_due_date.rb
new file mode 100644
index 00000000000..1d0d6f68d01
--- /dev/null
+++ b/lib/api/v3/notifications/details_factory/date_alert_due_date.rb
@@ -0,0 +1,43 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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.
+# ++
+
+module API::V3::Notifications::DetailsFactory
+ module DateAlertDueDate
+ extend ::API::V3::Utilities::PathHelper
+
+ module_function
+
+ def for(notification)
+ [
+ ::API::V3::Values::PropertyDateRepresenter
+ .new(::API::V3::Values::PropertyModel.new(:due_date, notification.resource.due_date),
+ self_link: api_v3_paths.notification_detail(notification.id, 0))
+ ]
+ end
+ end
+end
diff --git a/lib/api/v3/notifications/details_factory/date_alert_start_date.rb b/lib/api/v3/notifications/details_factory/date_alert_start_date.rb
new file mode 100644
index 00000000000..4dc0d936e6f
--- /dev/null
+++ b/lib/api/v3/notifications/details_factory/date_alert_start_date.rb
@@ -0,0 +1,43 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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.
+# ++
+
+module API::V3::Notifications::DetailsFactory
+ module DateAlertStartDate
+ extend ::API::V3::Utilities::PathHelper
+
+ module_function
+
+ def for(notification)
+ [
+ ::API::V3::Values::PropertyDateRepresenter
+ .new(::API::V3::Values::PropertyModel.new(:start_date, notification.resource.start_date),
+ self_link: api_v3_paths.notification_detail(notification.id, 0))
+ ]
+ end
+ end
+end
diff --git a/lib/api/v3/notifications/details_factory/default.rb b/lib/api/v3/notifications/details_factory/default.rb
new file mode 100644
index 00000000000..bddf4c3a45a
--- /dev/null
+++ b/lib/api/v3/notifications/details_factory/default.rb
@@ -0,0 +1,37 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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.
+# ++
+
+module API::V3::Notifications::DetailsFactory
+ module Default
+ module_function
+
+ def for(_notification)
+ []
+ end
+ end
+end
diff --git a/lib/api/v3/notifications/notification_representer.rb b/lib/api/v3/notifications/notification_representer.rb
index 48dccec7fec..9ab126fcf68 100644
--- a/lib/api/v3/notifications/notification_representer.rb
+++ b/lib/api/v3/notifications/notification_representer.rb
@@ -43,25 +43,17 @@ module API
property :reason
- property :message,
- exec_context: :decorator,
- getter: ->(*) do
- next unless represented.reason == 'date_alert'
-
- changes = represented.journal.get_changes
- if changes[:start_date]
- [{ startDate: datetime_formatter.format_date(changes[:start_date].last) }]
- elsif changes[:due_date]
- [{ dueDate: datetime_formatter.format_date(changes[:due_date].last) }]
- else
- []
- end
- end
-
date_time_property :created_at
date_time_property :updated_at
+ property :details,
+ embedded: true,
+ exec_context: :decorator,
+ getter: ->(*) do
+ DetailsFactory.for(represented)
+ end
+
link :readIAN do
next if represented.read_ian
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index 53ee693e0bd..2ccc898adf5 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -263,6 +263,10 @@ module API
"#{notification(id)}/unread_ian"
end
+ def self.notification_detail(notification_id, detail_id)
+ "#{notification(notification_id)}/details/#{detail_id}"
+ end
+
index :placeholder_user
show :placeholder_user
diff --git a/lib/api/v3/values/property_date_representer.rb b/lib/api/v3/values/property_date_representer.rb
new file mode 100644
index 00000000000..187320e2fc2
--- /dev/null
+++ b/lib/api/v3/values/property_date_representer.rb
@@ -0,0 +1,35 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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.
+# ++
+
+module API::V3::Values
+ class PropertyDateRepresenter < PropertyRepresenter
+ include API::Decorators::DateProperty
+
+ date_property :value
+ end
+end
diff --git a/lib/api/v3/values/property_model.rb b/lib/api/v3/values/property_model.rb
new file mode 100644
index 00000000000..238d8ef50a7
--- /dev/null
+++ b/lib/api/v3/values/property_model.rb
@@ -0,0 +1,39 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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.
+# ++
+
+module API::V3::Values
+ class PropertyModel
+ attr_reader :property,
+ :value
+
+ def initialize(property, value)
+ @property = property
+ @value = value
+ end
+ end
+end
diff --git a/lib/api/v3/values/property_representer.rb b/lib/api/v3/values/property_representer.rb
new file mode 100644
index 00000000000..d679e271882
--- /dev/null
+++ b/lib/api/v3/values/property_representer.rb
@@ -0,0 +1,57 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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.
+# ++
+
+module API::V3::Values
+ class PropertyRepresenter < ::Roar::Decorator
+ include ::Roar::JSON::HAL
+ include ::Roar::Hypermedia
+ include API::Decorators::SelfLink
+
+ def initialize(model, self_link:)
+ @self_link = self_link
+
+ super(model)
+ end
+
+ property :_type,
+ getter: ->(*) { 'Values::Property' }
+
+ property :property,
+ getter: ->(*) { property.to_s.camelcase(:lower) }
+
+ self_link title: false
+
+ def self_v3_path(*_args)
+ self_link
+ end
+
+ private
+
+ attr_reader :self_link
+ end
+end
diff --git a/spec/lib/api/v3/notifications/details_factory_spec.rb b/spec/lib/api/v3/notifications/details_factory_spec.rb
new file mode 100644
index 00000000000..6191bac87d2
--- /dev/null
+++ b/spec/lib/api/v3/notifications/details_factory_spec.rb
@@ -0,0 +1,83 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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 'spec_helper'
+
+describe ::API::V3::Notifications::DetailsFactory do
+ let(:resource) { build_stubbed :work_package }
+
+ let(:notification) do
+ build_stubbed :notification,
+ resource:,
+ reason:
+ end
+
+ let(:embed_links) { false }
+ let(:read_ian) { false }
+
+ describe '.for' do
+ context 'for a date_alert_start_date notification' do
+ let(:reason) { :date_alert_start_date }
+
+ it 'returns one detail' do
+ expect(described_class.for(notification).size)
+ .to eq 1
+ end
+
+ it 'returns an array of `Values::Property` representers' do
+ expect(described_class.for(notification)[0])
+ .to be_a ::API::V3::Values::PropertyDateRepresenter
+ end
+ end
+
+ context 'for a date_alert_due_date notification' do
+ let(:reason) { :date_alert_due_date }
+
+ it 'returns one detail' do
+ expect(described_class.for(notification).size)
+ .to eq 1
+ end
+
+ it 'returns an array of `Values::Property` representers' do
+ expect(described_class.for(notification)[0])
+ .to be_a ::API::V3::Values::PropertyDateRepresenter
+ end
+ end
+
+ (Notification::REASONS.keys - %i[date_alert_start_date date_alert_due_date]).each do |possible_reason|
+ context "for a #{possible_reason} notification" do
+ let(:reason) { possible_reason }
+
+ it 'is an empty array' do
+ expect(described_class.for(notification))
+ .to eq []
+ end
+ end
+ end
+ end
+end
diff --git a/spec/lib/api/v3/notifications/notification_representer_rendering_spec.rb b/spec/lib/api/v3/notifications/notification_representer_rendering_spec.rb
index c53a097db22..45f0081b49b 100644
--- a/spec/lib/api/v3/notifications/notification_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/notifications/notification_representer_rendering_spec.rb
@@ -34,7 +34,7 @@ describe ::API::V3::Notifications::NotificationRepresenter, 'rendering' do
subject(:generated) { representer.to_json }
shared_let(:project) { create :project }
- shared_let(:resource) { create :work_package, project: }
+ let(:resource) { build_stubbed :work_package, project: }
let(:recipient) { build_stubbed(:user) }
let(:journal) { nil }
@@ -118,41 +118,6 @@ describe ::API::V3::Notifications::NotificationRepresenter, 'rendering' do
it 'is expected to not have a message' do
expect(subject).not_to have_json_path('message')
end
-
- context 'when the reason is date alert' do
- let(:journal) { resource.journals.last }
- let(:reason) { :date_alert }
-
- context 'when the startDate is happening soon' do
- before do
- journal.data.update!(start_date: Date.tomorrow)
- end
-
- it_behaves_like 'date property', 'message/0/startDate' do
- let(:value) { Date.tomorrow }
- end
- end
-
- context 'when the dueDate is happening soon' do
- before do
- journal.data.update!(due_date: Date.tomorrow)
- end
-
- it_behaves_like 'date property', 'message/0/dueDate' do
- let(:value) { Date.tomorrow }
- end
- end
-
- context 'when the dueDate is in the past (overdue)' do
- before do
- journal.data.update!(due_date: Date.yesterday)
- end
-
- it_behaves_like 'date property', 'message/0/dueDate' do
- let(:value) { Date.yesterday }
- end
- end
- end
end
describe 'project' do
@@ -221,7 +186,7 @@ describe ::API::V3::Notifications::NotificationRepresenter, 'rendering' do
end
context 'when set' do
- let(:journal) { resource.journals.last }
+ let(:journal) { build_stubbed(:work_package_journal) }
it_behaves_like 'has an untitled link' do
let(:link) { 'activity' }
@@ -239,4 +204,51 @@ describe ::API::V3::Notifications::NotificationRepresenter, 'rendering' do
end
end
end
+
+ describe 'details' do
+ shared_examples_for 'embeds a Values::Property for startDate' do
+ it 'embeds a Values::Property' do
+ expect(generated)
+ .to be_json_eql('Values::Property'.to_json)
+ .at_path("_embedded/details/0/_type")
+ end
+
+ it 'has a startDate value for the `property` property' do
+ expect(generated)
+ .to be_json_eql('startDate'.to_json)
+ .at_path("_embedded/details/0/property")
+ end
+
+ it 'has a work_package`s start_date for the value' do
+ expect(generated)
+ .to be_json_eql(resource.start_date.to_json)
+ .at_path("_embedded/details/0/value")
+ end
+ end
+
+ context 'for a dateAlert when embedding' do
+ let(:reason) { :date_alert_start_date }
+ let(:embed_links) { true }
+
+ it_behaves_like 'embeds a Values::Property for startDate'
+ end
+
+ context 'for a dateAlert when not embedding' do
+ let(:reason) { :date_alert_start_date }
+ let(:embed_links) { false }
+
+ it_behaves_like 'embeds a Values::Property for startDate'
+ end
+
+ context 'for a mention when embedding' do
+ let(:reason) { :mentioned }
+ let(:embed_links) { true }
+
+ it 'has an empty details array' do
+ expect(generated)
+ .to have_json_size(0)
+ .at_path("_embedded/details")
+ end
+ end
+ end
end
diff --git a/spec/lib/api/v3/utilities/path_helper_spec.rb b/spec/lib/api/v3/utilities/path_helper_spec.rb
index 5fb04195e7c..902d8dabadd 100644
--- a/spec/lib/api/v3/utilities/path_helper_spec.rb
+++ b/spec/lib/api/v3/utilities/path_helper_spec.rb
@@ -245,6 +245,12 @@ describe ::API::V3::Utilities::PathHelper do
it_behaves_like 'api v3 path', '/notifications/42/unread_ian'
end
+
+ describe '#notification_detail' do
+ subject { helper.notification_detail(42, 0) }
+
+ it_behaves_like 'api v3 path', '/notifications/42/details/0'
+ end
end
describe 'markup paths' do
diff --git a/spec/lib/api/v3/values/property_date_representer_rendering_spec.rb b/spec/lib/api/v3/values/property_date_representer_rendering_spec.rb
new file mode 100644
index 00000000000..6f84374bf43
--- /dev/null
+++ b/spec/lib/api/v3/values/property_date_representer_rendering_spec.rb
@@ -0,0 +1,84 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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 'spec_helper'
+
+describe ::API::V3::Values::PropertyDateRepresenter, 'rendering' do
+ subject(:generated) { representer.to_json }
+
+ let(:property) { 'abc' }
+ let(:date_value) { Date.current }
+ let(:key_value) { Struct.new(:property, :value, keyword_init: true).new(property:, value: date_value) }
+ let(:self_link) { 'api/bogus/value' }
+ let(:representer) do
+ described_class.new key_value, self_link:
+ end
+
+ describe 'self link' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'self' }
+ let(:href) { self_link }
+ end
+ end
+
+ describe 'properties' do
+ describe '_type' do
+ it_behaves_like 'property', :_type do
+ let(:value) { 'Values::Property' }
+ end
+ end
+
+ describe 'property' do
+ it_behaves_like 'property', :property do
+ let(:value) { property }
+ end
+
+ context 'with a snake_case property' do
+ let(:property) { 'snake_case' }
+
+ it_behaves_like 'property', :property do
+ let(:value) { 'snakeCase' }
+ end
+ end
+ end
+
+ describe 'value' do
+ it_behaves_like 'date property', :value do
+ let(:value) { date_value }
+ end
+
+ context 'with an empty value' do
+ let(:date_value) { nil }
+
+ it_behaves_like 'date property', :value do
+ let(:value) { nil }
+ end
+ end
+ end
+ end
+end
diff --git a/spec/requests/api/v3/notifications/show_resource_examples.rb b/spec/requests/api/v3/notifications/show_resource_examples.rb
index 7ba28512dde..fae6dedc41d 100644
--- a/spec/requests/api/v3/notifications/show_resource_examples.rb
+++ b/spec/requests/api/v3/notifications/show_resource_examples.rb
@@ -27,20 +27,26 @@
shared_examples 'represents the notification' do
it 'represents the notification', :aggregate_failures do
- expect(last_response.status).to eq(200)
+ expect(last_response.status)
+ .to eq(200)
expect(last_response.body)
- .to(be_json_eql('Notification'.to_json).at_path('_type'))
+ .to be_json_eql('Notification'.to_json)
+ .at_path('_type')
expect(last_response.body)
- .to(be_json_eql(notification.read_ian.to_json).at_path('readIAN'))
+ .to be_json_eql(notification.read_ian.to_json)
+ .at_path('readIAN')
expect(last_response.body)
- .to(be_json_eql(::API::V3::Utilities::DateTimeFormatter.format_datetime(notification.created_at).to_json).at_path('createdAt'))
+ .to be_json_eql(::API::V3::Utilities::DateTimeFormatter.format_datetime(notification.created_at).to_json)
+ .at_path('createdAt')
expect(last_response.body)
- .to(be_json_eql(::API::V3::Utilities::DateTimeFormatter.format_datetime(notification.updated_at).to_json).at_path('updatedAt'))
+ .to be_json_eql(::API::V3::Utilities::DateTimeFormatter.format_datetime(notification.updated_at).to_json)
+ .at_path('updatedAt')
expect(last_response.body)
- .to(be_json_eql(notification.id.to_json).at_path('id'))
+ .to be_json_eql(notification.id.to_json)
+ .at_path('id')
end
end
diff --git a/spec/requests/api/v3/notifications/show_resource_spec.rb b/spec/requests/api/v3/notifications/show_resource_spec.rb
index f59d5daf3e5..d584ab7e3e9 100644
--- a/spec/requests/api/v3/notifications/show_resource_spec.rb
+++ b/spec/requests/api/v3/notifications/show_resource_spec.rb
@@ -55,19 +55,49 @@ describe ::API::V3::Notifications::NotificationsAPI,
get api_v3_paths.notification(notification.id)
end
- before do
- login_as current_user
- send_request
+ describe 'recipient user' do
+ current_user { recipient }
+
+ before do
+ send_request
+ end
+
+ it_behaves_like 'represents the notification'
end
- describe 'recipient user' do
- let(:current_user) { recipient }
+ describe 'recipient user for a dateAlert notification' do
+ current_user { recipient }
+
+ before do
+ notification.reason = :date_alert_due_date
+ notification.journal = nil
+ notification.actor = nil
+ notification.save!
+
+ resource.update_column(:due_date, Date.current)
+
+ send_request
+ end
it_behaves_like 'represents the notification'
+
+ it 'includes the value of the work package associated in the details', :aggregate_failures do
+ expect(last_response.body)
+ .to be_json_eql('dueDate'.to_json)
+ .at_path('_embedded/details/0/property')
+
+ expect(last_response.body)
+ .to be_json_eql(::API::V3::Utilities::DateTimeFormatter.format_date(resource.due_date).to_json)
+ .at_path('_embedded/details/0/value')
+ end
end
describe 'admin user' do
- let(:current_user) { build(:admin) }
+ current_user { build_stubbed(:admin) }
+
+ before do
+ send_request
+ end
it 'returns a 404 response' do
expect(last_response.status).to eq(404)
@@ -75,7 +105,11 @@ describe ::API::V3::Notifications::NotificationsAPI,
end
describe 'unauthorized user' do
- let(:current_user) { build(:user) }
+ current_user { build_stubbed(:user) }
+
+ before do
+ send_request
+ end
it 'returns a 404 response' do
expect(last_response.status).to eq(404)