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

5.4 KB
Raw Blame
# 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.

require "spec_helper"

RSpec.describe SCM::DeleteManagedRepositoryService, skip_if_command_unavailable: "svnadmin" do
  let(:user) { build(:user) }
  let(:config) { {} }
  let(:project) { build(:project) }

  let(:repository) { build(:repository_subversion) }

  subject(:service) { SCM::DeleteManagedRepositoryService.new(repository) }

  before do
    allow(OpenProject::Configuration).to receive(:[]).and_call_original
    allow(OpenProject::Configuration).to receive(:[]).with("scm").and_return(config)
    allow(Setting).to receive(:enabled_scm).and_return(["subversion", "git"])
  end

  shared_examples "does not delete the repository" do
    it "does not delete the repository" do
      expect(repository.managed?).to be false
      expect(service.call).to be false
    end
  end

  context "with no managed configuration" do
    it_behaves_like "does not delete the repository"
  end

  context "with managed repository, but no config" do
    let(:repository) { build(:repository_subversion, scm_type: :managed) }

    it "does allow to delete the repository" do
      expect(repository.managed?).to be true
      expect(service.call).to be true
    end
  end

  context "with managed repository and managed config" do
    include_context "with tmpdir"
    let(:config) do
      {
        subversion: { manages: File.join(tmpdir, "svn") },
        git: { manages: File.join(tmpdir, "git") }
      }
    end

    let(:repository) do
      repo = Repository::Subversion.new(scm_type: :managed)
      repo.project = project
      repo.configure(:managed, nil)

      repo.save!
      perform_enqueued_jobs
      repo
    end

    it "deletes the repository" do
      expect(File.directory?(repository.root_url)).to be true
      expect(service.call).to be true
      expect(File.directory?(repository.root_url)).to be false
    end

    it "does not raise an exception upon permission errors" do
      expect(File.directory?(repository.root_url)).to be true
      expect(SCM::DeleteLocalRepositoryJob)
        .to receive(:new).and_raise(Errno::EACCES)

      expect(service.call).to be false
    end

    context "and parent project" do
      let(:parent) { create(:project) }
      let(:project) { create(:project, parent:) }
      let(:repo_path) do
        Pathname.new(File.join(tmpdir, "svn", project.identifier))
      end

      it "does not delete anything but the repository itself" do
        expect(service.call).to be true

        path = Pathname.new(repository.root_url)
        expect(path).to eq(repo_path)

        expect(path.exist?).to be false
        expect(path.parent.exist?).to be true
        expect(path.parent.to_s).to eq(repository.class.managed_root)
      end
    end
  end

  context "with managed remote config", :webmock do
    let(:url) { "http://myreposerver.example.com/api/" }
    let(:config) do
      {
        subversion: { manages: url }
      }
    end

    let(:repository) do
      repo = Repository::Subversion.new(scm_type: :managed)
      repo.project = project
      repo.configure(:managed, nil)

      repo
    end

    context "with a valid remote" do
      before do
        stub_request(:post, url).to_return(status: 200, body: {}.to_json)
      end

      it "calls the callback" do
        expect(SCM::DeleteRemoteRepositoryJob)
          .to receive(:perform_now)
          .and_call_original

        expect(service.call).to be true
        expect(WebMock)
          .to have_requested(:post, url)
          .with(body: hash_including(identifier: repository.repository_identifier,
                                     action: "delete"))
      end
    end

    context "with a remote callback returning an error" do
      before do
        stub_request(:post, url)
          .to_return(status: 400, body: { success: false, message: "An error occurred" }.to_json)
      end

      it "calls the callback" do
        expect(SCM::DeleteRemoteRepositoryJob)
          .to receive(:perform_now)
          .and_call_original

        expect(service.call).to be false

        expect(service.localized_rejected_reason)
          .to eq("Calling the managed remote failed with message 'An error occurred' (Code: 400)")
        expect(WebMock)
          .to have_requested(:post, url)
                .with(body: hash_including(action: "delete"))
      end
    end
  end
end