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

[chore] change of logic for nextcloud storage hosts

Eric Schubert0541c04d0270
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index 07e02e575ab..d98d8ee83f3 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -160,7 +160,11 @@ module Storages
def uri
return unless host
- @uri ||= URI(host).normalize
+ @uri ||= if host.end_with?("/")
+ URI(host).normalize
+ else
+ URI("#{host}/").normalize
+ end
end
def connect_src
diff --git a/modules/storages/app/services/storages/storages/set_attributes_service.rb b/modules/storages/app/services/storages/storages/set_attributes_service.rb
index f35f6d5ab58..93ce25e4116 100644
--- a/modules/storages/app/services/storages/storages/set_attributes_service.rb
+++ b/modules/storages/app/services/storages/storages/set_attributes_service.rb
@@ -29,7 +29,7 @@
# See also: create_service.rb for comments
module Storages::Storages
class SetAttributesService < ::BaseServices::SetAttributes
- after_call :remove_host_trailing_slashes
+ after_call :sanitize_host
def set_default_attributes(_params)
storage.creator ||= user
@@ -38,22 +38,19 @@ module Storages::Storages
private
def set_attributes(params)
- super(replace_empty_host_with_nil(params))
+ super
unset_nextcloud_application_credentials if nextcloud_storage?
end
- def remove_host_trailing_slashes
- storage.host = storage.host&.gsub(/\/+$/, "")
- end
-
- def replace_empty_host_with_nil(params)
- cloned_param = params.clone
-
- if cloned_param[:host] == ""
- cloned_param[:host] = nil
- end
-
- cloned_param
+ def sanitize_host
+ host_input = storage.host
+ storage.host = if host_input.present? && !host_input.ends_with?("/")
+ "#{host_input}/"
+ elsif host_input == ""
+ nil
+ else
+ host_input
+ end
end
def unset_nextcloud_application_credentials
diff --git a/modules/storages/spec/models/storages/storage_spec.rb b/modules/storages/spec/models/storages/storage_spec.rb
index 6bd2bb9529a..4571d70480c 100644
--- a/modules/storages/spec/models/storages/storage_spec.rb
+++ b/modules/storages/spec/models/storages/storage_spec.rb
@@ -115,4 +115,27 @@ RSpec.describe Storages::Storage do
end
end
end
+
+ describe "uri" do
+ it "returns nil if host is nil" do
+ storage = build(:storage, host: nil)
+ expect(storage.uri).to be_nil
+ end
+
+ it "returns host with trailing slash" do
+ storage = build(:storage, host: "https://example.com")
+ expect(storage.uri.to_s).to eq("https://example.com/")
+ storage = build(:storage, host: "https://endor")
+ expect(storage.uri.to_s).to eq("https://endor/")
+ storage = build(:storage, host: "https://deathstar.org/html")
+ expect(storage.uri.to_s).to eq("https://deathstar.org/html/")
+
+ storage = build(:storage, host: "https://example.com/")
+ expect(storage.uri.to_s).to eq("https://example.com/")
+ storage = build(:storage, host: "https://endor/")
+ expect(storage.uri.to_s).to eq("https://endor/")
+ storage = build(:storage, host: "https://deathstar.org/html/")
+ expect(storage.uri.to_s).to eq("https://deathstar.org/html/")
+ end
+ end
end
diff --git a/modules/storages/spec/services/storages/storages/set_attributes_service_spec.rb b/modules/storages/spec/services/storages/storages/set_attributes_service_spec.rb
index e5fe1297283..fcd84d1bdcc 100644
--- a/modules/storages/spec/services/storages/storages/set_attributes_service_spec.rb
+++ b/modules/storages/spec/services/storages/storages/set_attributes_service_spec.rb
@@ -88,13 +88,33 @@ RSpec.describe Storages::Storages::SetAttributesService, type: :model do
expect(subject.result.provider_type).to eq Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
end
- context "when setting host" do
+ context "with host" do
before do
- params[:host] = "https://some.host.com//"
+ params[:host] = host_input
end
- it "removes trailing slashes from host" do
- expect(subject.result.host).to eq("https://some.host.com")
+ context "if host input has a trailing slashes" do
+ let(:host_input) { "https://some.host.com//" }
+
+ it "keeps a trailing slash" do
+ expect(subject.result.host).to eq(host_input)
+ end
+ end
+
+ context "if host input is empty string" do
+ let(:host_input) { "" }
+
+ it "sets host to nil" do
+ expect(subject.result.host).to be_nil
+ end
+ end
+
+ context "if host input has no trailing slash" do
+ let(:host_input) { "https://some.host.com" }
+
+ it "adds a trailing slash" do
+ expect(subject.result.host).to eq("https://some.host.com/")
+ end
end
end
end