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
[#49760] fixed test setups and specs
Eric Schubert9ac18b9b4a8e
diff --git a/app/contracts/base_contract.rb b/app/contracts/base_contract.rbindex 66906aa8214..5b4df1c55bb 100644--- a/app/contracts/base_contract.rb+++ b/app/contracts/base_contract.rb@@ -207,7 +207,7 @@ class BaseContract < Disposable::Twin# but those attributes would also be duplicated so that performance suffers significantly.attributes = klass.send(attribute_to_collect).dup- while klass.superclass != ModelContract+ while klass.superclass != ::BaseContract# Collect all the attribute_to_collect from ancestorsklass = klass.superclassdiff --git a/app/contracts/oauth_clients/create_contract.rb b/app/contracts/oauth_clients/create_contract.rbindex 858ea2d6d47..62d356f4687 100644--- a/app/contracts/oauth_clients/create_contract.rb+++ b/app/contracts/oauth_clients/create_contract.rb@@ -34,7 +34,7 @@ module OAuthClientsvalidates :client_id, presence: true, length: { maximum: 255 }attribute :client_secret, writable: true- validates :client_secret, length: { maximum: 255 }+ validates :client_secret, length: { in: 1..255 }, allow_nil: trueattribute :integration_type, writable: truevalidates :integration_type, presence: truediff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rbindex 83bd0bc1d5c..00948c4932e 100644--- a/modules/storages/app/contracts/storages/storages/base_contract.rb+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb@@ -36,11 +36,10 @@ require 'uri'# and uses the contract to validate the model under consideration# (normally it's a model).module Storages::Storages- class BaseContract < ::ModelContract+ class BaseContract < ::BaseContractMINIMAL_NEXTCLOUD_VERSION = 22include ::Storages::Storages::Concerns::ManageStoragesGuarded- include ActiveModel::Validationsattribute :namevalidates :name, presence: true, length: { maximum: 255 }@@ -49,5 +48,24 @@ module Storages::Storagesvalidates :provider_type, inclusion: { in: Storages::Storage::PROVIDER_TYPES }attribute :provider_fields++ validate :provider_type_strategy++ private++ def provider_type_strategy+ contract = if model.provider_type_nextcloud?+ NextcloudContract.new(model, @user, options: @options)+ else+ OneDriveContract.new(model, @user, options: @options)+ end++ # Append the attributes defined in the internal contract+ # to the list of writable attributes.+ # Otherwise, we get :readonly validation errors.+ contract.writable_attributes.append(*writable_attributes)+ contract.validate+ errors.merge!(contract.errors)+ endendenddiff --git a/modules/storages/app/contracts/storages/storages/one_drive_create_contract.rb b/modules/storages/app/contracts/storages/storages/create_contract.rbsimilarity index 95%rename from modules/storages/app/contracts/storages/storages/one_drive_create_contract.rbrename to modules/storages/app/contracts/storages/storages/create_contract.rbindex 49f491cf073..27f799bb983 100644--- a/modules/storages/app/contracts/storages/storages/one_drive_create_contract.rb+++ b/modules/storages/app/contracts/storages/storages/create_contract.rb@@ -29,7 +29,7 @@#++module Storages::Storages- class OneDriveCreateContract < ::Storages::Storages::OneDriveBaseContract+ class CreateContract < ::Storages::Storages::BaseContractattribute :creatorvalidate :creator_must_be_userdiff --git a/modules/storages/app/contracts/storages/storages/nextcloud_base_contract.rb b/modules/storages/app/contracts/storages/storages/nextcloud_contract.rbsimilarity index 98%rename from modules/storages/app/contracts/storages/storages/nextcloud_base_contract.rbrename to modules/storages/app/contracts/storages/storages/nextcloud_contract.rbindex b090fd076a4..75abbaa700d 100644--- a/modules/storages/app/contracts/storages/storages/nextcloud_base_contract.rb+++ b/modules/storages/app/contracts/storages/storages/nextcloud_contract.rb@@ -29,7 +29,7 @@#++module Storages::Storages- class NextcloudBaseContract < BaseContract+ class NextcloudContract < ::ModelContractattribute :hostvalidates :host, url: { message: I18n.t('activerecord.errors.messages.invalid_url') }, length: { maximum: 255 }# Check that a host actually is a storage server.diff --git a/modules/storages/app/contracts/storages/storages/nextcloud_create_contract.rb b/modules/storages/app/contracts/storages/storages/nextcloud_create_contract.rbdeleted file mode 100644index 2b27889d1ce..00000000000--- a/modules/storages/app/contracts/storages/storages/nextcloud_create_contract.rb+++ /dev/null@@ -1,44 +0,0 @@-# frozen_string_literal: true--# -- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2023 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::Storages- class NextcloudCreateContract < ::Storages::Storages::NextcloudBaseContract- attribute :creator- validate :creator_must_be_user-- private-- def creator_must_be_user- unless creator == user- errors.add(:creator, :invalid)- end- end- end-enddiff --git a/modules/storages/app/contracts/storages/storages/nextcloud_update_contract.rb b/modules/storages/app/contracts/storages/storages/nextcloud_update_contract.rbdeleted file mode 100644index 2c79c4988c2..00000000000--- a/modules/storages/app/contracts/storages/storages/nextcloud_update_contract.rb+++ /dev/null@@ -1,34 +0,0 @@-# frozen_string_literal: true--# -- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2023 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::Storages- class NextcloudUpdateContract < ::Storages::Storages::NextcloudBaseContract- end-enddiff --git a/modules/storages/app/contracts/storages/storages/one_drive_base_contract.rb b/modules/storages/app/contracts/storages/storages/one_drive_contract.rbsimilarity index 96%rename from modules/storages/app/contracts/storages/storages/one_drive_base_contract.rbrename to modules/storages/app/contracts/storages/storages/one_drive_contract.rbindex df73a8adb8f..c730b8ceb49 100644--- a/modules/storages/app/contracts/storages/storages/one_drive_base_contract.rb+++ b/modules/storages/app/contracts/storages/storages/one_drive_contract.rb@@ -29,7 +29,7 @@#++module Storages::Storages- class OneDriveBaseContract < BaseContract+ class OneDriveContract < BaseContractattribute :hostvalidates :host, absence: trueenddiff --git a/modules/storages/app/contracts/storages/storages/one_drive_update_contract.rb b/modules/storages/app/contracts/storages/storages/update_contract.rbsimilarity index 94%rename from modules/storages/app/contracts/storages/storages/one_drive_update_contract.rbrename to modules/storages/app/contracts/storages/storages/update_contract.rbindex 3864ce425c8..f078aa54566 100644--- a/modules/storages/app/contracts/storages/storages/one_drive_update_contract.rb+++ b/modules/storages/app/contracts/storages/storages/update_contract.rb@@ -29,6 +29,6 @@#++module Storages::Storages- class OneDriveUpdateContract < ::Storages::Storages::OneDriveBaseContract+ class UpdateContract < ::Storages::Storages::BaseContractendenddiff --git a/modules/storages/app/services/storages/storages/create_service.rb b/modules/storages/app/services/storages/storages/create_service.rbindex b8da1c25add..0263f26a1e8 100644--- a/modules/storages/app/services/storages/storages/create_service.rb+++ b/modules/storages/app/services/storages/storages/create_service.rb@@ -34,11 +34,6 @@# The comments here are also valid for the other *_service.rb filesmodule Storages::Storagesclass CreateService < ::BaseServices::Create- # def initialize(user:)- # binding.pry- # super(user:, contract_class: nil, contract_options: {})- # end-protecteddef after_perform(service_call)diff --git a/modules/storages/lib/api/v3/storages/storages_api.rb b/modules/storages/lib/api/v3/storages/storages_api.rbindex 264c7438f0b..9bb78f58909 100644--- a/modules/storages/lib/api/v3/storages/storages_api.rb+++ b/modules/storages/lib/api/v3/storages/storages_api.rb@@ -26,39 +26,17 @@# See COPYRIGHT and LICENSE files for more details.#++-# This class provides definitions for API routes and endpoints for the storages namespace. It inherits the-# functionality from the Grape REST API framework. It is mounted in lib/api/v3/root.rb.-# `modules/storages/lib/` is a defined root directory for grape, providing a root level look up for the namespaces.-# Hence, the modules of the class have to be represented in the directory structure.--# OpenProjectAPI is a simple subclass of Grape::API that handles patches.class API::V3::Storages::StoragesAPI < API::OpenProjectAPI- # helpers is defined by the grape framework. They make methods from the- # module available from within the endpoint context.helpers Storages::Peripherals::Scopes- # The `:resources` keyword defines the API namespace -> /api/v3/storages/...resources :storages do- # post &::API::V3::Storages::CreateEndpoint.new(model: ::Storages::Storage).mountpost &API::V3::Utilities::Endpoints::Create.new(model: Storages::Storage).mount- # `route_param` extends the route by a route parameter of the endpoint.- # The input parameter value is parsed into the `:storage_id` symbol.route_param :storage_id, type: Integer, desc: 'Storage id' do- # Execute the do...end lines after parameter validation but before the actual- # call to the API method.- # Please see: The after_validation call-back in Grape:- # https://github.com/ruby-grape/grape#before-after-and-finallyafter_validation do@storage = visible_storages.find(params[:storage_id])end- # A helper is used to define the behaviour at GET /api/v3/storages/:storage_id- # The endpoint helper standardizes a lot of the parsing, validation and rendering logic.- # the `mount` method from the endpoint returns a proc. This proc is- # passed as a block to the `get` helper thanks to the `&` operator.- # The block will get called everytime a GET request is sent to this- # route.get &API::V3::Utilities::Endpoints::Show.new(model: Storages::Storage).mountpatch &API::V3::Utilities::Endpoints::Update.new(model: Storages::Storage).mountdiff --git a/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rbindex ec056bbf2a9..4f169fced1a 100644--- a/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb+++ b/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb@@ -52,8 +52,7 @@ RSpec.describe 'API v3 storages resource', content_type: :json, webmock: true doendbefore do- allow(connection_manager).to receive(:get_authorization_uri).and_return(authorize_url)- allow(connection_manager).to receive(:authorization_state).and_return(:connected)+ allow(connection_manager).to receive_messages(get_authorization_uri: authorize_url, authorization_state: :connected)allow(OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)project_storagelogin_as current_user@@ -423,12 +422,12 @@ RSpec.describe 'API v3 storages resource', content_type: :json, webmock: true docontext 'if request body is invalid' dolet(:params) do{- clientId: 'only_an_id'+ clientSecret: 'only_an_id'}endit_behaves_like 'constraint violation' do- let(:message) { 'Client secret can\'t be blank.' }+ let(:message) { 'Client can\'t be blank.' }endendenddiff --git a/modules/storages/spec/services/storages/file_links/set_attributes_service_spec.rb b/modules/storages/spec/services/storages/file_links/set_attributes_service_spec.rbindex 3497d48bfdd..76fc55e3251 100644--- a/modules/storages/spec/services/storages/file_links/set_attributes_service_spec.rb+++ b/modules/storages/spec/services/storages/file_links/set_attributes_service_spec.rb@@ -53,11 +53,11 @@ RSpec.describe Storages::FileLinks::SetAttributesService, type: :model doendlet(:model_instance) { Storages::Storage.new }let(:contract_class) do- allow(Storages::Storages::CreateContract)+ allow(Storages::Storages::NextcloudCreateContract).to receive(:new).and_return(contract_instance)- Storages::Storages::CreateContract+ Storages::Storages::NextcloudCreateContractendlet(:params) { {} }diff --git a/modules/storages/spec/services/storages/storages/set_attributes_service_spec.rb b/modules/storages/spec/services/storages/storages/set_attributes_service_spec.rbindex 07b271a1903..0633712c184 100644--- a/modules/storages/spec/services/storages/storages/set_attributes_service_spec.rb+++ b/modules/storages/spec/services/storages/storages/set_attributes_service_spec.rb@@ -35,9 +35,8 @@ RSpec.describe Storages::Storages::SetAttributesService, type: :model dolet(:current_user) { build_stubbed(:admin) }let(:contract_instance) do- contract = instance_double(Storages::Storages::BaseContract, 'contract_instance')- allow(contract)- .to receive_messages(validate: contract_valid, errors: contract_errors)+ contract = instance_double(Storages::Storages::NextcloudCreateContract, 'contract_instance')+ allow(contract).to receive_messages(validate: contract_valid, errors: contract_errors)contractend@@ -53,11 +52,11 @@ RSpec.describe Storages::Storages::SetAttributesService, type: :model doendlet(:model_instance) { Storages::Storage.new }let(:contract_class) do- allow(Storages::Storages::CreateContract)+ allow(Storages::Storages::NextcloudCreateContract).to receive(:new)- .and_return(contract_instance)+ .and_return(contract_instance)- Storages::Storages::CreateContract+ Storages::Storages::NextcloudCreateContractendlet(:params) { { provider_type: Storages::Storage::PROVIDER_TYPE_NEXTCLOUD } }@@ -65,7 +64,7 @@ RSpec.describe Storages::Storages::SetAttributesService, type: :model dobefore doallow(model_instance).to receive(:valid?)- .and_return(model_valid)+ .and_return(model_valid)endsubject { instance.call(params) }@@ -90,7 +89,7 @@ RSpec.describe Storages::Storages::SetAttributesService, type: :model doendit 'sets name to "My Nextcloud" by default' do- expect(subject.result.name).to eq I18n.t('storages.provider_types.nextcloud.default_name')+ expect(subject.result.name).to eq I18n.t('storages.default_name')endcontext 'when setting host' dodiff --git a/modules/storages/spec/services/storages/storages/update_service_spec.rb b/modules/storages/spec/services/storages/storages/update_service_spec.rbindex d197e8e1d67..16180b5487d 100644--- a/modules/storages/spec/services/storages/storages/update_service_spec.rb+++ b/modules/storages/spec/services/storages/storages/update_service_spec.rb@@ -71,7 +71,9 @@ RSpec.describe Storages::Storages::UpdateService, type: :model doit 'cannot update storage creator' dostorage_creator = create(:admin, login: "storage_creator")storage = create(:nextcloud_storage, creator: storage_creator)- service = described_class.new(user: create(:admin), model: storage)+ service = described_class.new(user: create(:admin),+ model: storage,+ contract_class: Storages::Storages::NextcloudUpdateContract)service_result = service.call(creator: create(:user, login: "impostor"))@@ -88,7 +90,7 @@ RSpec.describe Storages::Storages::UpdateService, type: :model dosubject dodescribed_class- .new(user:, model: storage)+ .new(user:, model: storage, contract_class: Storages::Storages::NextcloudUpdateContract).call({ name: })enddiff --git a/spec/contracts/oauth_clients/create_contract_spec.rb b/spec/contracts/oauth_clients/create_contract_spec.rbindex 380a7f54ebd..6fde92f3d49 100644--- a/spec/contracts/oauth_clients/create_contract_spec.rb+++ b/spec/contracts/oauth_clients/create_contract_spec.rb@@ -48,27 +48,45 @@ RSpec.describe OAuthClients::CreateContract dodescribe 'validations' docontext 'when all attributes are valid' doinclude_examples 'contract is valid'++ context 'with client secret being nil' do+ let(:client_secret) { nil }++ include_examples 'contract is valid'+ endend- %i[client_id client_secret].each do |attribute_name|- context 'when client_id is invalid' do- context 'as it is too long' do- let(attribute_name) { 'X' * 257 }+ context 'when client_id is invalid' do+ context 'as it is too long' do+ let(:client_id) { 'X' * 257 }++ include_examples 'contract is invalid', client_id: :too_long+ end++ context 'as it is empty' do+ let(:client_id) { '' }- include_examples 'contract is invalid', attribute_name => :too_long- end+ include_examples 'contract is invalid', client_id: :blank+ end++ context 'as it is nil' do+ let(:client_id) { nil }++ include_examples 'contract is invalid', client_id: :blank+ end+ end- context 'as it is empty' do- let(attribute_name) { '' }+ context 'when client_secret is invalid' do+ context 'as it is too long' do+ let(:client_secret) { 'X' * 257 }- include_examples 'contract is invalid', attribute_name => :blank- end+ include_examples 'contract is invalid', client_secret: :too_long+ end- context 'as it is nil' do- let(attribute_name) { nil }+ context 'as it is empty' do+ let(:client_secret) { '' }- include_examples 'contract is invalid', attribute_name => :blank- end+ include_examples 'contract is invalid', client_secret: :too_shortendend