Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

14.9 KB
Raw Blame
"""Tests for adding the HubSpot crm.objects.deals.write OAuth scope."""

import importlib.util
import json
from pathlib import Path
from unittest.mock import patch

from alembic.migration import MigrationContext
from alembic.operations import Operations
from sqlalchemy import create_engine, text


def _load_migration_module():
    migration_file = (
        Path(__file__).parent.parent.parent
        / "src/xagent/migrations/versions/20260914_add_hubspot_deals_write_scope.py"
    )
    spec = importlib.util.spec_from_file_location(
        "add_hubspot_deals_write_scope_migration", migration_file
    )
    module = importlib.util.module_from_spec(spec)
    assert spec.loader is not None
    spec.loader.exec_module(module)
    return module


def _load_previous_migration_module():
    migration_file = (
        Path(__file__).parent.parent.parent
        / "src/xagent/migrations/versions/20260810_add_hubspot_marketing_scopes.py"
    )
    spec = importlib.util.spec_from_file_location(
        "add_hubspot_marketing_scopes_migration", migration_file
    )
    module = importlib.util.module_from_spec(spec)
    assert spec.loader is not None
    spec.loader.exec_module(module)
    return module


def _load_remote_mcp_migration_module():
    migration_file = (
        Path(__file__).parent.parent.parent
        / "src/xagent/migrations/versions/20261002_migrate_hubspot_to_remote_mcp.py"
    )
    spec = importlib.util.spec_from_file_location(
        "migrate_hubspot_to_remote_mcp", migration_file
    )
    module = importlib.util.module_from_spec(spec)
    assert spec.loader is not None
    spec.loader.exec_module(module)
    return module


def _operations(connection):
    return Operations(MigrationContext.configure(connection))


def _create_table(connection, description: str, with_description_column: bool = True):
    description_column = "description TEXT," if with_description_column else ""
    connection.execute(
        text(
            f"""
            CREATE TABLE public_mcp_apps (
                id INTEGER PRIMARY KEY,
                app_id VARCHAR(100) NOT NULL UNIQUE,
                {description_column}
                oauth_scopes JSON
            )
            """
        )
    )
    description_col = ", description" if with_description_column else ""
    description_val = ", :description" if with_description_column else ""
    connection.execute(
        text(
            f"INSERT INTO public_mcp_apps (app_id, oauth_scopes{description_col}) "
            f"VALUES ('hubspot', :scopes{description_val})"
        ),
        {
            "scopes": json.dumps(
                [
                    "crm.objects.contacts.read",
                    "crm.objects.contacts.write",
                    "crm.objects.companies.read",
                    "crm.objects.companies.write",
                    "crm.objects.deals.read",
                    "forms",
                ]
            ),
            "description": description,
        },
    )


def _create_user_oauth_table(connection, with_refresh_token_column: bool = True):
    refresh_column = "refresh_token VARCHAR," if with_refresh_token_column else ""
    connection.execute(
        text(
            f"""
            CREATE TABLE user_oauth (
                id INTEGER PRIMARY KEY,
                user_id INTEGER NOT NULL,
                provider VARCHAR(50) NOT NULL,
                {refresh_column}
                access_token VARCHAR NOT NULL
            )
            """
        )
    )
    refresh_col = ", refresh_token" if with_refresh_token_column else ""
    refresh_hubspot = ", 'old-hubspot-refresh'" if with_refresh_token_column else ""
    refresh_other = ", 'old-salesforce-refresh'" if with_refresh_token_column else ""
    connection.execute(
        text(
            f"INSERT INTO user_oauth (user_id, provider, access_token{refresh_col}) "
            f"VALUES (1, 'hubspot', 'old-hubspot-token'{refresh_hubspot}), "
            f"(1, 'salesforce', 'old-salesforce-token'{refresh_other})"
        )
    )


def _access_tokens(connection):
    return dict(
        connection.execute(text("SELECT provider, access_token FROM user_oauth")).all()
    )


def _refresh_tokens(connection):
    return dict(
        connection.execute(text("SELECT provider, refresh_token FROM user_oauth")).all()
    )


def _row(connection):
    row = connection.execute(
        text(
            "SELECT description, oauth_scopes FROM public_mcp_apps "
            "WHERE app_id='hubspot'"
        )
    ).first()
    description, scopes = row[0], row[1]
    return description, json.loads(scopes) if isinstance(scopes, str) else scopes


