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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add create contract spec
Oliver Günther2d3a246fa477
diff --git a/app/contracts/attachments/create_contract.rb b/app/contracts/attachments/create_contract.rbindex 60a7b57b13b..6c7c36078ce 100644--- a/app/contracts/attachments/create_contract.rb+++ b/app/contracts/attachments/create_contract.rb@@ -72,20 +72,27 @@ module Attachments### Validates the content type, if a whitelist is setdef validate_content_type- # In some cases, we do not want to process the whitelist- # for the attachment mime types. For example, when- # storing backups or exports- return unless options.fetch(:validate_content_type, true)-- whitelist = Array(Setting.attachment_whitelist)-# If the whitelist is empty, assume all files are allowed# as before- return if whitelist.empty?-- unless whitelist.include?(model.content_type) || whitelist.include?("*#{model.extension}")+ unless matches_whitelist?(attachment_whitelist)+ Rails.logger.info { "Uploaded file #{model.filename} with type #{model.content_type} does not match whitelist" }errors.add :content_type, :not_whitelisted, value: model.content_typeendend++ ##+ # Get the user-defined whitelist or a custom whitelist+ # defined for this invocation+ def attachment_whitelist+ Array(options.fetch(:whitelist, Setting.attachment_whitelist))+ end++ ##+ # Returns whether the attachment matches the whitelist+ def matches_whitelist?(whitelist)+ return true if whitelist.empty?++ whitelist.include?(model.content_type) || whitelist.include?("*#{model.extension}")+ endendenddiff --git a/modules/bim/spec/factories/bcf_viewpoint_attachment_factory.rb b/modules/bim/spec/factories/bcf_viewpoint_attachment_factory.rbindex 31a583e0ff7..8f88d96dec0 100644--- a/modules/bim/spec/factories/bcf_viewpoint_attachment_factory.rb+++ b/modules/bim/spec/factories/bcf_viewpoint_attachment_factory.rb@@ -36,7 +36,7 @@ FactoryBot.define doRack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/files/image.png"),'image/png',- true,+ true)endenddiff --git a/spec/contracts/attachments/create_contract_spec.rb b/spec/contracts/attachments/create_contract_spec.rbnew file mode 100644index 00000000000..efcce412ac3--- /dev/null+++ b/spec/contracts/attachments/create_contract_spec.rb@@ -0,0 +1,143 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'+require 'contracts/shared/model_contract_shared_context'++describe Attachments::CreateContract do+ include_context 'ModelContract shared context'++ let(:current_user) { FactoryBot.build_stubbed :user }+ let(:model) do+ FactoryBot.build :attachment,+ container: container,+ content_type: content_type,+ file: file,+ filename: filename,+ author: current_user+ end+ let(:contract) { described_class.new model, user, options: contract_options }+ let(:contract_options) { {} }++ let(:user) { current_user }+ let(:container) { nil }+ let(:file) do+ Rack::Test::UploadedFile.new(+ Rails.root.join("spec/fixtures/files/image.png"),+ 'image/png',+ true+ )+ end+ let(:content_type) { 'image/png' }+ let(:filename) { 'image.png' }++ let(:can_attach_global) { true }++ before do+ allow(Redmine::Acts::Attachable.attachables)+ .to receive(:none?).and_return(!can_attach_global)+ end++ context 'with user who has no permissions' do+ let(:can_attach_global) { false }++ it_behaves_like 'contract is invalid', base: :error_unauthorized+ end++ context 'with a user that is not the author' do+ let(:user) { FactoryBot.build_stubbed :user }++ it_behaves_like 'contract is invalid', author: :invalid+ end++ context 'with user who has permissions to add' do+ it_behaves_like 'contract is valid'+ end++ context 'with a container' do+ let(:container) { FactoryBot.build_stubbed :work_package }++ before do+ allow(container)+ .to receive(:attachments_addable?)+ .with(user)+ .and_return(can_attach)+ end++ context 'with user who has no permissions' do+ let(:can_attach) { false }++ it_behaves_like 'contract is invalid', base: :error_unauthorized+ end++ context 'with user who has permissions to add' do+ let(:can_attach) { true }++ it_behaves_like 'contract is valid'+ end++ end++ context 'with an empty whitelist',+ with_settings: { attachment_whitelist: %w[] } do+ it_behaves_like 'contract is valid'+ end++ context 'with a matching mime whitelist',+ with_settings: { attachment_whitelist: %w[image/png] } do+ it_behaves_like 'contract is valid'+ end++ context 'with a matching extension whitelist',+ with_settings: { attachment_whitelist: %w[*.png] } do+ it_behaves_like 'contract is valid'+ end++ context 'with a non-matching whitelist',+ with_settings: { attachment_whitelist: %w[*.jpg image/jpeg] } do+ it_behaves_like 'contract is invalid', content_type: :not_whitelisted++ context 'when disabling the whitelist check' do+ let(:contract_options) do+ { whitelist: [] }+ end++ it_behaves_like 'contract is valid'+ end++ context 'when overriding the whitelist' do+ let(:contract_options) do+ { whitelist: %w[*.png] }+ end++ it_behaves_like 'contract is valid'+ end+ end+enddiff --git a/spec/factories/attachment_factory.rb b/spec/factories/attachment_factory.rbindex e09b57ab4a6..55c37949cb8 100644--- a/spec/factories/attachment_factory.rb+++ b/spec/factories/attachment_factory.rb@@ -45,6 +45,10 @@ FactoryBot.define dobinary: trueend+ callback(:after_build, :after_stub) do |attachment, evaluator|+ attachment.filename = evaluator.filename if evaluator.filename+ end+factory :wiki_attachment docontainer factory: :wiki_page_with_contentend