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

[64181] Add SharePoint storage contract (#19670)

Yauheni Suhakoueff0bc0cd8e5
diff --git a/modules/storages/app/common/storages/adapters/providers/share_point/share_point_contract.rb b/modules/storages/app/common/storages/adapters/providers/share_point/share_point_contract.rb
new file mode 100644
index 00000000000..17deae142ca
--- /dev/null
+++ b/modules/storages/app/common/storages/adapters/providers/share_point/share_point_contract.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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
+ module Adapters
+ module Providers
+ module SharePoint
+ class SharePointContract < ::ModelContract
+ attribute :name
+ validates :name, presence: true, length: { maximum: 255 }
+
+ attribute :host
+ validates :host, presence: true, format: { with: URI::DEFAULT_PARSER.make_regexp }
+ validates :host,
+ format: { with: %r{\Ahttps://[^/]+/sites/[^/]+(/.*)?\z}i,
+ message: I18n.t("activerecord.errors.messages.invalid_sharepoint_url") }
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/adapters/providers/share_point/share_point_registry.rb b/modules/storages/app/common/storages/adapters/providers/share_point/share_point_registry.rb
index 937d832a049..618b55a6ec2 100644
--- a/modules/storages/app/common/storages/adapters/providers/share_point/share_point_registry.rb
+++ b/modules/storages/app/common/storages/adapters/providers/share_point/share_point_registry.rb
@@ -37,6 +37,10 @@ module Storages
register(:userless, ->(use_cache = true) { Input::Strategy.build(key: :oauth_client_credentials, use_cache:) })
register(:user_bound, ->(user, storage = nil) { Input::Strategy.build(key: :oauth_user_token, user:, storage:) })
end
+
+ namespace("contracts") do
+ register(:storage, SharePointContract)
+ end
end
end
end
diff --git a/modules/storages/app/models/storages/share_point_storage.rb b/modules/storages/app/models/storages/share_point_storage.rb
index cdcdbfdba46..ce6b700c79d 100644
--- a/modules/storages/app/models/storages/share_point_storage.rb
+++ b/modules/storages/app/models/storages/share_point_storage.rb
@@ -30,9 +30,11 @@
module Storages
class SharePointStorage < Storage
- store_attribute :provider_fields, :tenant_id, :string
-
- def self.visible? = false
+ # For now SharePoint is visible only in tests.
+ # This is to prevent it from being shown in the UI, as it is not ready yet.
+ def self.visible?
+ Rails.env.test?
+ end
def self.short_provider_name = :share_point
def audience = nil
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index 4a9a4a4ae11..5e8f5cff359 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -22,6 +22,11 @@ en:
project_folder_mode: Project folder mode
storage: Storage
storage_url: Storage URL
+ storages/share_point_storage:
+ host: Sharepoint Site URL
+ library: Library ID
+ name: Name
+ site: Site ID
storages/storage:
authentication_method: Authentication Method
creator: Creator
@@ -33,6 +38,7 @@ en:
errors:
messages:
invalid_host_url: is not a valid URL.
+ invalid_sharepoint_url: is not a valid SharePoint site, library, or document URL.
not_linked_to_project: is not linked to project.
models:
storages/file_link:
diff --git a/modules/storages/spec/common/storages/adapters/providers/share_point/share_point_contract_spec.rb b/modules/storages/spec/common/storages/adapters/providers/share_point/share_point_contract_spec.rb
new file mode 100644
index 00000000000..66e4f4de8e2
--- /dev/null
+++ b/modules/storages/spec/common/storages/adapters/providers/share_point/share_point_contract_spec.rb
@@ -0,0 +1,104 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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"
+require_module_spec_helper
+
+module Storages
+ module Adapters
+ module Providers
+ module SharePoint
+ RSpec.describe SharePointContract, :storage_server_helpers, :webmock do
+ let(:current_user) { create(:admin) }
+ let(:storage) { build(:share_point_storage) }
+
+ # As the SharePointContract is selected by the BaseContract to make writable attributes available,
+ # the BaseContract needs to be instantiated here.
+ subject(:contract) { Storages::BaseContract.new(storage, current_user) }
+
+ it "is valid with all required attributes" do
+ expect(subject).to be_valid
+ end
+
+ context "with missing name" do
+ before do
+ storage.name = nil
+ end
+
+ it "is not valid" do
+ expect(subject).not_to be_valid
+ expect(subject.errors[:name]).to be_present
+ end
+ end
+
+ describe "host" do
+ context "with valid SharePoint site url" do
+ it "is valid" do
+ expect(contract).to be_valid
+ end
+ end
+
+ context "with valid SharePoint library url" do
+ before do
+ storage.host = "https://openproject.sharepoint.com/sites/ProjectX/Documents/Report.rdl"
+ end
+
+ it "is valid" do
+ expect(contract).to be_valid
+ end
+ end
+
+ context "with invalid url" do
+ before do
+ storage.host = "not-a-url"
+ end
+
+ it "is not valid" do
+ expect(subject).not_to be_valid
+ expect(subject.errors[:host]).to include("is invalid.")
+ end
+ end
+
+ context "with invalid sharepoint structure" do
+ before do
+ storage.host = "https://openproject.sharepoint.com/invalidpath"
+ end
+
+ it "is not valid" do
+ expect(subject).not_to be_valid
+ expect(subject.errors[:host]).to include("is not a valid SharePoint site, library, or document URL.")
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rb
index c99c8256c8e..6be6b0dea7f 100644
--- a/modules/storages/spec/factories/storage_factory.rb
+++ b/modules/storages/spec/factories/storage_factory.rb
@@ -223,4 +223,19 @@ FactoryBot.define do
token_type: "bearer")
end
end
+
+ factory :share_point_storage,
+ parent: :storage,
+ class: "::Storages::SharePointStorage" do
+ host { "https://openproject.sharepoint.com/sites/ProjectX" }
+ automatically_managed { false }
+
+ trait :as_automatically_managed do
+ automatically_managed { true }
+ end
+
+ trait :with_tenant_id do
+ tenant_id { SecureRandom.uuid }
+ end
+ end
end