endpoint_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package appproxy
  2. import "testing"
  3. func TestNormalizeSlug(t *testing.T) {
  4. tests := []struct {
  5. in string
  6. want string
  7. }{
  8. {"Grafana", "grafana"},
  9. {"my_container.1", "my-container-1"},
  10. {" --Home Assistant-- ", "home-assistant"},
  11. {"/weird//name", "weird-name"},
  12. {"", ""},
  13. }
  14. for _, tt := range tests {
  15. if got := NormalizeSlug(tt.in); got != tt.want {
  16. t.Errorf("NormalizeSlug(%q) = %q, want %q", tt.in, got, tt.want)
  17. }
  18. }
  19. }
  20. func TestEndpointNormalize(t *testing.T) {
  21. base := func() Endpoint {
  22. return Endpoint{Slug: "app", Target: "127.0.0.1:8081", Mode: ModePath, Trusted: true, RequireLogin: true}
  23. }
  24. tests := []struct {
  25. name string
  26. edit func(e *Endpoint)
  27. wantErr bool
  28. check func(t *testing.T, e Endpoint)
  29. }{
  30. {"valid path mode", func(e *Endpoint) {}, false, func(t *testing.T, e Endpoint) {
  31. if e.Name != "app" || e.OpenIn != OpenInFloat {
  32. t.Errorf("defaults not applied: %+v", e)
  33. }
  34. }},
  35. {"path mode needs trust", func(e *Endpoint) { e.Trusted = false }, true, nil},
  36. {"bad slug", func(e *Endpoint) { e.Slug = "Bad Slug" }, true, nil},
  37. {"target without port", func(e *Endpoint) { e.Target = "127.0.0.1" }, true, nil},
  38. {"target scheme stripped", func(e *Endpoint) { e.Target = "http://127.0.0.1:8081/" }, false, func(t *testing.T, e Endpoint) {
  39. if e.Target != "127.0.0.1:8081" {
  40. t.Errorf("Target = %q", e.Target)
  41. }
  42. }},
  43. {"target is arozos", func(e *Endpoint) { e.Target = "localhost:8080" }, true, nil},
  44. {"other host on arozos port", func(e *Endpoint) { e.Target = "10.0.0.5:8080" }, false, nil},
  45. {"unknown mode", func(e *Endpoint) { e.Mode = "magic" }, true, nil},
  46. {"subdomain mode", func(e *Endpoint) {
  47. e.Mode = ModeSubdomain
  48. e.Trusted = false
  49. e.Hostname = "Jelly.Example.com:443"
  50. e.PortalOrigin = "https://nas.example.com/"
  51. e.RewriteRoot = true
  52. }, false, func(t *testing.T, e Endpoint) {
  53. if e.Hostname != "jelly.example.com" || e.PortalOrigin != "https://nas.example.com" || e.RewriteRoot {
  54. t.Errorf("subdomain not normalized: %+v", e)
  55. }
  56. }},
  57. {"subdomain bad hostname", func(e *Endpoint) {
  58. e.Mode = ModeSubdomain
  59. e.Hostname = "not a host"
  60. e.PortalOrigin = "https://nas.example.com"
  61. }, true, nil},
  62. {"subdomain same as portal", func(e *Endpoint) {
  63. e.Mode = ModeSubdomain
  64. e.Hostname = "nas.example.com"
  65. e.PortalOrigin = "https://nas.example.com"
  66. }, true, nil},
  67. {"subdomain portal with path", func(e *Endpoint) {
  68. e.Mode = ModeSubdomain
  69. e.Hostname = "a.example.com"
  70. e.PortalOrigin = "https://nas.example.com/desktop"
  71. }, true, nil},
  72. {"icon injection", func(e *Endpoint) { e.Icon = `x" onerror="alert(1)` }, true, nil},
  73. {"window size clamped", func(e *Endpoint) { e.Width = 5; e.Height = 99999 }, false, func(t *testing.T, e Endpoint) {
  74. if e.Width != minWindowSize || e.Height != maxWindowSize {
  75. t.Errorf("size = %dx%d", e.Width, e.Height)
  76. }
  77. }},
  78. {"groups deduplicated", func(e *Endpoint) { e.AllowedGroups = []string{"a", " a ", "", "b"} }, false, func(t *testing.T, e Endpoint) {
  79. if len(e.AllowedGroups) != 2 {
  80. t.Errorf("groups = %v", e.AllowedGroups)
  81. }
  82. }},
  83. }
  84. for _, tt := range tests {
  85. t.Run(tt.name, func(t *testing.T) {
  86. e := base()
  87. tt.edit(&e)
  88. err := e.Normalize(8080)
  89. if (err != nil) != tt.wantErr {
  90. t.Fatalf("Normalize err = %v, wantErr %v", err, tt.wantErr)
  91. }
  92. if err == nil && tt.check != nil {
  93. tt.check(t, e)
  94. }
  95. })
  96. }
  97. }
  98. func TestEndpointCanAccess(t *testing.T) {
  99. restricted := Endpoint{RequireLogin: true, AllowedGroups: []string{"media"}}
  100. everyone := Endpoint{RequireLogin: true}
  101. public := Endpoint{RequireLogin: false}
  102. member := &Identity{Username: "m", Groups: []string{"media"}}
  103. other := &Identity{Username: "o", Groups: []string{"users"}}
  104. admin := &Identity{Username: "a", IsAdmin: true}
  105. tests := []struct {
  106. name string
  107. ep Endpoint
  108. id *Identity
  109. want bool
  110. }{
  111. {"member of group", restricted, member, true},
  112. {"not in group", restricted, other, false},
  113. {"admin bypasses groups", restricted, admin, true},
  114. {"anonymous on protected", everyone, nil, false},
  115. {"any user when no groups", everyone, other, true},
  116. {"anonymous on public", public, nil, true},
  117. }
  118. for _, tt := range tests {
  119. t.Run(tt.name, func(t *testing.T) {
  120. if got := tt.ep.CanAccess(tt.id); got != tt.want {
  121. t.Errorf("CanAccess = %v, want %v", got, tt.want)
  122. }
  123. })
  124. }
  125. }
  126. func TestAppOrigin(t *testing.T) {
  127. tests := []struct {
  128. portal string
  129. want string
  130. }{
  131. {"https://nas.example.com", "https://app.example.com"},
  132. {"http://nas.local:8080", "http://app.example.com:8080"},
  133. {"", "https://app.example.com"},
  134. }
  135. for _, tt := range tests {
  136. e := Endpoint{Hostname: "app.example.com", PortalOrigin: tt.portal}
  137. if got := e.AppOrigin(); got != tt.want {
  138. t.Errorf("AppOrigin(%q) = %q, want %q", tt.portal, got, tt.want)
  139. }
  140. }
  141. }