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
use services/contracts for forum messages
ulfertsffc131695a58
diff --git a/app/contracts/messages/base_contract.rb b/app/contracts/messages/base_contract.rbnew file mode 100644index 00000000000..f0699227a21--- /dev/null+++ b/app/contracts/messages/base_contract.rb@@ -0,0 +1,47 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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.+#++++module Messages+ class BaseContract < ::ModelContract+ include Attachments::ValidateReplacements++ def self.model+ Message+ end++ attribute :forum_id+ attribute :parent_id+ attribute :subject+ attribute :content+ attribute :last_reply+ attribute :locked+ attribute :sticky+ end+enddiff --git a/app/contracts/messages/create_contract.rb b/app/contracts/messages/create_contract.rbnew file mode 100644index 00000000000..cf66041ae8d--- /dev/null+++ b/app/contracts/messages/create_contract.rb@@ -0,0 +1,35 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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.+#++++# TODO: This is but a stub+module Messages+ class CreateContract < BaseContract+ end+enddiff --git a/app/contracts/messages/update_contract.rb b/app/contracts/messages/update_contract.rbnew file mode 100644index 00000000000..980b1ca909e--- /dev/null+++ b/app/contracts/messages/update_contract.rb@@ -0,0 +1,35 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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.+#++++# TODO: This is but a stub+module Messages+ class UpdateContract < BaseContract+ end+enddiff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rbindex 764cc030a56..c9d224c239d 100644--- a/app/controllers/messages_controller.rb+++ b/app/controllers/messages_controller.rb@@ -62,24 +62,20 @@ class MessagesController < ApplicationController# new topicdef new- @message = Message.new.tap do |m|- m.author = User.current- m.forum = @forum- end+ Messages::SetAttributesService+ .new(user: current_user,+ model: Message.new,+ contract_class: NoopContract)+ .call(forum: @forum)+ .resultend# Create a new topicdef create- @message = Message.new.tap do |m|- m.author = User.current- m.forum = @forum- end+ call = create_message(@forum)+ @message = call.result- @message.attributes = permitted_params.message(@message)- @message.attach_files(permitted_params.attachments.to_h)-- if @message.save- render_attachment_warning_if_needed(@message)+ if call.success?call_hook(:controller_messages_new_after_save, params: params, message: @message)redirect_to topic_path(@message)@@ -92,15 +88,12 @@ class MessagesController < ApplicationControllerdef reply@topic = @message.root- @reply = Message.new- @reply.author = User.current- @reply.forum = @forum- @reply.attributes = permitted_params.reply- @reply.attach_files(permitted_params.attachments.to_h)+ call = create_message(@forum)+ @reply = call.result++ if call.success?+ @topic.children << @reply- @topic.children << @reply- unless @reply.new_record?- render_attachment_warning_if_needed(@reply)call_hook(:controller_messages_reply_after_save, params: params, message: @reply)endredirect_to topic_path(@topic, r: @reply)@@ -109,19 +102,19 @@ class MessagesController < ApplicationController# Edit a messagedef editreturn render_403 unless @message.editable_by?(User.current)+@message.attributes = permitted_params.message(@message)end# Edit a messagedef update+ # TODO: move into contractreturn render_403 unless @message.editable_by?(User.current)- @message.attributes = permitted_params.message(@message)- @message.attach_files(permitted_params.attachments.to_h)+ call = update_message(@message)- if @message.save- render_attachment_warning_if_needed(@message)- flash[:notice] = l(:notice_successful_update)+ if call.success?+ flash[:notice] = t(:notice_successful_update)@message.reloadredirect_to topic_path(@message.root, r: (@message.parent_id && @message.id))else@@ -131,9 +124,11 @@ class MessagesController < ApplicationController# Delete a messagesdef destroy+ # TODO: move into contractreturn render_403 unless @message.destroyable_by?(User.current)+@message.destroy- flash[:notice] = l(:notice_successful_delete)+ flash[:notice] = t(:notice_successful_delete)redirect_target = if @message.parent.nil?{ controller: '/forums', action: 'show', project_id: @project, id: @forum }else@@ -156,4 +151,34 @@ class MessagesController < ApplicationControllerformat.any { head :not_acceptable }endend++ private++ def update_message(message)+ Messages::UpdateService+ .new(user: current_user,+ model: message)+ .call(permitted_params.message(@project)+ .merge(attachment_params))+ end++ def create_message(forum)+ params = permitted_params.message(forum.project)+ .merge(forum: forum)+ .merge(attachment_params)++ Messages::CreateService+ .new(user: current_user)+ .call(params)+ end++ def attachment_params+ attachment_params = permitted_params.attachments.to_h++ if attachment_params.any?+ { attachment_ids: attachment_params.values.map(&:values).flatten }+ else+ {}+ end+ endenddiff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rbindex f15bfbc8e97..894666aff3e 100644--- a/app/models/permitted_params.rb+++ b/app/models/permitted_params.rb@@ -339,8 +339,9 @@ class PermittedParams# `params.fetch` and not `require` because the update controller action associated# with this is doing multiple things, therefore not requiring a message hash# all the time.- def message(instance = nil)- if instance && current_user.allowed_to?(:edit_messages, instance.project)+ def message(project = nil)+ # TODO: Move this distinction into the contract where it belongs+ if project && current_user.allowed_to?(:edit_messages, project)params.fetch(:message, {}).permit(:subject, :content, :forum_id, :locked, :sticky)elseparams.fetch(:message, {}).permit(:subject, :content, :forum_id)@@ -367,6 +368,7 @@ class PermittedParams# We rely on enum being an integer, an id that is. This will blow up# otherwise, which is fine.next if params[:enumerations][enum][param].nil?+whitelist[enum][param] = params[:enumerations][enum][param]endenddiff --git a/app/services/attachments/replace_attachments.rb b/app/services/attachments/replace_attachments.rbnew file mode 100644index 00000000000..b210e24d236--- /dev/null+++ b/app/services/attachments/replace_attachments.rb@@ -0,0 +1,49 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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.+#++++module Attachments+ module ReplaceAttachments+ extend ActiveSupport::Concern++ included do+ private++ def set_attributes(attributes)+ call = super++ if call.success?+ call.result.attachments = call.result.attachments_replacements if call.result.attachments_replacements+ end++ call+ end+ end+ end+enddiff --git a/app/services/attachments/set_replacements.rb b/app/services/attachments/set_replacements.rbindex 22b76779dc7..561273e3e32 100644--- a/app/services/attachments/set_replacements.rb+++ b/app/services/attachments/set_replacements.rb@@ -46,7 +46,7 @@ module Attachmentsreturn unless attachment_ids- work_package.attachments_replacements = Attachment.where(id: attachment_ids)+ model.attachments_replacements = Attachment.where(id: attachment_ids)endendenddiff --git a/app/services/messages/create_service.rb b/app/services/messages/create_service.rbnew file mode 100644index 00000000000..9cc3709b4c3--- /dev/null+++ b/app/services/messages/create_service.rb@@ -0,0 +1,33 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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.+#++++class Messages::CreateService < ::BaseServices::Create+ include Attachments::ReplaceAttachments+enddiff --git a/app/services/messages/set_attributes_service.rb b/app/services/messages/set_attributes_service.rbnew file mode 100644index 00000000000..2764d8b508a--- /dev/null+++ b/app/services/messages/set_attributes_service.rb@@ -0,0 +1,48 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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.+#++++# TODO: This is but a stub+module Messages+ class SetAttributesService < ::BaseServices::SetAttributes+ include Attachments::SetReplacements++ private++ def set_default_attributes(*)+ set_default_author+ end++ def set_default_author+ change_by_system do+ model.author = user+ end+ end+ end+enddiff --git a/app/services/messages/update_service.rb b/app/services/messages/update_service.rbnew file mode 100644index 00000000000..d17c7b560fb--- /dev/null+++ b/app/services/messages/update_service.rb@@ -0,0 +1,33 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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.+#++++class Messages::UpdateService < ::BaseServices::Update+ include Attachments::ReplaceAttachments+enddiff --git a/frontend/src/app/modules/attachments/attachment-list/attachment-list-item.html b/frontend/src/app/modules/attachments/attachment-list/attachment-list-item.htmlindex 15f5817dcfa..a096b4f2318 100644--- a/frontend/src/app/modules/attachments/attachment-list/attachment-list-item.html+++ b/frontend/src/app/modules/attachments/attachment-list/attachment-list-item.html@@ -29,5 +29,5 @@[icon-title]="text.removeFile({fileName: attachment.fileName})"></op-icon></a>- <input type="hidden" name="attachments[{{index}}][id]" value="{{attachment.id}}" *ngIf="!attachment.container">+ <input type="hidden" name="attachments[{{index}}][id]" value="{{attachment.id}}"></li>diff --git a/lib/api/v3/attachments/attachable_representer_mixin.rb b/lib/api/v3/attachments/attachable_representer_mixin.rbindex 04b6558c85a..a3d92d9ce72 100644--- a/lib/api/v3/attachments/attachable_representer_mixin.rb+++ b/lib/api/v3/attachments/attachable_representer_mixin.rb@@ -60,14 +60,7 @@ module APIuncacheable: truedef attachments- attachments = represented.attachments.includes(:container).to_a-- # Concat any claimed attachments on this resource- # (e.g., when new an coming back from backend with an error)- claimed = represented.attachments_claimed- attachments.concat(claimed) unless claimed.nil?-- ::API::V3::Attachments::AttachmentCollectionRepresenter.new(attachments,+ ::API::V3::Attachments::AttachmentCollectionRepresenter.new(attachment_set,attachments_by_resource,current_user: current_user)end@@ -77,6 +70,22 @@ module APIapi_v3_paths.send(path, represented.id)end++ def attachment_set+ # Depending on the way attachments are handled we have three different cases:+ # * The attachments are replaced completely (but are not yet persisted)+ # * Additional attachments will be added to the container (but are not yet persisted)+ # * We only have the already persisted attachments+ #+ # The first two cases can happen e.g., when new and coming back from backend with an error.+ if represented.attachments_replacements+ represented.attachments_replacements+ elsif represented.attachments_claimed+ represented.attachments.concat(represented.attachments_claimed)+ else+ represented.attachments+ end+ endendendenddiff --git a/spec/contracts/messages/create_contract_spec.rb b/spec/contracts/messages/create_contract_spec.rbnew file mode 100644index 00000000000..50d79d73290--- /dev/null+++ b/spec/contracts/messages/create_contract_spec.rb@@ -0,0 +1,52 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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_relative './shared_contract_examples'++describe Messages::CreateContract do+ it_behaves_like 'message contract' do+ let(:message) do+ Message.new(forum: message_forum,+ parent: message_parent,+ subject: message_subject,+ content: message_content,+ author: message_author,+ last_reply: message_last_reply,+ locked: message_locked,+ sticky: message_sticky)+ end+ let(:changed_by_system) { %w(author_id) }++ subject(:contract) do+ described_class.new(message, current_user, options: { changed_by_system: changed_by_system })+ end+ end+enddiff --git a/spec/contracts/messages/shared_contract_examples.rb b/spec/contracts/messages/shared_contract_examples.rbnew file mode 100644index 00000000000..bbde10b0868--- /dev/null+++ b/spec/contracts/messages/shared_contract_examples.rb@@ -0,0 +1,71 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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'++shared_examples_for 'message contract' do+ let(:current_user) do+ FactoryBot.build_stubbed(:user) do |user|+ allow(user)+ .to receive(:allowed_to?) do |permission, permission_project|+ permissions.include?(permission) && message_project == permission_project+ end+ end+ end+ let(:reply_message) { FactoryBot.build_stubbed(:message) }+ let(:other_user) { FactoryBot.build_stubbed(:user) }+ let(:message_forum) do+ FactoryBot.build_stubbed(:forum)+ end+ let(:message_project) { FactoryBot.build_stubbed(:project) }+ let(:message_parent) { FactoryBot.build_stubbed(:message) }+ let(:message_subject) { "Subject" }+ let(:message_content) { "A content" }+ let(:message_author) { other_user }+ let(:message_last_reply) { reply_message }+ let(:message_locked) { true }+ let(:message_sticky) { true }++ def expect_valid(valid, symbols = {})+ expect(contract.validate).to eq(valid)++ symbols.each do |key, arr|+ expect(contract.errors.symbols_for(key)).to match_array arr+ end+ end++ shared_examples 'is valid' do+ it 'is valid' do+ expect_valid(true)+ end+ end++ it_behaves_like 'is valid'+enddiff --git a/spec/contracts/messages/update_contract_spec.rb b/spec/contracts/messages/update_contract_spec.rbnew file mode 100644index 00000000000..308dcb05842--- /dev/null+++ b/spec/contracts/messages/update_contract_spec.rb@@ -0,0 +1,56 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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_relative './shared_contract_examples'++describe Messages::UpdateContract do+ it_behaves_like 'message contract' do+ let(:message) do+ FactoryBot.build_stubbed(:message).tap do |message|+ message.forum = message_forum+ message.parent = message_parent+ message.subject = message_subject+ message.content = message_content+ message.last_reply = message_last_reply+ message.locked = message_locked+ message.sticky = message_sticky+ end+ end+ subject(:contract) { described_class.new(message, current_user) }++ context 'if the author is changed' do+ it 'is invalid' do+ message.author = other_user+ expect_valid(false, author_id: %i(error_readonly))+ end+ end+ end+enddiff --git a/spec/contracts/time_entries/create_contract_spec.rb b/spec/contracts/time_entries/create_contract_spec.rbindex d090f2e61d8..7373afe672c 100644--- a/spec/contracts/time_entries/create_contract_spec.rb+++ b/spec/contracts/time_entries/create_contract_spec.rb@@ -1,4 +1,5 @@#-- encoding: UTF-8+#-- copyright# OpenProject is an open source project management software.# Copyright (C) 2012-2020 the OpenProject GmbHdiff --git a/spec/contracts/time_entries/update_contract_spec.rb b/spec/contracts/time_entries/update_contract_spec.rbindex e9b3e418aa3..6fe2343bd75 100644--- a/spec/contracts/time_entries/update_contract_spec.rb+++ b/spec/contracts/time_entries/update_contract_spec.rb@@ -1,4 +1,5 @@#-- encoding: UTF-8+#-- copyright# OpenProject is an open source project management software.# Copyright (C) 2012-2020 the OpenProject GmbHdiff --git a/spec/services/messages/create_service_spec.rb b/spec/services/messages/create_service_spec.rbnew file mode 100644index 00000000000..c520b16cd9e--- /dev/null+++ b/spec/services/messages/create_service_spec.rb@@ -0,0 +1,149 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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'++describe Messages::CreateService, type: :model do+ let(:user) { FactoryBot.build_stubbed(:user) }+ let(:contract_class) do+ double('contract_class', '<=': true)+ end+ let(:message_valid) { true }+ let(:instance) do+ described_class.new(user: user,+ contract_class: contract_class)+ end+ let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }+ let(:set_attributes_success) do+ true+ end+ let(:set_attributes_errors) do+ double('set_attributes_errors')+ end+ let(:set_attributes_result) do+ ServiceResult.new result: created_message,+ success: set_attributes_success,+ errors: set_attributes_errors+ end+ let!(:created_message) do+ message = FactoryBot.build_stubbed(:message)++ allow(Message)+ .to receive(:new)+ .and_return(message)++ allow(message)+ .to receive(:save)+ .and_return(message_valid)++ message+ end+ let!(:set_attributes_service) do+ service = double('set_attributes_service_instance')++ allow(Messages::SetAttributesService)+ .to receive(:new)+ .with(user: user,+ model: created_message,+ contract_class: contract_class)+ .and_return(service)++ allow(service)+ .to receive(:call)+ .and_return(set_attributes_result)+ end+ let(:new_project_role) { FactoryBot.build_stubbed(:role) }++ describe 'call' do+ subject { instance.call(call_attributes) }++ it 'is successful' do+ expect(subject.success?).to be_truthy+ end++ it 'returns the result of the SetAttributesService' do+ expect(subject)+ .to eql set_attributes_result+ end++ it 'persists the message' do+ expect(created_message)+ .to receive(:save)+ .and_return(message_valid)++ subject+ end++ it 'creates a message' do+ expect(subject.result)+ .to eql created_message+ end++ context 'if the SetAttributeService is unsuccessful' do+ let(:set_attributes_success) { false }++ it 'is unsuccessful' do+ expect(subject.success?).to be_falsey+ end++ it 'returns the result of the SetAttributesService' do+ expect(subject)+ .to eql set_attributes_result+ end++ it 'does not persist the changes' do+ expect(created_message)+ .to_not receive(:save)++ subject+ end++ it "exposes the contract's errors" do+ subject++ expect(subject.errors).to eql set_attributes_errors+ end+ end++ context 'when the message is invalid' do+ let(:message_valid) { false }++ it 'is unsuccessful' do+ expect(subject.success?).to be_falsey+ end++ it "exposes the message's errors" do+ subject++ expect(subject.errors).to eql created_message.errors+ end+ end+ end+enddiff --git a/spec/services/messages/set_attributes_service_spec.rb b/spec/services/messages/set_attributes_service_spec.rbnew file mode 100644index 00000000000..1e42c8c9a83--- /dev/null+++ b/spec/services/messages/set_attributes_service_spec.rb@@ -0,0 +1,141 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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'++describe Messages::SetAttributesService, type: :model do+ let(:user) { FactoryBot.build_stubbed(:user) }+ let(:forum) { FactoryBot.build_stubbed(:forum) }+ let(:contract_instance) do+ contract = double('contract_instance')+ allow(contract)+ .to receive(:validate)+ .and_return(contract_valid)+ allow(contract)+ .to receive(:errors)+ .and_return(contract_errors)+ contract+ end++ let(:contract_errors) { double('contract_errors') }+ let(:contract_valid) { true }+ let(:time_entry_valid) { true }++ let(:instance) do+ described_class.new(user: user,+ model: message_instance,+ contract_class: contract_class)+ end+ let(:message_instance) { Message.new }+ let(:contract_class) do+ allow(Messages::CreateContract)+ .to receive(:new)+ .with(message_instance, user, options: { changed_by_system: ["author_id"] })+ .and_return(contract_instance)++ Messages::CreateContract+ end++ let(:params) { {} }++ before do+ allow(message_instance)+ .to receive(:valid?)+ .and_return(time_entry_valid)+ end++ subject { instance.call(params) }++ it 'returns the message instance as the result' do+ expect(subject.result)+ .to eql message_instance+ end++ it 'is a success' do+ is_expected+ .to be_success+ end++ it "has the service's user assigned as author" do+ subject++ expect(message_instance.author)+ .to eql user+ end++ it 'notes the author to be system changed' do+ subject++ expect(instance.changed_by_system)+ .to include('author_id')+ end++ context 'with params' do+ let(:params) do+ {+ forum: forum+ }+ end++ let(:expected) do+ {+ author_id: user.id,+ forum_id: forum.id+ }.with_indifferent_access+ end++ it 'assigns the params' do+ subject++ attributes_of_interest = message_instance+ .attributes+ .slice(*expected.keys)++ expect(attributes_of_interest)+ .to eql(expected)+ end+ end++ context 'with an invalid contract' do+ let(:contract_valid) { false }+ let(:expect_time_instance_save) do+ expect(message_instance)+ .not_to receive(:save)+ end++ it 'returns failure' do+ is_expected+ .not_to be_success+ end++ it "returns the contract's errors" do+ expect(subject.errors)+ .to eql(contract_errors)+ end+ end+enddiff --git a/spec/services/messages/update_service_spec.rb b/spec/services/messages/update_service_spec.rbnew file mode 100644index 00000000000..d3c9128a232--- /dev/null+++ b/spec/services/messages/update_service_spec.rb@@ -0,0 +1,150 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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'++describe Messages::UpdateService, type: :model do+ let(:user) { FactoryBot.build_stubbed(:user) }+ let(:contract_class) do+ double('contract_class', "<=": true)+ end+ let(:message_valid) { true }+ let(:instance) do+ described_class.new(user: user,+ model: message,+ contract_class: contract_class)+ end+ let(:call_attributes) { {} }+ let(:set_attributes_success) do+ true+ end+ let(:set_attributes_errors) do+ double('set_attributes_errors')+ end+ let(:set_attributes_result) do+ ServiceResult.new result: message,+ success: set_attributes_success,+ errors: set_attributes_errors+ end+ let!(:message) do+ message = FactoryBot.build_stubbed(:message)++ allow(message)+ .to receive(:save)+ .and_return(message_valid)++ message+ end+ let!(:set_attributes_service) do+ service = double('set_attributes_service_instance')++ allow(Messages::SetAttributesService)+ .to receive(:new)+ .with(user: user,+ model: message,+ contract_class: contract_class)+ .and_return(service)++ allow(service)+ .to receive(:call)+ .and_return(set_attributes_result)++ service+ end++ describe 'call' do+ shared_examples_for 'service call' do+ subject { instance.call(call_attributes) }++ it 'is successful' do+ expect(subject.success?).to be_truthy+ end++ it 'returns the result of the SetAttributesService' do+ expect(subject)+ .to eql set_attributes_result+ end++ it 'persists the message' do+ expect(message)+ .to receive(:save)+ .and_return(message_valid)++ subject+ end++ context 'when the SetAttributeService is unsuccessful' do+ let(:set_attributes_success) { false }++ it 'is unsuccessful' do+ expect(subject.success?).to be_falsey+ end++ it 'returns the result of the SetAttributesService' do+ expect(subject)+ .to eql set_attributes_result+ end++ it 'does not persist the changes' do+ expect(message)+ .to_not receive(:save)++ subject+ end++ it "exposes the contract's errors" do+ subject++ expect(subject.errors).to eql set_attributes_errors+ end+ end++ context 'when the message is invalid' do+ let(:message_valid) { false }++ it 'is unsuccessful' do+ expect(subject.success?).to be_falsey+ end++ it "exposes the message's errors" do+ subject++ expect(subject.errors).to eql message.errors+ end+ end+ end++ context 'with parameters' do+ let(:call_attributes) { { sticky: true } }++ it_behaves_like 'service call'+ end+ end+end