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

[#56892] extracted static open link business logic

Eric Schubert7baea6e9c98c
diff --git a/modules/storages/app/common/storages/errors.rb b/modules/storages/app/common/storages/errors.rb
index 0150d948d25..97c57a1918a 100644
--- a/modules/storages/app/common/storages/errors.rb
+++ b/modules/storages/app/common/storages/errors.rb
@@ -36,6 +36,8 @@ module Storages
class PollingRequired < BaseError; end
+ class ConfigurationError < BaseError; end
+
class MissingContract < ResolverStandardError; end
class OperationNotSupported < ResolverStandardError; end
diff --git a/modules/storages/app/components/storages/admin/general_info_component.rb b/modules/storages/app/components/storages/admin/general_info_component.rb
index 7791c397a34..a97b7e8e726 100644
--- a/modules/storages/app/components/storages/admin/general_info_component.rb
+++ b/modules/storages/app/components/storages/admin/general_info_component.rb
@@ -27,31 +27,25 @@
#
# See COPYRIGHT and LICENSE files for more details.
#++
-#
-module Storages::Admin
- class GeneralInfoComponent < ApplicationComponent
- include OpPrimer::ComponentHelpers
- include OpTurbo::Streamable
- include StorageViewInformation
-
- alias_method :storage, :model
- def self.wrapper_key = :storage_general_info_section
+module Storages
+ module Admin
+ class GeneralInfoComponent < ApplicationComponent
+ include OpPrimer::ComponentHelpers
+ include OpTurbo::Streamable
+ include StorageViewInformation
- def open_href
- url = ::API::V3::Utilities::PathHelper::ApiV3Path.storage_open(storage.id)
+ alias_method :storage, :model
- return url if storage.provider_type_nextcloud?
+ def self.wrapper_key = :storage_general_info_section
- oauth_clients_ensure_connection_url(
- oauth_client_id: storage.oauth_client.client_id,
- storage_id: storage.id,
- destination_url: url
- )
- end
+ def open_href
+ OpenStorageLinks.static_link(storage)
+ end
- def can_show_open_link?
- storage.provider_type_nextcloud? || storage.oauth_client.present?
+ def can_show_open_link?
+ OpenStorageLinks.can_generate_static_link?(storage)
+ end
end
end
end
diff --git a/modules/storages/app/helpers/storages/open_storage_links.rb b/modules/storages/app/helpers/storages/open_storage_links.rb
new file mode 100644
index 00000000000..ddc50c4c276
--- /dev/null
+++ b/modules/storages/app/helpers/storages/open_storage_links.rb
@@ -0,0 +1,67 @@
+# frozen_string_literal:true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+module Storages
+ class OpenStorageLinks
+ class << self
+ include OpenProject::StaticRouting::UrlHelpers
+
+ def static_link(storage)
+ api_static_link = ::API::V3::Utilities::PathHelper::ApiV3Path.storage_open(storage.id)
+
+ case storage.provider_type
+ when Storage::PROVIDER_TYPE_NEXTCLOUD
+ api_static_link
+ when Storage::PROVIDER_TYPE_ONE_DRIVE
+ raise Errors::ConfigurationError, "No OAuth credential information configured." if storage.oauth_client.nil?
+
+ oauth_clients_ensure_connection_url(
+ oauth_client_id: storage.oauth_client.client_id,
+ storage_id: storage.id,
+ destination_url: api_static_link
+ )
+ else
+ raise ArgumentError, "Cannot generate static open link for storage provider type: #{storage.provider_type}"
+ end
+ end
+
+ def can_generate_static_link?(storage)
+ case storage.provider_type
+ when Storage::PROVIDER_TYPE_NEXTCLOUD
+ true
+ when Storage::PROVIDER_TYPE_ONE_DRIVE
+ storage.oauth_client.present?
+ else
+ false
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/common/storages/open_storage_links_spec.rb b/modules/storages/spec/common/storages/open_storage_links_spec.rb
new file mode 100644
index 00000000000..9836f059fb7
--- /dev/null
+++ b/modules/storages/spec/common/storages/open_storage_links_spec.rb
@@ -0,0 +1,113 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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"
+
+RSpec.describe Storages::OpenStorageLinks do
+ describe "static_link" do
+ subject { described_class.static_link(storage) }
+
+ context "if storage is of provider type nextcloud" do
+ let(:storage) { create(:nextcloud_storage) }
+
+ it "returns the API static link" do
+ expect(subject).to eq("/api/v3/storages/#{storage.id}/open")
+ end
+ end
+
+ context "if storage is of provider type one drive" do
+ context "if storage has configured oauth credentials" do
+ let(:oauth_client) { create(:oauth_client) }
+ let(:storage) { create(:one_drive_storage, oauth_client:) }
+
+ it "returns the 'ensure connections' link" do
+ expect(subject).to end_with("/oauth_clients/#{oauth_client.client_id}/ensure_connection?" \
+ "destination_url=%2Fapi%2Fv3%2Fstorages%2F#{storage.id}%2Fopen&" \
+ "storage_id=#{storage.id}")
+ end
+ end
+
+ context "if storage is not fully configured" do
+ let(:storage) { create(:one_drive_storage) }
+
+ it "raises an error" do
+ expect { subject }.to raise_error(Storages::Errors::ConfigurationError)
+ end
+ end
+ end
+
+ context "if storage is of an unknown provider type" do
+ let(:storage) { create(:storage, provider_type: "Storage::Dropbox") }
+
+ it "raises an error" do
+ expect { subject }.to raise_error(ArgumentError)
+ end
+ end
+ end
+
+ describe "can_generate_static_link?" do
+ subject { described_class.can_generate_static_link?(storage) }
+
+ context "if storage is of provider type nextcloud" do
+ let(:storage) { create(:nextcloud_storage) }
+
+ it "returns true" do
+ expect(subject).to be_truthy
+ end
+ end
+
+ context "if storage is of provider type one drive" do
+ context "if storage has configured oauth credentials" do
+ let(:oauth_client) { create(:oauth_client) }
+ let(:storage) { create(:one_drive_storage, oauth_client:) }
+
+ it "returns true" do
+ expect(subject).to be_truthy
+ end
+ end
+
+ context "if storage is not fully configured" do
+ let(:storage) { create(:one_drive_storage) }
+
+ it "returns false" do
+ expect(subject).to be_falsy
+ end
+ end
+ end
+
+ context "if storage is of an unknown provider type" do
+ let(:storage) { create(:storage, provider_type: "Storage::Dropbox") }
+
+ it "returns false" do
+ expect(subject).to be_falsy
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/components/storages/admin/general_info_component_spec.rb b/modules/storages/spec/components/storages/admin/general_info_component_spec.rb
index b54d4bb97a8..2b9b508b991 100644
--- a/modules/storages/spec/components/storages/admin/general_info_component_spec.rb
+++ b/modules/storages/spec/components/storages/admin/general_info_component_spec.rb
@@ -37,8 +37,8 @@ RSpec.describe Storages::Admin::GeneralInfoComponent, type: :component do
it "must show a link to the storage" do
storage = create(:nextcloud_storage)
component = described_class.new(storage)
- expect(component.open_link_was_generated).to be_truthy
- expect(component.open_href).not_to be_nil
+ expect(component).to be_can_show_open_link
+ expect(component.open_href).to eq("/api/v3/storages/#{storage.id}/open")
end
end
end