handlers_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package appproxy
  2. import (
  3. "encoding/json"
  4. "net/http/httptest"
  5. "testing"
  6. )
  7. // TestHandleUserAppsContainers checks the desktop is told whether the host
  8. // runs containers, which decides if it shows the Container Apps category
  9. func TestHandleUserAppsContainers(t *testing.T) {
  10. cases := []struct {
  11. name string
  12. available func() bool
  13. want bool
  14. }{
  15. {"no callback", nil, false},
  16. {"docker missing", func() bool { return false }, false},
  17. {"docker usable", func() bool { return true }, true},
  18. }
  19. for _, c := range cases {
  20. t.Run(c.name, func(t *testing.T) {
  21. m := newTestManager(t, nil)
  22. m.opts.ContainersAvailable = c.available
  23. if _, err := m.Save(pathEndpoint("grafana", "127.0.0.1:3000"), true, "admin", "nas.local"); err != nil {
  24. t.Fatalf("create: %v", err)
  25. }
  26. req := httptest.NewRequest("GET", "/system/appproxy/apps", nil)
  27. req.Header.Set("X-Test-User", "alice")
  28. rec := httptest.NewRecorder()
  29. m.HandleUserApps(rec, req)
  30. var resp struct {
  31. Apps []AppInfo
  32. IsAdmin bool
  33. Containers bool
  34. }
  35. if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
  36. t.Fatalf("decode %q: %v", rec.Body.String(), err)
  37. }
  38. if resp.Containers != c.want {
  39. t.Errorf("Containers = %v, want %v", resp.Containers, c.want)
  40. }
  41. if len(resp.Apps) != 1 || resp.Apps[0].Slug != "grafana" {
  42. t.Errorf("Apps = %+v, want the grafana app", resp.Apps)
  43. }
  44. })
  45. }
  46. }