|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# -- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +# ++ |
| 30 | + |
| 31 | +require "contracts/shared/model_contract_shared_context" |
| 32 | + |
| 33 | +RSpec.shared_context "as sprint contract" do |
| 34 | + include_context "ModelContract shared context" |
| 35 | + |
| 36 | + let(:user) { build_stubbed(:user) } |
| 37 | + |
| 38 | + let(:sprint_project) { build_stubbed(:project) } |
| 39 | + let(:sprint_name) { "Sprint 1" } |
| 40 | + let(:sprint_start_date) { Time.zone.today } |
| 41 | + let(:sprint_finish_date) { Time.zone.today + 14.days } |
| 42 | + let(:sprint_status) { "in_planning" } |
| 43 | + let(:permissions) { [:create_sprints] } |
| 44 | + |
| 45 | + subject(:contract) { described_class.new(sprint, user) } |
| 46 | + |
| 47 | + before do |
| 48 | + mock_permissions_for(user) do |mock| |
| 49 | + mock.allow_in_project(*permissions, project: sprint_project) if sprint_project |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + describe "validation" do |
| 54 | + context "with valid attributes and permissions" do |
| 55 | + it_behaves_like "contract is valid" |
| 56 | + end |
| 57 | + |
| 58 | + context "when project is nil" do |
| 59 | + let(:sprint_project) { nil } |
| 60 | + |
| 61 | + it_behaves_like "contract is invalid", project: :blank |
| 62 | + end |
| 63 | + |
| 64 | + context "when user does not have create_sprints permission" do |
| 65 | + let(:permissions) { [:view_work_packages] } |
| 66 | + |
| 67 | + it_behaves_like "contract is invalid", base: :error_unauthorized |
| 68 | + end |
| 69 | + |
| 70 | + context "when user has no permissions in project" do |
| 71 | + let(:permissions) { [] } |
| 72 | + |
| 73 | + it_behaves_like "contract is invalid", base: :error_unauthorized |
| 74 | + end |
| 75 | + |
| 76 | + context "when name is blank" do |
| 77 | + let(:sprint_name) { "" } |
| 78 | + |
| 79 | + it_behaves_like "contract is invalid", name: :blank |
| 80 | + end |
| 81 | + |
| 82 | + context "when finish_date is before start_date" do |
| 83 | + let(:sprint_start_date) { Time.zone.today } |
| 84 | + let(:sprint_finish_date) { Time.zone.today - 1.day } |
| 85 | + |
| 86 | + it_behaves_like "contract is invalid", finish_date: :greater_than_or_equal_to |
| 87 | + end |
| 88 | + |
| 89 | + context "when finish_date is same as start_date" do |
| 90 | + let(:sprint_start_date) { Time.zone.today } |
| 91 | + let(:sprint_finish_date) { sprint_start_date } |
| 92 | + |
| 93 | + it_behaves_like "contract is valid" |
| 94 | + end |
| 95 | + |
| 96 | + context "when the sprint is active" do |
| 97 | + let(:sprint_status) { "active" } |
| 98 | + |
| 99 | + context "when start_date is blank" do |
| 100 | + let(:sprint_start_date) { nil } |
| 101 | + |
| 102 | + it_behaves_like "contract is invalid", start_date: :blank |
| 103 | + end |
| 104 | + |
| 105 | + context "when finish_date is blank" do |
| 106 | + let(:sprint_finish_date) { nil } |
| 107 | + |
| 108 | + it_behaves_like "contract is invalid", finish_date: :blank |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + context "when the sprint is in_planning" do |
| 113 | + context "when start_date is blank" do |
| 114 | + let(:sprint_start_date) { nil } |
| 115 | + |
| 116 | + it_behaves_like "contract is valid" |
| 117 | + end |
| 118 | + |
| 119 | + context "when finish_date is blank" do |
| 120 | + let(:sprint_finish_date) { nil } |
| 121 | + |
| 122 | + it_behaves_like "contract is valid" |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + context "when user is admin without project permission" do |
| 127 | + let(:user) { build_stubbed(:admin) } |
| 128 | + let(:permissions) { [] } |
| 129 | + |
| 130 | + it_behaves_like "contract is valid" |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments