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

[#49760] fixed test setups and specs

Eric Schubert9ac18b9b4a8e
diff --git a/app/contracts/base_contract.rb b/app/contracts/base_contract.rb
index 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 ancestors
klass = klass.superclass
diff --git a/app/contracts/oauth_clients/create_contract.rb b/app/contracts/oauth_clients/create_contract.rb
index 858ea2d6d47..62d356f4687 100644
--- a/app/contracts/oauth_clients/create_contract.rb
+++ b/app/contracts/oauth_clients/create_contract.rb
@@ -34,7 +34,7 @@ module OAuthClients
validates :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: true
attribute :integration_type, writable: true
validates :integration_type, presence: true
diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rb
index 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 < ::BaseContract
MINIMAL_NEXTCLOUD_VERSION = 22
include ::Storages::Storages::Concerns::ManageStoragesGuarded
- include ActiveModel::Validations
attribute :name
validates :name, presence: true, length: { maximum: 255 }
@@ -49,5 +48,24 @@ module Storages::Storages
validates :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)
+ end
end
end
diff --git a/modules/storages/app/contracts/storages/storages/one_drive_create_contract.rb b/modules/storages/app/contracts/storages/storages/create_contract.rb
similarity index 95%
rename from modules/storages/app/contracts/storages/storages/one_drive_create_contract.rb
rename to modules/storages/app/contracts/storages/storages/create_contract.rb
index 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::BaseContract
attribute :creator
validate :creator_must_be_user
diff --git a/modules/storages/app/contracts/storages/storages/nextcloud_base_contract.rb b/modules/storages/app/contracts/storages/storages/nextcloud_contract.rb
similarity index 98%
rename from modules/storages/app/contracts/storages/storages/nextcloud_base_contract.rb
rename to modules/storages/app/contracts/storages/storages/nextcloud_contract.rb
index 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 < ::ModelContract
attribute :host
validates :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.rb
deleted file mode 100644
index 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
-end
diff --git a/modules/storages/app/contracts/storages/storages/nextcloud_update_contract.rb b/modules/storages/app/contracts/storages/storages/nextcloud_update_contract.rb
deleted file mode 100644
index 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
-end
diff --git a/modules/storages/app/contracts/storages/storages/one_drive_base_contract.rb b/modules/storages/app/contracts/storages/storages/one_drive_contract.rb
similarity index 96%
rename from modules/storages/app/contracts/storages/storages/one_drive_base_contract.rb
rename to modules/storages/app/contracts/storages/storages/one_drive_contract.rb
index 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 < BaseContract
attribute :host
validates :host, absence: true
end
diff --git a/modules/storages/app/contracts/storages/storages/one_drive_update_contract.rb b/modules/storages/app/contracts/storages/storages/update_contract.rb
similarity index 94%
rename from modules/storages/app/contracts/storages/storages/one_drive_update_contract.rb
rename to modules/storages/app/contracts/storages/storages/update_contract.rb
index 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::BaseContract
end
end
diff --git a/modules/storages/app/services/storages/storages/create_service.rb b/modules/storages/app/services/storages/storages/create_service.rb
index 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 files
module Storages::Storages
class CreateService < ::BaseServices::Create
- # def initialize(user:)
- # binding.pry
- # super(user:, contract_class: nil, contract_options: {})
- # end
-
protected
def 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.rb
index 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).mount
post &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-finally
after_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).mount
patch &API::V3::Utilities::Endpoints::Update.new(model: Storages::Storage).mount
diff --git a/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb
index 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 do
end
before 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_storage
login_as current_user
@@ -423,12 +422,12 @@ RSpec.describe 'API v3 storages resource', content_type: :json, webmock: true do
context 'if request body is invalid' do
let(:params) do
{
- clientId: 'only_an_id'
+ clientSecret: 'only_an_id'
}
end
it_behaves_like 'constraint violation' do
- let(:message) { 'Client secret can\'t be blank.' }
+ let(:message) { 'Client can\'t be blank.' }
end
end
end
diff --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.rb
index 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 do
end
let(: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::NextcloudCreateContract
end
let(: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.rb
index 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 do
let(: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)
contract
end
@@ -53,11 +52,11 @@ RSpec.describe Storages::Storages::SetAttributesService, type: :model do
end
let(: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::NextcloudCreateContract
end
let(:params) { { provider_type: Storages::Storage::PROVIDER_TYPE_NEXTCLOUD } }
@@ -65,7 +64,7 @@ RSpec.describe Storages::Storages::SetAttributesService, type: :model do
before do
allow(model_instance)
.to receive(:valid?)
- .and_return(model_valid)
+ .and_return(model_valid)
end
subject { instance.call(params) }
@@ -90,7 +89,7 @@ RSpec.describe Storages::Storages::SetAttributesService, type: :model do
end
it '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')
end
context 'when setting host' do
diff --git a/modules/storages/spec/services/storages/storages/update_service_spec.rb b/modules/storages/spec/services/storages/storages/update_service_spec.rb
index 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 do
it 'cannot update storage creator' do
storage_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 do
subject do
described_class
- .new(user:, model: storage)
+ .new(user:, model: storage, contract_class: Storages::Storages::NextcloudUpdateContract)
.call({ name: })
end
diff --git a/spec/contracts/oauth_clients/create_contract_spec.rb b/spec/contracts/oauth_clients/create_contract_spec.rb
index 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 do
describe 'validations' do
context 'when all attributes are valid' do
include_examples 'contract is valid'
+
+ context 'with client secret being nil' do
+ let(:client_secret) { nil }
+
+ include_examples 'contract is valid'
+ end
end
- %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_short
end
end