import unittest
from unittest.mock import patch, MagicMock
import json
from app import app, unsupported_request_handler
class AppTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
app.config['TESTING'] = True
self.sf_client_patcher = patch('app.sf_client', MagicMock())
self.mock_sf_client = self.sf_client_patcher.start()
def tearDown(self):
self.sf_client_patcher.stop()
@patch('app.llm_model.generate_content')
def test_mcp_gateway_missing_intent_defaults_to_unsupported(self, mock_generate_content):
# Mock LLM to return a valid JSON but without the "intent" key
mock_llm_response = MagicMock()
mock_llm_response.parts = [MagicMock(text=json.dumps({"slots": {}}))]
mock_generate_content.return_value = mock_llm_response
# Make a POST request to the gateway
response = self.app.post('/mcp_gateway',
data=json.dumps({'query': 'some query'}),
content_type='application/json')
# Assertions
self.assertEqual(response.status_code, 200)
response_data = json.loads(response.data)
self.assertEqual(response_data['status'], 'success')
self.assertEqual(response_data['message'], "I'm sorry, I can't help with that request. I am focused on Salesforce related queries.")
if __name__ == '__main__':
unittest.main()