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 `GET /api/v3/reminders` endpoint (#19083)

Kabiru Mwenja1b336f34a652
diff --git a/docs/api/apiv3/components/schemas/reminder_model.yml b/docs/api/apiv3/components/schemas/reminder_model.yml
index 08af277c428..01b4a8089e0 100644
--- a/docs/api/apiv3/components/schemas/reminder_model.yml
+++ b/docs/api/apiv3/components/schemas/reminder_model.yml
@@ -17,6 +17,7 @@ properties:
_links:
required:
- creator
+ - remindable
type: object
properties:
creator:
@@ -26,3 +27,10 @@ properties:
The person that created the reminder
**Resource**: User
+ remindable:
+ allOf:
+ - $ref: "./link.yml"
+ - description: |-
+ The resource that the reminder is associated with
+
+ **Resource**: WorkPackage
diff --git a/docs/api/apiv3/openapi-spec.yml b/docs/api/apiv3/openapi-spec.yml
index 2f2580eb1c9..3b4ed442142 100644
--- a/docs/api/apiv3/openapi-spec.yml
+++ b/docs/api/apiv3/openapi-spec.yml
@@ -388,6 +388,8 @@ paths:
"$ref": "./paths/render_plain.yml"
"/api/v3/revisions/{id}":
"$ref": "./paths/revision.yml"
+ "/api/v3/reminders":
+ "$ref": "./paths/reminders.yml"
"/api/v3/roles":
"$ref": "./paths/roles.yml"
"/api/v3/roles/{id}":
diff --git a/docs/api/apiv3/paths/reminders.yml b/docs/api/apiv3/paths/reminders.yml
new file mode 100644
index 00000000000..39eb4192903
--- /dev/null
+++ b/docs/api/apiv3/paths/reminders.yml
@@ -0,0 +1,43 @@
+# /api/v3/reminders
+---
+get:
+ summary: List all active reminders
+ operationId: list_reminders
+ tags:
+ - Reminders
+ description: |-
+ Gets a list of all active reminders for the user.
+ responses:
+ '200':
+ description: OK
+ content:
+ application/hal+json:
+ schema:
+ type: object
+ required:
+ - _type
+ - total
+ - count
+ - _embedded
+ properties:
+ _type:
+ type: string
+ enum:
+ - Collection
+ total:
+ type: integer
+ description: The total amount of elements available in the collection.
+ minimum: 0
+ count:
+ type: integer
+ description: Actual amount of elements in this response.
+ minimum: 0
+ _embedded:
+ type: object
+ required:
+ - elements
+ properties:
+ elements:
+ type: array
+ items:
+ $ref: '../components/schemas/reminder_model.yml'
diff --git a/docs/api/apiv3/paths/work_package_reminder.yml b/docs/api/apiv3/paths/work_package_reminder.yml
index 12c2530f985..212ca4ab77c 100644
--- a/docs/api/apiv3/paths/work_package_reminder.yml
+++ b/docs/api/apiv3/paths/work_package_reminder.yml
@@ -2,7 +2,7 @@
---
patch:
summary: Update a work package reminder
- operationId: update_reminder
+ operationId: update_work_package_reminder
tags:
- Work Packages
- Reminders
@@ -58,7 +58,7 @@ patch:
delete:
summary: Delete a work package reminder
- operationId: delete_reminder
+ operationId: delete_work_package_reminder
tags:
- Work Packages
- Reminders
diff --git a/docs/api/apiv3/paths/work_package_reminders.yml b/docs/api/apiv3/paths/work_package_reminders.yml
index cf9a8fe8506..d072d7182fe 100644
--- a/docs/api/apiv3/paths/work_package_reminders.yml
+++ b/docs/api/apiv3/paths/work_package_reminders.yml
@@ -2,7 +2,7 @@
---
get:
summary: List work package reminders
- operationId: list_reminders
+ operationId: list_work_package_reminders
tags:
- Work Packages
- Reminders
@@ -73,7 +73,7 @@ get:
post:
summary: Create a work package reminder
- operationId: create_reminder
+ operationId: create_work_package_reminder
tags:
- Work Packages
- Reminders
diff --git a/lib/api/v3/reminders/reminder_representer.rb b/lib/api/v3/reminders/reminder_representer.rb
index 3e991c509bd..de7fe2cccb9 100644
--- a/lib/api/v3/reminders/reminder_representer.rb
+++ b/lib/api/v3/reminders/reminder_representer.rb
@@ -33,6 +33,27 @@ module API
include API::Decorators::DateProperty
include API::Decorators::LinkedResource
+ def self.associated_remindable_getter
+ ->(*) {
+ next unless embed_links && remindable_representer
+
+ remindable_representer
+ .create(represented.remindable, current_user:)
+ }
+ end
+
+ def self.associated_remindable_link
+ ->(*) {
+ return nil unless v3_remindable_name == "nil_class" || api_v3_paths.respond_to?(v3_remindable_name)
+
+ ::API::Decorators::LinkObject
+ .new(represented,
+ path: v3_remindable_name,
+ property_name: :remindable)
+ .to_hash
+ }
+ end
+
property :id
date_time_property :remind_at
@@ -43,9 +64,27 @@ module API
v3_path: :user,
representer: ::API::V3::Users::UserRepresenter
+ associated_resource :remindable,
+ getter: associated_remindable_getter,
+ link: associated_remindable_link
+
def _type
"Reminder"
end
+
+ def remindable_representer
+ name = v3_remindable_name.camelcase
+
+ "::API::V3::#{name.pluralize}::#{name}Representer".constantize
+ rescue NameError
+ nil
+ end
+
+ def v3_remindable_name
+ ar_name = represented.remindable_type.underscore
+
+ ::API::Utilities::PropertyNameConverter.from_ar_name(ar_name).underscore
+ end
end
end
end
diff --git a/lib/api/v3/reminders/reminders_api.rb b/lib/api/v3/reminders/reminders_api.rb
index f2b9999f571..22756d8d47a 100644
--- a/lib/api/v3/reminders/reminders_api.rb
+++ b/lib/api/v3/reminders/reminders_api.rb
@@ -31,45 +31,11 @@ module API
module Reminders
class RemindersAPI < ::API::OpenProjectAPI
resource :reminders do
- after_validation do
- authorize_in_project(:view_work_packages, project: @work_package.project)
- end
-
- helpers do
- def reminders
- @work_package.reminders.upcoming_and_visible_to(User.current)
- end
-
- def restrict_multiple_reminders
- if reminders.any?
- raise ::API::Errors::Conflict.new(
- message: I18n.t("api_v3.errors.conflict.multiple_reminders_not_allowed")
- )
- end
- end
- end
-
get do
- ReminderCollectionRepresenter.new(reminders,
- self_link: api_v3_paths.work_package_reminders(@work_package.id),
+ ReminderCollectionRepresenter.new(Reminder.upcoming_and_visible_to(User.current),
+ self_link: api_v3_paths.reminders,
current_user: User.current)
end
-
- post(&API::V3::Utilities::Endpoints::Create.new(model: Reminder,
- before_hook: ->(request:) { request.restrict_multiple_reminders },
- params_modifier: ->(params) do
- params.merge(remindable: @work_package,
- creator: User.current)
- end).mount)
-
- route_param :reminder_id, type: Integer, desc: "Reminder ID" do
- after_validation do
- @reminder = reminders.find(declared_params[:reminder_id])
- end
-
- patch(&API::V3::Utilities::Endpoints::Update.new(model: Reminder).mount)
- delete(&API::V3::Utilities::Endpoints::Delete.new(model: Reminder).mount)
- end
end
end
end
diff --git a/lib/api/v3/reminders/reminders_by_work_package_api.rb b/lib/api/v3/reminders/reminders_by_work_package_api.rb
new file mode 100644
index 00000000000..bd87b9c127d
--- /dev/null
+++ b/lib/api/v3/reminders/reminders_by_work_package_api.rb
@@ -0,0 +1,79 @@
+# 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.
+#++
+
+module API
+ module V3
+ module Reminders
+ class RemindersByWorkPackageAPI < ::API::OpenProjectAPI
+ resource :reminders do
+ after_validation do
+ authorize_in_project(:view_work_packages, project: @work_package.project)
+ end
+
+ helpers do
+ def reminders
+ @work_package.reminders.upcoming_and_visible_to(User.current)
+ end
+
+ def restrict_multiple_reminders
+ if reminders.any?
+ raise ::API::Errors::Conflict.new(
+ message: I18n.t("api_v3.errors.conflict.multiple_reminders_not_allowed")
+ )
+ end
+ end
+ end
+
+ get do
+ ReminderCollectionRepresenter.new(reminders,
+ self_link: api_v3_paths.work_package_reminders(@work_package.id),
+ current_user: User.current)
+ end
+
+ post(&API::V3::Utilities::Endpoints::Create.new(model: Reminder,
+ before_hook: ->(request:) { request.restrict_multiple_reminders },
+ params_modifier: ->(params) do
+ params.merge(remindable: @work_package,
+ creator: User.current)
+ end).mount)
+
+ route_param :reminder_id, type: Integer, desc: "Reminder ID" do
+ after_validation do
+ @reminder = reminders.find(declared_params[:reminder_id])
+ end
+
+ patch(&API::V3::Utilities::Endpoints::Update.new(model: Reminder).mount)
+ delete(&API::V3::Utilities::Endpoints::Delete.new(model: Reminder).mount)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/root.rb b/lib/api/v3/root.rb
index f7a31d47c29..815ac373c3b 100644
--- a/lib/api/v3/root.rb
+++ b/lib/api/v3/root.rb
@@ -72,6 +72,7 @@ module API
mount ::API::V3::Queries::QueriesAPI
mount ::API::V3::Render::RenderAPI
mount ::API::V3::Relations::RelationsAPI
+ mount ::API::V3::Reminders::RemindersAPI
mount ::API::V3::Repositories::RevisionsAPI
mount ::API::V3::Roles::RolesAPI
mount ::API::V3::Shares::SharesAPI
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index dcad91b7d48..a8e279a6c54 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -476,6 +476,10 @@ module API
index :shares
show :share
+ def self.reminders
+ "#{root}/reminders"
+ end
+
def self.work_package_reminder(work_package_id, reminder_id)
"#{work_package_reminders(work_package_id)}/#{reminder_id}"
end
diff --git a/lib/api/v3/work_packages/work_packages_api.rb b/lib/api/v3/work_packages/work_packages_api.rb
index 6e843612b33..d2418fd484c 100644
--- a/lib/api/v3/work_packages/work_packages_api.rb
+++ b/lib/api/v3/work_packages/work_packages_api.rb
@@ -98,7 +98,7 @@ module API
mount ::API::V3::WorkPackages::AvailableProjectsOnEditAPI
mount ::API::V3::WorkPackages::AvailableRelationCandidatesAPI
mount ::API::V3::WorkPackages::WorkPackageRelationsAPI
- mount ::API::V3::Reminders::RemindersAPI
+ mount ::API::V3::Reminders::RemindersByWorkPackageAPI
mount ::API::V3::EmojiReactions::EmojiReactionsByWorkPackageCommentsAPI
end
diff --git a/spec/requests/api/v3/reminders/reminder_representer_spec.rb b/spec/requests/api/v3/reminders/reminder_representer_spec.rb
new file mode 100644
index 00000000000..9b732c84b54
--- /dev/null
+++ b/spec/requests/api/v3/reminders/reminder_representer_spec.rb
@@ -0,0 +1,65 @@
+# 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 "spec_helper"
+
+RSpec.describe API::V3::Reminders::ReminderRepresenter do
+ let(:user) { build_stubbed(:user) }
+ let(:remindable) { build_stubbed(:work_package) }
+ let(:reminder) { build_stubbed(:reminder, remindable:, creator: user) }
+ let(:representer) { described_class.new(reminder, current_user: user) }
+ let(:parsed) { representer.to_hash }
+
+ it "renders the id" do
+ expect(parsed["id"]).to eq reminder.id
+ end
+
+ it "renders the remindAt" do
+ expect(parsed["remindAt"]).to eq reminder.remind_at.iso8601(3)
+ end
+
+ it "renders the note" do
+ expect(parsed["note"]).to eq reminder.note
+ end
+
+ it "renders the _type" do
+ expect(parsed["_type"]).to eq "Reminder"
+ end
+
+ it "renders the creator link" do
+ expect(parsed["_links"]).to have_key("creator")
+ expect(parsed["_links"]["creator"]["href"]).to include("/api/v3/users/#{user.id}")
+ end
+
+ it "renders the remindable link" do
+ expect(parsed["_links"]).to have_key("remindable")
+ expect(parsed["_links"]["remindable"]["href"]).to include("/api/v3/work_packages/#{remindable.id}")
+ end
+end
diff --git a/spec/requests/api/v3/reminders/reminders_api_spec.rb b/spec/requests/api/v3/reminders/reminders_api_spec.rb
index 96b101fee47..fe894b7db6d 100644
--- a/spec/requests/api/v3/reminders/reminders_api_spec.rb
+++ b/spec/requests/api/v3/reminders/reminders_api_spec.rb
@@ -33,298 +33,26 @@ require "spec_helper"
RSpec.describe API::V3::Reminders::RemindersAPI do
include API::V3::Utilities::PathHelper
- let!(:project) { create(:project) }
-
- let!(:role_with_permissions) { create(:project_role, permissions: %i[view_work_packages]) }
- let!(:role_without_permissions) { create(:project_role, permissions: %i[view_project]) }
-
- let!(:user_with_permissions) do
- create(:user, member_with_roles: { project => role_with_permissions })
- end
- let!(:other_user_without_permissions) do
- create(:user, member_with_roles: { project => role_without_permissions })
- end
-
- let!(:work_package) { create(:work_package, project:) }
- let!(:other_work_package) { create(:work_package, project:) }
-
- let!(:user_with_permissions_past_reminder) do
- Reminders::CreateService.new(user: user_with_permissions, contract_class: nil)
- .call(remindable: work_package,
- remind_at: 1.day.ago,
- creator: user_with_permissions,
- note: "I'm the user with permissions and my reminder is in the past")
- .result
- end
- let!(:user_with_permissions_future_reminder) do
- Reminders::CreateService.new(user: user_with_permissions, contract_class: nil)
- .call(remindable: work_package,
- remind_at: 1.day.from_now,
- creator: user_with_permissions,
- note: "I'm the user with permissions and my reminder is in the future")
- .result
- end
- let!(:user_with_permissions_future_reminder_in_other_work_package) do
- Reminders::CreateService.new(user: user_with_permissions, contract_class: nil)
- .call(remindable: other_work_package,
- remind_at: 1.day.from_now,
- creator: user_with_permissions,
- note: "I'm the user with permissions and my reminder is in the future in another work package")
- .result
- end
- let!(:other_user_without_permissions_reminder) do
- Reminders::CreateService.new(user: other_user_without_permissions, contract_class: nil)
- .call(remindable: other_work_package,
- remind_at: 1.day.from_now,
- creator: other_user_without_permissions,
- note: "I'm the other user without permissions and my reminder is in the future")
- .result
- end
- let!(:other_user_without_permissions_reminder_past) do
- Reminders::CreateService.new(user: other_user_without_permissions, contract_class: nil)
- .call(remindable: other_work_package,
- remind_at: 1.day.ago,
- creator: other_user_without_permissions,
- note: "I'm the other user without permissions and my reminder is in the past")
- .result
- end
- let!(:other_user_without_permissions_reminder_in_other_work_package) do
- Reminders::CreateService.new(user: other_user_without_permissions, contract_class: nil)
- .call(remindable: other_work_package,
- remind_at: 1.day.from_now,
- creator: other_user_without_permissions,
- note: "I'm the other user without permissions and my reminder is in the" \
- "future in another work package")
- .result
- end
-
- describe "GET /api/v3/work_packages/:work_package_id/reminders" do
- let(:make_request) { get api_v3_paths.work_package_reminders(work_package.id) }
-
- context "with permissions" do
- current_user { user_with_permissions }
-
- before { make_request }
-
- it_behaves_like "API V3 collection response", 1, 1, "Reminder" do
- let(:elements) { [user_with_permissions_future_reminder] }
-
- it "returns the future reminders for the current user in the given work package" do
- expect(last_response.body)
- .to be_json_eql("I'm the user with permissions and my reminder is in the future".to_json)
- .at_path("_embedded/elements/0/note")
-
- expect(last_response.body)
- .to be_json_eql(
- API::V3::Utilities::DateTimeFormatter.format_datetime(user_with_permissions_future_reminder.remind_at).to_json
- )
- .at_path("_embedded/elements/0/remindAt")
-
- expect(last_response.body)
- .to be_json_eql({ "href" => "/api/v3/users/#{user_with_permissions.id}",
- "title" => user_with_permissions.name }.to_json)
- .at_path("_embedded/elements/0/_links/creator")
- end
- end
- end
-
- context "with no permissions" do
- current_user { other_user_without_permissions }
-
- before { make_request }
-
- it_behaves_like "error response",
- 404, "NotFound",
- "The work package you are looking for cannot be found or has been deleted."
- end
+ shared_let(:user) { create(:user) }
+ shared_let(:other_user) { create(:user) }
+ shared_let(:user_reminders) { create_list(:reminder, 2, creator: user, remind_at: 1.day.from_now) }
+ shared_let(:user_reminders_completed) do
+ create_list(:reminder, 2, creator: user, remind_at: 1.day.ago, completed_at: 1.day.ago)
end
- describe "POST /api/v3/work_packages/:work_package_id/reminders", :freeze_time do
- let(:path) { api_v3_paths.work_package_reminders(work_package.id) }
- let(:headers) { { "CONTENT_TYPE" => "application/json" } }
- let(:remind_at) { 1.day.from_now }
- let(:note) { "Remind me to do something" }
- let(:params) { { remindAt: remind_at, note: } }
-
- def make_request
- post path, params.to_json, headers
- end
-
- context "with permissions" do
- current_user { user_with_permissions }
-
- before do
- Reminder.destroy_all
- make_request
- end
-
- it_behaves_like "successful response", 201, "Reminder" do
- it "returns reminder attributes" do
- expect(last_response.body)
- .to be_json_eql(API::V3::Utilities::DateTimeFormatter.format_datetime(remind_at).to_json)
- .at_path("remindAt")
-
- expect(last_response.body)
- .to be_json_eql(note.to_json)
- .at_path("note")
-
- expect(last_response.body)
- .to be_json_eql({ "href" => "/api/v3/users/#{current_user.id}", "title" => current_user.name }.to_json)
- .at_path("_links/creator")
- end
- end
- end
-
- context "with an existing reminder" do
- current_user { user_with_permissions }
-
- before { make_request }
-
- it_behaves_like "error response",
- 409, "UpdateConflict",
- "You can only set one reminder at a time for a work package. Please delete or update the existing reminder."
- end
-
- context "with no permissions" do
- current_user { other_user_without_permissions }
-
- before { make_request }
-
- it_behaves_like "error response",
- 404, "NotFound",
- "The work package you are looking for cannot be found or has been deleted."
- end
- end
-
- describe "PATCH /api/v3/work_packages/:work_package_id/reminders/:reminder_id" do
- let(:headers) { { "CONTENT_TYPE" => "application/json" } }
-
- let(:reminder) { create(:reminder, remindable: work_package, creator: user_with_permissions) }
- let(:other_user_reminder) { create(:reminder, remindable: work_package, creator: other_user_without_permissions) }
+ describe "GET /api/v3/reminders" do
+ let(:path) { api_v3_paths.reminders }
- let(:completed_reminder) do
- create(:reminder, :completed, remindable: work_package, creator: user_with_permissions)
- end
-
- def make_request
- patch path, params.to_json, headers
- end
+ context "with logged in user" do
+ current_user { user }
- context "with permissions updating own reminder" do
- let(:path) { api_v3_paths.work_package_reminder(work_package.id, reminder.id) }
- let(:params) { { note: "UPDATED reminder note!" } }
+ before { get path }
- current_user { user_with_permissions }
-
- before { make_request }
-
- it_behaves_like "successful response", 200, "Reminder" do
- it "returns updated reminder attributes" do
- expect(last_response.body)
- .to be_json_eql("UPDATED reminder note!".to_json)
- .at_path("note")
- end
+ it_behaves_like "API V3 collection response", 2, 2, "Reminder" do
+ let(:elements) { user_reminders }
end
end
- context "with permissions updating completed reminder" do
- let(:path) { api_v3_paths.work_package_reminder(work_package.id, completed_reminder.id) }
- let(:params) { { note: "UPDATED reminder note!" } }
-
- current_user { user_with_permissions }
-
- before { make_request }
-
- it_behaves_like "error response",
- 404, "NotFound",
- "The reminder you are looking for cannot be found or has been deleted."
- end
-
- context "with permissions updating other user's reminder" do
- let(:path) { api_v3_paths.work_package_reminder(work_package.id, other_user_reminder.id) }
- let(:params) { { note: "CANNOT update!" } }
-
- current_user { user_with_permissions }
-
- before { make_request }
-
- it_behaves_like "error response",
- 404, "NotFound",
- "The reminder you are looking for cannot be found or has been deleted."
- end
-
- context "with no permissions updating own reminder" do
- let(:path) { api_v3_paths.work_package_reminder(work_package.id, other_user_reminder.id) }
- let(:params) { { note: "UPDATED reminder note!" } }
-
- current_user { other_user_without_permissions }
-
- before { make_request }
-
- it_behaves_like "error response",
- 404, "NotFound",
- "The work package you are looking for cannot be found or has been deleted."
- end
- end
-
- describe "DELETE /api/v3/work_packages/:work_package_id/reminders/:reminder_id" do
- let(:headers) { { "CONTENT_TYPE" => "application/json" } }
-
- let(:reminder) { create(:reminder, remindable: work_package, creator: user_with_permissions) }
- let(:other_user_reminder) { create(:reminder, remindable: work_package, creator: other_user_without_permissions) }
-
- let(:completed_reminder) do
- create(:reminder, :completed, remindable: work_package, creator: user_with_permissions)
- end
-
- def make_request
- delete path, headers
- end
-
- context "with permissions deleting own reminder" do
- let(:path) { api_v3_paths.work_package_reminder(work_package.id, reminder.id) }
-
- current_user { user_with_permissions }
-
- before { make_request }
-
- it_behaves_like "successful no content response"
- end
-
- context "with permissions deleting completed reminder" do
- let(:path) { api_v3_paths.work_package_reminder(work_package.id, completed_reminder.id) }
-
- current_user { user_with_permissions }
-
- before { make_request }
-
- it_behaves_like "error response",
- 404, "NotFound",
- "The reminder you are looking for cannot be found or has been deleted."
- end
-
- context "with permissions deleting other user's reminder" do
- let(:path) { api_v3_paths.work_package_reminder(work_package.id, other_user_reminder.id) }
-
- current_user { user_with_permissions }
-
- before { make_request }
-
- it_behaves_like "error response",
- 404, "NotFound",
- "The reminder you are looking for cannot be found or has been deleted."
- end
-
- context "with no permissions deleting own reminder" do
- let(:path) { api_v3_paths.work_package_reminder(work_package.id, other_user_reminder.id) }
-
- current_user { other_user_without_permissions }
-
- before { make_request }
-
- it_behaves_like "error response",
- 404, "NotFound",
- "The work package you are looking for cannot be found or has been deleted."
- end
+ it_behaves_like "handling anonymous user"
end
end
diff --git a/spec/requests/api/v3/reminders/reminders_by_work_package_api_spec.rb b/spec/requests/api/v3/reminders/reminders_by_work_package_api_spec.rb
new file mode 100644
index 00000000000..a7fa390f280
--- /dev/null
+++ b/spec/requests/api/v3/reminders/reminders_by_work_package_api_spec.rb
@@ -0,0 +1,330 @@
+# 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 "spec_helper"
+
+RSpec.describe API::V3::Reminders::RemindersByWorkPackageAPI do
+ include API::V3::Utilities::PathHelper
+
+ let!(:project) { create(:project) }
+
+ let!(:role_with_permissions) { create(:project_role, permissions: %i[view_work_packages]) }
+ let!(:role_without_permissions) { create(:project_role, permissions: %i[view_project]) }
+
+ let!(:user_with_permissions) do
+ create(:user, member_with_roles: { project => role_with_permissions })
+ end
+ let!(:other_user_without_permissions) do
+ create(:user, member_with_roles: { project => role_without_permissions })
+ end
+
+ let!(:work_package) { create(:work_package, project:) }
+ let!(:other_work_package) { create(:work_package, project:) }
+
+ let!(:user_with_permissions_past_reminder) do
+ Reminders::CreateService.new(user: user_with_permissions, contract_class: nil)
+ .call(remindable: work_package,
+ remind_at: 1.day.ago,
+ creator: user_with_permissions,
+ note: "I'm the user with permissions and my reminder is in the past")
+ .result
+ end
+ let!(:user_with_permissions_future_reminder) do
+ Reminders::CreateService.new(user: user_with_permissions, contract_class: nil)
+ .call(remindable: work_package,
+ remind_at: 1.day.from_now,
+ creator: user_with_permissions,
+ note: "I'm the user with permissions and my reminder is in the future")
+ .result
+ end
+ let!(:user_with_permissions_future_reminder_in_other_work_package) do
+ Reminders::CreateService.new(user: user_with_permissions, contract_class: nil)
+ .call(remindable: other_work_package,
+ remind_at: 1.day.from_now,
+ creator: user_with_permissions,
+ note: "I'm the user with permissions and my reminder is in the future in another work package")
+ .result
+ end
+ let!(:other_user_without_permissions_reminder) do
+ Reminders::CreateService.new(user: other_user_without_permissions, contract_class: nil)
+ .call(remindable: other_work_package,
+ remind_at: 1.day.from_now,
+ creator: other_user_without_permissions,
+ note: "I'm the other user without permissions and my reminder is in the future")
+ .result
+ end
+ let!(:other_user_without_permissions_reminder_past) do
+ Reminders::CreateService.new(user: other_user_without_permissions, contract_class: nil)
+ .call(remindable: other_work_package,
+ remind_at: 1.day.ago,
+ creator: other_user_without_permissions,
+ note: "I'm the other user without permissions and my reminder is in the past")
+ .result
+ end
+ let!(:other_user_without_permissions_reminder_in_other_work_package) do
+ Reminders::CreateService.new(user: other_user_without_permissions, contract_class: nil)
+ .call(remindable: other_work_package,
+ remind_at: 1.day.from_now,
+ creator: other_user_without_permissions,
+ note: "I'm the other user without permissions and my reminder is in the" \
+ "future in another work package")
+ .result
+ end
+
+ describe "GET /api/v3/work_packages/:work_package_id/reminders" do
+ let(:make_request) { get api_v3_paths.work_package_reminders(work_package.id) }
+
+ context "with permissions" do
+ current_user { user_with_permissions }
+
+ before { make_request }
+
+ it_behaves_like "API V3 collection response", 1, 1, "Reminder" do
+ let(:elements) { [user_with_permissions_future_reminder] }
+
+ it "returns the future reminders for the current user in the given work package" do
+ expect(last_response.body)
+ .to be_json_eql("I'm the user with permissions and my reminder is in the future".to_json)
+ .at_path("_embedded/elements/0/note")
+
+ expect(last_response.body)
+ .to be_json_eql(
+ API::V3::Utilities::DateTimeFormatter.format_datetime(user_with_permissions_future_reminder.remind_at).to_json
+ )
+ .at_path("_embedded/elements/0/remindAt")
+
+ expect(last_response.body)
+ .to be_json_eql({ "href" => "/api/v3/users/#{user_with_permissions.id}",
+ "title" => user_with_permissions.name }.to_json)
+ .at_path("_embedded/elements/0/_links/creator")
+ end
+ end
+ end
+
+ context "with no permissions" do
+ current_user { other_user_without_permissions }
+
+ before { make_request }
+
+ it_behaves_like "error response",
+ 404, "NotFound",
+ "The work package you are looking for cannot be found or has been deleted."
+ end
+ end
+
+ describe "POST /api/v3/work_packages/:work_package_id/reminders", :freeze_time do
+ let(:path) { api_v3_paths.work_package_reminders(work_package.id) }
+ let(:headers) { { "CONTENT_TYPE" => "application/json" } }
+ let(:remind_at) { 1.day.from_now }
+ let(:note) { "Remind me to do something" }
+ let(:params) { { remindAt: remind_at, note: } }
+
+ def make_request
+ post path, params.to_json, headers
+ end
+
+ context "with permissions" do
+ current_user { user_with_permissions }
+
+ before do
+ Reminder.destroy_all
+ make_request
+ end
+
+ it_behaves_like "successful response", 201, "Reminder" do
+ it "returns reminder attributes" do
+ expect(last_response.body)
+ .to be_json_eql(API::V3::Utilities::DateTimeFormatter.format_datetime(remind_at).to_json)
+ .at_path("remindAt")
+
+ expect(last_response.body)
+ .to be_json_eql(note.to_json)
+ .at_path("note")
+
+ expect(last_response.body)
+ .to be_json_eql({ "href" => "/api/v3/users/#{current_user.id}", "title" => current_user.name }.to_json)
+ .at_path("_links/creator")
+ end
+ end
+ end
+
+ context "with an existing reminder" do
+ current_user { user_with_permissions }
+
+ before { make_request }
+
+ it_behaves_like "error response",
+ 409, "UpdateConflict",
+ "You can only set one reminder at a time for a work package. Please delete or update the existing reminder."
+ end
+
+ context "with no permissions" do
+ current_user { other_user_without_permissions }
+
+ before { make_request }
+
+ it_behaves_like "error response",
+ 404, "NotFound",
+ "The work package you are looking for cannot be found or has been deleted."
+ end
+ end
+
+ describe "PATCH /api/v3/work_packages/:work_package_id/reminders/:reminder_id" do
+ let(:headers) { { "CONTENT_TYPE" => "application/json" } }
+
+ let(:reminder) { create(:reminder, remindable: work_package, creator: user_with_permissions) }
+ let(:other_user_reminder) { create(:reminder, remindable: work_package, creator: other_user_without_permissions) }
+
+ let(:completed_reminder) do
+ create(:reminder, :completed, remindable: work_package, creator: user_with_permissions)
+ end
+
+ def make_request
+ patch path, params.to_json, headers
+ end
+
+ context "with permissions updating own reminder" do
+ let(:path) { api_v3_paths.work_package_reminder(work_package.id, reminder.id) }
+ let(:params) { { note: "UPDATED reminder note!" } }
+
+ current_user { user_with_permissions }
+
+ before { make_request }
+
+ it_behaves_like "successful response", 200, "Reminder" do
+ it "returns updated reminder attributes" do
+ expect(last_response.body)
+ .to be_json_eql("UPDATED reminder note!".to_json)
+ .at_path("note")
+ end
+ end
+ end
+
+ context "with permissions updating completed reminder" do
+ let(:path) { api_v3_paths.work_package_reminder(work_package.id, completed_reminder.id) }
+ let(:params) { { note: "UPDATED reminder note!" } }
+
+ current_user { user_with_permissions }
+
+ before { make_request }
+
+ it_behaves_like "error response",
+ 404, "NotFound",
+ "The reminder you are looking for cannot be found or has been deleted."
+ end
+
+ context "with permissions updating other user's reminder" do
+ let(:path) { api_v3_paths.work_package_reminder(work_package.id, other_user_reminder.id) }
+ let(:params) { { note: "CANNOT update!" } }
+
+ current_user { user_with_permissions }
+
+ before { make_request }
+
+ it_behaves_like "error response",
+ 404, "NotFound",
+ "The reminder you are looking for cannot be found or has been deleted."
+ end
+
+ context "with no permissions updating own reminder" do
+ let(:path) { api_v3_paths.work_package_reminder(work_package.id, other_user_reminder.id) }
+ let(:params) { { note: "UPDATED reminder note!" } }
+
+ current_user { other_user_without_permissions }
+
+ before { make_request }
+
+ it_behaves_like "error response",
+ 404, "NotFound",
+ "The work package you are looking for cannot be found or has been deleted."
+ end
+ end
+
+ describe "DELETE /api/v3/work_packages/:work_package_id/reminders/:reminder_id" do
+ let(:headers) { { "CONTENT_TYPE" => "application/json" } }
+
+ let(:reminder) { create(:reminder, remindable: work_package, creator: user_with_permissions) }
+ let(:other_user_reminder) { create(:reminder, remindable: work_package, creator: other_user_without_permissions) }
+
+ let(:completed_reminder) do
+ create(:reminder, :completed, remindable: work_package, creator: user_with_permissions)
+ end
+
+ def make_request
+ delete path, headers
+ end
+
+ context "with permissions deleting own reminder" do
+ let(:path) { api_v3_paths.work_package_reminder(work_package.id, reminder.id) }
+
+ current_user { user_with_permissions }
+
+ before { make_request }
+
+ it_behaves_like "successful no content response"
+ end
+
+ context "with permissions deleting completed reminder" do
+ let(:path) { api_v3_paths.work_package_reminder(work_package.id, completed_reminder.id) }
+
+ current_user { user_with_permissions }
+
+ before { make_request }
+
+ it_behaves_like "error response",
+ 404, "NotFound",
+ "The reminder you are looking for cannot be found or has been deleted."
+ end
+
+ context "with permissions deleting other user's reminder" do
+ let(:path) { api_v3_paths.work_package_reminder(work_package.id, other_user_reminder.id) }
+
+ current_user { user_with_permissions }
+
+ before { make_request }
+
+ it_behaves_like "error response",
+ 404, "NotFound",
+ "The reminder you are looking for cannot be found or has been deleted."
+ end
+
+ context "with no permissions deleting own reminder" do
+ let(:path) { api_v3_paths.work_package_reminder(work_package.id, other_user_reminder.id) }
+
+ current_user { other_user_without_permissions }
+
+ before { make_request }
+
+ it_behaves_like "error response",
+ 404, "NotFound",
+ "The work package you are looking for cannot be found or has been deleted."
+ end
+ end
+end