"""Shared test fixtures for Bitbucket MCP Server tests."""
import json
import os
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
@pytest.fixture(autouse=True)
def mock_config(tmp_path, monkeypatch):
"""Use a temp config directory for all tests so we never touch real config."""
config_dir = tmp_path / ".bitbucket-mcp"
config_file = config_dir / "config.json"
monkeypatch.setattr("bitbucket_mcp.server.CONFIG_DIR", config_dir)
monkeypatch.setattr("bitbucket_mcp.server.CONFIG_FILE", config_file)
# Also patch in every tool module that imports these
for mod in [
"bitbucket_mcp.tools.config",
]:
try:
monkeypatch.setattr(f"{mod}.CONFIG_FILE", config_file)
except AttributeError:
pass
return config_dir, config_file
@pytest.fixture
def saved_config(mock_config):
"""Save a valid config file and return its path."""
config_dir, config_file = mock_config
config_dir.mkdir(parents=True, exist_ok=True)
config_file.write_text(json.dumps({
"workspace": "test-workspace",
"username": "test@example.com",
"token": "test-token-123"
}))
return config_file
@pytest.fixture(autouse=True)
def clear_env(monkeypatch):
"""Ensure no env vars leak into tests."""
monkeypatch.delenv("BITBUCKET_API_TOKEN", raising=False)
monkeypatch.delenv("BITBUCKET_USERNAME", raising=False)
monkeypatch.delenv("BITBUCKET_WORKSPACE", raising=False)
def make_mock_response(status_code=200, json_data=None, text=""):
"""Create a mock httpx.Response."""
resp = MagicMock()
resp.status_code = status_code
resp.text = text or (json.dumps(json_data) if json_data else "")
if json_data is not None:
resp.json.return_value = json_data
else:
resp.json.side_effect = json.JSONDecodeError("", "", 0)
return resp
def make_paginated_response(values, size=None, page=1, pagelen=25):
"""Create a standard paginated API response."""
return make_mock_response(json_data={
"values": values,
"size": size if size is not None else len(values),
"page": page,
"pagelen": pagelen,
})