Co-authored-by: b3nw <189466+b3nw@users.noreply.github.com>
This commit is contained in:
1 parent
725d323b96
commit
feae6dedfb
9 files changed
+178
-53
No files matched your search
+108
-2
@@ -1,10 +1,11 @@
|
||||
"""Tests for NpmClient."""
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from httpx import Response
|
||||
|
||||
from npm_mcp.client import NpmClient
|
||||
from npm_mcp.exceptions import NpmApiError, NpmAuthenticationError, NpmConnectionError
|
||||
from npm_mcp.exceptions import NpmApiError, NpmAuthenticationError
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -253,3 +254,108 @@ class TestNpmClientEndpoints:
|
||||
|
||||
msg = str(exc_info.value)
|
||||
assert msg.count("API error") == 0, f"Client should not prefix error: {msg}"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_created_certificate():
|
||||
"""Mock response for created certificate."""
|
||||
return {
|
||||
"id": 7,
|
||||
"created_on": "2024-01-15T10:00:00Z",
|
||||
"modified_on": "2024-01-15T10:00:00Z",
|
||||
"owner_user_id": 1,
|
||||
"provider": "letsencrypt",
|
||||
"nice_name": "app.example.com",
|
||||
"domain_names": ["app.example.com"],
|
||||
"expires_on": "2024-04-15T10:00:00Z",
|
||||
"meta": {"dns_challenge": False},
|
||||
}
|
||||
|
||||
|
||||
def _certificate_payload(httpx_mock) -> dict:
|
||||
"""Return the JSON payload sent to POST /nginx/certificates."""
|
||||
requests = [
|
||||
r for r in httpx_mock.get_requests(method="POST") if r.url.path == "/api/nginx/certificates"
|
||||
]
|
||||
assert len(requests) == 1
|
||||
return json.loads(requests[0].content)
|
||||
|
||||
|
||||
class TestNpmClientCertificates:
|
||||
"""Test certificate creation (issue #17: NPM 2.x meta schema)."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _mock_api(self, httpx_mock, mock_token_response, mock_created_certificate):
|
||||
httpx_mock.add_response(
|
||||
method="POST",
|
||||
url="http://localhost:81/api/tokens",
|
||||
json=mock_token_response,
|
||||
)
|
||||
httpx_mock.add_response(
|
||||
method="POST",
|
||||
url="http://localhost:81/api/nginx/certificates",
|
||||
json=mock_created_certificate,
|
||||
status_code=201,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _client() -> NpmClient:
|
||||
return NpmClient(
|
||||
base_url="http://localhost:81/api",
|
||||
identity="test@test.com",
|
||||
secret="password",
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_certificate_payload(self, httpx_mock):
|
||||
"""Payload must only contain meta keys accepted by NPM 2.x."""
|
||||
async with self._client() as client:
|
||||
await client.create_certificate(domain_names=["app.example.com"])
|
||||
|
||||
payload = _certificate_payload(httpx_mock)
|
||||
assert payload == {
|
||||
"domain_names": ["app.example.com"],
|
||||
"meta": {"dns_challenge": False},
|
||||
"provider": "letsencrypt",
|
||||
}
|
||||
assert "letsencrypt_email" not in payload["meta"]
|
||||
assert "letsencrypt_agree" not in payload["meta"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_certificate_with_deprecated_email(self, httpx_mock):
|
||||
"""Deprecated email argument is accepted but never sent."""
|
||||
async with self._client() as client:
|
||||
await client.create_certificate(
|
||||
domain_names=["app.example.com"], email="test@example.com"
|
||||
)
|
||||
|
||||
payload = _certificate_payload(httpx_mock)
|
||||
assert "letsencrypt_email" not in payload["meta"]
|
||||
assert "email" not in payload["meta"]
|
||||
assert "letsencrypt_email" not in payload
|
||||
assert "email" not in payload
|
||||
assert "test@example.com" not in json.dumps(payload)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_certificate_dns_challenge(self, httpx_mock):
|
||||
"""dns_challenge flag is forwarded in meta."""
|
||||
async with self._client() as client:
|
||||
await client.create_certificate(domain_names=["app.example.com"], dns_challenge=True)
|
||||
|
||||
payload = _certificate_payload(httpx_mock)
|
||||
assert payload["meta"] == {"dns_challenge": True}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_certificate_response_parsing(self):
|
||||
"""Response is parsed into a Certificate model."""
|
||||
async with self._client() as client:
|
||||
cert = await client.create_certificate(domain_names=["app.example.com"])
|
||||
|
||||
assert cert.id == 7
|
||||
assert cert.domain_names == ["app.example.com"]
|
||||
assert cert.provider == "letsencrypt"
|
||||
assert cert.nice_name == "app.example.com"
|
||||
assert cert.owner_user_id == 1
|
||||
assert cert.expires_on is not None
|
||||
assert cert.expires_on.strftime("%Y-%m-%d") == "2024-04-15"
|
||||
assert cert.meta == {"dns_challenge": False}
|
||||
+5
-5
@@ -10,15 +10,15 @@ from npm_mcp.logs import (
|
||||
)
|
||||
|
||||
SAMPLE_ACCESS_LOG = (
|
||||
'[22/May/2025:10:00:01 +0000] - 200 200 - GET https app.example.com'
|
||||
"[22/May/2025:10:00:01 +0000] - 200 200 - GET https app.example.com"
|
||||
' "/" [Client 10.0.0.1] [Length 1542] "Mozilla/5.0" "-"\n'
|
||||
'[22/May/2025:10:00:02 +0000] - 301 301 - GET http app.example.com'
|
||||
"[22/May/2025:10:00:02 +0000] - 301 301 - GET http app.example.com"
|
||||
' "/old-path" [Client 10.0.0.2] [Length 0] "curl/7.88" "-"\n'
|
||||
'[22/May/2025:10:00:03 +0000] - 404 404 - GET https app.example.com'
|
||||
"[22/May/2025:10:00:03 +0000] - 404 404 - GET https app.example.com"
|
||||
' "/missing" [Client 10.0.0.1] [Length 548] "Mozilla/5.0" "-"\n'
|
||||
'[22/May/2025:10:00:04 +0000] - 200 200 - POST https app.example.com'
|
||||
"[22/May/2025:10:00:04 +0000] - 200 200 - POST https app.example.com"
|
||||
' "/api/data" [Client 10.0.0.3] [Length 256] "python-requests/2.31" "-"\n'
|
||||
'[22/May/2025:10:00:05 +0000] - 502 502 - GET https app.example.com'
|
||||
"[22/May/2025:10:00:05 +0000] - 502 502 - GET https app.example.com"
|
||||
' "/health" [Client 10.0.0.1] [Length 166] "kube-probe/1.28" "-"\n'
|
||||
)
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@ class TestMultiInstance:
|
||||
|
||||
def test_multi_instance_from_dict(self):
|
||||
s = Settings(
|
||||
identity="ignored", secret="ignored",
|
||||
identity="ignored",
|
||||
secret="ignored",
|
||||
instances={
|
||||
"prod": {"api_url": "http://prod:81/api", "identity": "a@b", "secret": "p"},
|
||||
"staging": {"api_url": "http://staging:81/api", "identity": "c@d", "secret": "s"},
|
||||
@@ -28,7 +29,8 @@ class TestMultiInstance:
|
||||
|
||||
def test_default_instance_is_first(self):
|
||||
s = Settings(
|
||||
identity="i", secret="s",
|
||||
identity="i",
|
||||
secret="s",
|
||||
instances={
|
||||
"alpha": {"api_url": "http://a:81/api", "identity": "x", "secret": "y"},
|
||||
"beta": {"api_url": "http://b:81/api", "identity": "x", "secret": "y"},
|
||||
@@ -49,7 +51,8 @@ class TestMultiInstance:
|
||||
|
||||
def test_instance_proxy_defaults(self):
|
||||
s = Settings(
|
||||
identity="i", secret="s",
|
||||
identity="i",
|
||||
secret="s",
|
||||
instances={
|
||||
"prod": {
|
||||
"api_url": "http://p:81/api",
|
||||
@@ -76,7 +79,8 @@ class TestMultiInstance:
|
||||
def test_missing_required_field_raises(self):
|
||||
with pytest.raises(ValueError, match="missing required field"):
|
||||
Settings(
|
||||
identity="i", secret="s",
|
||||
identity="i",
|
||||
secret="s",
|
||||
instances={"bad": {"identity": "a", "secret": "b"}},
|
||||
)
|
||||
|
||||
@@ -88,28 +92,46 @@ class TestMultiInstance:
|
||||
# Invalid api_url type (int)
|
||||
with pytest.raises(ValueError, match="must be a string"):
|
||||
Settings(
|
||||
identity="i", secret="s",
|
||||
instances={"site1": {"api_url": 123, "identity": "a", "secret": "b"}}
|
||||
identity="i",
|
||||
secret="s",
|
||||
instances={"site1": {"api_url": 123, "identity": "a", "secret": "b"}},
|
||||
)
|
||||
|
||||
# Invalid log_dir type (list)
|
||||
with pytest.raises(ValueError, match="must be a string"):
|
||||
Settings(
|
||||
identity="i", secret="s",
|
||||
instances={"site1": {"api_url": "http://npm:81/api", "identity": "a", "secret": "b", "log_dir": []}}
|
||||
identity="i",
|
||||
secret="s",
|
||||
instances={
|
||||
"site1": {
|
||||
"api_url": "http://npm:81/api",
|
||||
"identity": "a",
|
||||
"secret": "b",
|
||||
"log_dir": [],
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Invalid proxy_defaults type (str)
|
||||
with pytest.raises(ValueError, match="must be an object"):
|
||||
Settings(
|
||||
identity="i", secret="s",
|
||||
instances={"site1": {"api_url": "http://npm:81/api", "identity": "a", "secret": "b", "proxy_defaults": "invalid"}}
|
||||
identity="i",
|
||||
secret="s",
|
||||
instances={
|
||||
"site1": {
|
||||
"api_url": "http://npm:81/api",
|
||||
"identity": "a",
|
||||
"secret": "b",
|
||||
"proxy_defaults": "invalid",
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
def test_default_instance_selection(self):
|
||||
# Explicit default instance configuration
|
||||
s = Settings(
|
||||
identity="i", secret="s",
|
||||
identity="i",
|
||||
secret="s",
|
||||
default_instance="beta",
|
||||
instances={
|
||||
"alpha": {"api_url": "http://a:81/api", "identity": "x", "secret": "y"},
|
||||
@@ -123,7 +145,8 @@ class TestMultiInstance:
|
||||
# Explicit default instance configured but not present in instances list
|
||||
with pytest.raises(ValueError, match="is not in configured instances"):
|
||||
Settings(
|
||||
identity="i", secret="s",
|
||||
identity="i",
|
||||
secret="s",
|
||||
default_instance="gamma",
|
||||
instances={
|
||||
"alpha": {"api_url": "http://a:81/api", "identity": "x", "secret": "y"},
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
"""Tests for the server module and its FastMCP initialization."""
|
||||
|
||||
import pytest
|
||||
|
||||
def test_server_imports():
|
||||
"""Ensure that the server and mcp instance can be imported without errors."""
|
||||
from npm_mcp.server import mcp
|
||||
|
||||
assert mcp is not None
|
||||
assert mcp.name == "npm-mcp"
|
||||
|
||||
|
||||
def test_server_http_app():
|
||||
"""Ensure that the http_app can be retrieved without AttributeError."""
|
||||
from npm_mcp.server import mcp
|
||||
|
||||
app = mcp.http_app()
|
||||
assert app is not None
|
||||
Reference in new issue
Block a user