Files
odoo_source/addons/microsoft_calendar/tests/test_sync_odoo2microsoft.py
T
Aurélien (avd) 005741e509 [IMP] microsoft_calendar: add stop synchronization button
Add the possibility to stop/restart the synchronization with
Outlook calendar.

Change the calendar event popover in the calendar view to
display an Archive button if event synced at least once.

Add some tests for the new stop/restart feature.

(PS: this is essentially the same commit as the one for the
google_calendar module except for the JS popover and python
tests).
2020-12-10 10:23:10 +01:00

107 lines
4.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime, date
from dateutil.relativedelta import relativedelta
from unittest.mock import MagicMock, patch
from odoo.tests.common import TransactionCase
from odoo.addons.microsoft_calendar.utils.microsoft_calendar import MicrosoftCalendarService
from odoo.addons.microsoft_calendar.models.res_users import User
from odoo.addons.microsoft_calendar.models.microsoft_sync import MicrosoftSync
from odoo.modules.registry import Registry
from odoo.addons.microsoft_account.models.microsoft_service import TIMEOUT
def patch_api(func):
@patch.object(MicrosoftSync, '_microsoft_insert', MagicMock())
@patch.object(MicrosoftSync, '_microsoft_delete', MagicMock())
@patch.object(MicrosoftSync, '_microsoft_patch', MagicMock())
def patched(self, *args, **kwargs):
return func(self, *args, **kwargs)
return patched
@patch.object(User, '_get_microsoft_calendar_token', lambda user: 'dummy-token')
class TestSyncOdoo2Microsoft(TransactionCase):
def setUp(self):
super().setUp()
self.microsoft_service = MicrosoftCalendarService(self.env['microsoft.service'])
def assertMicrosoftEventInserted(self, values):
MicrosoftSync._microsoft_insert.assert_called_once_with(self.microsoft_service, values)
def assertMicrosoftEventNotInserted(self):
MicrosoftSync._microsoft_insert.assert_not_called()
def assertMicrosoftEventPatched(self, microsoft_id, values, timeout=None):
expected_args = (microsoft_id, values)
expected_kwargs = {'timeout': timeout} if timeout else {}
MicrosoftSync._microsoft_patch.assert_called_once()
args, kwargs = MicrosoftSync._microsoft_patch.call_args
self.assertEqual(args[1:], expected_args) # skip Google service arg
self.assertEqual(kwargs, expected_kwargs)
@patch_api
def test_stop_synchronization(self):
self.env.user.stop_microsoft_synchronization()
self.assertTrue(self.env.user.microsoft_synchronization_stopped, "The microsoft synchronization flag should be switched on")
self.assertFalse(self.env.user._sync_microsoft_calendar(self.microsoft_service), "The microsoft synchronization should be stopped")
# If synchronization stopped, creating a new event should not call _google_insert.
self.env['calendar.event'].create({
'name': "Event",
'start': datetime(2020, 1, 15, 8, 0),
'stop': datetime(2020, 1, 15, 18, 0),
'privacy': 'private',
})
self.assertMicrosoftEventNotInserted()
@patch_api
def test_restart_synchronization(self):
# Test new event created after stopping synchronization are correctly patched when restarting sync.
self.maxDiff = None
microsoft_id = 'aaaaaaaaa'
partner = self.env['res.partner'].create({'name': 'Jean-Luc', 'email': 'jean-luc@opoo.com'})
user = self.env['res.users'].create({
'name': 'Test user Calendar',
'login': 'jean-luc@opoo.com',
'partner_id': partner.id,
})
user.stop_microsoft_synchronization()
event = self.env['calendar.event'].with_user(user).create({
'microsoft_id': microsoft_id,
'name': "Event",
'start': datetime(2020, 1, 15, 8, 0),
'stop': datetime(2020, 1, 15, 18, 0),
'partner_ids': [(4, partner.id)],
})
user.with_user(user).restart_microsoft_synchronization()
event.with_user(user)._sync_odoo2microsoft(self.microsoft_service)
microsoft_guid = self.env['ir.config_parameter'].sudo().get_param('microsoft_calendar.microsoft_guid', False)
self.assertMicrosoftEventPatched(event.microsoft_id, {
'id': event.microsoft_id,
'start': {'dateTime': '2020-01-15T08:00:00+00:00', 'timeZone': 'Europe/London'},
'end': {'dateTime': '2020-01-15T18:00:00+00:00', 'timeZone': 'Europe/London'},
'subject': 'Event',
'body': {'content': '', 'contentType': 'text'},
'attendees': [],
'isAllDay': False,
'isOrganizer': True,
'isReminderOn': False,
'sensitivity': 'normal',
'showAs': 'busy',
'location': {'displayName': ''},
'organizer': {'emailAddress': {'address': 'jean-luc@opoo.com', 'name': 'Test user Calendar'}},
'reminderMinutesBeforeStart': 0,
'singleValueExtendedProperties': [{
'id': 'String {%s} Name odoo_id' % microsoft_guid,
'value': str(event.id),
}, {
'id': 'String {%s} Name owner_odoo_id' % microsoft_guid,
'value': str(user.id),
}
]
})