gates / gates (push) Successful in 3m40s
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
97 lines
4.3 KiB
Go
97 lines
4.3 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
|
|
)
|
|
|
|
func enrollBody(h http.Handler, body, pass string) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/host-enroll", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if pass != "" {
|
|
req.Header.Set("X-Retrieval-Password", pass)
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
h.ServeHTTP(rr, req)
|
|
return rr
|
|
}
|
|
|
|
// D83 (R-1398 part 2): an install that went on with --accept-small-disk says so at its enrolment, and the hub keeps
|
|
// it for the box's page — at the first enrolment and at a reinstall over the same record. An ordinary enrolment, and
|
|
// an older installer, mark nothing.
|
|
func TestD83_EnrolmentStoresTheSmallDiskMark(t *testing.T) {
|
|
h, st, _ := newTestHandler(t)
|
|
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "k1", RetrievalPassword: "pass-phrase"})
|
|
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c2", APIKey: "k2", RetrievalPassword: "pass-phrase"})
|
|
|
|
rr := enrollBody(h, `{"customer_id":"c1","small_disk":true}`, "pass-phrase") // what installer 1.38.0 sends
|
|
if rr.Code != http.StatusCreated {
|
|
t.Fatalf("status %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
var got enrollResp
|
|
_ = json.Unmarshal(rr.Body.Bytes(), &got)
|
|
sd := st.SmallDiskOf(got.HostID)
|
|
if sd == nil || sd.Bytes != 0 || sd.NotedAt.IsZero() {
|
|
t.Fatalf("the hub did not keep the mark of %s: %+v", got.HostID, sd)
|
|
}
|
|
// a reinstall over the same record, now with the size (a later installer): the size is kept
|
|
rr = enrollBody(h, `{"customer_id":"c1","small_disk":true,"system_disk_bytes":128035676160}`, "pass-phrase")
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("reuse status %d", rr.Code)
|
|
}
|
|
if sd := st.SmallDiskOf(got.HostID); sd == nil || sd.Bytes != 128035676160 {
|
|
t.Fatalf("the size of the second enrolment was not kept: %+v", sd)
|
|
}
|
|
// the control: an ordinary enrolment marks nothing
|
|
rr = enrollBody(h, `{"customer_id":"c2"}`, "pass-phrase")
|
|
var other enrollResp
|
|
_ = json.Unmarshal(rr.Body.Bytes(), &other)
|
|
if rr.Code != http.StatusCreated || st.SmallDiskOf(other.HostID) != nil {
|
|
t.Fatalf("an ordinary enrolment (status %d) was marked small: %+v", rr.Code, st.SmallDiskOf(other.HostID))
|
|
}
|
|
}
|
|
|
|
// What a box sends is a box's word: only the JSON `true` is the flag, a size that is not a whole number is „not
|
|
// told”, and a request that is REFUSED marks nothing.
|
|
func TestD83_OnlyTrueIsTheFlag_AndARefusedRequestMarksNothing(t *testing.T) {
|
|
h, st, _ := newTestHandler(t)
|
|
for i, body := range []string{
|
|
`{"customer_id":"c%d","small_disk":"true"}`, `{"customer_id":"c%d","small_disk":1}`,
|
|
`{"customer_id":"c%d","small_disk":false}`, `{"customer_id":"c%d","small_disk":null}`,
|
|
`{"customer_id":"c%d","small_disk":{"a":true}}`,
|
|
} {
|
|
cid := "c" + string(rune('a'+i))
|
|
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: cid, APIKey: "k" + cid, RetrievalPassword: "pass-phrase"})
|
|
rr := enrollBody(h, strings.Replace(body, "c%d", cid, 1), "pass-phrase")
|
|
var got enrollResp
|
|
_ = json.Unmarshal(rr.Body.Bytes(), &got)
|
|
if rr.Code != http.StatusCreated {
|
|
t.Fatalf("%s: status %d — the enrolment itself must not fail on a strange flag", body, rr.Code)
|
|
}
|
|
if st.SmallDiskOf(got.HostID) != nil {
|
|
t.Fatalf("%s was read as the flag", body)
|
|
}
|
|
}
|
|
// a size that is not a whole number: the flag stands, the size is „not told”
|
|
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "cz", APIKey: "kz", RetrievalPassword: "pass-phrase"})
|
|
rr := enrollBody(h, `{"customer_id":"cz","small_disk":true,"system_disk_bytes":"<b>big</b>"}`, "pass-phrase")
|
|
var got enrollResp
|
|
_ = json.Unmarshal(rr.Body.Bytes(), &got)
|
|
if sd := st.SmallDiskOf(got.HostID); rr.Code != http.StatusCreated || sd == nil || sd.Bytes != 0 {
|
|
t.Fatalf("status %d, mark %+v", rr.Code, sd)
|
|
}
|
|
// a wrong passphrase: refused, and nothing is marked for any box of that household
|
|
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "cw", APIKey: "kw", RetrievalPassword: "pass-phrase"})
|
|
if rr := enrollBody(h, `{"customer_id":"cw","small_disk":true}`, "wrong"); rr.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status %d, want 401", rr.Code)
|
|
}
|
|
if n := len(st.OSAlarmKeys("small_disk:cw")); n != 0 {
|
|
t.Fatalf("a refused enrolment left %d mark(s)", n)
|
|
}
|
|
}
|