From 99bf0f266401a0d8d20bedebd75c11d99156b7d4 Mon Sep 17 00:00:00 2001 From: Miku Laitinen Date: Fri, 22 Dec 2023 20:24:23 +0200 Subject: [PATCH] [FIX] microsoft_calendar: Make searching organizer by their email address case-insensitive Before this commit Odoo was unable to match Outlook address Firstname.Lastname@example.com to Odoo user email address firstname.lastname@example.com. This lead to events being created in Odoo without an organizer. closes odoo/odoo#148715 X-original-commit: c6e174f99d741b82f9e2cb0789e7ad7786879825 Signed-off-by: Arnaud Joset (arj) --- .../tests/test_create_events.py | 26 +++++++++++++++++++ .../utils/microsoft_event.py | 8 +++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/addons/microsoft_calendar/tests/test_create_events.py b/addons/microsoft_calendar/tests/test_create_events.py index ad077066109..35edc71da41 100644 --- a/addons/microsoft_calendar/tests/test_create_events.py +++ b/addons/microsoft_calendar/tests/test_create_events.py @@ -111,6 +111,32 @@ class TestCreateEvents(TestCommon): self.assertEqual(len(new_records), 1) self.assert_odoo_event(new_records, expected_event) + @patch.object(MicrosoftCalendarService, 'get_events') + def test_create_simple_event_from_outlook_attendee_calendar_where_email_addresses_are_capitalized(self, mock_get_events): + """ + An event has been created in Outlook and synced in the Odoo attendee calendar. + The email addresses of the attendee and the organizer are in different case than in Odoo. + """ + + # arrange + outlook_event = dict(self.simple_event_from_outlook_attendee, organizer={ + 'emailAddress': {'address': "Mike@organizer.com", 'name': "Mike Organizer"}, + }, attendees=[{'type': 'required', 'status': {'response': 'none', 'time': '0001-01-01T00:00:00Z'}, + 'emailAddress': {'name': 'John Attendee', 'address': 'John@attendee.com'}}]) + + mock_get_events.return_value = (MicrosoftEvent([outlook_event]), None) + existing_records = self.env["calendar.event"].search([]) + + # act + self.organizer_user.with_user(self.organizer_user).sudo()._sync_microsoft_calendar() + + # assert + records = self.env["calendar.event"].search([]) + new_records = (records - existing_records) + self.assertEqual(len(new_records), 1) + self.assert_odoo_event(new_records, self.expected_odoo_event_from_outlook) + self.assertEqual(new_records.user_id, self.organizer_user) + @patch.object(MicrosoftCalendarService, 'insert') def test_create_recurrent_event_without_sync(self, mock_insert): """ diff --git a/addons/microsoft_calendar/utils/microsoft_event.py b/addons/microsoft_calendar/utils/microsoft_event.py index b560d0c5da5..3f35b3d67f7 100644 --- a/addons/microsoft_calendar/utils/microsoft_event.py +++ b/addons/microsoft_calendar/utils/microsoft_event.py @@ -3,7 +3,7 @@ from odoo.api import model from typing import Iterator, Mapping from collections import abc -from odoo.tools import ReadonlyDict +from odoo.tools import ReadonlyDict, email_normalize from odoo.addons.microsoft_calendar.utils.event_id_storage import combine_ids @@ -166,9 +166,11 @@ class MicrosoftEvent(abc.Set): """ if self.isOrganizer: return env.user.id - if self.organizer.get('emailAddress') and self.organizer.get('emailAddress').get('address'): + + organizer_email = self.organizer.get('emailAddress') and email_normalize(self.organizer.get('emailAddress').get('address')) + if organizer_email: # Warning: In Microsoft: 1 email = 1 user; but in Odoo several users might have the same email - user = env['res.users'].search([('email', '=', self.organizer.get('emailAddress').get('address'))], limit=1) + user = env['res.users'].search([('email', '=', organizer_email)], limit=1) return user.id if user else False return False