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

improve validation error messages of Storages::Storage

Christophe Bliard8b6b19538a72
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 31432024b57..9f8e3cfdd4c 100644
--- a/modules/storages/app/contracts/storages/file_links/create_contract.rb
+++ b/modules/storages/app/contracts/storages/file_links/create_contract.rb
@@ -47,6 +47,6 @@ class Storages::FileLinks::CreateContract < BaseContract
def validate_project_storage_link
storage_is_not_linked = model.storage.present? && model.project.storages.exclude?(model.storage)
- errors.add(:storage_id, 'not linked to project.') if storage_is_not_linked
+ errors.add(:storage_id, :not_linked_to_project) if storage_is_not_linked
end
end
diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rb
index 2c601b8cb9e..d2dfb0b6489 100644
--- a/modules/storages/app/contracts/storages/storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb
@@ -41,10 +41,10 @@ module Storages::Storages
include ActiveModel::Validations
attribute :name
- validates :name, length: { minimum: 1, maximum: 255 }, allow_nil: false
+ validates :name, presence: true, length: { maximum: 255 }
attribute :provider_type
- validates :provider_type, inclusion: { in: ->(*) { Storages::Storage::PROVIDER_TYPES } }, allow_nil: false
+ validates :provider_type, inclusion: { in: ->(*) { Storages::Storage::PROVIDER_TYPES } }
attribute :creator, writable: false do
validate_creator_is_user
@@ -69,12 +69,12 @@ module Storages::Storages
response = request_capabilities
unless response.is_a? Net::HTTPSuccess
- errors.add(:host, :invalid)
+ errors.add(:host, :cannot_be_connected_to)
return
end
unless major_version_sufficient?(response)
- errors.add(:host, :invalid)
+ errors.add(:host, :minimal_nextcloud_version_unmet)
end
end
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index 53620d5439f..726a07f8e28 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -14,6 +14,16 @@ en:
creator: "Creator"
provider_type: "Provider type"
+ errors:
+ messages:
+ not_linked_to_project: "is not linked to project."
+ models:
+ storages/storage:
+ attributes:
+ host:
+ cannot_be_connected_to: "can not be connected to."
+ minimal_nextcloud_version_unmet: "does not meet minimal version requirements (must be Nextcloud 23 or higher)"
+
storages:
page_titles:
project_settings:
diff --git a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
index b74d2891be9..d90e2e8eaf8 100644
--- a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
+++ b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
@@ -52,26 +52,26 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true d
describe 'validations' do
context 'when all attributes are valid' do
- it_behaves_like 'contract is valid'
+ include_examples 'contract is valid'
end
context 'when name is invalid' do
context 'as it is too long' do
let(:storage_name) { 'X' * 257 }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', name: :too_long
end
context 'as it is empty' do
let(:storage_name) { '' }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', name: :blank
end
context 'as it is nil' do
let(:storage_name) { nil }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', name: :blank
end
end
@@ -79,19 +79,19 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true d
context 'as it is unknown' do
let(:storage_provider_type) { 'unkwown_provider_type' }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', provider_type: :inclusion
end
context 'as it is empty' do
let(:storage_provider_type) { '' }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', provider_type: :inclusion
end
context 'as it is nil' do
let(:storage_provider_type) { nil }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', provider_type: :inclusion
end
end
@@ -99,19 +99,19 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true d
context 'as host is not a URL' do
let(:storage_host) { '---invalid-url---' }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', host: :url
end
context 'as host is an empty string' do
let(:storage_host) { '' }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', host: :url
end
context 'as host is nil' do
let(:storage_host) { nil }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', host: :url
end
context 'when provider_type is nextcloud' do
@@ -125,25 +125,25 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true d
allow(Net::HTTP).to receive(:start).and_raise(SocketError, 'Failed to open TCP connection (SIMULATED)')
end
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', host: :cannot_be_connected_to
end
context 'when response code is a 404 NOT FOUND' do
let(:host_response_code) { '404' }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', host: :cannot_be_connected_to
end
context 'when response code is a 500 PERMISSION DENIED' do
let(:host_response_code) { '500' }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', host: :cannot_be_connected_to
end
context 'when Nextcloud version is below the required minimal version which is 23' do
let(:host_response_major_version) { '22' }
- it_behaves_like 'contract is invalid'
+ include_examples 'contract is invalid', host: :minimal_nextcloud_version_unmet
end
end
end
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 3ceb1253097..61a1ab03006 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
@@ -152,7 +152,7 @@ describe 'API v3 file links resource', type: :request do
let(:storage_url2) { storage.host }
it_behaves_like 'constraint violation' do
- let(:message) { 'Storage not linked to project' }
+ let(:message) { 'Storage is not linked to project' }
end
end
end