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

Started implementing a FileLinkRepresenter parsing

Christophe Bliardf7f9bede6709
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index cdfc651842b..f7079fa8e94 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -41,7 +41,10 @@ module API
property :originData,
exec_context: :decorator,
- getter: ->(*) { make_origin_data(represented) }
+ getter: ->(*) { make_origin_data(represented) },
+ setter: ->(fragment:, **) do
+ represented.assign_attributes(parse_origin_data(fragment))
+ end
link :self do
{
@@ -103,6 +106,17 @@ module API
lastModifiedByName: model.origin_last_modified_by_name
}
end
+
+ def parse_origin_data(origin_data)
+ {
+ origin_id: origin_data["id"].to_s,
+ origin_name: origin_data["name"],
+ origin_mime_type: origin_data["mimeType"],
+ origin_created_by_name: origin_data["createdByName"],
+ origin_last_modified_by_name: origin_data["lastModifiedByName"]
+ # createdAt and lastModifiedAt to be done
+ }
+ end
end
end
end
diff --git a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb
new file mode 100644
index 00000000000..a1e003994fd
--- /dev/null
+++ b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb
@@ -0,0 +1,76 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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::FileLinks::FileLinkRepresenter, 'parsing' do
+ # include ::API::V3::Utilities::PathHelper
+
+ let(:file_link) { build_stubbed(:file_link) }
+
+ current_user { build_stubbed(:user) }
+
+ describe 'parsing' do
+ subject(:parsed) { representer.from_hash parsed_hash }
+
+ let(:representer) do
+ described_class.create(file_link, current_user: current_user)
+ end
+
+ context 'with originData given' do
+ let(:parsed_hash) do
+ {
+ "_type" => "FileLink",
+ "createdAt" => "2022-02-09T10:03:37Z",
+ "updatedAt" => "2022-02-09T10:03:37Z",
+ "originData" => {
+ "id" => 5503,
+ "name" => "logo.png",
+ "mimeType" => "image/png",
+ "createdAt" => "2021-12-19T09:42:10.170Z",
+ "lastModifiedAt" => "2021-12-20T14:00:13.987Z",
+ "createdByName" => "Luke Skywalker",
+ "lastModifiedByName" => "Anakin Skywalker"
+ }
+ }
+ end
+
+ it 'parses correctly', pending: "work in progress" do
+ expect(parsed).to have_attributes(
+ origin_id: "5503",
+ origin_name: "logo.png",
+ origin_mime_type: "image/png",
+ origin_created_by_name: "Luke Skywalker",
+ origin_last_modified_by_name: "Anakin Skywalker",
+ origin_created_at: "to be done",
+ orig_updated_at: "to be done"
+ )
+ end
+ end
+ end
+end
diff --git a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb b/spec/lib/api/v3/utilities/date_time_formatter_spec.rb
index 069369136ca..68ef36e3472 100644
--- a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb
+++ b/spec/lib/api/v3/utilities/date_time_formatter_spec.rb
@@ -79,14 +79,20 @@ describe :DateTimeFormatter do
bad_format = date.strftime('%d.%m.%Y')
expect do
subject.parse_date(bad_format, 'prop')
- end.to raise_error(API::Errors::PropertyFormatError)
+ end.to raise_error(API::Errors::PropertyFormatError) do |error| # rubocop:disable Style/MultilineBlockChain
+ expect(error.message).to include("Invalid format for property 'prop'")
+ expect(error.message).to include("Expected format like 'YYYY-MM-DD (ISO 8601 date only)'")
+ end
end
it 'rejects parsing ISO 8601 date + time formats' do
bad_format = datetime.iso8601
expect do
subject.parse_date(bad_format, 'prop')
- end.to raise_error(API::Errors::PropertyFormatError)
+ end.to raise_error(API::Errors::PropertyFormatError) do |error| # rubocop:disable Style/MultilineBlockChain
+ expect(error.message).to include("Invalid format for property 'prop'")
+ expect(error.message).to include("Expected format like 'YYYY-MM-DD (ISO 8601 date only)'")
+ end
end
it_behaves_like 'can parse nil' do