package api
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/zitcli"
)
// fleet.pack is for a site admin (the s letter at the forge): anyone
// else is refused before the maintenance lane hears of it.
func TestFleetPackOpRefusesANonSiteAdmin(t *testing.T) {
r := ops.NewRegistry()
asked := 0
d := Deps{Version: "v", Repos: NewRepos(map[string]*Repo{"tcl": {Zit: zitcli.New("tcl")}}),
Pack: func(string) (bool, error) { asked++; return true, nil }}
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
h, err := RouterWithCaps(r, "test", func(user, _ string) CapSet {
if user == "root" {
return oltp.CapSet("s")
}
return oltp.CapSet("rw")
}, nil)
if err != nil {
t.Fatal(err)
}
post := func(user string) int {
req := httptest.NewRequest("POST", "/api/v1/fleet/pack", strings.NewReader("repo=tcl"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req = req.WithContext(ops.WithUser(req.Context(), user))
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
return w.Code
}
if code := post("eve"); code != http.StatusForbidden || asked != 0 {
t.Errorf("FAIL: eve got %d with %d requests at the lane, want 403 and none", code, asked)
}
if code := post("root"); code != http.StatusOK || asked != 1 {
t.Errorf("FAIL: root got %d with %d requests at the lane, want 200 and one", code, asked)
}
if !t.Failed() {
t.Log("ok")
}
}