diff --git a/parsedmarc/mail/graph.py b/parsedmarc/mail/graph.py index 7713d45..6f3dbcc 100644 --- a/parsedmarc/mail/graph.py +++ b/parsedmarc/mail/graph.py @@ -289,10 +289,6 @@ class MSGraphConnection(MailboxConnection): parent_folder_id = folder_id return self._find_folder_id_with_parent(path_parts[-1], parent_folder_id) else: - # Shared mailboxes can fail root listing; try well-known folders first. - well_known_folder_id = self._get_well_known_folder_id(folder_name) - if well_known_folder_id: - return well_known_folder_id return self._find_folder_id_with_parent(folder_name, None) def _get_well_known_folder_id(self, folder_name: str) -> Optional[str]: @@ -302,7 +298,7 @@ class MSGraphConnection(MailboxConnection): return None url = f"/users/{self.mailbox_name}/mailFolders/{alias}?$select=id,displayName" - folder_resp = self._client.get(url) + folder_resp = self._request_with_retries("get", url) if folder_resp.status_code != 200: return None payload = folder_resp.json() diff --git a/tests.py b/tests.py index 6807477..0b9d3cf 100755 --- a/tests.py +++ b/tests.py @@ -1346,16 +1346,50 @@ class TestMSGraphFolderFallback(unittest.TestCase): connection = MSGraphConnection.__new__(MSGraphConnection) connection.mailbox_name = "shared@example.com" connection._client = _FakeGraphClient() + connection._request_with_retries = MagicMock( + side_effect=lambda method_name, *args, **kwargs: getattr( + connection._client, method_name + )( + *args, **kwargs + ) + ) - folder_id = connection._find_folder_id_from_folder_path("Inbox") + folder_id = connection._find_folder_id_with_parent("Inbox", None) self.assertEqual(folder_id, "inbox-id") + connection._request_with_retries.assert_any_call( + "get", "/users/shared@example.com/mailFolders?$filter=displayName eq 'Inbox'" + ) + connection._request_with_retries.assert_any_call( + "get", "/users/shared@example.com/mailFolders/inbox?$select=id,displayName" + ) def testUnknownFolderStillFails(self): connection = MSGraphConnection.__new__(MSGraphConnection) connection.mailbox_name = "shared@example.com" connection._client = _FakeGraphClient() + connection._request_with_retries = MagicMock( + side_effect=lambda method_name, *args, **kwargs: getattr( + connection._client, method_name + )( + *args, **kwargs + ) + ) with self.assertRaises(RuntimeWarning): connection._find_folder_id_from_folder_path("Custom") + + def testSingleSegmentPathAvoidsExtraWellKnownLookupWhenListingSucceeds(self): + connection = MSGraphConnection.__new__(MSGraphConnection) + connection.mailbox_name = "shared@example.com" + connection._find_folder_id_with_parent = MagicMock(return_value="custom-id") + connection._get_well_known_folder_id = MagicMock(return_value="inbox-id") + + folder_id = connection._find_folder_id_from_folder_path("Inbox") + + self.assertEqual(folder_id, "custom-id") + connection._find_folder_id_with_parent.assert_called_once_with("Inbox", None) + connection._get_well_known_folder_id.assert_not_called() + + if __name__ == "__main__": unittest.main(verbosity=2)