Skip to content

Commit 9eed759

Browse files
committed
[29029] DELETE option for TimeEntries
1 parent 083ff71 commit 9eed759

7 files changed

Lines changed: 315 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ npm-debug.log*
4141
/tmp
4242
*.swp
4343

44+
# Ignore Visual Studio Code files
45+
/.vscode
46+
4447
# Ignore RubyMine files
4548
/.idea
4649
/frontend/.idea
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#-- encoding: UTF-8
2+
3+
#-- copyright
4+
# OpenProject is a project management system.
5+
# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2017 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See doc/COPYRIGHT.rdoc for more details.
29+
#++
30+
31+
module TimeEntries
32+
class DeleteContract < BaseContract
33+
def validate
34+
unless user_allowed_to_delete?
35+
errors.add :base, :error_unauthorized
36+
end
37+
38+
super
39+
end
40+
41+
private
42+
43+
##
44+
# Users may delete time entries IF
45+
# they have the :edit_time_entries or
46+
# user == deleting user and :edit_own_time_entries
47+
def user_allowed_to_delete?
48+
edit_all = user.allowed_to?(:edit_time_entries, model.project)
49+
edit_own = user.allowed_to?(:edit_own_time_entries, model.project)
50+
51+
if model.user == user
52+
edit_own || edit_all
53+
else
54+
edit_all
55+
end
56+
end
57+
end
58+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#-- encoding: UTF-8
2+
3+
#-- copyright
4+
# OpenProject is a project management system.
5+
# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2017 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See docs/COPYRIGHT.rdoc for more details.
29+
#++
30+
31+
##
32+
# Implements the deletion of a time entry.
33+
class TimeEntries::DeleteService
34+
include Concerns::Contracted
35+
attr_accessor :user, :time_entry
36+
37+
def initialize(user:, time_entry:)
38+
self.user = user
39+
self.time_entry = time_entry
40+
self.contract_class = TimeEntries::DeleteContract
41+
end
42+
43+
##
44+
# Deletes the given time entry if allowed.
45+
#
46+
# @return True if the deletion has been initiated, false otherwise.
47+
def call
48+
validate_and_yield(time_entry, user) do
49+
time_entry.destroy
50+
end
51+
end
52+
end

docs/api/apiv3/endpoints/time_entries.apib

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,48 @@ Depending on custom fields defined for time entries, additional properties might
107107
"message": "The requested resource could not be found."
108108
}
109109
110+
111+
## Delete time entry [DELETE]
112+
113+
Permanently deletes the specified time entry.
114+
115+
+ Parameters
116+
+ id (required, integer, `1`) ... Time entry id
117+
118+
+ Response 202
119+
120+
Returned if the time entry was deleted successfully.
121+
122+
Note that the response body is empty as of now. In future versions of the API a body
123+
*might* be returned, indicating the progress of deletion.
124+
125+
+ Body
126+
127+
+ Response 403 (application/hal+json)
128+
129+
Returned if the client does not have sufficient permissions
130+
131+
+ Body
132+
133+
{
134+
"_type": "Error",
135+
"errorIdentifier": "urn:openproject-org:api:v3:errors:MissingPermission",
136+
"message": "You are not allowed to delete this time entry."
137+
}
138+
139+
+ Response 404 (application/hal+json)
140+
141+
Returned if the time entry does not exist.
142+
143+
+ Body
144+
145+
{
146+
"_type": "Error",
147+
"errorIdentifier": "urn:openproject-org:api:v3:errors:NotFound",
148+
"message": "The specified user does not exist."
149+
}
150+
151+
110152
## Time entries [/api/v3/time_entries{?offset,pageSize,filters,sortBy}]
111153
112154
+ Model

lib/api/v3/time_entries/time_entries_api.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ class TimeEntriesAPI < ::API::OpenProjectAPI
105105
fail ::API::Errors::ErrorBase.create_and_merge_errors(result.errors)
106106
end
107107
end
108+
109+
delete do
110+
if ::TimeEntries::DeleteService.new(time_entry: @time_entry, user: current_user).call
111+
status 202
112+
else
113+
fail ::API::Errors::Unauthorized
114+
end
115+
end
108116
end
109117

