Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements the BaseConfigurationValidator #18316

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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 Peripherals
module ConnectionValidators
CheckResult = Data.define(:key, :state, :message, :timestamp) do
private_class_method :new
def self.skipped(key)
new(key:, state: :skipped, message: nil, timestamp: nil)
end

def self.failure(key, message)
new(key:, state: :failure, message: message, timestamp: Time.zone.now)
end

def self.success(key)
new(key:, state: :success, message: nil, timestamp: Time.zone.now)
end

def success? = state == :success
def failure? = state == :failure
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# 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 Peripherals
module ConnectionValidators
module Nextcloud
class BaseConfigurationValidator
def initialize(storage)
@storage = storage
@results = build_result_list
end

def call
catch :interrupted do
capabilities_request_failed
host_url_not_found
missing_dependencies
version_mismatch
end

@results
end

private

def capabilities_request_failed
if capabilities.failure? && capabilities.result != :not_found
fail_check(__method__, message(:error))
else
pass_check(__method__)
end
end

def version_mismatch
min_app_version = SemanticVersion.parse(nextcloud_dependencies.dig("dependencies", "integration_app", "min_version"))
capabilities_result = capabilities.result

if capabilities_result.app_version < min_app_version
fail_check(__method__, message(:app_version_mismatch))
else
pass_check(__method__)
end
end

def missing_dependencies
capabilities_result = capabilities.result
app_name = I18n.t("storages.dependencies.nextcloud.integration_app")

if capabilities_result.app_disabled?
fail_check(__method__, message(:missing_dependencies, dependency: app_name))
else
pass_check(__method__)
end
end

def host_url_not_found
if capabilities.result == :not_found
fail_check(__method__, message(:host_not_found))
else
pass_check(__method__)
end
end

def message(key, context = {})
I18n.t("storages.health.connection_validation.#{key}", **context)
end

def noop = StorageInteraction::AuthenticationStrategies::Noop.strategy

def capabilities
@capabilities ||= Peripherals::Registry.resolve("#{@storage}.queries.capabilities")
.call(storage: @storage, auth_strategy: noop)
end

def nextcloud_dependencies
@nextcloud_dependencies ||= YAML.load_file(path_to_config).deep_stringify_keys!
end

def path_to_config = Rails.root.join("modules/storages/config/nextcloud_dependencies.yml")

def fail_check(key, message)
update_result(key, CheckResult.failure(key, message))
throw :interrupted
end

def pass_check(key)
update_result(key, CheckResult.success(key))
end

def update_result(method, value)
@results[method.to_sym] = value
end

def build_result_list
{
capabilities_request_failed: CheckResult.skipped(:capabilities_request_failed),
host_url_not_found: CheckResult.skipped(:host_url_not_found),
missing_dependencies: CheckResult.skipped(:missing_dependencies),
version_mismatch: CheckResult.skipped(:version_mismatch)
}
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module StorageInteraction
module Nextcloud
class CapabilitiesQuery
include Dry::Monads[:result]
include Dry::Monads::Do.for(:call, :parse_capabilities)
include Dry::Monads::Do.for(:parse_capabilities)

def self.call(storage:, auth_strategy:)
new(storage).call(auth_strategy:)
Expand All @@ -47,9 +47,7 @@ def initialize(storage)
def call(auth_strategy:)
http_options = Util.ocs_api_request.deep_merge(Util.accept_json)
result = Authentication[auth_strategy].call(storage: @storage, http_options:) do |http|
json = yield handle_response(http.get(url))

parse_capabilities(json)
handle_response(http.get(url))
end

to_service_result(result)
Expand All @@ -64,7 +62,8 @@ def handle_response(response)

case response
in { status: 200..299 }
Success(response.json(symbolize_keys: true))
json = response.json(symbolize_keys: true)
parse_capabilities(json)
in { status: 404 }
Failure(StorageError.new(code: :not_found,
log_message: "Outbound request destination not found!",
Expand Down Expand Up @@ -98,7 +97,8 @@ def parse_capabilities(json)
# rubocop:enable Metrics/AbcSize

def version(str)
failure = Failure(StorageError.new(code: :error, log_message: "'#{str}' is not a valid version string"))
failure = Failure(StorageError.new(code: :invalid_version_number,
log_message: "'#{str}' is not a valid version string"))

return failure if str.nil?

Expand Down
11 changes: 5 additions & 6 deletions modules/storages/app/models/storages/nextcloud_capabilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
#++

module Storages
NextcloudCapabilities = Data.define(
:app_enabled?,
:app_version,
:group_folder_enabled?,
:group_folder_version
) do
NextcloudCapabilities = Data.define(:app_enabled?, :app_version, :group_folder_enabled?, :group_folder_version) do
def self.empty
new(app_enabled?: false, group_folder_enabled?: false, app_version: nil, group_folder_version: nil)
end

def group_folder_disabled? = !group_folder_enabled?

def app_disabled? = !app_enabled?
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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 Peripherals
module ConnectionValidators
module Nextcloud
RSpec.describe BaseConfigurationValidator, :webmock do
let(:storage) { create(:nextcloud_storage_with_local_connection, :as_not_automatically_managed) }

subject(:validator) { described_class.new(storage) }

it "returns a hash with check 'key' as key and a CheckResult as value", vcr: "nextcloud/capabilities_success" do
results = validator.call

expect(results).to be_a(Hash)
expect(results.values).to all(be_success)
end

describe "when errors occurs" do
it "base url could not be reached" do
stub_request(:get, UrlBuilder.url(storage.uri, "/ocs/v2.php/cloud/capabilities"))
.to_return(status: 404, body: "Not Found")

results = validator.call
expect(results[:host_url_not_found]).to be_a_failure
expect(results[:host_url_not_found].message).to eq(I18n.t(i18n_key(:host_not_found)))
end

it "integration app version mismatch", vcr: "nextcloud/capabilities_success" do
absurd_version = { dependencies: { integration_app: { min_version: "2099.10.138" } } }.deep_stringify_keys
allow(subject).to receive(:nextcloud_dependencies).and_return(absurd_version)

results = validator.call
expect(results[:version_mismatch]).to be_a_failure
expect(results[:version_mismatch].message).to eq(I18n.t(i18n_key(:app_version_mismatch)))
end

it "integration app disabled / missing", vcr: "nextcloud/capabilities_success_app_disabled" do
results = validator.call

expect(results[:missing_dependencies]).to be_a_failure
expect(results[:missing_dependencies].message)
.to eq(I18n.t(i18n_key(:missing_dependencies), dependency: "Integration OpenProject"))
end
end

private

def i18n_key(key) = "storages.health.connection_validation.#{key}"
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

error = result.errors
expect(error).to be_a(Storages::StorageError)
expect(error.code).to eq(:error)
expect(error.code).to eq(:invalid_version_number)
expect(error.log_message).to include("not a valid version string")
end
end
Expand Down
Loading