Skip to content

Commit

Permalink
[57347] Added unexpected content validation to NextcloudConnectionVal…
Browse files Browse the repository at this point in the history
…idator
  • Loading branch information
apfohl committed Sep 11, 2024
1 parent 8261bc8 commit ec581a8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def validate
.or { missing_dependencies }
.or { version_mismatch }
.or { request_failed_with_unknown_error }
.or { with_unexpected_content }
.value_or(ConnectionValidation.new(type: :healthy,
error_code: :none,
timestamp: Time.current,
Expand All @@ -59,6 +60,17 @@ def query
.call(storage: @storage, auth_strategy:)
end

def files
@files ||= Peripherals::Registry
.resolve("#{@storage.short_provider_type}.queries.files")
.call(
storage: @storage,
auth_strategy: Peripherals::Registry
.resolve("#{@storage.short_provider_type}.authentication.userless").call,
folder: ParentFolder.new(@storage.group_folder)
)
end

def maybe_is_not_configured
return None() if @storage.configured?

Expand Down Expand Up @@ -147,17 +159,39 @@ def version_mismatch
def request_failed_with_unknown_error
return None() if query.success?

Rails.logger.error("Connection validation failed with unknown error:\n\t" \
"storage: ##{@storage.id} #{@storage.name}\n\t" \
"status: #{query.result}\n\t" \
"response: #{query.error_payload}")
Rails.logger.error(
"Connection validation failed with unknown error:\n\t" \
"storage: ##{@storage.id} #{@storage.name}\n\t" \
"status: #{query.result}\n\t" \
"response: #{query.error_payload}"
)

Some(ConnectionValidation.new(type: :error,
error_code: :err_unknown,
timestamp: Time.current,
description: I18n.t("storages.health.connection_validation.unknown_error")))
end

# rubocop:disable Metrics/AbcSize
def with_unexpected_content
return None() unless @storage.automatic_management_enabled?
return None() if files.failure?

expected_folder_ids = @storage.project_storages
.where(project_folder_mode: "automatic")
.map(&:project_folder_id)

unexpected_files = files.result.files.reject { |file| expected_folder_ids.include?(file.id) }
return None() if unexpected_files.empty?

Some(ConnectionValidation.new(type: :warning,
error_code: :wrn_unexpected_content,
timestamp: Time.current,
description: I18n.t("storages.health.connection_validation.unexpected_content")))
end

# rubocop:enable Metrics/AbcSize

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

def path_to_config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(storage)
end

def call(auth_strategy:, folder:)
validate_input_data(auth_strategy, folder).on_failure { return _1 }
validate_input_data(folder).on_failure { return _1 }

origin_user = Util.origin_user_id(caller: self.class, storage: @storage, auth_strategy:)
.on_failure { return _1 }
Expand All @@ -56,12 +56,10 @@ def call(auth_strategy:, folder:)

private

def validate_input_data(auth_strategy, folder)
def validate_input_data(folder)
error_data = StorageErrorData.new(source: self.class)

if auth_strategy.user.nil?
Util.error(:error, "Cannot execute query without user context.", error_data)
elsif folder.is_a?(ParentFolder)
if folder.is_a?(ParentFolder)
ServiceResult.success
else
Util.error(:error, "Folder input is not a ParentFolder object.", error_data)
Expand Down

0 comments on commit ec581a8

Please sign in to comment.