Skip to content

Personal AI, On Personal Devices

Python

feat: add Outlook connector as IMAP subclass

Jon Saad-Falcon0f03059eec70
diff --git a/src/openjarvis/connectors/outlook.py b/src/openjarvis/connectors/outlook.py
new file mode 100644
index 00000000..9302c11d
--- /dev/null
+++ b/src/openjarvis/connectors/outlook.py
@@ -0,0 +1,60 @@
+"""Outlook / Microsoft 365 connector — reads email via IMAP with app password.
+
+Thin subclass of GmailIMAPConnector that defaults to the Outlook IMAP host
+and relabels documents with source='outlook'.
+
+Setup: enable 2FA on your Microsoft account, then generate an app password
+at https://account.microsoft.com/security
+"""
+
+from __future__ import annotations
+
+from datetime import datetime
+from typing import Iterator, Optional
+
+from openjarvis.connectors._stubs import Document
+from openjarvis.connectors.gmail_imap import GmailIMAPConnector
+from openjarvis.core.config import DEFAULT_CONFIG_DIR
+from openjarvis.core.registry import ConnectorRegistry
+
+_DEFAULT_CREDENTIALS_PATH = str(
+ DEFAULT_CONFIG_DIR / "connectors" / "outlook.json"
+)
+
+
+class OutlookConnector(GmailIMAPConnector):
+ """Outlook connector using IMAP + app password."""
+
+ connector_id = "outlook"
+ display_name = "Outlook / Microsoft 365"
+ _default_imap_host = "outlook.office365.com"
+
+ def __init__(
+ self,
+ email_address: str = "",
+ app_password: str = "",
+ credentials_path: str = "",
+ *,
+ max_messages: int = 500,
+ ) -> None:
+ super().__init__(
+ email_address,
+ app_password,
+ credentials_path or _DEFAULT_CREDENTIALS_PATH,
+ max_messages=max_messages,
+ )
+
+ def auth_url(self) -> str:
+ return "https://account.microsoft.com/security"
+
+ def sync(
+ self,
+ *,
+ since: Optional[datetime] = None,
+ cursor: Optional[str] = None,
+ ) -> Iterator[Document]:
+ for doc in super().sync(since=since, cursor=cursor):
+ doc.source = "outlook"
+ doc.doc_id = doc.doc_id.replace("gmail:", "outlook:", 1)
+ yield doc
diff --git a/tests/connectors/test_outlook.py b/tests/connectors/test_outlook.py
new file mode 100644
index 00000000..57a6a910
--- /dev/null
+++ b/tests/connectors/test_outlook.py
@@ -0,0 +1,81 @@
+"""Tests for OutlookConnector — thin subclass of GmailIMAPConnector."""
+
+from __future__ import annotations
+
+from pathlib import Path
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from openjarvis.connectors.oauth import load_tokens
+from openjarvis.connectors.outlook import OutlookConnector
+from openjarvis.core.registry import ConnectorRegistry
+
+
+def test_outlook_registered() -> None:
+ ConnectorRegistry.register_value("outlook", OutlookConnector)
+ assert ConnectorRegistry.contains("outlook")
+ cls = ConnectorRegistry.get("outlook")
+ assert cls.connector_id == "outlook"
+ assert cls.display_name == "Outlook / Microsoft 365"
+
+
+def test_outlook_uses_correct_imap_host() -> None:
+ conn = OutlookConnector()
+ assert conn._imap_host == "outlook.office365.com"
+
+
+def test_outlook_auth_url() -> None:
+ conn = OutlookConnector()
+ assert "microsoft.com" in conn.auth_url()
+
+
+def test_outlook_handle_callback(tmp_path: Path) -> None:
+ creds_path = str(tmp_path / "outlook.json")
+ conn = OutlookConnector(credentials_path=creds_path)
+ conn.handle_callback("[email protected]:mypassword123")
+ tokens = load_tokens(creds_path)
+ assert tokens is not None
+ assert tokens["email"] == "[email protected]"
+ assert tokens["password"] == "mypassword123"
+
+
+def test_outlook_is_connected(tmp_path: Path) -> None:
+ creds_path = str(tmp_path / "outlook.json")
+ conn = OutlookConnector(credentials_path=creds_path)
+ assert conn.is_connected() is False
+ conn.handle_callback("[email protected]:pass")
+ assert conn.is_connected() is True
+
+
+def test_outlook_sync_source_is_outlook(tmp_path: Path) -> None:
+ creds_path = str(tmp_path / "outlook.json")
+ conn = OutlookConnector(credentials_path=creds_path)
+ conn.handle_callback("[email protected]:pass")
+
+ mock_imap = MagicMock()
+ mock_imap.login.return_value = ("OK", [])
+ mock_imap.select.return_value = ("OK", [])
+ mock_imap.search.return_value = ("OK", [b"1"])
+
+ raw_email = (
+ b"From: [email protected]\r\n"
+ b"To: [email protected]\r\n"
+ b"Subject: Test Email\r\n"
+ b"Date: Mon, 01 Jan 2024 00:00:00 +0000\r\n"
+ b"Message-ID: <[email protected]>\r\n"
+ b"\r\n"
+ b"Hello from Outlook test"
+ )
+ mock_imap.fetch.return_value = ("OK", [(b"1", raw_email)])
+ mock_imap.logout.return_value = ("OK", [])
+
+ with patch("openjarvis.connectors.gmail_imap.imaplib") as mock_imaplib:
+ mock_imaplib.IMAP4_SSL.return_value = mock_imap
+ mock_imaplib.IMAP4 = type(mock_imap)
+ docs = list(conn.sync())
+
+ assert len(docs) == 1
+ assert docs[0].source == "outlook"
+ assert docs[0].doc_id.startswith("outlook:")
+ assert docs[0].title == "Test Email"