wastebin-mcp-go/e2e_mcp_test.go
2026-07-01 09:54:51 +00:00

380 lines
12 KiB
Go

//go:build e2e
package main
import (
"context"
"io"
"net/http"
"os"
"strings"
"testing"
"time"
)
func TestBasicPaste(t *testing.T) {
wastebinURL := os.Getenv("WASTEBIN_SERVER_URL")
if wastebinURL == "" {
t.Skip("WASTEBIN_SERVER_URL not set")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
session, stderr, _ := startMCPSession(ctx, t, wastebinURL)
// Verify tool listing works and contains create_paste
tool := findCreatePasteTool(ctx, t, session, stderr)
t.Logf("found tool: %s", tool.Name)
response := requirePasteResponse(ctx, t, session, map[string]any{
"content": "Hello, World! Basic paste test",
}, stderr, "basic paste")
// Verify response has expected fields
if response.Hostname == "" {
t.Fatalf("hostname is empty\nresponse: %#v\nstderr:\n%s", response, stderr.String())
}
if response.ID == "" {
t.Fatalf("id is empty\nresponse: %#v\nstderr:\n%s", response, stderr.String())
}
if response.URL == "" {
t.Fatalf("url is empty\nresponse: %#v\nstderr:\n%s", response, stderr.String())
}
if response.Raw == "" {
t.Fatalf("raw is empty\nresponse: %#v\nstderr:\n%s", response, stderr.String())
}
// Verify raw URL returns actual content via HTTP
rawURL := response.Hostname + response.Raw
t.Logf("verifying raw content at: %s", rawURL)
httpCtx, httpCancel := context.WithTimeout(ctx, 10*time.Second)
defer httpCancel()
req, err := http.NewRequestWithContext(httpCtx, http.MethodGet, rawURL, nil)
if err != nil {
fatalOrWarn(t, "create raw URL request: %v", err)
}
httpResp, err := http.DefaultClient.Do(req)
if err != nil {
fatalOrWarn(t, "GET raw URL failed: %v", err)
}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
fatalOrWarn(t, "raw URL status = %d, want %d", httpResp.StatusCode, http.StatusOK)
}
body, err := io.ReadAll(httpResp.Body)
if err != nil {
fatalOrWarn(t, "read raw response body: %v", err)
}
if !strings.Contains(string(body), "Hello, World!") {
fatalOrWarn(t, "raw content does not contain expected text: got %q", string(body))
}
t.Logf("raw content verified successfully (status %d, %d bytes)", httpResp.StatusCode, len(body))
t.Log("basic paste test verified")
}
// fatalOrWarn is a test helper that calls t.Fatalf by default in E2E tests.
// Set E2E_RAW_RETRIEVAL_WARN=true to downgrade failures to warnings for
// local runs where the paste may have expired.
func fatalOrWarn(t *testing.T, format string, args ...any) {
t.Helper()
if os.Getenv("E2E_RAW_RETRIEVAL_WARN") == "true" {
t.Logf("WARN: "+format, args...)
} else {
t.Fatalf(format, args...)
}
}
func TestPasteWithExtension(t *testing.T) {
wastebinURL := os.Getenv("WASTEBIN_SERVER_URL")
if wastebinURL == "" {
t.Skip("WASTEBIN_SERVER_URL not set")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
session, stderr, _ := startMCPSession(ctx, t, wastebinURL)
response := requirePasteResponse(ctx, t, session, map[string]any{
"content": "package main\n\nfunc main() {\n\tprintln(\"hello\")\n}",
"extension": "go",
}, stderr, "paste with .go extension")
// Response should NOT have markdown_rendered or hint
if response.MarkdownRendered != "" {
t.Fatalf("markdown_rendered should be empty for .go extension, got %q\nresponse: %#v\nstderr:\n%s",
response.MarkdownRendered, response, stderr.String())
}
if response.Hint != "" {
t.Fatalf("hint should be empty for .go extension, got %q\nresponse: %#v\nstderr:\n%s",
response.Hint, response, stderr.String())
}
t.Log("paste with extension verified")
}
func TestPasteMarkdownExtension(t *testing.T) {
wastebinURL := os.Getenv("WASTEBIN_SERVER_URL")
if wastebinURL == "" {
t.Skip("WASTEBIN_SERVER_URL not set")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
session, stderr, _ := startMCPSession(ctx, t, wastebinURL)
response := requirePasteResponse(ctx, t, session, map[string]any{
"content": "# Hello\n\nThis is a markdown paste.",
"extension": "md",
}, stderr, "paste with .md extension")
// Response MUST have markdown_rendered field in the response body
if response.MarkdownRendered == "" {
t.Fatalf("markdown_rendered should be present for .md extension\nresponse: %#v\nstderr:\n%s",
response, stderr.String())
}
// Verify markdown rendered URL is valid
if !strings.HasPrefix(response.MarkdownRendered, "/md/") {
t.Fatalf("markdown_rendered should start with /md/, got %q\nresponse: %#v\nstderr:\n%s",
response.MarkdownRendered, response, stderr.String())
}
// Verify the rendered markdown URL returns actual HTML content via HTTP
if !t.Failed() {
renderedURL := response.Hostname + response.MarkdownRendered
t.Logf("verifying rendered markdown content at: %s", renderedURL)
httpCtx, httpCancel := context.WithTimeout(ctx, 10*time.Second)
defer httpCancel()
req, err := http.NewRequestWithContext(httpCtx, http.MethodGet, renderedURL, nil)
if err != nil {
t.Fatalf("create rendered markdown request: %v\nstderr:\n%s", err, stderr.String())
}
httpResp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("GET rendered markdown URL failed: %v\nstderr:\n%s", err, stderr.String())
}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
t.Fatalf("rendered markdown URL status = %d, want %d\nstderr:\n%s",
httpResp.StatusCode, http.StatusOK, stderr.String())
}
body, err := io.ReadAll(httpResp.Body)
if err != nil {
t.Fatalf("read rendered markdown body: %v\nstderr:\n%s", err, stderr.String())
}
if !strings.Contains(string(body), "<h1>Hello</h1>") && !strings.Contains(string(body), "<h1>Hello") {
t.Fatalf("rendered markdown does not contain expected heading \"Hello\"\nrendered content:\n%s", string(body))
}
t.Logf("rendered markdown content verified (status %d, %d bytes)", httpResp.StatusCode, len(body))
}
t.Log("paste with markdown extension verified")
}
func TestPasteWithTitle(t *testing.T) {
wastebinURL := os.Getenv("WASTEBIN_SERVER_URL")
if wastebinURL == "" {
t.Skip("WASTEBIN_SERVER_URL not set")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
session, stderr, _ := startMCPSession(ctx, t, wastebinURL)
response := requirePasteResponse(ctx, t, session, map[string]any{
"content": "Content with a title",
"title": "My Test Paste",
}, stderr, "paste with title")
// Verify response is valid
if response.ID == "" {
t.Fatalf("id is empty for titled paste\nresponse: %#v\nstderr:\n%s", response, stderr.String())
}
t.Log("paste with title verified")
}
func TestPasteBurnAfterReading(t *testing.T) {
wastebinURL := os.Getenv("WASTEBIN_SERVER_URL")
if wastebinURL == "" {
t.Skip("WASTEBIN_SERVER_URL not set")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
session, stderr, _ := startMCPSession(ctx, t, wastebinURL)
// Burn-after-reading paste should be created successfully
response := requirePasteResponse(ctx, t, session, map[string]any{
"content": "This paste will self-destruct",
"burn_after_reading": true,
}, stderr, "burn-after-reading paste")
if response.ID == "" {
t.Fatalf("id is empty for burn-after-reading paste\nresponse: %#v\nstderr:\n%s", response, stderr.String())
}
t.Log("burn after reading paste verified")
}
func TestPastePasswordProtected(t *testing.T) {
wastebinURL := os.Getenv("WASTEBIN_SERVER_URL")
if wastebinURL == "" {
t.Skip("WASTEBIN_SERVER_URL not set")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
session, stderr, _ := startMCPSession(ctx, t, wastebinURL)
response := requirePasteResponse(ctx, t, session, map[string]any{
"content": "Secret content",
"password": "my-secret-password",
}, stderr, "password-protected paste")
// Response MUST have password_hint field
if response.PasswordHint == "" {
t.Fatalf("password_hint should be present for password-protected paste\nresponse: %#v\nstderr:\n%s",
response, stderr.String())
}
if !strings.Contains(response.PasswordHint, "Wastebin-Password") {
t.Fatalf("password_hint should mention Wastebin-Password header\npassword_hint: %q\nstderr:\n%s",
response.PasswordHint, stderr.String())
}
if strings.Contains(response.PasswordHint, "?password=") {
t.Fatalf("password_hint must not recommend query-parameter password transport (URL secrets are logged)\npassword_hint: %q\nstderr:\n%s",
response.PasswordHint, stderr.String())
}
t.Log("password protected paste verified")
}
func TestPasteNoExtension(t *testing.T) {
wastebinURL := os.Getenv("WASTEBIN_SERVER_URL")
if wastebinURL == "" {
t.Skip("WASTEBIN_SERVER_URL not set")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
session, stderr, _ := startMCPSession(ctx, t, wastebinURL)
response := requirePasteResponse(ctx, t, session, map[string]any{
"content": "Content without extension",
}, stderr, "paste without extension")
// Response SHOULD have hint field (fuzzy match hint)
if response.Hint == "" {
t.Logf("hint is empty for extensionless paste (non-fatal)\nresponse: %#v", response)
} else if !strings.Contains(strings.ToLower(response.Hint), "extension") {
t.Fatalf("hint should mention 'extension', got %q\nresponse: %#v\nstderr:\n%s",
response.Hint, response, stderr.String())
}
t.Log("paste without extension verified")
}
func TestPasteBothContentAndFilePath_FileModeDisabled(t *testing.T) {
// NOTE: The current e2e environment disables file mode (no ALLOWED_PATHS
// configured), so file_path is excluded from the MCP tool schema entirely.
// This test verifies JSON Schema rejection of an unexpected parameter
// ("additional properties") — NOT mutual exclusivity between content and
// file_path. When file mode IS enabled, file_path appears in the schema
// and both parameters can coexist; a dedicated e2e test for that scenario
// is a future gap (see TestBuildPasteSchemaFileModeEnabled in mcp_test.go
// for the unit-level schema test).
wastebinURL := os.Getenv("WASTEBIN_SERVER_URL")
if wastebinURL == "" {
t.Skip("WASTEBIN_SERVER_URL not set")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
session, stderr, _ := startMCPSession(ctx, t, wastebinURL)
// Both content and file_path — file_path is not in the schema (file mode
// disabled), so the schema rejects it as an unexpected property.
result := createPaste(ctx, t, session, map[string]any{
"content": "Some content",
"file_path": "/tmp/test.txt",
}, stderr)
if !result.IsError {
t.Fatalf("IsError = false, want true when both content and file_path provided\nresult: %#v\nstderr:\n%s",
result, stderr.String())
}
if len(result.Content) != 1 {
t.Fatalf("content length = %d, want 1\nresult: %#v\nstderr:\n%s",
len(result.Content), result, stderr.String())
}
text := toolText(t, result)
if !strings.Contains(text, "additional properties") {
t.Fatalf("error text should mention unexpected properties, got %q\nstderr:\n%s", text, stderr.String())
}
t.Log("both content and file_path (file mode disabled) error verified")
}
func TestPasteEmptyContent(t *testing.T) {
wastebinURL := os.Getenv("WASTEBIN_SERVER_URL")
if wastebinURL == "" {
t.Skip("WASTEBIN_SERVER_URL not set")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
session, stderr, _ := startMCPSession(ctx, t, wastebinURL)
// Empty content string — should return error
result := createPaste(ctx, t, session, map[string]any{
"content": "",
}, stderr)
if !result.IsError {
t.Fatalf("IsError = false, want true for empty content\nresult: %#v\nstderr:\n%s",
result, stderr.String())
}
text := toolText(t, result)
t.Logf("empty content error text: %q", text)
// The exact error message may vary; just verify we get an error
if strings.TrimSpace(text) == "" {
t.Fatalf("error text is empty for empty content\nstderr:\n%s", stderr.String())
}
t.Log("empty content error verified")
}