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.2 KB
Raw Blame
#-- 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 Grids::CreateContract, "for Grids::Overview" do
  let(:project) do
    build_stubbed(:project).tap do |p|
      allow(Project)
        .to receive(:find)
        .with(p.identifier)
        .and_return(p)
    end
  end
  let(:grid) do
    scope = OpenProject::StaticRouting::StaticUrlHelpers.new.project_overview_path(project)
    Grids::Factory.build(scope, current_user)
  end
  let(:instance) { described_class.new(grid, current_user) }
  let(:permissions) { %i[manage_dashboards] }
  let(:current_user) { build_stubbed(:user) }

  before do
    mock_permissions_for(current_user) do |mock|
      mock.allow_in_project *permissions, project:
    end
  end

  include_context "model contract"

  describe "user_id" do
    it_behaves_like "is not writable" do
      let(:attribute) { :user_id }
      let(:value) { 5 }
    end
  end

  describe "project_id" do
    it_behaves_like "is writable" do
      let(:attribute) { :project_id }
      let(:value) { 5 }
    end
  end

  context "if an overview grid already exists for the project" do
    before do
      allow(Grids::Overview)
        .to receive(:exists?)
        .with(project_id: project.id)
        .and_return(true)
    end

    it "reports an error on scope" do
      instance.validate

      expect(instance.errors.symbols_for(:scope))
        .to contain_exactly(:taken)
    end
  end

  context "if the user lacks :manage_dashboards permission" do
    # View project is a public permission so every member will have it but as we stub...
    let(:permissions) { [:view_project] }

    before do
      # Have to remove the widgets (members, work_package_overview) that is not allowed to the user
      # as the necessary permission is lacking.
      grid.widgets = grid.widgets.reject { |w| %w(members work_packages_overview).include?(w.identifier) }
    end

    context "if the grid does not have any changes compared to the default" do
      it "is valid" do
        expect(instance.validate)
          .to be_truthy
      end
    end

    context "if the grid has changes compared to the default" do
      context "(row_count)" do
        before do
          grid.row_count += 1
        end

        it "reports an error on row_count" do
          instance.validate

          expect(instance.errors.symbols_for(:row_count))
            .to contain_exactly(:unchangeable)
        end
      end

      context "(column_count)" do
        before do
          grid.column_count += 1
        end

        it "reports an error on row_count" do
          instance.validate

          expect(instance.errors.symbols_for(:column_count))
            .to contain_exactly(:unchangeable)
        end
      end

      context "(widget added)" do
        before do
          grid.row_count = 4
          grid.widgets.build(identifier: "project_description",
                             start_row: 3,
                             end_row: 4,
                             start_column: 1,
                             end_column: 3,
                             options: {
                               name: I18n.t("js.grid.widgets.project_description.title")
                             })
        end

        it "reports an error on widgets" do
          instance.validate

          expect(instance.errors.symbols_for(:widgets))
            .to contain_exactly(:unchangeable)
        end
      end

      context "(widget altered)" do
        before do
          grid.row_count = 4
          grid.widgets.last.end_row += 1
        end

        it "reports an error on widgets" do
          instance.validate

          expect(instance.errors.symbols_for(:widgets))
            .to contain_exactly(:unchangeable)
        end
      end

      context "(widget options altered)" do
        before do
          grid.widgets[0].options = { name: "My own name" }
        end

        it "reports an error on widgets" do
          instance.validate

          expect(instance.errors.symbols_for(:widgets))
            .to contain_exactly(:unchangeable)
        end
      end
    end
  end
end