|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +require "spec_helper" |
| 32 | +require_module_spec_helper |
| 33 | + |
| 34 | +module Storages |
| 35 | + module Peripherals |
| 36 | + module ConnectionValidators |
| 37 | + module Nextcloud |
| 38 | + RSpec.describe AmpfConnectionValidator, :webmock do |
| 39 | + let(:storage) { create(:nextcloud_storage_configured, :as_automatically_managed) } |
| 40 | + let(:project_folder_id) { "1337" } |
| 41 | + let!(:project_storage) do |
| 42 | + create(:project_storage, :as_automatically_managed, project_folder_id:, storage:, project: create(:project)) |
| 43 | + end |
| 44 | + |
| 45 | + let(:files_response) do |
| 46 | + ServiceResult.success(result: StorageFiles.new( |
| 47 | + [StorageFile.new(id: project_folder_id, name: project_storage.managed_project_folder_name)], |
| 48 | + StorageFile.new(id: "root", name: "root"), |
| 49 | + [] |
| 50 | + )) |
| 51 | + end |
| 52 | + |
| 53 | + subject(:validator) { described_class.new(storage) } |
| 54 | + |
| 55 | + before do |
| 56 | + Registry.stub("nextcloud.queries.files", ->(*) { files_response }) |
| 57 | + end |
| 58 | + |
| 59 | + it "pass all checks" do |
| 60 | + results = validator.call |
| 61 | + |
| 62 | + expect(results.values).to all(be_success) |
| 63 | + end |
| 64 | + |
| 65 | + context "if userless authentication fails" do |
| 66 | + let(:files_response) { build_failure(code: :unauthorized, payload: nil) } |
| 67 | + |
| 68 | + it "fails and skips the next checks" do |
| 69 | + results = validator.call |
| 70 | + |
| 71 | + states = results.values.map { it.state }.tally |
| 72 | + expect(states).to eq({ failure: 1, skipped: 3 }) |
| 73 | + expect(results[:userless_access]).to be_failure |
| 74 | + expect(results[:userless_access].message).to eq(i18n_message(:userless_access_denied)) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + context "if the files request returns not_found" do |
| 79 | + let(:files_response) { build_failure(code: :not_found, payload: nil) } |
| 80 | + |
| 81 | + it "fails the check" do |
| 82 | + results = validator.call |
| 83 | + |
| 84 | + expect(results[:group_folder_presence]).to be_failure |
| 85 | + expect(results[:group_folder_presence].message).to eq(i18n_message(:group_folder_not_found)) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + context "if the files request returns an unknown error" do |
| 90 | + let(:files_response) { StorageInteraction::Nextcloud::Util.error(:error) } |
| 91 | + |
| 92 | + before { allow(Rails.logger).to receive(:error) } |
| 93 | + |
| 94 | + it "fails the check and logs the error" do |
| 95 | + results = validator.call |
| 96 | + |
| 97 | + expect(results[:files_request]).to be_failure |
| 98 | + expect(results[:files_request].message) |
| 99 | + .to eq(i18n_message(:unknown_error)) |
| 100 | + |
| 101 | + expect(Rails.logger).to have_received(:error).with(/Connection validation failed with unknown error/) |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + context "if the files request returns unexpected files" do |
| 106 | + let(:files_response) do |
| 107 | + ServiceResult.success(result: StorageFiles.new( |
| 108 | + [ |
| 109 | + StorageFile.new(id: project_folder_id, name: "I am your father"), |
| 110 | + StorageFile.new(id: "noooooooooo", name: "testimony_of_luke_skywalker.md") |
| 111 | + ], |
| 112 | + StorageFile.new(id: "root", name: "root"), |
| 113 | + [] |
| 114 | + )) |
| 115 | + end |
| 116 | + |
| 117 | + it "warns the user about extraneous folders" do |
| 118 | + results = validator.call |
| 119 | + |
| 120 | + expect(results[:group_folder_contents]).to be_a_warning |
| 121 | + expect(results[:group_folder_contents].message).to eq(i18n_message(:unexpected_content)) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + private |
| 126 | + |
| 127 | + def i18n_message(key, context = {}) = I18n.t("storages.health.connection_validation.#{key}", **context) |
| 128 | + |
| 129 | + def build_failure(code:, payload:) |
| 130 | + data = StorageErrorData.new(source: "query", payload:) |
| 131 | + error = StorageError.new(code:, data:) |
| 132 | + ServiceResult.failure(result: code, errors: error) |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | +end |
0 commit comments