refactor

This commit is contained in:
Yauheni Suhakou
2026-06-12 16:30:37 +02:00
parent de5456d772
commit 490f93fa54
7 changed files with 60 additions and 31 deletions
@@ -0,0 +1,48 @@
# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 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 Wikis::XWikiProviders::Concerns::FetchesInstanceId
private
def after_validate(service_call)
model = service_call.result
return service_call unless should_fetch_instance_id?(model)
result = Wikis::XWikiProviders::FetchInstanceIdService.new(provider: model).call
if result.success?
model.universal_identifier = result.value!
else
service_call.errors.add(:url, :xwiki_unreachable)
service_call.success = false
end
service_call
end
end
@@ -31,21 +31,11 @@
module Wikis
module XWikiProviders
class CreateService < ::BaseServices::Create
include Concerns::FetchesInstanceId
private
def after_validate(service_call)
model = service_call.result
return service_call if model.url.blank?
FetchInstanceIdService.new(provider: model).call
.fmap { |id| model.universal_identifier = id }
.or do
service_call.errors.add(:url, :xwiki_unreachable)
service_call.success = false
end
service_call
end
def should_fetch_instance_id?(model) = model.url.present?
end
end
end
@@ -38,8 +38,9 @@ module Wikis
end
def call
auth_strategy = provider.resolve("authentication.noop").call.value!
provider.resolve("queries.instance_id").call(auth_strategy:)
provider.resolve("authentication.noop").call.bind do |auth_strategy|
provider.resolve("queries.instance_id").call(auth_strategy:)
end
end
end
end
@@ -31,21 +31,11 @@
module Wikis
module XWikiProviders
class UpdateService < ::BaseServices::Update
include Concerns::FetchesInstanceId
private
def after_validate(service_call)
model = service_call.result
return service_call if model.url.blank? || !model.url_changed?
FetchInstanceIdService.new(provider: model).call
.fmap { |id| model.universal_identifier = id }
.or do
service_call.errors.add(:url, :xwiki_unreachable)
service_call.success = false
end
service_call
end
def should_fetch_instance_id?(model) = model.url.present? && model.url_changed?
end
end
end
@@ -84,7 +84,7 @@ RSpec.describe Wikis::Admin::WikiProvidersController do
context "with valid params" do
before do
allow(Wikis::XWikiProviders::FetchInstanceIdService).to receive(:new)
.and_return(double(call: Dry::Monads::Success("xwiki-test-id")))
.and_return(instance_double(Wikis::XWikiProviders::FetchInstanceIdService, call: Dry::Monads::Success("xwiki-test-id")))
end
it "creates a provider and redirects to the wizard" do
@@ -41,7 +41,7 @@ RSpec.describe Wikis::XWikiProviders::CreateService, type: :model do
before do
allow(Wikis::XWikiProviders::FetchInstanceIdService).to receive(:new)
.and_return(double(call: Dry::Monads::Success("test-id")))
.and_return(instance_double(Wikis::XWikiProviders::FetchInstanceIdService, call: Dry::Monads::Success("test-id")))
end
end
@@ -44,7 +44,7 @@ RSpec.describe Wikis::XWikiProviders::UpdateService, type: :model do
let(:current_user) { build_stubbed(:admin) }
let(:provider) { create(:xwiki_provider, url: "https://old.example.com/", universal_identifier: "old-id") }
let(:service) { described_class.new(user: current_user, model: provider) }
let(:fetch_service) { instance_double(Wikis::XWikiProviders::FetchInstanceIdService) }
let(:fetch_service) { instance_spy(Wikis::XWikiProviders::FetchInstanceIdService) }
before do
allow(Wikis::XWikiProviders::FetchInstanceIdService).to receive(:new).and_return(fetch_service)