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

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.rb
index 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::Storages
class BaseContract < ::ModelContract
+ MINIMAL_NEXTCLOUD_VERSION = 23
include ::Storages::Storages::Concerns::ManageStoragesGuarded
include ActiveModel::Validations
@@ -43,9 +44,10 @@ module Storages::Storages
attribute :host
validates :host, length: { minimum: 1, maximum: 255 }, allow_nil: false
validates_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_user
unless creator == user
@@ -54,6 +56,31 @@ module Storages::Storages
end
def 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_capabilities
uri = 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::Storages
use_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)
- end
end
end
end
diff --git a/modules/storages/app/contracts/storages/storages/create_contract.rb b/modules/storages/app/contracts/storages/storages/create_contract.rb
index 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::BaseContract
end
end
diff --git a/modules/storages/spec/contracts/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/shared_contract_examples.rb
index 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 do
let(: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 do
before do
unless 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
- # )
end
it_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' do
it_behaves_like 'contract is valid'
end
@@ -89,7 +88,7 @@ shared_examples_for 'storage contract', webmock: true do
end
context '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 do
it_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
+ end
end
end
end
diff --git a/modules/storages/spec/contracts/storages/update_contract_spec.rb b/modules/storages/spec/contracts/storages/update_contract_spec.rb
index 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 do
include_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) }
end
end
diff --git a/modules/storages/spec/services/storages/set_attributes_service_spec.rb b/modules/storages/spec/services/storages/set_attributes_service_spec.rb
index 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 do
let(: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 do
contract
end
- 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 do
contract_class: contract_class,
contract_options: {})
end
- let(:model_instance) { ::Storages::Storages.new }
+ let(:model_instance) { ::Storages::Storage.new }
let(:contract_class) do
allow(::Storages::Storages::CreateContract)
.to receive(:new)
@@ -77,7 +77,7 @@ describe ::Storages::Storages::SetAttributesService, type: :model do
end
it 'is a success' do
- is_expected
+ expect(subject)
.to be_success
end
@@ -97,13 +97,9 @@ describe ::Storages::Storages::SetAttributesService, type: :model do
context 'with an invalid contract' do
let(:contract_valid) { false }
- let(:expect_time_instance_save) do
- expect(model_instance)
- .not_to receive(:save)
- end
it 'returns failure' do
- is_expected.not_to be_success
+ expect(subject).not_to be_success
end
it "returns the contract's errors" do