You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

51 lines
1.6 KiB

package handlers
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestLoginReadinessRequiresSuccessfulStartupAndPolicy(t *testing.T) {
for _, tc := range []struct {
name, startup string
policy int
ready bool
}{
{"pending", `{"ready":false,"state":"starting","completed":1,"total":3}`, 200, false},
{"failed", `{"ready":false,"state":"failed","stages":[{"label":"serial","state":"failed"}]}`, 200, false},
{"malformed", `{`, 200, false},
{"policy failure", `{"ready":true,"state":"ready"}`, 503, false},
{"ready", `{"ready":true,"state":"ready","elapsed_ms":2500}`, 200, true},
} {
t.Run(tc.name, func(t *testing.T) {
policyCalls := 0
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v1/auth/startup" {
fmt.Fprint(w, tc.startup)
return
}
policyCalls++
w.WriteHeader(tc.policy)
fmt.Fprint(w, `{"captcha_enabled":true}`)
}))
defer upstream.Close()
target, _ := url.Parse(upstream.URL)
h := &DeviceHandler{apiTarget: target, apiClient: upstream.Client()}
response := httptest.NewRecorder()
h.handleLoginBackendStatus(response, httptest.NewRequest("GET", "/login/backend-status", nil))
if strings.Contains(response.Body.String(), `"ready":true`) != tc.ready {
t.Fatal(response.Body.String())
}
if !strings.Contains(tc.startup, `"ready":true`) && policyCalls != 0 {
t.Fatal("policy requested before readiness")
}
if tc.name == "failed" && !strings.Contains(response.Body.String(), `"state":"failed"`) {
t.Fatal("failure lost")
}
})
}
}