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

[#49379] The work package show endpoint allows any timestamps when not EE

Dombi Attila8389c14e3ceb
diff --git a/app/models/query.rb b/app/models/query.rb
index 8c20401bc18..34f35d01320 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -437,8 +437,7 @@ class Query < ApplicationRecord
end
def allowed_timestamps
- return timestamps if EnterpriseToken.allows_to?(:baseline_comparison)
- timestamps.select { |t| t.one_day_ago? || t.to_time >= Date.yesterday }
+ Timestamp.allowed(timestamps)
end
def valid_filter_subset!
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index dd5f57ac388..0bdfada5b68 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -102,6 +102,12 @@ class Timestamp
def now
new(ActiveSupport::Duration.build(0).iso8601)
end
+
+ def allowed(timestamps)
+ return timestamps if EnterpriseToken.allows_to?(:baseline_comparison)
+
+ timestamps.select { |t| t.one_day_ago? || t.to_time >= Date.yesterday }
+ end
end
def initialize(arg = Timestamp.now.to_s)
diff --git a/lib/api/v3/work_packages/show_end_point.rb b/lib/api/v3/work_packages/show_end_point.rb
index 261b9258ce9..5e252800fae 100644
--- a/lib/api/v3/work_packages/show_end_point.rb
+++ b/lib/api/v3/work_packages/show_end_point.rb
@@ -29,11 +29,21 @@
module API::V3::WorkPackages
class ShowEndPoint < API::V3::Utilities::Endpoints::Show
def render(request)
+ timestamps = Timestamp.parse_multiple(request.params[:timestamps])
+ forbidden_timestamps = timestamps - Timestamp.allowed(timestamps)
+
+ if forbidden_timestamps.any?
+ message =
+ I18n.t(:'activerecord.errors.models.query.attributes.timestamps.forbidden',
+ values: forbidden_timestamps.join(", "))
+ raise ::API::Errors::BadRequest.new(message)
+ end
+
API::V3::WorkPackages::WorkPackageRepresenter
.create(request.instance_exec(request.params, &instance_generator),
current_user: request.current_user,
embed_links: true,
- timestamps: Timestamp.parse_multiple(request.params[:timestamps]))
+ timestamps:)
end
end
end
diff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rb
index a84fde3878e..c4210dd92a6 100644
--- a/spec/requests/api/v3/work_packages/show_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/show_resource_spec.rb
@@ -134,17 +134,17 @@ RSpec.describe 'API v3 Work package resource',
let(:children) do
# This will be in another project but the user is still allowed to see the dates
[create(:work_package,
- start_date: Date.today,
- due_date: Date.today + 5.days)]
+ start_date: Time.zone.today,
+ due_date: Time.zone.today + 5.days)]
end
it 'has derived dates' do
expect(subject)
- .to be_json_eql(Date.today.to_json)
+ .to be_json_eql(Time.zone.today.to_json)
.at_path('derivedStartDate')
expect(subject)
- .to be_json_eql((Date.today + 5.days).to_json)
+ .to be_json_eql((Time.zone.today + 5.days).to_json)
.at_path('derivedDueDate')
end
end
@@ -235,197 +235,112 @@ RSpec.describe 'API v3 Work package resource',
let(:original_journal) { work_package.journals.first }
let(:current_journal) { work_package.journals.last }
- it 'responds with 200' do
- expect(subject && last_response.status).to eq(200)
- end
-
- it 'has the current attributes as attributes' do
- expect(subject)
- .to be_json_eql("The current work package".to_json)
- .at_path('subject')
- end
-
- it 'has an embedded link to the baseline work package' do
- expect(subject)
- .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.first).to_json)
- .at_path('_embedded/attributesByTimestamp/0/_links/self/href')
- end
-
- it 'has the absolute timestamps within the self link' do
- Timecop.freeze do
- expect(subject)
- .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.map(&:absolute)).to_json)
- .at_path('_links/self/href')
+ context 'with EE', with_ee: %i[baseline_comparison] do
+ it 'responds with 200' do
+ expect(subject && last_response.status).to eq(200)
end
- end
- describe "attributesByTimestamp" do
- it 'embeds the attributesByTimestamp' do
+ it 'has the current attributes as attributes' do
expect(subject)
- .to be_json_eql("The original work package".to_json)
- .at_path("_embedded/attributesByTimestamp/0/subject")
- expect(subject)
- .to have_json_path("_embedded/attributesByTimestamp/1")
+ .to be_json_eql("The current work package".to_json)
+ .at_path('subject')
end
- it 'does not embed the attributes in attributesByTimestamp if they are the same as the current attributes' do
- expect(subject)
- .not_to have_json_path("_embedded/attributesByTimestamp/0/description")
+ it 'has an embedded link to the baseline work package' do
expect(subject)
- .not_to have_json_path("_embedded/attributesByTimestamp/1/description")
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.first).to_json)
+ .at_path('_embedded/attributesByTimestamp/0/_links/self/href')
end
- describe '_meta' do
- describe 'timestamp' do
- it 'has the relative timestamps' do
- expect(subject)
- .to be_json_eql('2015-01-01T00:00:00Z'.to_json)
- .at_path('_embedded/attributesByTimestamp/0/_meta/timestamp')
- expect(subject)
- .to be_json_eql('PT0S'.to_json)
- .at_path('_embedded/attributesByTimestamp/1/_meta/timestamp')
- end
+ it 'has the absolute timestamps within the self link' do
+ Timecop.freeze do
+ expect(subject)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.map(&:absolute)).to_json)
+ .at_path('_links/self/href')
end
end
- end
-
- describe "when the work package has not been present at the baseline time" do
- let(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z'), Timestamp.now] }
- let(:created_at) { 10.days.ago }
describe "attributesByTimestamp" do
- describe "exists" do
- it "marks the work package as not existing at the baseline time" do
- expect(subject)
- .to be_json_eql(false.to_json)
- .at_path("_embedded/attributesByTimestamp/0/_meta/exists")
- end
-
- it "marks the work package as existing at the current time" do
- expect(subject)
- .to be_json_eql(true.to_json)
- .at_path("_embedded/attributesByTimestamp/1/_meta/exists")
- end
- end
- end
-
- describe "_meta" do
- describe "exits" do
- it "is true because the work package does exist at the last given timestamp" do
- expect(subject)
- .to be_json_eql(true.to_json)
- .at_path("_meta/exists")
- end
+ it 'embeds the attributesByTimestamp' do
+ expect(subject)
+ .to be_json_eql("The original work package".to_json)
+ .at_path("_embedded/attributesByTimestamp/0/subject")
+ expect(subject)
+ .to have_json_path("_embedded/attributesByTimestamp/1")
end
- end
- end
-
- describe "when the work package does not exist at the only requested timestamp" do
- let(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z')] }
- let(:created_at) { 10.days.ago }
- describe "attributesByTimestamp" do
- describe "exists" do
- it "marks the work package as not existing at the requested time" do
- expect(subject)
- .to be_json_eql(false.to_json)
- .at_path("_embedded/attributesByTimestamp/0/_meta/exists")
- end
+ it 'does not embed the attributes in attributesByTimestamp if they are the same as the current attributes' do
+ expect(subject)
+ .not_to have_json_path("_embedded/attributesByTimestamp/0/description")
+ expect(subject)
+ .not_to have_json_path("_embedded/attributesByTimestamp/1/description")
end
- end
- describe "_meta" do
- describe "exits" do
- it "is false because the work package does not exist at the requested timestamp" do
- expect(subject)
- .to be_json_eql(false.to_json)
- .at_path("_meta/exists")
+ describe '_meta' do
+ describe 'timestamp' do
+ it 'has the relative timestamps' do
+ expect(subject)
+ .to be_json_eql('2015-01-01T00:00:00Z'.to_json)
+ .at_path('_embedded/attributesByTimestamp/0/_meta/timestamp')
+ expect(subject)
+ .to be_json_eql('PT0S'.to_json)
+ .at_path('_embedded/attributesByTimestamp/1/_meta/timestamp')
+ end
end
end
end
- end
-
- context "with caching" do
- before { login_as current_user }
- context "with relative timestamps" do
- let(:timestamps) { [Timestamp.parse("P-2D"), Timestamp.now] }
- let(:created_at) { Date.parse('2015-01-01') }
+ describe "when the work package has not been present at the baseline time" do
+ let(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z'), Timestamp.now] }
+ let(:created_at) { 10.days.ago }
describe "attributesByTimestamp" do
- it "does not cache the self link" do
- get get_path
- expect do
- Timecop.travel 20.minutes do
- get get_path
- end
- end.to change {
- JSON.parse(last_response.body)
- .dig("_embedded", "attributesByTimestamp", 0, "_links", "self", "href")
- }
- end
+ describe "exists" do
+ it "marks the work package as not existing at the baseline time" do
+ expect(subject)
+ .to be_json_eql(false.to_json)
+ .at_path("_embedded/attributesByTimestamp/0/_meta/exists")
+ end
- it "does not cache the attributes" do
- get get_path
- expect do
- Timecop.travel 2.days do
- get get_path
- end
- end.to change {
- JSON.parse(last_response.body)
- .dig("_embedded", "attributesByTimestamp", 0, "subject")
- }
+ it "marks the work package as existing at the current time" do
+ expect(subject)
+ .to be_json_eql(true.to_json)
+ .at_path("_embedded/attributesByTimestamp/1/_meta/exists")
+ end
end
end
describe "_meta" do
- describe "exists" do
- let(:timestamps) { [Timestamp.parse("P-2D")] }
- let(:created_at) { 25.hours.ago }
-
- it "is not cached" do
- get get_path
- expect do
- Timecop.travel 2.days do
- get get_path
- end
- end.to change {
- JSON.parse(last_response.body)
- .dig("_meta", "exists")
- }
+ describe "exits" do
+ it "is true because the work package does exist at the last given timestamp" do
+ expect(subject)
+ .to be_json_eql(true.to_json)
+ .at_path("_meta/exists")
end
end
end
end
- end
- context 'when the timestamps are relative date keywords' do
- let(:timestamps) { [Timestamp.new('oneWeekAgo@12:00+00:00'), Timestamp.now] }
-
- it 'has an embedded link to the baseline work package' do
- expect(subject)
- .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.first).to_json)
- .at_path('_embedded/attributesByTimestamp/0/_links/self/href')
- end
+ describe "when the work package does not exist at the only requested timestamp" do
+ let(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z')] }
+ let(:created_at) { 10.days.ago }
- it 'has the absolute timestamps within the self link' do
- Timecop.freeze do
- expect(subject)
- .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.map(&:absolute)).to_json)
- .at_path('_links/self/href')
+ describe "attributesByTimestamp" do
+ describe "exists" do
+ it "marks the work package as not existing at the requested time" do
+ expect(subject)
+ .to be_json_eql(false.to_json)
+ .at_path("_embedded/attributesByTimestamp/0/_meta/exists")
+ end
+ end
end
- end
- describe "attributesByTimestamp" do
- describe '_meta' do
- describe 'timestamp' do
- it 'has the relative timestamps' do
- expect(subject)
- .to be_json_eql('oneWeekAgo@12:00+00:00'.to_json)
- .at_path('_embedded/attributesByTimestamp/0/_meta/timestamp')
+ describe "_meta" do
+ describe "exits" do
+ it "is false because the work package does not exist at the requested timestamp" do
expect(subject)
- .to be_json_eql('PT0S'.to_json)
- .at_path('_embedded/attributesByTimestamp/1/_meta/timestamp')
+ .to be_json_eql(false.to_json)
+ .at_path("_meta/exists")
end
end
end
@@ -435,18 +350,14 @@ RSpec.describe 'API v3 Work package resource',
before { login_as current_user }
context "with relative timestamps" do
- let(:timestamps) { [Timestamp.parse("oneDayAgo@00:00+00:00"), Timestamp.now] }
+ let(:timestamps) { [Timestamp.parse("P-2D"), Timestamp.now] }
let(:created_at) { Date.parse('2015-01-01') }
describe "attributesByTimestamp" do
it "does not cache the self link" do
get get_path
-
expect do
- # Travel 1 day to test the href not being cached, because the
- # relative date keyword has a fixed hour part, which means the timestamp
- # will change its value only in 1 day units
- Timecop.travel 1.day do
+ Timecop.travel 20.minutes do
get get_path
end
end.to change {
@@ -470,7 +381,7 @@ RSpec.describe 'API v3 Work package resource',
describe "_meta" do
describe "exists" do
- let(:timestamps) { [Timestamp.parse("oneDayAgo@00:00+00:00")] }
+ let(:timestamps) { [Timestamp.parse("P-2D")] }
let(:created_at) { 25.hours.ago }
it "is not cached" do
@@ -488,6 +399,183 @@ RSpec.describe 'API v3 Work package resource',
end
end
end
+
+ context 'when the timestamps are relative date keywords' do
+ let(:timestamps) { [Timestamp.new('oneWeekAgo@12:00+00:00'), Timestamp.now] }
+
+ it 'has an embedded link to the baseline work package' do
+ expect(subject)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.first).to_json)
+ .at_path('_embedded/attributesByTimestamp/0/_links/self/href')
+ end
+
+ it 'has the absolute timestamps within the self link' do
+ Timecop.freeze do
+ expect(subject)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.map(&:absolute)).to_json)
+ .at_path('_links/self/href')
+ end
+ end
+
+ describe "attributesByTimestamp" do
+ describe '_meta' do
+ describe 'timestamp' do
+ it 'has the relative timestamps' do
+ expect(subject)
+ .to be_json_eql('oneWeekAgo@12:00+00:00'.to_json)
+ .at_path('_embedded/attributesByTimestamp/0/_meta/timestamp')
+ expect(subject)
+ .to be_json_eql('PT0S'.to_json)
+ .at_path('_embedded/attributesByTimestamp/1/_meta/timestamp')
+ end
+ end
+ end
+ end
+
+ context "with caching" do
+ before { login_as current_user }
+
+ context "with relative timestamps" do
+ let(:timestamps) { [Timestamp.parse("oneDayAgo@00:00+00:00"), Timestamp.now] }
+ let(:created_at) { Date.parse('2015-01-01') }
+
+ describe "attributesByTimestamp" do
+ it "does not cache the self link" do
+ get get_path
+
+ expect do
+ # Travel 1 day to test the href not being cached, because the
+ # relative date keyword has a fixed hour part, which means the timestamp
+ # will change its value only in 1 day units
+ Timecop.travel 1.day do
+ get get_path
+ end
+ end.to change {
+ JSON.parse(last_response.body)
+ .dig("_embedded", "attributesByTimestamp", 0, "_links", "self", "href")
+ }
+ end
+
+ it "does not cache the attributes" do
+ get get_path
+ expect do
+ Timecop.travel 2.days do
+ get get_path
+ end
+ end.to change {
+ JSON.parse(last_response.body)
+ .dig("_embedded", "attributesByTimestamp", 0, "subject")
+ }
+ end
+ end
+
+ describe "_meta" do
+ describe "exists" do
+ let(:timestamps) { [Timestamp.parse("oneDayAgo@00:00+00:00")] }
+ let(:created_at) { 25.hours.ago }
+
+ it "is not cached" do
+ get get_path
+ expect do
+ Timecop.travel 2.days do
+ get get_path
+ end
+ end.to change {
+ JSON.parse(last_response.body)
+ .dig("_meta", "exists")
+ }
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+
+ context 'without EE' do
+ shared_examples 'success' do
+ it 'responds with 200' do
+ expect(subject && last_response.status).to eq(200)
+ end
+ end
+
+ shared_examples 'error' do
+ it 'responds with 400' do
+ expect(subject && last_response.status).to eq(400)
+ end
+
+ it 'has the invalid timestamps message' do
+ message = JSON.parse(subject)['message']
+ expect(message)
+ .to eq("Bad request: Timestamps contain forbidden values: #{timestamps.join(',')}")
+ end
+ end
+
+ context "when the 'oneDayAgo' value is provided" do
+ let(:timestamps) { [Timestamp.parse("oneDayAgo@12:00+00:00")] }
+
+ it_behaves_like 'success'
+ end
+
+ context "when the shortcut value 'now' is provided" do
+ let(:timestamps) { [Timestamp.parse("now")] }
+
+ it_behaves_like 'success'
+ end
+
+ context "when the 'PT0S' duration value is provided" do
+ let(:timestamps) { [Timestamp.parse("PT0S")] }
+
+ it_behaves_like 'success'
+ end
+
+ context "when the 'P-1D' duration value is provided" do
+ let(:timestamps) { [Timestamp.parse("P-1D")] }
+
+ it_behaves_like 'success'
+ end
+
+ context "when an iso8601 datetime value from yesterday is provided" do
+ let(:timestamps) { [1.day.ago.beginning_of_day.iso8601] }
+
+ it_behaves_like 'success'
+ end
+
+ context "when the 'lastWorkingDay' value is provided and it's yesterday" do
+ let(:timestamps) { [Timestamp.parse("lastWorkingDay@00:00+00:00")] }
+
+ it_behaves_like 'success'
+ end
+
+ Timestamp::ALLOWED_DATE_KEYWORDS[2..].each do |timestamp_date_keyword|
+ context "when the '#{timestamp_date_keyword}' value is provided" do
+ let(:timestamps) { [Timestamp.parse("#{timestamp_date_keyword}@12:00+00:00")] }
+
+ it_behaves_like 'error'
+ end
+ end
+
+ context "when the 'lastWorkingDay' value is provided and it's before yesterday" do
+ let(:timestamps) { [Timestamp.parse("lastWorkingDay@00:00+00:00")] }
+
+ before do
+ allow(Day).to receive(:last_working) { Day.new(date: 7.days.ago) }
+ end
+
+ it_behaves_like 'error'
+ end
+
+ context "when a duration value older than yesterday is provided" do
+ let(:timestamps) { [Timestamp.parse("P-2D")] }
+
+ it_behaves_like 'error'
+ end
+
+ context "when an iso8601 datetime value older than yesterday is provided" do
+ let(:timestamps) { [2.days.ago.end_of_day.iso8601] }
+
+ it_behaves_like 'error'
+ end
end
end
end