[FIX] mail, test_mail: avoid removing chat users on archive/delete

Before this commit:

When deleting/archiving any user, the user’s related chat changed name due to
losing one of its members.

After this commit:

Chat name should remain the same after archiving/deleting the user.

Reasoning:

The unsubscribe was meant to target channels of type channel specifically.

`test_channel_auto_unsubscribe_archived_or_deleted_users` has been reintroduced
after having been removed by mistake in eda542c82f84d7b5589846691b9cb6b7f1021947
task-2442235

closes odoo/odoo#67884

X-original-commit: cb4bd4cbc4a36276cc6c9cb68e51c260e7c0d761
Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
This commit is contained in:
bit-odoo
2021-03-15 15:59:41 +00:00
committed by Sébastien Theys
parent cd117b15d4
commit f22e5b66a7
2 changed files with 46 additions and 3 deletions
+1 -1
View File
@@ -109,7 +109,7 @@ GROUP BY channel_moderator.res_users_id""", [tuple(self.ids)])
public channels are mailing list (e-mail based) and so users should always receive
updates from public channels until they manually un-subscribe themselves.
"""
self.mapped('partner_id.channel_ids').filtered(lambda c: c.public != 'public').write({
self.mapped('partner_id.channel_ids').filtered(lambda c: c.public != 'public' and c.channel_type == 'channel').write({
'channel_partner_ids': [Command.unlink(pid) for pid in self.mapped('partner_id').ids]
})
+45 -2
View File
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import Command
from odoo.addons.mail.tests.common import mail_new_test_user
from odoo.addons.mail.tests.common import MailCommon
from odoo.exceptions import AccessError, ValidationError, UserError
@@ -8,7 +9,6 @@ from odoo.tests import tagged, Form
from odoo.tests.common import users
from odoo.tools import mute_logger, formataddr
class TestChannelAccessRights(MailCommon):
@classmethod
@@ -92,7 +92,6 @@ class TestChannelAccessRights(MailCommon):
with self.assertRaises(AccessError):
self.env['mail.channel'].browse(self.group_private.id).unlink()
@mute_logger('odoo.addons.base.models.ir_rule', 'odoo.models')
@users('user_portal')
def test_access_portal(self):
@@ -341,6 +340,50 @@ class TestChannelInternals(MailCommon):
"Last message id should stay the same after mark channel as seen with an older message"
)
@mute_logger('odoo.models.unlink')
def test_channel_auto_unsubscribe_archived_or_deleted_users(self):
"""Archiving / deleting a user should automatically unsubscribe related partner from private channels"""
test_user = self.env['res.users'].create({
"login": "adam",
"name": "Jonas",
})
test_partner = test_user.partner_id
test_channel_private = self.env['mail.channel'].with_context(self._test_context).create({
'name': 'Winden caves',
'description': 'Channel to travel through time',
'public': 'private',
'channel_partner_ids': [Command.link(self.user_employee.partner_id.id), Command.link(test_partner.id)],
})
test_channel_group = self.env['mail.channel'].with_context(self._test_context).create({
'name': 'Sic Mundus',
'public': 'groups',
'group_public_id': self.env.ref('base.group_user').id,
'channel_partner_ids': [Command.link(self.user_employee.partner_id.id), Command.link(test_partner.id)],
})
self.test_channel.with_context(self._test_context).write({
'channel_partner_ids': [Command.link(self.user_employee.partner_id.id), Command.link(test_partner.id)],
})
test_chat = self.env['mail.channel'].with_context(self._test_context).create({
'name': 'test',
'channel_type': 'chat',
'public': 'private',
'channel_partner_ids': [Command.link(self.user_employee.partner_id.id), Command.link(test_partner.id)],
})
# Unsubscribe archived user from the private channels, but not from public channels and not from chat
self.user_employee.active = False
self.assertEqual(test_channel_private.channel_partner_ids, test_partner)
self.assertEqual(test_channel_group.channel_partner_ids, test_partner)
self.assertEqual(self.test_channel.channel_partner_ids, self.user_employee.partner_id | test_partner)
self.assertEqual(test_chat.channel_partner_ids, self.user_employee.partner_id | test_partner)
# Unsubscribe deleted user from the private channels, but not from public channels and not from chat
test_user.unlink()
self.assertEqual(test_channel_private.channel_partner_ids, self.env['res.partner'])
self.assertEqual(test_channel_group.channel_partner_ids, self.env['res.partner'])
self.assertEqual(self.test_channel.channel_partner_ids, self.user_employee.partner_id | test_partner)
self.assertEqual(test_chat.channel_partner_ids, self.user_employee.partner_id | test_partner)
def test_multi_company_chat(self):
self._activate_multi_company()
self.assertEqual(self.env.user.company_id, self.company_admin)