def test_upgrade_adds_deals_write_scope_and_description(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()
        description, scopes = _row(connection)
        assert description == migration.CURRENT_DESCRIPTION
        assert scopes == migration.CURRENT_SCOPES


def test_upgrade_is_idempotent(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()
            migration.upgrade()
        description, scopes = _row(connection)
        assert description == migration.CURRENT_DESCRIPTION
        assert scopes == migration.CURRENT_SCOPES


def test_downgrade_restores_previous_scopes_and_description(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()
            migration.downgrade()
        description, scopes = _row(connection)
        assert description == migration.PREVIOUS_DESCRIPTION
        assert scopes == migration.PREVIOUS_SCOPES


def test_upgrade_downgrade_upgrade_round_trip(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()
            migration.downgrade()
            migration.upgrade()
        description, scopes = _row(connection)
        assert description == migration.CURRENT_DESCRIPTION
        assert scopes == migration.CURRENT_SCOPES


def test_upgrade_preserves_admin_customized_description(tmp_path):
    """description is not in _BUILTIN_PROTECTED_FIELDS (admin_mcp.py), so an
    operator can have edited it via the admin PATCH endpoint. The migration
    must not clobber a value that no longer matches the last-known default.
    """
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description="Our internal HubSpot connector")
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()
        description, scopes = _row(connection)
        assert description == "Our internal HubSpot connector"
        assert scopes == migration.CURRENT_SCOPES


def test_downgrade_preserves_admin_customized_description(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()
            connection.execute(
                text(
                    "UPDATE public_mcp_apps SET description = :d "
                    "WHERE app_id = 'hubspot'"
                ),
                {"d": "Our internal HubSpot connector"},
            )
            migration.downgrade()
        description, scopes = _row(connection)
        assert description == "Our internal HubSpot connector"
        assert scopes == migration.PREVIOUS_SCOPES


def test_upgrade_without_description_column_still_updates_scopes(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(
            connection,
            description=migration.PREVIOUS_DESCRIPTION,
            with_description_column=False,
        )
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()  # must not raise when description is missing
        scopes = connection.execute(
            text("SELECT oauth_scopes FROM public_mcp_apps WHERE app_id='hubspot'")
        ).scalar()
        assert json.loads(scopes) == migration.CURRENT_SCOPES


def test_upgrade_without_table_is_a_noop(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()  # must not raise when the table doesn't exist


def test_upgrade_invalidates_existing_hubspot_grant_only(tmp_path):
    """Both tokens must be cleared. The current token resolver already
    short-circuits on a falsy access_token before reaching
    refresh_oauth_token_if_needed, so clearing refresh_token too is
    defense-in-depth against a future resolver shape, not a fix for a
    live path today.
    """
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        _create_user_oauth_table(connection)
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()
        tokens = _access_tokens(connection)
        assert tokens["hubspot"] == ""
        assert tokens["salesforce"] == "old-salesforce-token"
        refresh_tokens = _refresh_tokens(connection)
        assert refresh_tokens["hubspot"] is None
        assert refresh_tokens["salesforce"] == "old-salesforce-refresh"


def test_upgrade_logs_a_warning_when_grants_are_disconnected(tmp_path, caplog):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        _create_user_oauth_table(connection)
        with patch.object(migration, "op", _operations(connection)):
            with caplog.at_level("WARNING", logger=migration.logger.name):
                migration.upgrade()
    assert any(
        "Disconnected" in r.message and "crm.objects.deals.write" in r.message
        for r in caplog.records
    )


def test_upgrade_does_not_log_when_no_grants_exist(tmp_path, caplog):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        with patch.object(migration, "op", _operations(connection)):
            with caplog.at_level("WARNING", logger=migration.logger.name):
                migration.upgrade()  # no user_oauth table at all
    assert caplog.records == []


def test_upgrade_clears_access_token_without_refresh_token_column(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        _create_user_oauth_table(connection, with_refresh_token_column=False)
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()  # must not raise when refresh_token is missing
        tokens = _access_tokens(connection)
        assert tokens["hubspot"] == ""


def test_upgrade_without_user_oauth_table_is_a_noop(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()  # must not raise when user_oauth doesn't exist


def test_downgrade_does_not_touch_user_oauth(tmp_path):
    """Cleared access tokens are gone for good; downgrade only reverts
    public_mcp_apps, mirroring the earlier HubSpot scope migration."""
    engine = create_engine(f"sqlite:///{tmp_path / 'test.db'}")
    migration = _load_migration_module()
    with engine.begin() as connection:
        _create_table(connection, description=migration.PREVIOUS_DESCRIPTION)
        _create_user_oauth_table(connection)
        with patch.object(migration, "op", _operations(connection)):
            migration.upgrade()
            migration.downgrade()
        tokens = _access_tokens(connection)
        assert tokens["hubspot"] == ""


def test_migration_fields_chain_to_remote_mcp_migration():
    """This migration's CURRENT_DESCRIPTION is a historical snapshot, not the
    app's final value - 20260916_update_hubspot_description layers another
    description update on top of it, and the hosted-MCP migration later
    replaces both the description and scope model. CURRENT_SCOPES must still
    match the legacy snapshot consumed by that final migration."""
    migration = _load_migration_module()
    remote_migration = _load_remote_mcp_migration_module()
    assert migration.CURRENT_SCOPES == remote_migration.LEGACY_SCOPES


def test_previous_fields_chain_from_the_prior_migration():
    """This migration's PREVIOUS_SCOPES/PREVIOUS_DESCRIPTION must exactly
    equal 20260810's CURRENT_SCOPES/CURRENT_DESCRIPTION - not just "close
    enough" - or the two migrations' downgrade() calls stop being inverses
    of each other. This exact coupling silently broke once already (20260810's
    constants were bumped forward to match the live registry, which broke
    its own downgrade() once this migration's downgrade() ran first and left
    a value 20260810 no longer recognized as "unchanged"); this test pins the
    invariant directly instead of relying on it only showing up as a
    full-chain downgrade failure.
    """
    migration = _load_migration_module()
    previous_migration = _load_previous_migration_module()
    assert migration.PREVIOUS_SCOPES == previous_migration.CURRENT_SCOPES
    assert migration.PREVIOUS_DESCRIPTION == previous_migration.CURRENT_DESCRIPTION