110118
mount ::API::V3::TimeEntries::TimeEntriesActivityAPI
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#-- encoding: UTF-8
2+
#-- copyright
3+
# OpenProject is a project management system.
4+
# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
5+
#
6+
# This program is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU General Public License version 3.
8+
#
9+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
10+
# Copyright (C) 2006-2017 Jean-Philippe Lang
11+
# Copyright (C) 2010-2013 the ChiliProject Team
12+
#
13+
# This program is free software; you can redistribute it and/or
14+
# modify it under the terms of the GNU General Public License
15+
# as published by the Free Software Foundation; either version 2
16+
# of the License, or (at your option) any later version.
17+
#
18+
# This program is distributed in the hope that it will be useful,
19+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
# GNU General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU General Public License
24+
# along with this program; if not, write to the Free Software
25+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26+
#
27+
# See docs/COPYRIGHT.rdoc for more details.
28+
#++
29+
30+
require 'spec_helper'
31+
32+
describe TimeEntries::DeleteContract do
33+
let(:current_user) do
34+
FactoryBot.build_stubbed(:user) do |user|
35+
allow(user)
36+
.to receive(:allowed_to?) do |permission, permission_project|
37+
permissions.include?(permission) && time_entry_project == permission_project
38+
end
39+
end
40+
end
41+
let(:other_user) { FactoryBot.build_stubbed(:user) }
42+
let(:time_entry_work_package) do
43+
FactoryBot.build_stubbed(:work_package,
44+
project: time_entry_project)
45+
end
46+
let(:time_entry_project) { FactoryBot.build_stubbed(:project) }
47+
let(:time_entry_activity) { FactoryBot.build_stubbed(:time_entry_activity) }
48+
let(:time_entry_user) { current_user }
49+
let(:time_entry_spent_on) { Date.today }
50+
let(:time_entry_hours) { 5 }
51+
let(:time_entry_comments) { "A comment" }
52+
let(:time_entry) do
53+
TimeEntry.create(project: time_entry_project,
54+
work_package: time_entry_work_package,
55+
user: time_entry_user,
56+
activity: time_entry_activity,
57+
spent_on: time_entry_spent_on,
58+
hours: time_entry_hours,
59+
comments: time_entry_comments)
60+
end
61+
let(:permissions) { %i(edit_time_entries) }
62+
63+
subject(:contract) { described_class.new(time_entry, current_user) }
64+
65+
def expect_valid(valid, symbols = {})
66+
expect(contract.validate).to eq(valid)
67+
68+
symbols.each do |key, arr|
69+
expect(contract.errors.symbols_for(key)).to match_array arr
70+
end
71+
end
72+
73+
shared_examples 'is valid' do
74+
it 'is valid' do
75+
expect_valid(true)
76+
end
77+
end
78+
79+
it_behaves_like 'is valid'
80+
81+
context 'when user is not allowed to delete time entries' do
82+
let(:permissions) { [] }
83+
84+
it 'is invalid' do
85+
expect_valid(false, base: %i(error_unauthorized))
86+
end
87+
end
88+
89+
context 'when time_entry user is not contract user' do
90+
let(:time_entry_user) { other_user }
91+
92+
context 'when has permission' do
93+
let(:permissions) { %i[edit_time_entries] }
94+
95+
it 'is valid' do
96+
expect_valid(true)
97+
end
98+
end
99+
100+
context 'when has no permission' do
101+
let(:permissions) { %i[edit_own_time_entries] }
102+
it 'is invalid' do
103+
expect_valid(false, base: %i(error_unauthorized))
104+
end
105+
end
106+
end
107+
end

spec/requests/api/v3/time_entry_resource_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,4 +493,49 @@
493493
end
494494
end
495495
end
496+
497+
describe 'DELETE /api/v3/time_entries/:id' do
498+
let(:path) { api_v3_paths.time_entry time_entry.id }
499+
500+
before do
501+
delete path
502+
end
503+
504+
subject(:response) { last_response }
505+
506+
shared_examples_for 'deletes the time_entry' do
507+
it 'responds with HTTP No Content' do
508+
expect(subject.status).to eq 204
509+
end
510+
511+
it 'removes the time_entry from the DB' do
512+
expect(TimeEntry.exists?(time_entry.id)).to be_falsey
513+
end
514+
end
515+
516+
shared_examples_for 'does not delete the time_entry' do |status = 403|
517+
it "responds with #{status}" do
518+
expect(subject.status).to eq status
519+
end
520+
521+
it 'does not delete the time_entry' do
522+
expect(TimeEntry.exists?(time_entry.id)).to be_truthy
523+
end
524+
end
525+
526+
context "with an uncontainered time_entry" do
527+
let(:container) { nil }
528+
529+
context 'with the user being the author' do
530+
it_behaves_like 'deletes the time_entry'
531+
end
532+
533+
context 'with the user not being the author' do
534+
let(:author) { FactoryBot.create(:user) }
535+
536+
it_behaves_like 'does not delete the time_entry', 404
537+
end
538+
end
539+
end
540+
496541
end

0 commit comments

Comments
 (0)