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

Validate user and allow passing multiple

Oliver Güntherc9887bd6a77a
diff --git a/modules/meeting/app/contracts/meeting_participants/create_contract.rb b/modules/meeting/app/contracts/meeting_participants/create_contract.rb
new file mode 100644
index 00000000000..6a0f10dc70a
--- /dev/null
+++ b/modules/meeting/app/contracts/meeting_participants/create_contract.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+module MeetingParticipants
+ class CreateContract < ::ModelContract
+ attribute :meeting
+ attribute :user_id
+ attribute :invited
+ attribute :attended
+
+ validate :user_can_see_meetings_in_project
+ validate :user_allowed_to_edit
+
+ private
+
+ def user_allowed_to_edit
+ return if model.meeting.nil?
+
+ unless user.allowed_in_project?(:edit_meetings, model.meeting.project)
+ errors.add(:base, :error_unauthorized)
+ end
+ end
+
+ def user_can_see_meetings_in_project
+ return if model.user.nil? || model.meeting.nil?
+
+ unless model.user.allowed_in_project?(:view_meetings, model.meeting.project)
+ errors.add(:user, :invalid_user, name: model.name)
+ end
+ end
+ end
+end
diff --git a/modules/meeting/app/controllers/meeting_participants_controller.rb b/modules/meeting/app/controllers/meeting_participants_controller.rb
index b22b729365c..37e0ab34f63 100644
--- a/modules/meeting/app/controllers/meeting_participants_controller.rb
+++ b/modules/meeting/app/controllers/meeting_participants_controller.rb
@@ -38,19 +38,15 @@ class MeetingParticipantsController < ApplicationController
before_action :set_participant, only: %i[toggle_attendance destroy]
def create
- user_id = params[:meeting_participant][:user_id]
+ user_ids = Array(params.dig(:meeting_participant, :user_id)).compact_blank
- if MeetingParticipant.exists?(user_id: user_id, meeting_id: @meeting.id) || user_id.blank?
+ if user_ids.empty?
update_add_user_form_component_via_turbo_stream
update_list_component_via_turbo_stream
-
- respond_with_turbo_streams
- return
+ else
+ create_new_participants(user_ids)
end
- create_new_participant(user_id)
-
- update_list_component_via_turbo_stream
respond_with_turbo_streams
end
@@ -100,27 +96,20 @@ class MeetingParticipantsController < ApplicationController
@participant = MeetingParticipant.find(params[:id])
end
- def send_notification(user)
- if Journal::NotificationConfiguration.active? && [email protected]? && @meeting.notify?
- MeetingMailer.invited(@meeting, user, User.current).deliver_later
- end
+ def create_new_participants(user_ids)
+ user_ids.each { |user_id| create_new_participant(user_id) }
+
+ update_list_component_via_turbo_stream
+ update_add_user_form_component_via_turbo_stream
+ update_sidebar_participants_component_via_turbo_stream(meeting: @meeting)
end
def create_new_participant(user_id)
- participant = MeetingParticipant.create(
- meeting: @meeting,
- user_id:,
- invited: true,
- attended: false
- )
-
- if participant.persisted?
- send_notification(User.find(user_id))
-
- update_add_user_form_component_via_turbo_stream
- update_sidebar_participants_component_via_turbo_stream(meeting: @meeting)
- else
- participant.errors.full_messages.each do |msg|
+ MeetingParticipants::CreateService
+ .new(user: User.current)
+ .call(meeting: @meeting, user_id: user_id, invited: true, attended: false)
+ .on_failure do |call|
+ call.errors.full_messages.each do |msg|
@meeting.errors.add(:base, msg)
end
end
diff --git a/modules/meeting/app/forms/meeting/participant.rb b/modules/meeting/app/forms/meeting/participant.rb
index f61813a004b..1df3781f408 100644
--- a/modules/meeting/app/forms/meeting/participant.rb
+++ b/modules/meeting/app/forms/meeting/participant.rb
@@ -42,7 +42,9 @@ class Meeting::Participant < ApplicationForm
searchKey: "any_name_attribute",
resource: "principals",
focusDirectly: true,
- multiple: false,
+ multiple: true,
+ closeOnSelect: false,
+ hideSelected: true,
isOpenedInModal: true,
hoverCards: true,
placeholder: I18n.t("meeting.participants.text.search_for_members"),
diff --git a/modules/meeting/app/models/meeting_participant.rb b/modules/meeting/app/models/meeting_participant.rb
index 7cfac0b3ce0..f322dd38582 100644
--- a/modules/meeting/app/models/meeting_participant.rb
+++ b/modules/meeting/app/models/meeting_participant.rb
@@ -32,6 +32,8 @@ class MeetingParticipant < ApplicationRecord
belongs_to :meeting
belongs_to :user
+ validates :user, :meeting, presence: true
+
scope :invited, -> { where(invited: true) }
scope :attended, -> { where(attended: true) }
diff --git a/modules/meeting/app/services/meeting_participants/create_service.rb b/modules/meeting/app/services/meeting_participants/create_service.rb
new file mode 100644
index 00000000000..53d939bd75a
--- /dev/null
+++ b/modules/meeting/app/services/meeting_participants/create_service.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+module MeetingParticipants
+ class CreateService < BaseServices::Create
+ protected
+
+ def after_perform(call)
+ send_notification call.result
+
+ call
+ end
+
+ def send_notification(meeting_participant)
+ meeting = meeting_participant.meeting
+
+ if Journal::NotificationConfiguration.active? && !meeting.templated? && meeting.notify?
+ MeetingMailer.invited(meeting, meeting_participant.user, user).deliver_later
+ end
+ end
+ end
+end
diff --git a/modules/meeting/app/services/meeting_participants/set_attributes_service.rb b/modules/meeting/app/services/meeting_participants/set_attributes_service.rb
new file mode 100644
index 00000000000..b967aa653f5
--- /dev/null
+++ b/modules/meeting/app/services/meeting_participants/set_attributes_service.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+module MeetingParticipants
+ class SetAttributesService < BaseServices::SetAttributes
+ end
+end
diff --git a/modules/meeting/app/services/meetings/create_service.rb b/modules/meeting/app/services/meetings/create_service.rb
index de778195777..d260ebc8f19 100644
--- a/modules/meeting/app/services/meetings/create_service.rb
+++ b/modules/meeting/app/services/meetings/create_service.rb
@@ -36,7 +36,6 @@ module Meetings
meeting = call.result
if call.success? && Journal::NotificationConfiguration.active? && meeting.notify?
-
meeting.participants.where(invited: true).each do |participant|
MeetingMailer
.invited(meeting, participant.user, User.current)
diff --git a/modules/meeting/config/locales/en.yml b/modules/meeting/config/locales/en.yml
index ffccccd2211..d621d4fdf73 100644
--- a/modules/meeting/config/locales/en.yml
+++ b/modules/meeting/config/locales/en.yml
@@ -70,8 +70,13 @@ en:
end_after: "Meeting series ends"
end_date: "End date"
iterations: "Occurrences"
+ meeting_participant:
+ invited: "Invited"
+ attended: "Attended"
errors:
models:
+ meeting_participant:
+ invalid_user: "%{name} is not a valid participant."
recurring_meeting:
must_cover_existing_meetings:
one: "There is one open meeting in the series that is not covered by the new schedule. Adjust the schedule to include all existing meetings."
diff --git a/modules/meeting/spec/contracts/meeting_participants/create_contract_spec.rb b/modules/meeting/spec/contracts/meeting_participants/create_contract_spec.rb
new file mode 100644
index 00000000000..b86b3c69015
--- /dev/null
+++ b/modules/meeting/spec/contracts/meeting_participants/create_contract_spec.rb
@@ -0,0 +1,86 @@
+# frozen_string_literal: true
+
+#-- 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 MeetingParticipants::CreateContract do
+ shared_let(:project) { create(:project, enabled_module_names: %i[meetings]) }
+ shared_let(:meeting) { create(:meeting, project:) }
+ shared_let(:user_with_meeting_permissions) do
+ create(:user,
+ member_with_permissions: { project => %i[view_meetings edit_meetings] })
+ end
+ shared_let(:user_without_meeting_permissions) { create(:user, member_with_permissions: { project => %i[view_project] }) }
+ shared_let(:user_not_in_project) { create(:user) }
+
+ let(:participant) { build(:meeting_participant, meeting:, user:) }
+ let(:contract) { described_class.new(participant, user) }
+
+ describe "validation" do
+ subject { contract.validate }
+
+ context "when all attributes are valid" do
+ let(:user) { user_with_meeting_permissions }
+
+ it "is valid" do
+ expect(subject).to be true
+ expect(contract.errors).to be_empty
+ end
+ end
+
+ context "when meeting is missing" do
+ let(:user) { user_with_meeting_permissions }
+ let(:participant) { build(:meeting_participant, meeting: nil, user:) }
+
+ it "is invalid" do
+ expect(subject).to be false
+ expect(contract.errors[:meeting]).to include("can't be blank.")
+ end
+ end
+
+ context "when user does not have meeting permissions" do
+ let(:user) { user_without_meeting_permissions }
+
+ it "is invalid" do
+ expect(subject).to be false
+ expect(contract.errors[:user]).to include("#{user_without_meeting_permissions.name} is not a valid participant.")
+ end
+ end
+
+ context "when user is not in project" do
+ let(:user) { user_not_in_project }
+
+ it "is invalid" do
+ expect(subject).to be false
+ expect(contract.errors[:user]).to include("#{user_not_in_project.name} is not a valid participant.")
+ end
+ end
+ end
+end
diff --git a/modules/meeting/spec/requests/meeting_participants_spec.rb b/modules/meeting/spec/requests/meeting_participants_spec.rb
new file mode 100644
index 00000000000..904c18a368d
--- /dev/null
+++ b/modules/meeting/spec/requests/meeting_participants_spec.rb
@@ -0,0 +1,256 @@
+# frozen_string_literal: true
+
+#-- 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 "MeetingParticipants requests",
+ :skip_csrf,
+ type: :rails_request do
+ shared_let(:project) { create(:project, enabled_module_names: %i[meetings]) }
+ shared_let(:user) { create(:user, member_with_permissions: { project => %i[view_meetings create_meetings edit_meetings] }) }
+ shared_let(:meeting) { create(:meeting, project:, author: user) }
+
+ # Users with different permission levels
+ shared_let(:user_with_meeting_permissions) { create(:user, member_with_permissions: { project => %i[view_meetings] }) }
+ shared_let(:user_with_meeting_permissions2) { create(:user, member_with_permissions: { project => %i[view_meetings] }) }
+ shared_let(:user_without_meeting_permissions) { create(:user, member_with_permissions: { project => %i[view_project] }) }
+ shared_let(:user_not_in_project) { create(:user) }
+
+ before do
+ meeting.participants.delete_all
+ login_as user
+ end
+
+ describe "POST /meetings/:meeting_id/participants" do
+ let(:base_params) do
+ {
+ meeting_id: meeting.id,
+ meeting_participant: {
+ user_id: []
+ }
+ }
+ end
+
+ context "when inviting a single participant" do
+ let(:params) { base_params.deep_merge(meeting_participant: { user_id: [user_with_meeting_permissions.id] }) }
+
+ it "creates a single participant" do
+ expect do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+ end.to change { meeting.participants.count }.by(1)
+
+ expect(response).to have_http_status(:ok)
+
+ participant = meeting.participants.reload.last
+ expect(participant.user).to eq(user_with_meeting_permissions)
+ expect(participant.invited).to be true
+ expect(participant.attended).to be false
+ end
+
+ it "sends notification email" do
+ expect do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+ perform_enqueued_jobs
+ end.to change { ActionMailer::Base.deliveries.size }.by(1)
+ end
+ end
+
+ context "when inviting multiple participants" do
+ let(:params) do
+ base_params.deep_merge(
+ meeting_participant: {
+ user_id: [user_with_meeting_permissions.id, user_with_meeting_permissions.id]
+ }
+ )
+ end
+
+ it "creates multiple participants" do
+ expect do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+ end.to change { meeting.participants.count }.by(2)
+
+ expect(response).to have_http_status(:ok)
+
+ participants = meeting.participants.reload.last(2)
+ expect(participants.map(&:user)).to all(eq(user_with_meeting_permissions))
+ expect(participants.map(&:attended)).to all(be false)
+ end
+ end
+
+ context "when inviting participants with different permission levels" do
+ let(:params) do
+ base_params.deep_merge(
+ meeting_participant: {
+ user_id: [user_with_meeting_permissions.id, user_without_meeting_permissions.id]
+ }
+ )
+ end
+
+ it "creates participants for users with meeting permissions" do
+ expect do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+ end.to change { meeting.participants.count }.by(1)
+
+ expect(response).to have_http_status(:ok)
+
+ participant = meeting.participants.reload.last
+ expect(participant.user).to eq(user_with_meeting_permissions)
+ end
+
+ it "adds errors for users without meeting permissions" do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to include "User #{user_without_meeting_permissions.name} is not a valid participant."
+
+ meeting.participants.reload
+ expect(meeting.participants.count).to eq(1)
+ expect(meeting.participants.first.user).to eq(user_with_meeting_permissions)
+ end
+ end
+
+ context "when inviting users not in the project" do
+ let(:params) do
+ base_params.deep_merge(
+ meeting_participant: {
+ user_id: [user_not_in_project.id]
+ }
+ )
+ end
+
+ it "does not create participants for users not in project" do
+ expect do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+ end.not_to change { meeting.participants.count }
+
+ expect(response).to have_http_status(:ok)
+ end
+
+ it "adds appropriate errors" do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+
+ expect(response.body).to include "User #{user_not_in_project.name} is not a valid participant."
+
+ meeting.participants.reload
+ expect(meeting.participants.count).to eq(0)
+ end
+ end
+
+ context "when providing empty user_ids" do
+ let(:params) { base_params }
+
+ it "does not create any participants" do
+ expect do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+ end.not_to change { meeting.participants.count }
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
+ context "when providing nil user_ids" do
+ let(:params) { base_params.deep_merge(meeting_participant: { user_id: nil }) }
+
+ it "handles nil gracefully" do
+ expect do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+ end.not_to change { meeting.participants.count }
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
+ context "when providing invalid user_ids" do
+ let(:params) do
+ base_params.deep_merge(
+ meeting_participant: {
+ user_id: [999999, user_with_meeting_permissions.id]
+ }
+ )
+ end
+
+ it "creates participants for valid users only" do
+ expect do
+ post meeting_participants_path(meeting), params: params, as: :turbo_stream
+ end.to change { meeting.participants.count }.by(1)
+
+ expect(response).to have_http_status(:ok)
+
+ meeting.participants.reload
+ expect(meeting.participants.count).to eq(1)
+ expect(meeting.participants.first.user).to eq(user_with_meeting_permissions)
+ end
+ end
+ end
+
+ describe "POST /meetings/:meeting_id/participants/mark_all_attended" do
+ let!(:participant1) { create(:meeting_participant, meeting:, user: user_with_meeting_permissions, attended: false) }
+ let!(:participant2) { create(:meeting_participant, meeting:, user: user_with_meeting_permissions2, attended: false) }
+
+ it "marks all participants as attended" do
+ post mark_all_attended_meeting_participants_path(meeting), as: :turbo_stream
+
+ expect(response).to have_http_status(:ok)
+ expect(participant1.reload.attended).to be true
+ expect(participant2.reload.attended).to be true
+ end
+ end
+
+ describe "POST /meetings/:meeting_id/participants/:id/toggle_attendance" do
+ let!(:participant) { create(:meeting_participant, meeting:, user: user_with_meeting_permissions, attended: false) }
+
+ it "toggles attendance status" do
+ expect do
+ post toggle_attendance_meeting_participant_path(meeting, participant), as: :turbo_stream
+ end.to change { participant.reload.attended }.from(false).to(true)
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
+ describe "DELETE /meetings/:meeting_id/participants/:id" do
+ let!(:participant) { create(:meeting_participant, meeting:, user: user_with_meeting_permissions) }
+
+ it "removes the participant" do
+ expect do
+ delete meeting_participant_path(meeting, participant), as: :turbo_stream
+ end.to change { meeting.participants.count }.by(-1)
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
+ describe "GET /meetings/:meeting_id/participants/manage_participants_dialog" do
+ it "responds with the manage participants dialog" do
+ get manage_participants_dialog_meeting_participants_path(meeting), as: :turbo_stream
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
+end
diff --git a/modules/meeting/spec/services/meeting_participants/create_service_spec.rb b/modules/meeting/spec/services/meeting_participants/create_service_spec.rb
new file mode 100644
index 00000000000..565adc65d12
--- /dev/null
+++ b/modules/meeting/spec/services/meeting_participants/create_service_spec.rb
@@ -0,0 +1,134 @@
+# frozen_string_literal: true
+
+#-- 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 MeetingParticipants::CreateService do
+ shared_let(:project) { create(:project, enabled_module_names: %i[meetings]) }
+ shared_let(:meeting) { create(:meeting, project:) }
+ shared_let(:current_user) { create(:user, member_with_permissions: { project => %i[view_meetings edit_meetings] }) }
+ shared_let(:user_with_meeting_permissions) { create(:user, member_with_permissions: { project => %i[view_meetings] }) }
+ shared_let(:user_without_meeting_permissions) { create(:user, member_with_permissions: { project => %i[view_project] }) }
+ shared_let(:user_not_in_project) { create(:user) }
+
+ describe "#call" do
+ subject { described_class.new(user: current_user).call(meeting:, user_id:, invited: true, attended: false) }
+
+ context "when user has meeting permissions" do
+ let(:user_id) { user_with_meeting_permissions.id }
+
+ it "creates a participant successfully" do
+ expect { subject }.to change { meeting.participants.count }.by(1)
+
+ expect(subject).to be_success
+ expect(subject.result).to be_present
+ expect(subject.result.user).to eq(user_with_meeting_permissions)
+ expect(subject.result.invited).to be true
+ expect(subject.result.attended).to be false
+ end
+ end
+
+ context "when user does not have meeting permissions" do
+ let(:user_id) { user_without_meeting_permissions.id }
+
+ it "fails to create participant" do
+ expect { subject }.not_to change { meeting.participants.count }
+
+ expect(subject).to be_failure
+ expect(subject.result).to be_new_record
+ expect(subject.errors.full_messages).to include(/is not a valid participant/)
+ end
+ end
+
+ context "when user is not in project" do
+ let(:user_id) { user_not_in_project.id }
+
+ it "fails to create participant" do
+ expect { subject }.not_to change { meeting.participants.count }
+
+ expect(subject).to be_failure
+ expect(subject.result).to be_new_record
+ expect(subject.errors.full_messages).to include(/is not a valid participant/)
+ end
+ end
+
+ context "when user_id is blank" do
+ let(:user_id) { "" }
+
+ it "fails to create participant" do
+ expect { subject }.not_to change { meeting.participants.count }
+
+ expect(subject).to be_failure
+ expect(subject.result).to be_new_record
+ expect(subject.errors.full_messages).to include("User can't be blank.")
+ end
+ end
+
+ context "when user_id is nil" do
+ let(:user_id) { nil }
+
+ it "fails to create participant" do
+ expect { subject }.not_to change { meeting.participants.count }
+
+ expect(subject).to be_failure
+ expect(subject.result).to be_new_record
+ expect(subject.errors.full_messages).to include("User can't be blank.")
+ end
+ end
+
+ context "when user_id is invalid" do
+ let(:user_id) { 999999 }
+
+ it "fails to create participant" do
+ expect { subject }.not_to change { meeting.participants.count }
+
+ expect(subject).to be_failure
+ expect(subject.result).to be_new_record
+ expect(subject.errors.full_messages).to include("User can't be blank.")
+ end
+ end
+
+ context "when creating with custom attributes" do
+ let(:user_id) { user_with_meeting_permissions.id }
+ let(:invited) { false }
+ let(:attended) { true }
+
+ subject { described_class.new(user: current_user).call(meeting:, user_id:, invited:, attended:) }
+
+ it "creates participant with custom attributes" do
+ expect { subject }.to change { meeting.participants.count }.by(1)
+
+ expect(subject).to be_success
+ expect(subject.result.invited).to be false
+ expect(subject.result.attended).to be true
+ end
+ end
+ end
+end