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

differentiate between storage errors 'is blank' and 'does not exist'

Christophe Bliarde3b7d4b64cdc
diff --git a/modules/storages/app/contracts/storages/file_links/create_contract.rb b/modules/storages/app/contracts/storages/file_links/create_contract.rb
index 063b24d8e05..c8735429840 100644
--- a/modules/storages/app/contracts/storages/file_links/create_contract.rb
+++ b/modules/storages/app/contracts/storages/file_links/create_contract.rb
@@ -42,11 +42,18 @@ class Storages::FileLinks::CreateContract < BaseContract
end
def validate_storage_presence
- errors.add(:storage, :invalid) if model.storage.nil?
+ case model.storage
+ when Storages::Storage::InexistentStorage
+ errors.add(:storage, :does_not_exist)
+ when nil
+ errors.add(:storage, :blank)
+ end
end
def validate_project_storage_link
- storage_is_not_linked = model.storage.present? && model.project.storages.exclude?(model.storage)
- errors.add(:storage, :not_linked_to_project) if storage_is_not_linked
+ return if errors.include?(:storage)
+ return if model.project.storages.include?(model.storage)
+
+ errors.add(:storage, :not_linked_to_project)
end
end
diff --git a/modules/storages/app/models/storages/storage/inexistent_storage.rb b/modules/storages/app/models/storages/storage/inexistent_storage.rb
new file mode 100644
index 00000000000..0e7f229064c
--- /dev/null
+++ b/modules/storages/app/models/storages/storage/inexistent_storage.rb
@@ -0,0 +1,31 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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.
+#++
+
+class Storages::Storage::InexistentStorage < Storages::Storage
+ _validators.clear
+end
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 0ba53c49c40..fa56ca13615 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
@@ -84,8 +84,11 @@ module API
skip_render: ->(*) { true },
getter: ->(*) {},
setter: ->(fragment:, **) {
+ break if fragment['href'].blank?
+
canonical_url = fragment['href'].gsub(/\/+$/, '')
represented.storage = ::Storages::Storage.find_by(host: canonical_url)
+ represented.storage ||= ::Storages::Storage::InexistentStorage.new(host: canonical_url)
}
associated_resource :container,
diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
index 3b5c8a46f95..8c6b8c72a77 100644
--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
@@ -143,7 +143,23 @@ describe 'API v3 file links resource', type: :request do
let(:storage_url1) { 'https://invalid.host.org/' }
it_behaves_like 'constraint violation' do
- let(:message) { 'Storage is invalid' }
+ let(:message) { 'Storage does not exist' }
+ end
+ end
+
+ context 'when nil' do
+ let(:storage_url1) { nil }
+
+ it_behaves_like 'constraint violation' do
+ let(:message) { "Storage can't be blank." }
+ end
+ end
+
+ context 'when empty' do
+ let(:storage_url1) { "" }
+
+ it_behaves_like 'constraint violation' do
+ let(:message) { "Storage can't be blank." }
end
end