package glpi import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "time" ) func TestClientDiscoversAndReadsKnowledgeBase(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.URL.Path { case "/api.php/token": _ = json.NewEncoder(w).Encode(map[string]any{"access_token": "token", "expires_in": 3600}) case "/api.php/doc.json": _ = json.NewEncoder(w).Encode(map[string]any{"paths": map[string]any{"/api.php/v2.3/Knowledge/KnowbaseItem": map[string]any{"get": map[string]any{}}}}) case "/api.php/v2.3/Knowledge/KnowbaseItem": _ = json.NewEncoder(w).Encode(map[string]any{"data": []map[string]any{{"id": 5, "name": "VPN", "answer": "
Gateway prüfen
", "knowbase_category": map[string]any{"id": 9}, "date_mod": "now"}}}) case "/api.php/v2.3/Dropdowns/ITILCategory": _ = json.NewEncoder(w).Encode([]map[string]any{{"id": 7, "name": "Netzwerk", "completename": "Infra > Netzwerk", "knowbase_category": map[string]any{"id": 9}}}) default: http.NotFound(w, r) } })) defer server.Close() client := New(server.URL, "v2.3", "id", "secret", "user", "password", time.Second) path, err := client.DiscoverKnowledgeBasePath(context.Background(), "auto") if err != nil { t.Fatal(err) } if path != "/Knowledge/KnowbaseItem" { t.Fatalf("unexpected path %q", path) } items, err := client.ListKnowledgeBaseItems(context.Background(), path, 100, "") if err != nil { t.Fatal(err) } if len(items) != 1 || items[0].ID != 5 || len(items[0].CategoryIDs) != 1 || items[0].CategoryIDs[0] != 9 { t.Fatalf("unexpected items: %+v", items) } categories, err := client.GetCategories(context.Background()) if err != nil { t.Fatal(err) } if len(categories) != 1 || categories[0].KnowbaseCategoryID != 9 { t.Fatalf("unexpected categories: %+v", categories) } }