OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/spec/services/notifications/journal_wp_mail_service_spe...

440 lines
14 KiB

#-- 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'
describe Notifications::JournalWpMailService do
let(:project) { FactoryBot.create(:project_with_types) }
let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }
let(:author) do
User custom fields in the invite user modal (#9220) * Projects form working with formly 50% * Removed console.log * Working with formattable * Working with formattable * Input with id and label * Input with id and label * Useless dependencies removed * Saving forms + required labels with * * First backend validation approach * Removed reload on type change + keep model on route changes * Handlig backend validations with setError * Formatting the form model to submit * Make up refactor * working with op-form-field * Form creation moved to the service * Working with op-form-field wrapper * Working with validation and op-form-field * Working with []CustomFields * Clean up * Clean up * Clean up * Clean up * Form routing working * Notification on form error and success * Refactor + removed useless dynamic form observable * DynamicFieldsService with tests * Refactor: inputs catalog + catch form load error * Filter out non writable fields * Refactor: naming consistency * Cleaning comments * dynamic-fields-service tests + wrapper component * DynamicForm Tests * @ngx-formly/core dependency added * Cleaning up * Provide DynamicFieldsService in root so it can be used independently * DynamicForm working as a FormControl * Getting route params sync * Global FormsService: submit + formatting + error handling * Fix: @Optional() FormGroupDirective in OpFormFieldComponent * Code climate fix * Removed CdkTextareaAutosize because of CDK issue 22469 * DynamicFormComponent tests * Dynamic input test helpers + boolean and text tests * Refactor edit fields to avoid circular dependencies in the dynamic forms * Naming fix * IntegerInputComponent tests * SelectInputComponent tests * Fix: duplicated identifier on inputs * Extract toolbar to be reused for now Still TBD whether we want to move them right now to the frontend? * Create new project route and redirect to rails view after saving * fieldsSettingsPipe + hide 'identifier' on projects * Handling multi-values (also as links) and passwords * Some TODOs removed * FormattableTextareaInputComponent tests * Projects form working with formly 50% * Removed console.log * Working with formattable * Working with formattable * Input with id and label * Input with id and label * Useless dependencies removed * Saving forms + required labels with * * First backend validation approach * Removed reload on type change + keep model on route changes * Handlig backend validations with setError * Formatting the form model to submit * Make up refactor * working with op-form-field * Form creation moved to the service * Working with op-form-field wrapper * Working with validation and op-form-field * Working with []CustomFields * Clean up * Clean up * Clean up * Clean up * Form routing working * Notification on form error and success * Refactor + removed useless dynamic form observable * DynamicFieldsService with tests * Refactor: inputs catalog + catch form load error * Filter out non writable fields * Refactor: naming consistency * Cleaning comments * dynamic-fields-service tests + wrapper component * DynamicForm Tests * @ngx-formly/core dependency added * Cleaning up * DynamicForm working as a FormControl * Getting route params sync * Global FormsService: submit + formatting + error handling * Fix: @Optional() FormGroupDirective in OpFormFieldComponent * Code climate fix * Removed CdkTextareaAutosize because of CDK issue 22469 * DynamicFormComponent tests * Dynamic input test helpers + boolean and text tests * Refactor edit fields to avoid circular dependencies in the dynamic forms * Naming fix * IntegerInputComponent tests * SelectInputComponent tests * Fix: duplicated identifier on inputs * Extract toolbar to be reused for now Still TBD whether we want to move them right now to the frontend? * Create new project route and redirect to rails view after saving * fieldsSettingsPipe + hide 'identifier' on projects * Handling multi-values (also as links) and passwords * Some TODOs removed * FormattableTextareaInputComponent tests * _isResourceSchema based on parent?.location * Scope DynamicFieldsService to DynamicFormComponent * Added backend validation method to FormsService * Removed projects routes and ruby template * Removed projects routes and dynamic forms from Projects * Revert "Provide DynamicFieldsService in root so it can be used independently" This reverts commit ab56f3c56f6c8c3c3b791c081c762aa713e83a41. * Provide DynamicFieldsService in root so it can be used independently * TODO: test ProjectsComponent * Code climate fixes (remove TODOs) * Default OpFormFieldComponent.inlineLabel to false * Dynamic components tests xkipped * Typing improvements * DynamicFormComponent working as a FormControl * Global FormsService: submit + formatting + error handling * Fix: @Optional() FormGroupDirective in OpFormFieldComponent * Code climate fixes * noWrapLabel default to false * Started adding user custom fields to the ium * Import the dynamic-forms module into the common module * Refactor edit fields to avoid circular dependencies in the dynamic forms * Using DynamicFormsModule in OpenprojectInviteUserModalModule * Add formly form * Update principal name filter * Dynamic form field is rendering * Handling multi-values (also as links) and passwords * Added backend validation method to FormsService * Remove form from DynamicForm when not isStandaloneForm * Allow multiple form keys to validate * Remove form from non standalone forms * Remove duplicated button * Moved to FormGroup input for dynamic form * Custom field happy path is done * Add explanatory comment to payload structure transformation * add op-form class to ium steps * Add shrinkwrap back in * Fix test, fix dynamic form resource path * gimme a shirnkwrap * Remove failing tests * Remove another failing test * Remove more failing specs * Fix double loading of principals * Add custom field spec * Fix spec * Reset shrinkwrap * Forbid Factory.build(:user, member_in_project) If you use the trait member_in_project(s), the user is implicitly saved to create the member. This is very confusing if trying to use required custom fields, as this will fail with the Member#user_id foreign key being nil, as the user cannot be saved. Instead, raise an error when trying to use this factory trait * Change additional spec factory Co-authored-by: Aleix Suau <info@macrofonoestudio.es> Co-authored-by: Oliver Günther <mail@oliverguenther.de>
4 years ago
FactoryBot.create(:user,
mail_notification: 'none',
member_in_project: project,
member_through_role: role)
end
let(:recipient) do
FactoryBot.create(:user,
member_in_project: project,
member_through_role: role,
login: "johndoe")
end
let(:work_package) do
FactoryBot.create(:work_package,
project: project,
author: author,
assigned_to: recipient,
type: project.types.first)
end
let(:journal) { journal_1 }
let(:journal_1) { work_package.journals.first }
let(:journal_2_with_notes) do
work_package.add_journal author, 'something I have to say'
work_package.save(validate: false)
work_package.journals.last
end
let(:journal_2_with_status) do
work_package.status = FactoryBot.create(:status)
work_package.save(validate: false)
work_package.journals.last
end
let(:journal_2_with_priority) do
work_package.priority = FactoryBot.create(:priority)
work_package.save(validate: false)
work_package.journals.last
end
let(:send_mails) { true }
let(:notification_setting) do
%w(work_package_added work_package_updated work_package_note_added status_updated work_package_priority_updated)
end
def call
described_class.call(journal, send_mails)
end
before do
# make sure no other calls are made due to WP creation/update
allow(OpenProject::Notifications).to receive(:send) # ... and do nothing
login_as(author)
allow(Setting).to receive(:notified_events).and_return(notification_setting)
end
shared_examples_for 'sends mail' do
let(:sender) { author }
it 'sends a mail' do
expect { call }
.to enqueue_job(Mails::WorkPackageJob)
.with(journal.id, recipient.id, sender.id)
end
end
shared_examples_for 'sends no mail' do
it 'sends no mail' do
expect { call }.to_not enqueue_job(Mails::WorkPackageJob)
call
end
end
it_behaves_like 'sends mail'
context 'assignee is placeholder user' do
let(:recipient) { FactoryBot.create :placeholder_user }
it_behaves_like 'sends no mail'
end
context 'responsible is placeholder user' do
let(:recipient) { FactoryBot.create :placeholder_user }
let(:work_package) do
FactoryBot.create(:work_package,
project: project,
author: author,
responsible: recipient,
type: project.types.first)
end
it_behaves_like 'sends no mail'
end
context 'notification for work_package_added disabled' do
let(:notification_setting) { %w(work_package_updated work_package_note_added) }
it_behaves_like 'sends no mail'
end
context 'if the journal has a note' do
let(:journal) { journal_2_with_notes }
context 'notification for work_package_updated and work_package_note_added disabled' do
let(:notification_setting) { %w(work_package_added status_updated work_package_priority_updated) }
it_behaves_like 'sends no mail'
end
context 'notification for work_package_updated enabled' do
let(:notification_setting) { %w(work_package_updated) }
it_behaves_like 'sends mail'
end
context 'notification for work_package_note_added enabled' do
let(:notification_setting) { %w(work_package_note_added) }
it_behaves_like 'sends mail'
end
end
context 'if the journal has status update' do
let(:journal) { journal_2_with_status }
context 'notification for work_package_updated and status_updated disabled' do
let(:notification_setting) { %w(work_package_added work_package_note_added work_package_priority_updated) }
it_behaves_like 'sends no mail'
end
context 'notification for work_package_updated enabled' do
let(:notification_setting) { %w(work_package_updated) }
it_behaves_like 'sends mail'
end
context 'notification for status_updated enabled' do
let(:notification_setting) { %w(status_updated) }
it_behaves_like 'sends mail'
end
end
context 'if the journal has priority' do
let(:journal) { journal_2_with_priority }
context 'notification for work_package_updated and work_package_priority_updated disabled' do
let(:notification_setting) { %w(work_package_added work_package_note_added status_updated) }
it_behaves_like 'sends no mail'
end
context 'notification for work_package_updated enabled' do
let(:notification_setting) { %w(work_package_updated) }
it_behaves_like 'sends mail'
end
context 'notification for work_package_priority_updated enabled' do
let(:notification_setting) { %w(work_package_priority_updated) }
it_behaves_like 'sends mail'
end
end
context 'if the author has been deleted' do
let!(:deleted_user) { DeletedUser.first }
before do
work_package
author.destroy
end
it_behaves_like 'sends mail' do
let(:sender) { deleted_user }
end
end
context 'user is mentioned' do
let(:work_package) do
FactoryBot.create(:work_package,
project: project,
author: author,
type: project.types.first)
end
let(:recipient) do
FactoryBot.create(:user,
mail_notification: 'only_assigned',
member_in_project: project,
member_through_role: role,
login: "johndoe")
end
shared_examples_for 'group mention' do
context 'group member is allowed to view the work package' do
context 'user wants to receive notifications' do
it_behaves_like 'sends mail'
end
context 'user disabled notifications' do
let(:recipient) { FactoryBot.create(:user, mail_notification: User::USER_MAIL_OPTION_NON.first) }
it_behaves_like 'sends no mail'
end
end
context 'group is not allowed to view the work package' do
let(:role) { FactoryBot.create(:role, permissions: []) }
it_behaves_like 'sends no mail'
context 'but group member is allowed individually' do
let(:recipient) do
FactoryBot.create(:user,
mail_notification: 'only_assigned',
member_in_project: project,
member_with_permissions: [:view_work_packages])
end
it_behaves_like 'sends mail'
end
end
end
shared_examples_for 'mentioned' do
context 'for users' do
context "mentioned is allowed to view the work package" do
context "The added text contains a login name" do
let(:note) { "Hello user:\"#{recipient.login}\"" }
context "that is pretty normal word" do
it_behaves_like 'sends mail'
end
context "that is an email address" do
let(:recipient) do
FactoryBot.create(:user,
mail_notification: 'only_assigned',
member_in_project: project,
member_through_role: role,
login: "foo@bar.com")
end
it_behaves_like 'sends mail'
end
end
context "The added text contains a user ID" do
let(:note) { "Hello user##{recipient.id}" }
it_behaves_like 'sends mail'
end
context "The added text contains a user mention tag in one way" do
let(:note) do
<<~NOTE
Hello <mention class="mention" data-id="#{recipient.id}" data-type="user" data-text="@#{recipient.name}">@#{recipient.name}</mention>
NOTE
end
it_behaves_like 'sends mail'
end
context "The added text contains a user mention tag in the other way" do
let(:note) do
<<~NOTE
Hello <mention class="mention" data-type="user" data-id="#{recipient.id}" data-text="@#{recipient.name}">@#{recipient.name}</mention>
NOTE
end
it_behaves_like 'sends mail'
end
context "the recipient turned off all mail notifications" do
let(:recipient) do
FactoryBot.create(:user,
member_in_project: project,
member_through_role: role,
mail_notification: 'none')
end
let(:note) do
"Hello user:\"#{recipient.login}\", hey user##{recipient.id}"
end
it_behaves_like 'sends no mail'
end
end
context "mentioned user is not allowed to view the work package" do
let(:recipient) do
FactoryBot.create(:user,
mail_notification: 'only_assigned',
login: "foo@bar.com")
end
let(:note) do
"Hello user:#{recipient.login}, hey user##{recipient.id}"
end
it_behaves_like 'sends no mail'
end
end
# TODO: test for group mention.
# Probably need to distinguish between user and group in mention tag
context 'for groups' do
let(:recipient) do
FactoryBot.create(:user,
mail_notification: 'only_assigned')
end
let(:group) do
FactoryBot.create(:group, members: recipient) do |group|
Members::CreateService
.new(user: nil, contract_class: EmptyContract)
.call(project: project, principal: group, roles: [role])
end
end
context 'on a hash/id based mention' do
let(:note) do
"Hello group##{group.id}"
end
it_behaves_like 'group mention'
end
context 'on a tag based mention with the type after' do
let(:note) do
<<~NOTE
Hello <mention class="mention" data-id="#{group.id}" data-type="group" data-text="@#{group.name}">@#{group.name}</mention>
NOTE
end
it_behaves_like 'group mention'
end
context 'on a tag based mention with the type before' do
let(:note) do
<<~NOTE
Hello <mention data-type="group" class="mention" data-id="#{group.id}" data-text="@#{group.name}">@#{group.name}</mention>
NOTE
end
it_behaves_like 'group mention'
end
end
end
context 'in the journal notes' do
let(:journal) { journal_2_with_notes }
let(:journal_2_with_notes) do
work_package.add_journal author, note
work_package.save(validate: false)
work_package.journals.last
end
it_behaves_like 'mentioned'
end
context 'in the description' do
let(:journal) { journal_2_with_description }
let(:journal_2_with_description) do
work_package.description = note
work_package.save(validate: false)
work_package.journals.last
end
it_behaves_like 'mentioned'
end
context 'in the subject' do
let(:journal) { journal_2_with_subject }
let(:journal_2_with_subject) do
work_package.subject = note
work_package.save(validate: false)
work_package.journals.last
end
it_behaves_like 'mentioned'
end
end
context 'aggregated journal is empty' do
let(:journal) { journal_2_empty_change }
let(:journal_2_empty_change) do
work_package.add_journal(author, 'temp')
work_package.save(validate: false)
work_package.journals.last.tap do |j|
j.update_column(:notes, nil)
end
end
it_behaves_like 'sends no mail'
end
end
describe 'initialization' do
it 'subscribes the listener' do
expect(Notifications::JournalWpMailService).to receive(:call)
OpenProject::Notifications.send(
OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,
journal: double('journal', initial?: true, journable: double('WorkPackage'))
)
end
end