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.0 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 CustomActions::UpdateService do
  let(:action) do
    action = build_stubbed(:custom_action)

    allow(action)
      .to receive(:save)
      .and_return(save_success)

    action
  end
  let(:user) { build_stubbed(:user) }
  let(:save_success) { true }
  let(:contract_success) { true }
  let(:contract_errors) { double("contract errors") }
  let(:instance) do
    contract
    described_class.new(action:, user:)
  end
  let(:contract) do
    contract_instance = double("contract instance")

    allow(CustomActions::CuContract)
      .to receive(:new)
      .with(action)
      .and_return(contract_instance)

    allow(contract_instance)
      .to receive(:validate)
      .and_return(contract_success)
    allow(contract_instance)
      .to receive(:errors)
      .and_return(contract_errors)

    contract_instance
  end

  describe "#call" do
    it "is successful" do
      expect(instance.call(attributes: {}))
        .to be_success
    end

    it "is has the action in the result" do
      expect(instance.call(attributes: {}).result)
        .to eql action
    end

    it "yields the result" do
      yielded = false

      proc = Proc.new do |call|
        yielded = call
      end

      instance.call(attributes: {}, &proc)

      expect(yielded)
        .to be_success
    end

    context "unsuccessful saving" do
      let(:save_success) { false }

      it "yields the result" do
        yielded = false

        proc = Proc.new do |call|
          yielded = call
        end

        instance.call(attributes: {}, &proc)

        expect(yielded)
          .to be_failure
      end
    end

    context "unsuccessful contract" do
      let(:contract_success) { false }

      it "yields the result" do
        yielded = false

        proc = Proc.new do |call|
          yielded = call
        end

        instance.call(attributes: {}, &proc)

        expect(yielded)
          .to be_failure
      end
    end

    it "sets the name of the action" do
      expect(instance.call(attributes: { name: "new name" }).result.name)
        .to eql "new name"
    end

    it "updates the actions" do
      action.actions = [CustomActions::Actions::AssignedTo.new("1"),
                        CustomActions::Actions::Status.new("3")]

      new_actions = instance
                    .call(attributes: { actions: { assigned_to: ["2"], priority: ["3"] } })
                    .result
                    .actions
                    .map { |a| [a.key, a.values] }

      expect(new_actions)
        .to contain_exactly([:assigned_to, [2]], [:priority, [3]])
    end

    it "handles unknown actions" do
      new_actions = instance
                    .call(attributes: { actions: { some_bogus_name: ["3"] } })
                    .result
                    .actions
                    .map { |a| [a.key, a.values] }

      expect(new_actions)
        .to contain_exactly([:inexistent, ["3"]])
    end

    it "updates the conditions" do
      old_status = create(:status)
      new_status = create(:status)

      action.conditions = [CustomActions::Conditions::Status.new(old_status.id)]

      new_conditions = instance
                       .call(attributes: { conditions: { status: new_status.id } })
                       .result
                       .conditions
                       .map { |a| [a.key, a.values] }

      expect(new_conditions)
        .to contain_exactly([:status, [new_status.id]])
    end

    it "handles unknown conditions" do
      new_conditions = instance
                       .call(attributes: { conditions: { some_bogus_name: ["3"] } })
                       .result
                       .conditions
                       .map { |a| [a.key, a.values] }

      expect(new_conditions)
        .to contain_exactly([:inexistent, [3]])
    end
  end
end