@@ -17,8 +17,10 @@ package server
1717import (
1818 "bytes"
1919 "context"
20+ "encoding/json"
2021 "fmt"
2122 "net/http"
23+ "reflect"
2224 "testing"
2325
2426 "github.com/goccy/go-yaml"
@@ -225,3 +227,104 @@ func TestAdminDeleteEndpoint(t *testing.T) {
225227 })
226228 }
227229}
230+
231+ func TestAdminGetEndpoint (t * testing.T ) {
232+ mockSources := map [string ]sources.Source {"test-source" : testutils.MockSource {MockSourceConfig : testutils.MockSourceConfig {Foo : "foo" }}}
233+ mockAuthServices := map [string ]auth.AuthService {"test-auth-service" : testutils.MockAuthService {MockAuthServiceConfig : testutils.MockAuthServiceConfig {Foo : "foo" }}}
234+ mockEmbeddingModel := map [string ]embeddingmodels.EmbeddingModel {"test-embedding-model" : testutils.MockEmbeddingModel {MockEmbeddingModelConfig : testutils.MockEmbeddingModelConfig {Foo : "foo" }}}
235+ mockTool := map [string ]tools.Tool {"test-tool" : testutils.MockTool {MockToolConfig : testutils.MockToolConfig {Foo : "foo" }}}
236+ mockToolset := map [string ]tools.Toolset {"test-toolset" : tools.Toolset {ToolsetConfig : tools.ToolsetConfig {ToolNames : []string {"test-tool" }}}}
237+ mockPrompt := map [string ]prompts.Prompt {"test-prompt" : testutils.MockPrompt {MockPromptConfig : testutils.MockPromptConfig {Foo : "foo" }}}
238+ r , shutdown := setUpServer (t , "admin" , mockSources , mockAuthServices , mockEmbeddingModel , mockTool , mockToolset , mockPrompt , map [string ]prompts.Promptset {})
239+ defer shutdown ()
240+ ts := runServer (r , false )
241+ defer ts .Close ()
242+
243+ tests := []struct {
244+ name string
245+ kind string
246+ resourceName string
247+ want any
248+ expectedStatusCode int
249+ }{
250+ {
251+ name : "Get Source - Success" ,
252+ kind : "source" ,
253+ resourceName : "test-source" ,
254+ want : mockSources ,
255+ expectedStatusCode : http .StatusOK ,
256+ },
257+ {
258+ name : "Get Auth Service - Success" ,
259+ kind : "authService" ,
260+ resourceName : "test-auth-service" ,
261+ want : mockAuthServices ,
262+ expectedStatusCode : http .StatusOK ,
263+ },
264+ {
265+ name : "Get Embedding Model - Success" ,
266+ kind : "embeddingModel" ,
267+ resourceName : "test-embedding-model" ,
268+ want : mockEmbeddingModel ,
269+ expectedStatusCode : http .StatusOK ,
270+ },
271+ {
272+ name : "Get Tool - Success" ,
273+ kind : "tool" ,
274+ resourceName : "test-tool" ,
275+ want : mockTool ,
276+ expectedStatusCode : http .StatusOK ,
277+ },
278+ {
279+ name : "Get Toolset - Success" ,
280+ kind : "toolset" ,
281+ resourceName : "test-toolset" ,
282+ want : mockToolset ,
283+ expectedStatusCode : http .StatusOK ,
284+ },
285+ {
286+ name : "Get Prompt - Success" ,
287+ kind : "prompt" ,
288+ resourceName : "test-prompt" ,
289+ want : mockPrompt ,
290+ expectedStatusCode : http .StatusOK ,
291+ },
292+ {
293+ name : "Get Non-existent Primitive - Not Found" ,
294+ kind : "source" ,
295+ resourceName : "non-existent-source" ,
296+ expectedStatusCode : http .StatusNotFound ,
297+ },
298+ {
299+ name : "Get with Invalid Kind - Bad Request" ,
300+ kind : "invalidKind" ,
301+ resourceName : "some-name" ,
302+ expectedStatusCode : http .StatusBadRequest ,
303+ },
304+ }
305+
306+ for _ , tt := range tests {
307+ t .Run (tt .name , func (t * testing.T ) {
308+ resp , body , err := runRequest (ts , http .MethodGet , fmt .Sprintf ("/%s/%s" , tt .kind , tt .resourceName ), nil , nil )
309+ if err != nil {
310+ t .Fatalf ("unexpected error during request: %s" , err )
311+ }
312+ if resp .StatusCode != tt .expectedStatusCode {
313+ t .Fatalf ("response status code is not %d, got %d, %s" , tt .expectedStatusCode , resp .StatusCode , string (body ))
314+ }
315+ if tt .expectedStatusCode == http .StatusOK {
316+ var got map [string ]any
317+ if err := json .Unmarshal (body , & got ); err != nil {
318+ t .Fatalf ("error unmarshaling response body" )
319+ }
320+ want , err := json .Marshal (tt .want )
321+ if err != nil {
322+ t .Fatalf ("error marshaling want struct" )
323+ }
324+ if ! reflect .DeepEqual (got , want ) {
325+ t .Fatalf ("unexpected output: got %+v, want %+v" , got , want )
326+ }
327+ }
328+ })
329+ }
330+ }
0 commit comments