Schedule a demo

Testing Microsoft 365 connections

When your Zato service reads SharePoint lists, sends Teams messages, accesses OneDrive files, or works with any Microsoft 365 API, you need to test that logic without real Microsoft credentials. This page shows how to mock Microsoft 365 responses so your tests run fast and work without Azure AD setup.

Note: If you're new to unit testing with Zato, check the tutorial first.

How it works

When your Zato service calls Microsoft 365 APIs, the code uses method chaining. For example, here is a service that gets SharePoint list items:

from zato.server.service import Service

class GetItems(Service):
    name = 'sharepoint.items.get'

    def handle(self):
        conn = self.cloud.ms365.get('ms365.Sharepoint').conn
        with conn.client() as client:
            site = client.impl.sharepoint().get_site('sites/main')
            sp_list = site.get_list_by_name('Tasks')
            items = sp_list.get_items()
        self.response.payload = {'items': list(items)}

To mock this, you configure responses for each step in the chain using dot-separated paths:

from zato_testing import ServiceTestCase

class TestGetItems(ServiceTestCase):

    def test_returns_items(self):

        self.set_response('ms365.Sharepoint.sharepoint.get_site', {'id': 'site-123'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name', {'id': 'list-456'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name.get_items', [
            {'Title': 'Item 1'},
            {'Title': 'Item 2'}
        ])

        service = self.invoke(GetItems)

        items = service.response.payload['items']
        self.assertEqual(len(items), 2)

The framework detects Microsoft 365 paths by the dot-separated method chain (at least 2 dots).

Request matching

For operations that send data, use request matching:

self.set_response(
    'ms365.Sharepoint.sharepoint.get_site.get_list_by_name.create_list_item',
    response={'id': 'new-item-123'},
    request={'Title': 'New Item', 'Status': 'Active'}
)

Missing responses

If a service calls a Microsoft 365 method without a configured response, the framework raises an exception:

ValueError: No response configured for 'ms365.Sharepoint...get_items'.
Use set_response('ms365.Sharepoint...get_items', <response>) to configure it.

Microsoft 365 resources

Each Microsoft 365 resource has its own documentation:

ResourceDescription
SharePointLists, items, columns, subsites
MailboxMessages, folders, sending email
OneDriveFiles, folders, sharing, versions
TeamsTeams, channels, chats, presence
CalendarEvents, calendars, scheduling
DirectoryUsers, groups, organization
PlannerPlans, tasks, buckets
TasksToDo lists and tasks

See also