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] removed configuration check concern

Eric Schubert71672603fb73
diff --git a/modules/storages/app/models/storages/nextcloud_storage.rb b/modules/storages/app/models/storages/nextcloud_storage.rb
index c175466ab82..126827992b8 100644
--- a/modules/storages/app/models/storages/nextcloud_storage.rb
+++ b/modules/storages/app/models/storages/nextcloud_storage.rb
@@ -55,6 +55,19 @@ class Storages::NextcloudStorage < Storages::Storage
end
end
+ scope :configured, -> do
+ where.associated(:oauth_client, :oauth_application)
+ .where("storages.host IS NOT NULL AND storages.name IS NOT NULL")
+ end
+
+ def configuration_checks
+ {
+ storage_oauth_client_configured: oauth_client.present?,
+ openproject_oauth_application_configured: oauth_application.present?,
+ host_name_configured: host.present? && name.present?
+ }
+ end
+
def automatic_management_unspecified?
automatically_managed.nil?
end
diff --git a/modules/storages/app/models/storages/one_drive_storage.rb b/modules/storages/app/models/storages/one_drive_storage.rb
index 686ad52220a..77fa281fa36 100644
--- a/modules/storages/app/models/storages/one_drive_storage.rb
+++ b/modules/storages/app/models/storages/one_drive_storage.rb
@@ -32,5 +32,17 @@ module Storages
class OneDriveStorage < Storage
store_attribute :provider_fields, :tenant_id, :string, default: 'consumers'
store_attribute :provider_fields, :drive_id, :string
+
+ scope :configured, -> do
+ if OpenProject::FeatureDecisions.storage_one_drive_integration_active?
+ where.associated(:oauth_client).where.not(storages: { name: nil })
+ else
+ none
+ end
+ end
+
+ def configuration_checks
+ { storage_oauth_client_configured: oauth_client.present? }
+ end
end
end
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index de84381138b..0dabfe6a6c8 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -39,8 +39,6 @@
class Storages::Storage < ApplicationRecord
self.inheritance_column = :provider_type
- include ::Storages::Common::ConfigurationChecks
-
# One Storage can have multiple FileLinks, representing external files.
#
# FileLink deletion is done:
@@ -82,6 +80,10 @@ class Storages::Storage < ApplicationRecord
end
end
+ scope :configured, -> do
+ subclasses.map { |clazz| clazz.configured.to_a }.flatten
+ end
+
scope :not_enabled_for_project, ->(project) do
where.not(id: project.project_storages.pluck(:storage_id))
end
@@ -97,6 +99,14 @@ class Storages::Storage < ApplicationRecord
end
end
+ def configured?
+ configuration_checks.values.all?
+ end
+
+ def configuration_checks
+ raise NotImplementedError
+ end
+
def short_provider_type
@short_provider_type ||= self.class.shorten_provider_type(provider_type)
end
diff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rb
index 4a8e18f93fc..b91b0e4a6aa 100644
--- a/modules/storages/spec/factories/storage_factory.rb
+++ b/modules/storages/spec/factories/storage_factory.rb
@@ -29,31 +29,31 @@
#++
FactoryBot.define do
- factory :storage, class: 'Storages::Storage' do
- provider_type { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }
+ factory :storage, class: '::Storages::Storage' do
sequence(:name) { |n| "Storage #{n}" }
- sequence(:host) { |n| "https://host#{n}.example.com" }
creator factory: :user
- factory :one_drive_storage, class: "Storages::OneDriveStorage" do
- host { nil }
- creator factory: :user
- provider_type { Storages::Storage::PROVIDER_TYPE_ONE_DRIVE }
-
- trait :with_oauth_client do
- oauth_client
- end
+ trait :with_oauth_client do
+ oauth_client
end
- factory :nextcloud_storage, class: 'Storages::NextcloudStorage' do
+ factory :nextcloud_storage, class: '::Storages::NextcloudStorage' do
+ provider_type { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }
+ sequence(:host) { |n| "https://host#{n}.example.com" }
+
trait :as_automatically_managed do
automatically_managed { true }
username { 'OpenProject' }
password { 'Password123' }
end
+
trait :as_not_automatically_managed do
automatically_managed { false }
end
end
+
+ factory :one_drive_storage, class: '::Storages::OneDriveStorage' do
+ provider_type { Storages::Storage::PROVIDER_TYPE_ONE_DRIVE }
+ end
end
end
diff --git a/modules/storages/spec/models/common/configuration_checks_spec.rb b/modules/storages/spec/models/common/configuration_checks_spec.rb
deleted file mode 100644
index 2b835962550..00000000000
--- a/modules/storages/spec/models/common/configuration_checks_spec.rb
+++ /dev/null
@@ -1,96 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2023 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_relative '../../spec_helper'
-
-RSpec.describe Storages::Common::ConfigurationChecks do
- describe '.configured' do
- let!(:configured_storage) do
- storage = create(:storage)
- create(:oauth_application, integration: storage)
- create(:oauth_client, integration: storage)
- storage
- end
- let!(:unconfigured_storage) { create(:storage) }
-
- it 'returns only storages with complete configuration' do
- configured_storage_model = configured_storage.provider_type.constantize.find(configured_storage.id)
- unconfigured_storage_model = unconfigured_storage.provider_type.constantize.find(unconfigured_storage.id)
-
- expect(Storages::Storage.configured).to contain_exactly(configured_storage_model)
- expect(Storages::Storage.configured).not_to include(unconfigured_storage_model)
- end
- end
-
- describe '#configured?' do
- context 'with a complete configuration' do
- let(:storage) do
- build_stubbed(:storage,
- oauth_application: build_stubbed(:oauth_application),
- oauth_client: build_stubbed(:oauth_client))
- end
-
- it 'returns true' do
- expect(storage.configured?).to be(true)
-
- aggregate_failures 'configuration_checks' do
- expect(storage.configuration_checks)
- .to eq(host_name_configured: true,
- storage_oauth_client_configured: true,
- openproject_oauth_application_configured: true)
- end
- end
- end
-
- context 'without host name' do
- let(:storage) { build_stubbed(:storage, host: nil, name: nil) }
-
- it 'returns false' do
- aggregate_failures do
- expect(storage.configured?).to be(false)
- aggregate_failures 'configuration_checks' do
- expect(storage.configuration_checks[:host_name_configured]).to be(false)
- end
- end
- end
- end
-
- context 'without openproject and storage integrations' do
- let(:storage) { build_stubbed(:storage) }
-
- it 'returns false' do
- expect(storage.configured?).to be(false)
-
- aggregate_failures 'configuration_checks' do
- expect(storage.configuration_checks[:openproject_oauth_application_configured]).to be(false)
- expect(storage.configuration_checks[:storage_oauth_client_configured]).to be(false)
- end
- end
- end
- end
-end
diff --git a/modules/storages/spec/models/file_link_spec.rb b/modules/storages/spec/models/file_link_spec.rb
index 216b3a08bf0..2a234647d33 100644
--- a/modules/storages/spec/models/file_link_spec.rb
+++ b/modules/storages/spec/models/file_link_spec.rb
@@ -35,7 +35,7 @@ RSpec.describe Storages::FileLink do
let(:creator) { create(:user) }
let(:project) { create(:project) }
let(:work_package) { create(:work_package, project:) }
- let(:storage) { create(:storage) }
+ let(:storage) { create(:nextcloud_storage) }
let(:attributes) do
{
storage:,
diff --git a/modules/storages/spec/models/nextcloud_storage_spec.rb b/modules/storages/spec/models/nextcloud_storage_spec.rb
index dec27dd2676..25a03819a16 100644
--- a/modules/storages/spec/models/nextcloud_storage_spec.rb
+++ b/modules/storages/spec/models/nextcloud_storage_spec.rb
@@ -29,11 +29,66 @@
#++
require 'spec_helper'
+require_relative 'shared_base_storage_spec'
require_module_spec_helper
RSpec.describe Storages::NextcloudStorage do
let(:storage) { build(:nextcloud_storage) }
+ it_behaves_like 'base storage'
+
+ describe '#provider_type?' do
+ it { expect(storage).to be_a_provider_type_nextcloud }
+ it { expect(storage).not_to be_a_provider_type_one_drive }
+ end
+
+ describe '#configured?' do
+ context 'with a complete configuration' do
+ let(:storage) do
+ build(:nextcloud_storage,
+ oauth_application: build(:oauth_application),
+ oauth_client: build(:oauth_client))
+ end
+
+ it 'returns true' do
+ expect(storage.configured?).to be(true)
+
+ aggregate_failures 'configuration_checks' do
+ expect(storage.configuration_checks)
+ .to eq(host_name_configured: true,
+ storage_oauth_client_configured: true,
+ openproject_oauth_application_configured: true)
+ end
+ end
+ end
+
+ context 'without host name' do
+ let(:storage) { build(:nextcloud_storage, host: nil, name: nil) }
+
+ it 'returns false' do
+ aggregate_failures do
+ expect(storage.configured?).to be(false)
+ aggregate_failures 'configuration_checks' do
+ expect(storage.configuration_checks[:host_name_configured]).to be(false)
+ end
+ end
+ end
+ end
+
+ context 'without openproject and storage integrations' do
+ let(:storage) { build(:nextcloud_storage) }
+
+ it 'returns false' do
+ expect(storage.configured?).to be(false)
+
+ aggregate_failures 'configuration_checks' do
+ expect(storage.configuration_checks[:openproject_oauth_application_configured]).to be(false)
+ expect(storage.configuration_checks[:storage_oauth_client_configured]).to be(false)
+ end
+ end
+ end
+ end
+
shared_examples 'a stored attribute with default value' do |attribute, default_value|
context "when the provider fields are empty" do
let(:storage) { build(:nextcloud_storage, provider_fields: {}) }
@@ -149,9 +204,7 @@ RSpec.describe Storages::NextcloudStorage do
let(:storage) { build(:nextcloud_storage) }
it 'returns the default values for nextcloud' do
- expect(storage.provider_fields_defaults).to eq(
- { automatically_managed: true, username: 'OpenProject' }
- )
+ expect(storage.provider_fields_defaults).to eq({ automatically_managed: true, username: 'OpenProject' })
end
end
end
diff --git a/modules/storages/spec/models/one_drive_storage_spec.rb b/modules/storages/spec/models/one_drive_storage_spec.rb
new file mode 100644
index 00000000000..47d4f45980b
--- /dev/null
+++ b/modules/storages/spec/models/one_drive_storage_spec.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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_relative 'shared_base_storage_spec'
+
+RSpec.describe Storages::OneDriveStorage do
+ let(:storage) { build(:one_drive_storage) }
+
+ it_behaves_like 'base storage'
+
+ describe '#provider_type?' do
+ it { expect(storage).to be_a_provider_type_one_drive }
+ it { expect(storage).not_to be_a_provider_type_nextcloud }
+ end
+
+ describe '#configured?' do
+ context 'with a complete configuration' do
+ let(:storage) { build(:one_drive_storage, oauth_client: build(:oauth_client)) }
+
+ it 'returns true' do
+ expect(storage.configured?).to be(true)
+
+ aggregate_failures 'configuration_checks' do
+ expect(storage.configuration_checks)
+ .to eq(storage_oauth_client_configured: true)
+ end
+ end
+ end
+
+ context 'without oauth client' do
+ let(:storage) { build(:one_drive_storage) }
+
+ it 'returns false' do
+ expect(storage.configured?).to be(false)
+
+ aggregate_failures 'configuration_checks' do
+ expect(storage.configuration_checks[:storage_oauth_client_configured]).to be(false)
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/models/project_storage_spec.rb b/modules/storages/spec/models/project_storage_spec.rb
index 2d7c0c72471..3f4cf7a65cf 100644
--- a/modules/storages/spec/models/project_storage_spec.rb
+++ b/modules/storages/spec/models/project_storage_spec.rb
@@ -34,7 +34,7 @@ require_module_spec_helper
RSpec.describe Storages::ProjectStorage do
let(:creator) { create(:user) }
let(:project) { create(:project, enabled_module_names: %i[storages work_packages]) }
- let(:storage) { create(:storage) }
+ let(:storage) { create(:nextcloud_storage) }
let(:attributes) do
{
storage:,
diff --git a/modules/storages/spec/models/storage_spec.rb b/modules/storages/spec/models/shared_base_storage_spec.rb
similarity index 72%
rename from modules/storages/spec/models/storage_spec.rb
rename to modules/storages/spec/models/shared_base_storage_spec.rb
index 41040847e26..aa18563b08e 100644
--- a/modules/storages/spec/models/storage_spec.rb
+++ b/modules/storages/spec/models/shared_base_storage_spec.rb
@@ -28,16 +28,13 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require 'spec_helper'
-require_module_spec_helper
-
-RSpec.describe Storages::Storage do
- let(:creator) { create(:user) }
+RSpec.shared_examples_for 'base storage' do
let(:default_attributes) do
- { name: "NC 1",
- provider_type: Storages::Storage::PROVIDER_TYPE_NEXTCLOUD,
+ {
+ name: 'My storage',
host: 'https://example.com',
- creator: }
+ creator: create(:user)
+ }
end
describe '.shorten_provider_type' do
@@ -46,6 +43,9 @@ RSpec.describe Storages::Storage do
expect(
described_class.shorten_provider_type(described_class::PROVIDER_TYPE_NEXTCLOUD)
).to eq('nextcloud')
+ expect(
+ described_class.shorten_provider_type(described_class::PROVIDER_TYPE_ONE_DRIVE)
+ ).to eq('one_drive')
end
end
@@ -53,22 +53,16 @@ RSpec.describe Storages::Storage do
it 'raises an error', aggregate_failures: true do
expect do
described_class.shorten_provider_type('Storages::Nextcloud')
- end.to raise_error(
- 'Unknown provider_type! Given: Storages::Nextcloud. ' \
- 'Expected the following signature: Storages::{Name of the provider}Storage'
- )
+ end.to raise_error('Unknown provider_type! Given: Storages::Nextcloud. ' \
+ 'Expected the following signature: Storages::{Name of the provider}Storage')
expect do
described_class.shorten_provider_type('Storages:NextcloudStorage')
- end.to raise_error(
- 'Unknown provider_type! Given: Storages:NextcloudStorage. ' \
- 'Expected the following signature: Storages::{Name of the provider}Storage'
- )
+ end.to raise_error('Unknown provider_type! Given: Storages:NextcloudStorage. ' \
+ 'Expected the following signature: Storages::{Name of the provider}Storage')
expect do
described_class.shorten_provider_type('Storages::NextcloudStorag')
- end.to raise_error(
- 'Unknown provider_type! Given: Storages::NextcloudStorag. ' \
- 'Expected the following signature: Storages::{Name of the provider}Storage'
- )
+ end.to raise_error('Unknown provider_type! Given: Storages::NextcloudStorag. ' \
+ 'Expected the following signature: Storages::{Name of the provider}Storage')
end
end
end
@@ -91,7 +85,7 @@ RSpec.describe Storages::Storage do
end
it "fails the validation if host is not unique" do
- expect(described_class.create(default_attributes.merge({ name: 'NC 2' }))).not_to be_valid
+ expect(described_class.create(default_attributes.merge({ name: 'Another storage' }))).not_to be_valid
end
end
end
@@ -99,7 +93,7 @@ RSpec.describe Storages::Storage do
describe '#destroy' do
let(:project) { create(:project) }
let(:storage) { described_class.create(default_attributes) }
- let(:project_storage) { create(:project_storage, project:, storage:, creator:) }
+ let(:project_storage) { create(:project_storage, project:, storage:, creator: create(:user)) }
let(:work_package) { create(:work_package, project:) }
let(:file_link) { create(:file_link, storage:, container_id: work_package.id) }
@@ -115,18 +109,4 @@ RSpec.describe Storages::Storage do
expect(Storages::FileLink.count).to be 0
end
end
-
- describe '#provider_type_nextcloud?' do
- context 'when provider_type is nextcloud' do
- let(:storage) { build(:storage) }
-
- it { expect(storage).to be_a_provider_type_nextcloud }
- end
-
- context 'when provider_type is not nextcloud' do
- let(:storage) { build(:storage, provider_type: 'Storages::DropboxStorage') }
-
- it { expect(storage).not_to be_a_provider_type_nextcloud }
- end
- end
end
diff --git a/modules/storages/app/models/storages/common/configuration_checks.rb b/modules/storages/spec/models/storage_scopes_spec.rb
similarity index 57%
rename from modules/storages/app/models/storages/common/configuration_checks.rb
rename to modules/storages/spec/models/storage_scopes_spec.rb
index d410a5fbd8a..158a38cb321 100644
--- a/modules/storages/app/models/storages/common/configuration_checks.rb
+++ b/modules/storages/spec/models/storage_scopes_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
@@ -25,28 +27,25 @@
#
# See COPYRIGHT and LICENSE files for more details.
#++
-#
-module Storages::Common
- module ConfigurationChecks
- extend ActiveSupport::Concern
- included do
- scope :configured, -> do
- where.associated(:oauth_client, :oauth_application)
- .where("storages.host IS NOT NULL AND storages.name IS NOT NULL")
- end
- end
+require 'spec_helper'
- def configured?
- configuration_checks.values.all?
- end
+RSpec.describe Storages::Storage, with_flag: { storage_one_drive_integration: true } do
+ describe 'scopes' do
+ describe '.configured' do
+ let!(:configured_nextcloud_storage) do
+ create(:nextcloud_storage,
+ oauth_application: build(:oauth_application),
+ oauth_client: build(:oauth_client))
+ end
+ let!(:unconfigured_nextcloud_storage) { create(:nextcloud_storage) }
+ let!(:configured_one_drive_storage) { create(:one_drive_storage, oauth_client: build(:oauth_client)) }
+ let!(:unconfigured_one_drive_storage) { create(:one_drive_storage) }
- def configuration_checks
- { storage_oauth_client_configured: oauth_client.present? }.tap do |configuration_checks|
- if provider_type_nextcloud?
- configuration_checks.merge!({ openproject_oauth_application_configured: oauth_application.present?,
- host_name_configured: (host.present? && name.present?) })
- end
+ it 'returns only storages with complete configuration' do
+ configured_storages = described_class.configured
+ expect(configured_storages.count).to eq 2
+ expect(configured_storages).to contain_exactly(configured_nextcloud_storage, configured_one_drive_storage)
end
end
end