init
This commit is contained in:
24 files changed
+3822
No files matched your search
@@ -0,0 +1,83 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
)
|
||||
|
||||
func testServer(t *testing.T) (*Server, string) {
|
||||
dir := t.TempDir()
|
||||
cfg := roots.Config{
|
||||
StoreDir: filepath.Join(dir, "store"), StateDir: filepath.Join(dir, "state"),
|
||||
NixStoreBin: "false", NixBin: "false", NixEnvBin: "true",
|
||||
}
|
||||
sp := filepath.Join(cfg.StoreDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello")
|
||||
os.MkdirAll(sp, 0o755)
|
||||
link := filepath.Join(dir, "result")
|
||||
os.Symlink(sp, link)
|
||||
os.MkdirAll(filepath.Join(cfg.StateDir, "gcroots", "auto"), 0o755)
|
||||
os.Symlink(link, filepath.Join(cfg.StateDir, "gcroots", "auto", "h1"))
|
||||
|
||||
set := roots.NewSet(cfg)
|
||||
if err := set.Refresh(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv, err := New(context.Background(), set, Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return srv, link
|
||||
}
|
||||
|
||||
func TestAuthAndRender(t *testing.T) {
|
||||
srv, link := testServer(t)
|
||||
h := srv.Handler()
|
||||
|
||||
do := func(method, url string, hdr map[string]string, body string) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(method, url, strings.NewReader(body))
|
||||
for k, v := range hdr {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
if method == http.MethodPost && body != "" && !strings.HasPrefix(body, "{") {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
|
||||
if w := do("GET", "/", nil, ""); w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("no token: %d", w.Code)
|
||||
}
|
||||
w := do("GET", "/?token="+srv.token, nil, "")
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), link) {
|
||||
t.Fatalf("index: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if len(w.Result().Cookies()) == 0 {
|
||||
t.Error("token should set a cookie")
|
||||
}
|
||||
if w := do("GET", "/static/htmx.min.js", nil, ""); w.Code != 200 {
|
||||
t.Errorf("static: %d", w.Code)
|
||||
}
|
||||
if w := do("GET", "/api/roots?q=hello", map[string]string{"Authorization": "Bearer " + srv.token}, ""); w.Code != 200 || !strings.Contains(w.Body.String(), `"kind": "indirect"`) {
|
||||
t.Errorf("api: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
// POST with only the cookie (no X-Token) must be rejected
|
||||
if w := do("POST", "/delete", map[string]string{"Cookie": cookieName + "=" + srv.token}, "link="+link); w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("cookie-only post: %d", w.Code)
|
||||
}
|
||||
w = do("POST", "/delete", map[string]string{"X-Token": srv.token}, "link="+link)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), "Deleted 1 of 1") {
|
||||
t.Errorf("delete: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if _, err := os.Lstat(link); err == nil {
|
||||
t.Error("link still exists")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user