feat: unify NocoBase API key authentication
This commit is contained in:
+60
-1
@@ -4,7 +4,7 @@ import subprocess
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from nocobase_crm_mcp.nb_client import NbClient, NbCommandError
|
||||
from nocobase_crm_mcp.nb_client import NbAuthenticationError, NbClient, NbCommandError
|
||||
|
||||
|
||||
class NbClientTests(unittest.TestCase):
|
||||
@@ -59,6 +59,42 @@ class NbClientTests(unittest.TestCase):
|
||||
self.assertTrue(calls[0]["text"])
|
||||
self.assertFalse(calls[0]["check"])
|
||||
|
||||
def test_api_base_url_and_token_override_saved_env_auth_for_every_api_command(self) -> None:
|
||||
commands: list[list[str]] = []
|
||||
|
||||
def runner(cmd, **kwargs):
|
||||
commands.append(cmd)
|
||||
return subprocess.CompletedProcess(cmd, 0, stdout='{"data":[]}', stderr="")
|
||||
|
||||
client = NbClient(
|
||||
env_name="nb-mcp",
|
||||
api_base_url="https://nb.example/api/",
|
||||
api_token="long-lived-key",
|
||||
runner=runner,
|
||||
)
|
||||
client.list_records("customers")
|
||||
client.list_collection_fields("customers")
|
||||
|
||||
for command in commands:
|
||||
self.assertEqual(command[command.index("--api-base-url") + 1], "https://nb.example/api")
|
||||
self.assertEqual(command[command.index("--token") + 1], "long-lived-key")
|
||||
|
||||
def test_health_uses_token_aware_resource_probe(self) -> None:
|
||||
commands: list[list[str]] = []
|
||||
|
||||
def runner(cmd, **kwargs):
|
||||
commands.append(cmd)
|
||||
return subprocess.CompletedProcess(cmd, 0, stdout='{"data":[],"meta":{"count":0}}', stderr="")
|
||||
|
||||
health = NbClient(api_token="long-lived-key", runner=runner).health("customers", "id")
|
||||
|
||||
self.assertTrue(health["ok"])
|
||||
self.assertEqual(commands[0][:4], ["nb", "api", "resource", "list"])
|
||||
self.assertEqual(commands[0][commands[0].index("--resource") + 1], "customers")
|
||||
self.assertEqual(commands[0][commands[0].index("--fields") + 1], "id")
|
||||
self.assertEqual(commands[0][commands[0].index("--page-size") + 1], "1")
|
||||
self.assertEqual(commands[0][commands[0].index("--token") + 1], "long-lived-key")
|
||||
|
||||
def test_get_record_and_query_records_use_expected_commands(self) -> None:
|
||||
commands: list[list[str]] = []
|
||||
|
||||
@@ -229,6 +265,29 @@ class NbClientTests(unittest.TestCase):
|
||||
self.assertNotIn("super-secret-token", message)
|
||||
self.assertIn("<redacted>", message)
|
||||
|
||||
def test_authentication_failures_have_a_specific_error_type(self) -> None:
|
||||
cases = (
|
||||
"Request failed with status 401",
|
||||
'{"errors":[{"code":"EXPIRED_TOKEN"}]}',
|
||||
)
|
||||
|
||||
for stderr in cases:
|
||||
with self.subTest(stderr=stderr):
|
||||
def runner(cmd, **kwargs):
|
||||
return subprocess.CompletedProcess(cmd, 1, stdout="", stderr=stderr)
|
||||
|
||||
with self.assertRaises(NbAuthenticationError):
|
||||
NbClient(api_token="secret", runner=runner).list_records("customers")
|
||||
|
||||
def test_plain_forbidden_error_remains_an_acl_command_failure(self) -> None:
|
||||
def runner(cmd, **kwargs):
|
||||
return subprocess.CompletedProcess(cmd, 1, stdout="", stderr="Request failed with status 403: No permissions")
|
||||
|
||||
with self.assertRaises(NbCommandError) as caught:
|
||||
NbClient(api_token="secret", runner=runner).list_records("customers")
|
||||
|
||||
self.assertNotIsInstance(caught.exception, NbAuthenticationError)
|
||||
|
||||
def test_invalid_json_output_raises_command_error(self) -> None:
|
||||
def runner(cmd, **kwargs):
|
||||
return subprocess.CompletedProcess(cmd, 0, stdout="HTTP 200", stderr="")
|
||||
|
||||
Reference in New Issue
Block a user