Files
Django_TasksApp/main/mcp_server/tests.py
T

192 lines
12 KiB
Python

import asyncio
import base64
import hashlib
import io
import os
import socket
import subprocess
import sys
import time
import uuid
from datetime import timedelta
from decimal import Decimal
from urllib.parse import parse_qs, urlparse
import httpx2
from PIL import Image
from django.conf import settings
from django.contrib.auth import get_user_model
from django.test import LiveServerTestCase
from django.utils import timezone
from mcp import Client
from mcp.client.streamable_http import streamable_http_client
from oauth2_provider.models import Application
from food.api import create_token
from food.models import FoodLogEntry, FoodProduct, InventoryLocation, KitchenInventoryItem
from planner.timezones import localdate_for_user
class McpHttpTests(LiveServerTestCase):
def setUp(self):
self.a = get_user_model().objects.create_user('mcp-a', password='test')
self.b = get_user_model().objects.create_user('mcp-b', password='test')
self.a_product = FoodProduct.objects.create(owner=self.a, product_name='Pasta', energy_kcal_100=Decimal('350'))
self.b_product = FoodProduct.objects.create(owner=self.b, product_name='Private rice')
self.b_entry = FoodLogEntry.objects.create(user=self.b, date=localdate_for_user(self.b), meal='lunch',
product=self.b_product, product_name_snapshot='Private rice', amount=Decimal('100'), unit='g')
location = InventoryLocation.objects.create(user=self.b, name='Private cupboard')
self.b_item = KitchenInventoryItem.objects.create(user=self.b, product=self.b_product, location=location)
_, self.secret = create_token(self.a, 'MCP test', ['food:read', 'food:write', 'inventory:read', 'inventory:write'])
_, self.b_secret = create_token(self.b, 'MCP test B', ['food:read', 'inventory:read'])
_, self.read_secret = create_token(self.a, 'MCP read', ['food:read'])
expired, self.expired_secret = create_token(self.a, 'MCP expired', ['food:read'], timezone.now() - timedelta(seconds=1))
revoked, self.revoked_secret = create_token(self.a, 'MCP revoked', ['food:read'])
revoked.revoked_at = timezone.now()
revoked.save(update_fields=['revoked_at'])
with socket.socket() as sock:
sock.bind(('127.0.0.1', 0))
self.port = sock.getsockname()[1]
self.mcp_url = f'http://127.0.0.1:{self.port}/mcp'
env = {**os.environ, 'TASKNEST_API_BASE_URL': self.live_server_url + '/api/v1',
'TASKNEST_MCP_PUBLIC_URL': self.mcp_url,
'TASKNEST_OAUTH_ISSUER_URL': self.live_server_url}
self.process = subprocess.Popen([sys.executable, '-m', 'uvicorn', 'mcp_server.server:app',
'--host', '127.0.0.1', '--port', str(self.port), '--log-level', 'error'],
cwd=settings.BASE_DIR, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
for _ in range(50):
try:
response = httpx2.get(f'http://127.0.0.1:{self.port}/.well-known/oauth-protected-resource/mcp', timeout=1)
if response.status_code == 200:
break
except httpx2.RequestError:
pass
time.sleep(.1)
else:
self.fail('MCP server did not start.')
def tearDown(self):
self.process.terminate()
try:
self.process.wait(timeout=5)
except subprocess.TimeoutExpired:
self.process.kill()
self.process.wait(timeout=5)
async def _client(self, secret, operation):
async with httpx2.AsyncClient(headers={'Authorization': 'Bearer ' + secret}, timeout=30) as http:
async with Client(streamable_http_client(self.mcp_url, http_client=http)) as client:
return await operation(client)
def test_authentication_and_scope_boundaries(self):
async def check():
for secret in ('bad-token', self.expired_secret, self.revoked_secret):
async with httpx2.AsyncClient(timeout=10) as http:
response = await http.post(self.mcp_url, headers={'Authorization': 'Bearer ' + secret,
'Accept': 'application/json, text/event-stream'}, json={'jsonrpc': '2.0', 'id': 1, 'method': 'tools/list'})
self.assertEqual(response.status_code, 401)
async def read_only(client):
self.assertFalse((await client.call_tool('food_get_today')).is_error)
self.assertTrue((await client.call_tool('inventory_list')).is_error)
self.assertTrue((await client.call_tool('food_log', {'food': {
'meal': 'lunch', 'amount': '1', 'unit': 'g', 'product_name': 'No write'}})).is_error)
await self._client(self.read_secret, read_only)
asyncio.run(check())
def test_oauth_issued_pat_authenticates_mcp(self):
application = Application.objects.create(user=self.a, name='Test ChatGPT style client',
client_type=Application.CLIENT_PUBLIC, authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
redirect_uris='http://localhost:9876/callback')
self.client.force_login(self.a)
verifier = 'v' * 64
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).decode().rstrip('=')
params = {'response_type': 'code', 'client_id': application.client_id,
'redirect_uri': 'http://localhost:9876/callback', 'scope': 'food:read',
'state': 'mcp-oauth-test', 'code_challenge': challenge, 'code_challenge_method': 'S256',
'resource': self.mcp_url}
response = self.client.get('/o/authorize/', params, HTTP_HOST='localhost')
self.assertEqual(response.status_code, 200)
form = response.context['form']
payload = {field.name: field.value() for field in form if field.field.widget.is_hidden and field.value() is not None}
payload['allow'] = 'Authorize'
redirect = self.client.post('/o/authorize/', payload, HTTP_HOST='localhost')
code = parse_qs(urlparse(redirect['Location']).query)['code'][0]
response = self.client.post('/o/token/', {'grant_type': 'authorization_code', 'code': code,
'redirect_uri': 'http://localhost:9876/callback', 'client_id': application.client_id,
'code_verifier': verifier, 'resource': self.mcp_url}, HTTP_HOST='localhost')
self.assertEqual(response.status_code, 200, response.content[:200])
secret = response.json()['access_token']
async def check(client):
self.assertFalse((await client.call_tool('food_get_today')).is_error)
self.assertTrue((await client.call_tool('inventory_list')).is_error)
asyncio.run(self._client(secret, check))
def test_food_inventory_tools_and_user_isolation(self):
image = Image.new('RGB', (2, 2), 'red')
output = io.BytesIO()
image.save(output, format='PNG')
encoded = base64.b64encode(output.getvalue()).decode()
async def check(client):
names = {tool.name for tool in (await client.list_tools()).tools}
self.assertEqual(len(names), 12)
today = (await client.call_tool('food_get_today')).structured_content
self.assertEqual(today['meals']['lunch'], [])
products = (await client.call_tool('food_search_products', {'query': 'Pasta'})).structured_content
self.assertEqual(products['count'], 1)
self.assertEqual((await client.call_tool('food_search_products', {'query': 'Private rice'})).structured_content['count'], 0)
food = {'meal': 'dinner', 'amount': '50', 'unit': 'g', 'product_id': str(self.a_product.api_id)}
first = (await client.call_tool('food_log', {'food': food, 'idempotency_key': 'mcp-food-1'})).structured_content
self.assertEqual(first['entry']['calories_kcal'], '175.000')
again = (await client.call_tool('food_log', {'food': food, 'idempotency_key': 'mcp-food-1'})).structured_content
self.assertEqual(first['entry']['id'], again['entry']['id'])
entry_id = first['entry']['id']
updated = (await client.call_tool('food_update_entry', {'entry_id': entry_id,
'changes': {'amount': '100'}})).structured_content
self.assertEqual(updated['entry']['calories_kcal'], '350.000')
image_result = (await client.call_tool('food_attach_image', {'entry_id': entry_id,
'image_base64': encoded, 'mime_type': 'image/png', 'filename': 'meal.png',
'idempotency_key': 'mcp-image-1'})).structured_content
self.assertTrue(image_result['success'])
combined_food = {'meal': 'lunch', 'amount': '1', 'unit': 'g',
'product_name': 'Estimated meal', 'calories_kcal': '850'}
combined = (await client.call_tool('food_log_with_image', {'food': combined_food,
'image_base64': encoded, 'mime_type': 'image/png', 'idempotency_key': 'mcp-combined-1'})).structured_content
self.assertTrue(combined['attachment_added'])
partial = (await client.call_tool('food_log_with_image', {'food': combined_food,
'image_base64': 'bad!', 'mime_type': 'image/png', 'idempotency_key': 'mcp-combined-2'})).structured_content
self.assertTrue(partial['entry_created'])
self.assertFalse(partial['attachment_added'])
self.assertTrue((await client.call_tool('food_log', {'food': {'meal': 'lunch', 'amount': '0',
'unit': 'g', 'product_name': 'Invalid'}})).is_error)
self.assertTrue((await client.call_tool('food_update_entry', {'entry_id': str(self.b_entry.api_id),
'changes': {'meal': 'dinner'}})).is_error)
self.assertTrue((await client.call_tool('food_attach_image', {'entry_id': str(self.b_entry.api_id),
'image_base64': encoded, 'mime_type': 'image/png'})).is_error)
self.assertTrue((await client.call_tool('food_delete_entry', {'entry_id': str(self.b_entry.api_id)})).is_error)
self.assertTrue((await client.call_tool('food_log', {'food': {'meal': 'dinner', 'amount': '1',
'unit': 'g', 'product_id': str(self.b_product.api_id)}})).is_error)
await client.call_tool('food_delete_entry', {'entry_id': entry_id})
await client.call_tool('food_delete_entry', {'entry_id': combined['entry']['id']})
await client.call_tool('food_delete_entry', {'entry_id': partial['entry']['id']})
self.assertEqual((await client.call_tool('inventory_list')).structured_content['count'], 0)
self.assertEqual((await client.call_tool('inventory_search', {'query': 'Private'})).structured_content['count'], 0)
args = {'product_id': str(self.a_product.api_id), 'quantity': 2, 'location': 'Pantry',
'idempotency_key': 'mcp-item-1'}
item = (await client.call_tool('inventory_add', args)).structured_content['item']
again = (await client.call_tool('inventory_add', args)).structured_content['item']
self.assertEqual(item['id'], again['id'])
self.assertEqual((await client.call_tool('inventory_search', {'query': 'Pasta'})).structured_content['count'], 1)
changed = (await client.call_tool('inventory_update', {'item_id': item['id'],
'changes': {'quantity': 3}})).structured_content
self.assertEqual(changed['item']['quantity'], 3)
self.assertTrue((await client.call_tool('inventory_update', {'item_id': str(self.b_item.api_id),
'changes': {'quantity': 3}})).is_error)
self.assertTrue((await client.call_tool('inventory_remove', {'item_id': str(self.b_item.api_id)})).is_error)
await client.call_tool('inventory_remove', {'item_id': item['id']})
asyncio.run(self._client(self.secret, check))
async def other(client):
self.assertEqual((await client.call_tool('food_get_today')).structured_content['meals']['lunch'][0]['name'], 'Private rice')
self.assertEqual((await client.call_tool('inventory_list')).structured_content['count'], 1)
asyncio.run(self._client(self.b_secret, other))