Build and publish / Test and build (darwin) (push) Failing after 1m42s
Build and publish / Test and build (linux) (push) Failing after 1m53s
Build and publish / Test and build (windows) (push) Failing after 2m50s
Build and publish / Build and publish Docker image (push) Skipped
Go rewrite of the Python Emby.MCP server with tool parity (20 tools), plus a streamable-HTTP transport with per-request Emby authentication (Basic / Bearer token passthrough), a multi-arch Docker build, and a Gitea CI workflow.
112 lines
3.0 KiB
Go
112 lines
3.0 KiB
Go
package emby
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func newTestClient(t *testing.T, handler http.HandlerFunc) (*Client, *httptest.Server) {
|
|
t.Helper()
|
|
srv := httptest.NewServer(handler)
|
|
t.Cleanup(srv.Close)
|
|
c := NewClient(srv.URL, Options{
|
|
ClientName: "Emby.MCP for AI",
|
|
ClientVersion: "1.0.2",
|
|
DeviceName: "testhost (linux)",
|
|
DeviceID: "dev-1",
|
|
VerifySSL: true,
|
|
})
|
|
return c, srv
|
|
}
|
|
|
|
func TestAuthenticate(t *testing.T) {
|
|
var gotAuth, gotBody string
|
|
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/Users/AuthenticateByName" {
|
|
t.Errorf("unexpected path %s", r.URL.Path)
|
|
}
|
|
gotAuth = r.Header.Get("X-Emby-Authorization")
|
|
var body map[string]string
|
|
json.NewDecoder(r.Body).Decode(&body)
|
|
gotBody = body["Username"] + "/" + body["Pw"]
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"AccessToken": "tok123",
|
|
"User": map[string]string{"Id": "u1", "Name": "user"},
|
|
})
|
|
})
|
|
res, err := c.Authenticate(context.Background(), "user", "pass")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.AccessToken != "tok123" || res.User.ID != "u1" {
|
|
t.Fatalf("res = %+v", res)
|
|
}
|
|
if gotBody != "user/pass" {
|
|
t.Errorf("body = %q", gotBody)
|
|
}
|
|
if !strings.Contains(gotAuth, `Client="Emby.MCP for AI"`) ||
|
|
!strings.Contains(gotAuth, `DeviceId="dev-1"`) ||
|
|
!strings.Contains(gotAuth, `Version="1.0.2"`) {
|
|
t.Errorf("auth header = %q", gotAuth)
|
|
}
|
|
if !c.UsesPasswordAuth() {
|
|
t.Error("UsesPasswordAuth should be true")
|
|
}
|
|
}
|
|
|
|
func TestTokenSentAfterAuth(t *testing.T) {
|
|
var tok string
|
|
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/Users/AuthenticateByName" {
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"AccessToken": "tok123",
|
|
"User": map[string]string{"Id": "u1"},
|
|
})
|
|
return
|
|
}
|
|
tok = r.Header.Get("X-Emby-Token")
|
|
json.NewEncoder(w).Encode(map[string]any{"Items": []any{}, "TotalRecordCount": 0})
|
|
})
|
|
if _, err := c.Authenticate(context.Background(), "u", "p"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := c.GetLibraryList(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tok != "tok123" {
|
|
t.Errorf("X-Emby-Token = %q", tok)
|
|
}
|
|
}
|
|
|
|
func TestAPIKeyMode(t *testing.T) {
|
|
var tok string
|
|
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
|
|
tok = r.Header.Get("X-Emby-Token")
|
|
json.NewEncoder(w).Encode(map[string]any{"Items": []any{}, "TotalRecordCount": 0})
|
|
})
|
|
c.SetAPIKey("mykey")
|
|
if _, err := c.GetLibraryList(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tok != "mykey" {
|
|
t.Errorf("X-Emby-Token = %q", tok)
|
|
}
|
|
if c.UsesPasswordAuth() {
|
|
t.Error("UsesPasswordAuth should be false for API key")
|
|
}
|
|
}
|
|
|
|
func TestHTTPError(t *testing.T) {
|
|
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "nope", http.StatusUnauthorized)
|
|
})
|
|
_, err := c.GetLibraryList(context.Background())
|
|
if err == nil || !strings.Contains(err.Error(), "401") {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
}
|