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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Fix Storages BaseContract
Wieland Lindenthal04b55625806e
diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rbindex 205d5dd8588..2804ed53466 100644--- a/modules/storages/app/contracts/storages/storages/base_contract.rb+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb@@ -31,6 +31,7 @@ require 'uri'module Storages::Storagesclass BaseContract < ::ModelContract+ MINIMAL_NEXTCLOUD_VERSION = 23include ::Storages::Storages::Concerns::ManageStoragesGuardedinclude ActiveModel::Validations@@ -43,9 +44,10 @@ module Storages::Storagesattribute :hostvalidates :host, length: { minimum: 1, maximum: 255 }, allow_nil: falsevalidates_url :host- # Check that a host actually is a storage server- validate :validate_host_reachable, if: -> { errors[:host].empty? }+ # Check that a host actually is a storage server.+ # But only do so if the validations above for URL were successful.+ validate :validate_host_reachable, if: -> { errors[:host].empty? }def validate_creator_is_userunless creator == user@@ -54,6 +56,31 @@ module Storages::Storagesenddef validate_host_reachable+ response = request_capabilities++ unless response.is_a? Net::HTTPSuccess+ errors.add(:host, :invalid)+ return+ end++ unless major_version_sufficient?(response)+ errors.add(:host, :invalid)+ end+ end++ def major_version_sufficient?(response)+ return false unless response.body++ version = JSON.parse(response.body).dig('ocs', 'data', 'version', 'major')+ return false if version.nil?+ return false if version < MINIMAL_NEXTCLOUD_VERSION++ true+ end++ private++ def request_capabilitiesuri = URI.parse(File.join(host, '/ocs/v2.php/cloud/capabilities'))request = Net::HTTP::Get.new(uri)request["Ocs-Apirequest"] = "true"@@ -63,17 +90,9 @@ module Storages::Storagesuse_ssl: uri.scheme == "https"}- response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|+ Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|http.request(request)end-- errors.add(:host, :invalid) unless response.is_a? Net::HTTPSuccess- end-- def validate_uri- unless host =~ /\A#{URI::regexp}\z/- errors.add(:host, :invalid)- endendendenddiff --git a/modules/storages/app/contracts/storages/storages/create_contract.rb b/modules/storages/app/contracts/storages/storages/create_contract.rbindex ceaa8972ddd..d70c6c9c335 100644--- a/modules/storages/app/contracts/storages/storages/create_contract.rb+++ b/modules/storages/app/contracts/storages/storages/create_contract.rb@@ -26,10 +26,11 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'net/http'-require 'uri'-module Storages::Storages+ # We create the empty class here, so that other pieces of code that looks for the contract in its default place will+ # find it. So, the CreateService for Storages will expect its contract to be here.+ #+ # We inherit from a BaseContract as the BaseContract here is sharing a lot in common with the UpdateContract.class CreateContract < ::Storages::Storages::BaseContractendenddiff --git a/modules/storages/spec/contracts/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/shared_contract_examples.rbindex e57223ea013..05dbb6c1625 100644--- a/modules/storages/spec/contracts/storages/shared_contract_examples.rb+++ b/modules/storages/spec/contracts/storages/shared_contract_examples.rb@@ -33,11 +33,13 @@ require 'spec_helper'shared_examples_for 'storage contract', webmock: true dolet(:storage_name) { 'Storage 1' }let(:storage_provider_type) { 'nextcloud' }- let(:storage_host) { 'http://host1.example.com' }- let(:storage_creator) { create(:admin) }+ let(:storage_host) { 'https://host1.example.com' }+ let(:storage_creator) { current_user }++ let(:current_user) { create(:admin) }let(:host_response_body) { '{"data": "that you want to return"}' }- let(:host_response_code) { '404' }+ let(:host_response_code) { '200' }let(:host_response_message) { 'OK' }let(:host_response_major_version) { 23 }let(:host_response_body) do@@ -61,22 +63,19 @@ shared_examples_for 'storage contract', webmock: true dobefore dounless storage_host.nil?- stub_request(:get,- File.join(storage_host.sub("https://", ""),- '/ocs/v2.php/cloud/capabilities'))+ stub_request(+ :get,+ File.join(storage_host, '/ocs/v2.php/cloud/capabilities')+ ).to_return(+ status: host_response_code,+ body: host_response_body+ )end- # to_return(- # status: host_response_code,- # body: host_response_body- # )endit_behaves_like 'contract is valid for active admins and invalid for regular users'describe 'validations' do- let(:current_user) { build_stubbed :admin }--context 'when all attributes are valid' doit_behaves_like 'contract is valid'end@@ -89,7 +88,7 @@ shared_examples_for 'storage contract', webmock: true doendcontext 'as it is empty' do- let(:storage_name) { ''}+ let(:storage_name) { '' }it_behaves_like 'contract is invalid'end@@ -138,12 +137,26 @@ shared_examples_for 'storage contract', webmock: true doit_behaves_like 'contract is invalid'end- #- # context 'when provider_type is nextcloud' do- # context 'with host not reachable' do- #- # end- # end++ context 'when provider_type is nextcloud' do+ context 'when response code is a 404 NOT FOUND' do+ let(:host_response_code) { '404' }++ it_behaves_like 'contract is invalid'+ end++ context 'when response code is a 500 PERMISSION DENIED' do+ let(:host_response_code) { '500' }++ it_behaves_like 'contract is invalid'+ end++ context 'when Nextcloud version is below the required minimal version which is 23' do+ let(:host_response_major_version) { '22' }++ it_behaves_like 'contract is invalid'+ end+ endendendenddiff --git a/modules/storages/spec/contracts/storages/update_contract_spec.rb b/modules/storages/spec/contracts/storages/update_contract_spec.rbindex 6000fabf59b..15412bc2527 100644--- a/modules/storages/spec/contracts/storages/update_contract_spec.rb+++ b/modules/storages/spec/contracts/storages/update_contract_spec.rb@@ -1,4 +1,5 @@#-- encoding: UTF-8+#-- copyright# OpenProject is an open source project management software.# Copyright (C) 2012-2021 the OpenProject GmbH@@ -31,11 +32,27 @@ require 'spec_helper'require 'contracts/shared/model_contract_shared_context'require_relative 'shared_contract_examples'-describe PlaceholderUsers::UpdateContract do+describe ::Storages::Storages::UpdateContract doinclude_context 'ModelContract shared context'- it_behaves_like 'placeholder user contract' do- let(:placeholder_user) { build_stubbed(:placeholder_user, name: placeholder_user_name) }- let(:contract) { described_class.new(placeholder_user, current_user) }+ it_behaves_like 'storage contract' do+ let(:storage) do+ build_stubbed(:storage,+ creator: current_user,+ host: 'https://host1.example.com',+ name: 'Storage 1',+ provider_type: 'nextcloud').tap do |storage|+ if storage_name == storage.name+ # trigger at least one change per default+ storage.name += " (DEFAULT UPDATED)"+ else+ storage.name = storage_name+ end+ storage.host = storage_host+ storage.provider_type = storage_provider_type+ storage.creator = storage_creator+ end+ end+ let(:contract) { described_class.new(storage, current_user) }endenddiff --git a/modules/storages/spec/services/storages/set_attributes_service_spec.rb b/modules/storages/spec/services/storages/set_attributes_service_spec.rbindex 4e0c43bb189..e812d2e8de3 100644--- a/modules/storages/spec/services/storages/set_attributes_service_spec.rb+++ b/modules/storages/spec/services/storages/set_attributes_service_spec.rb@@ -32,7 +32,7 @@ describe ::Storages::Storages::SetAttributesService, type: :model dolet(:current_user) { build_stubbed(:admin) }let(:contract_instance) do- contract = double('contract_instance')+ contract = instance_double(::Storages::Storages::BaseContract, 'contract_instance')allow(contract).to receive(:validate).and_return(contract_valid)@@ -42,7 +42,7 @@ describe ::Storages::Storages::SetAttributesService, type: :model docontractend- let(:contract_errors) { double('contract_errors') }+ let(:contract_errors) { instance_double(ActiveModel::Errors, 'contract_errors') }let(:contract_valid) { true }let(:model_valid) { true }@@ -52,7 +52,7 @@ describe ::Storages::Storages::SetAttributesService, type: :model docontract_class: contract_class,contract_options: {})end- let(:model_instance) { ::Storages::Storages.new }+ let(:model_instance) { ::Storages::Storage.new }let(:contract_class) doallow(::Storages::Storages::CreateContract).to receive(:new)@@ -77,7 +77,7 @@ describe ::Storages::Storages::SetAttributesService, type: :model doendit 'is a success' do- is_expected+ expect(subject).to be_successend@@ -97,13 +97,9 @@ describe ::Storages::Storages::SetAttributesService, type: :model docontext 'with an invalid contract' dolet(:contract_valid) { false }- let(:expect_time_instance_save) do- expect(model_instance)- .not_to receive(:save)- endit 'returns failure' do- is_expected.not_to be_success+ expect(subject).not_to be_successendit "returns the contract's errors" do