Files
hardlink/internal/handlers/build_command_test.go

44 lines
1.9 KiB
Go

package handlers
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"gitea.futuresens.co.uk/futuresens/cmstypes"
"gitea.futuresens.co.uk/futuresens/hardlink/v2/config"
)
func TestCardRoutesNeverExecuteStoredCommandAfterBuildFailure(t *testing.T) {
for _, path := range []string{"/issuedoorcard", "/testissuedoorcard"} {
t.Run(path, func(t *testing.T) {
d := &fakeDoorCardDispenser{}
lock := &fakeDoorCardLockServer{buildErr: errors.New("synthetic invalid lock command"), storedCommand: "PREVIOUS-ROOM"}
app := &App{cfg: &config.ConfigRec{}, disp: d, lockserver: lock}
mux := http.NewServeMux()
app.RegisterRoutes(mux)
r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"roomField":"CURRENT-ROOM","checkinTime":"2026-11-04 15:00:00 +0000","checkoutTime":"2026-11-05 11:00:00 +0000","followStr":"0"}`))
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
var response cmstypes.StatusRec
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if w.Code != http.StatusBadGateway || response.Code != http.StatusBadGateway || !strings.Contains(response.Message, "Build key command failed") {
t.Errorf("POST %s = %d %+v, want existing 502 JSON error", path, w.Code, response)
}
if lock.buildCalls != 1 || lock.sequenceCalls != 0 || lock.storedCommand != "PREVIOUS-ROOM" {
t.Errorf("POST %s build=%d sequence=%d stored=%q, want one rejected build and no execution of previous command", path, lock.buildCalls, lock.sequenceCalls, lock.storedCommand)
}
if !reflect.DeepEqual(d.calls, []string{"prepare current"}) || d.activity != 0 || d.outsideActivity {
t.Errorf("POST %s dispenser=%v active=%d outside=%t, want preparation only and released foreground", path, d.calls, d.activity, d.outsideActivity)
}
})
}
}