Add local and NetBox server notes and improve reliability
Release / release (push) Successful in 6m52s

This commit is contained in:
Sebastian Unterschütz committed 2026-10-27 17:51:59 +01:00
1 parent 2852dff2c4
commit 12cc64fdd9
17 files changed
+848 -83

No files matched your search

+80 -15
View File
@@ -318,21 +318,26 @@ func Test403_V2Token_NoV1Hint(t *testing.T) {
}
func TestGet_SendsAuthorizationHeader(t *testing.T) {
var gotAuth string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotAuth = r.Header.Get("Authorization")
w.Header().Set("Content-Type", "application/json")
b, _ := json.Marshal(deviceListResponse())
w.Write(b)
}))
defer srv.Close()
c := NewClient(srv.URL, "nbt_mytoken", 2)
c.Search(context.Background(), "", SearchOptions{}) //nolint:errcheck
want := "Token nbt_mytoken"
if gotAuth != want {
t.Errorf("Authorization header: got %q, want %q", gotAuth, want)
for _, token := range []string{"nbt_mytoken", "legacytoken"} {
t.Run(token, func(t *testing.T) {
want := "Token " + token
if strings.HasPrefix(token, "nbt_") {
want = "Bearer " + token
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("Authorization"); got != want {
t.Errorf("Authorization: got %q, want %q", got, want)
}
w.Header().Set("Content-Type", "application/json")
b, _ := json.Marshal(deviceListResponse())
w.Write(b)
}))
defer srv.Close()
c := NewClient(srv.URL, token, 0)
if _, err := c.Search(context.Background(), "", SearchOptions{}); err != nil {
t.Fatal(err)
}
})
}
}
@@ -434,3 +439,63 @@ func TestStripPrefix(t *testing.T) {
}
}
}
func TestCommentsPreserveFieldAndSendConflictHeader(t *testing.T) {
for _, kind := range []string{"device", "vm"} {
t.Run(kind, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer nbt_test" {
t.Error("wrong authentication")
}
if r.Method == http.MethodPatch {
if r.Header.Get("If-Match") != `W/"123"` {
t.Error("missing conflict protection")
}
var payload map[string]string
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
t.Error(err)
}
if len(payload) != 1 || payload["comments"] != "new\nnotes" {
t.Errorf("unexpected patch: %v", payload)
}
}
w.Header().Set("ETag", `W/"123"`)
w.Write([]byte(`{"comments":"existing notes"}`))
}))
defer srv.Close()
c := NewClient(srv.URL, "nbt_test", 0)
host := HostEntry{ID: 1, Kind: kind}
text, etag, err := c.GetComments(context.Background(), host)
if err != nil || text != "existing notes" {
t.Fatalf("read: %q, %v", text, err)
}
if err := c.SetComments(context.Background(), host, "new\nnotes", etag); err != nil {
t.Fatal(err)
}
})
}
}
func TestFullRefreshRejectsPartialFailure(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "virtualization") {
w.WriteHeader(http.StatusForbidden)
return
}
w.Write([]byte(`{"count":0,"results":[]}`))
}))
defer srv.Close()
c := NewClient(srv.URL, "token", 0)
if _, err := c.SearchAll(context.Background(), "", SearchOptions{}); err == nil {
t.Fatal("partial refresh reported as successful")
}
}
func TestCommentConflictIsReported(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusPreconditionFailed) }))
defer srv.Close()
c := NewClient(srv.URL, "token", 0)
if err := c.SetComments(context.Background(), HostEntry{ID: 1, Kind: "device"}, "draft", "old"); err == nil {
t.Fatal("conflict ignored")
}
}