HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vmi1674223.contaboserver.net 5.4.0-182-generic #202-Ubuntu SMP Fri Apr 26 12:29:36 UTC 2024 x86_64
User: root (0)
PHP: 7.4.3-4ubuntu2.22
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/openproject/spec/controllers/copy_projects_controller_spec.rb
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2020 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-2017 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 docs/COPYRIGHT.rdoc for more details.
#++

require 'spec_helper'

describe CopyProjectsController, type: :controller do
  let(:current_user) { FactoryBot.create(:admin) }
  let(:redirect_path) { "/projects/#{project.id}/settings/generic" }
  let(:permission) { :copy_projects }
  let(:project) { FactoryBot.create(:project_with_types, public: false) }
  let(:copy_project_params) do
    {
      'description' => 'Some pretty description',
      'enabled_module_names' => ['work_package_tracking', 'boards', ''],
      'public' => project.public,
      'type_ids' => project.types.map(&:id)
    }
  end

  before do
    allow(User).to receive(:current).and_return current_user
    # Prevent actually setting User.current.
    # Otherwise the set user might be used in the next spec.
    allow(User).to receive(:current=)

    request.env['HTTP_REFERER'] = redirect_path
  end

  describe 'copy_from_settings uses correct project to copy from' do
    before do
      get 'copy_project', params: { id: project.id, coming_from: :settings }
    end

    it { expect(assigns(:project)).to eq(project) }

    it { expect(assigns(:copy_project).id).to be_nil }

    it { expect(response).to render_template('copy_from_settings') }
  end

  describe 'copy_from_settings without valid project' do
    before { get 'copy_project', params: { id: 'invalid' } }

    it { expect(response.code).to eq('404') }
  end

  describe 'copy_from_settings without name and identifier' do
    before do
      post 'copy',
           params: { id: project.id, project: copy_project_params }
    end

    it { expect(response).to render_template('copy_from_settings') }
    it 'should display error validation messages' do
      expect(assigns(:copy_project).errors).not_to be_empty
    end
  end

  describe 'copy_from_settings permissions' do
    def fetch
      get 'copy_project', params: { id: project.id, coming_from: :settings }
    end

    it_should_behave_like 'a controller action which needs project permissions'
  end

  shared_examples_for 'successful copy' do
    it { expect(flash[:notice]).to eq(I18n.t('copy_project.started', source_project_name: source_project.name, target_project_name: target_project_name)) }
  end

  def copy_project(project)
    post 'copy',
         params: {
           id: project.id,
           project: copy_project_params.merge(identifier: 'copy', name: 'copy')
         }
    perform_enqueued_jobs
  end

  describe 'copy creates a new project' do
    before { copy_project(project) }

    def expect_redirect_to
      true
    end

    it { expect(Project.count).to eq(2) }

    it 'copied project enables modules of source project' do
      expect(Project.order(:id).last.enabled_modules.map(&:name))
        .to match_array(project.enabled_modules.map(&:name) - %w[repository])
    end

    it_behaves_like 'successful copy' do
      let(:source_project) { project }
      let(:target_project_name) { 'copy' }
    end

    it { expect(response).to redirect_to(redirect_path) }
  end

  describe 'copy permissions' do
    def fetch
      post 'copy',
           params: {
             id: project.id,
             project: copy_project_params.merge(identifier: 'copy', name: 'copy')
           }
    end

    def expect_redirect_to
      true
    end

    let(:permission) { [:copy_projects, :add_project] }
    let(:project) { FactoryBot.create(:project, public: false) }

    it_should_behave_like 'a controller action which needs project permissions'
  end

  describe 'copy sends eMail' do
    let(:maildouble) { double('Mail::Message', deliver: true) }

    before do
      allow(maildouble).to receive(:deliver_now).and_return nil
    end

    context 'on success' do
      it 'user receives success mail' do
        expect(ProjectMailer).to receive(:copy_project_succeeded).and_return(maildouble)

        copy_project(project)
      end
    end

    context 'on error' do
      before do
        allow(ProjectMailer).to receive(:with_deliveries).and_raise(ActiveRecord::RecordNotFound)
      end

      it 'user receives success mail' do
        expect(ProjectMailer).to receive(:copy_project_failed).and_return(maildouble)

        copy_project(project)
      end
    end
  end
end