|
| 1 | +#-- encoding: UTF-8 |
| 2 | +#-- copyright |
| 3 | +# OpenProject is a project management system. |
| 4 | +# Copyright (C) 2012-2018 the OpenProject Foundation (OPF) |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU General Public License version 3. |
| 8 | +# |
| 9 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 10 | +# Copyright (C) 2006-2017 Jean-Philippe Lang |
| 11 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 12 | +# |
| 13 | +# This program is free software; you can redistribute it and/or |
| 14 | +# modify it under the terms of the GNU General Public License |
| 15 | +# as published by the Free Software Foundation; either version 2 |
| 16 | +# of the License, or (at your option) any later version. |
| 17 | +# |
| 18 | +# This program is distributed in the hope that it will be useful, |
| 19 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | +# GNU General Public License for more details. |
| 22 | +# |
| 23 | +# You should have received a copy of the GNU General Public License |
| 24 | +# along with this program; if not, write to the Free Software |
| 25 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 26 | +# |
| 27 | +# See docs/COPYRIGHT.rdoc for more details. |
| 28 | +#++ |
| 29 | + |
| 30 | +require 'spec_helper' |
| 31 | + |
| 32 | +describe TimeEntries::DeleteContract do |
| 33 | + let(:current_user) do |
| 34 | + FactoryBot.build_stubbed(:user) do |user| |
| 35 | + allow(user) |
| 36 | + .to receive(:allowed_to?) do |permission, permission_project| |
| 37 | + permissions.include?(permission) && time_entry_project == permission_project |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + let(:other_user) { FactoryBot.build_stubbed(:user) } |
| 42 | + let(:time_entry_work_package) do |
| 43 | + FactoryBot.build_stubbed(:work_package, |
| 44 | + project: time_entry_project) |
| 45 | + end |
| 46 | + let(:time_entry_project) { FactoryBot.build_stubbed(:project) } |
| 47 | + let(:time_entry_activity) { FactoryBot.build_stubbed(:time_entry_activity) } |
| 48 | + let(:time_entry_user) { current_user } |
| 49 | + let(:time_entry_spent_on) { Date.today } |
| 50 | + let(:time_entry_hours) { 5 } |
| 51 | + let(:time_entry_comments) { "A comment" } |
| 52 | + let(:time_entry) do |
| 53 | + TimeEntry.create(project: time_entry_project, |
| 54 | + work_package: time_entry_work_package, |
| 55 | + user: time_entry_user, |
| 56 | + activity: time_entry_activity, |
| 57 | + spent_on: time_entry_spent_on, |
| 58 | + hours: time_entry_hours, |
| 59 | + comments: time_entry_comments) |
| 60 | + end |
| 61 | + let(:permissions) { %i(edit_time_entries) } |
| 62 | + |
| 63 | + subject(:contract) { described_class.new(time_entry, current_user) } |
| 64 | + |
| 65 | + def expect_valid(valid, symbols = {}) |
| 66 | + expect(contract.validate).to eq(valid) |
| 67 | + |
| 68 | + symbols.each do |key, arr| |
| 69 | + expect(contract.errors.symbols_for(key)).to match_array arr |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + shared_examples 'is valid' do |
| 74 | + it 'is valid' do |
| 75 | + expect_valid(true) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + it_behaves_like 'is valid' |
| 80 | + |
| 81 | + context 'when user is not allowed to delete time entries' do |
| 82 | + let(:permissions) { [] } |
| 83 | + |
| 84 | + it 'is invalid' do |
| 85 | + expect_valid(false, base: %i(error_unauthorized)) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + context 'when time_entry user is not contract user' do |
| 90 | + let(:time_entry_user) { other_user } |
| 91 | + |
| 92 | + context 'when has permission' do |
| 93 | + let(:permissions) { %i[edit_time_entries] } |
| 94 | + |
| 95 | + it 'is valid' do |
| 96 | + expect_valid(true) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + context 'when has no permission' do |
| 101 | + let(:permissions) { %i[edit_own_time_entries] } |
| 102 | + it 'is invalid' do |
| 103 | + expect_valid(false, base: %i(error_unauthorized)) |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments