68 lines
2.8 KiB
Go
68 lines
2.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestUpdateVersionComparison(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
a, b string
|
|
want int
|
|
}{{"1.10.0", "1.9.0", 1}, {"2.0.0", "2.0.0", 0}, {"1.99.99", "2.0.0", -1}, {"99999999999999999999999.0.0", "100000000000000000000000.0.0", -1}} {
|
|
got, err := compareUpdateVersions(tc.a, tc.b)
|
|
if err != nil || got != tc.want {
|
|
t.Fatalf("%s vs %s: %d %v", tc.a, tc.b, got, err)
|
|
}
|
|
}
|
|
for _, s := range []string{"v2.0", "1.0", "01.0.0", "1.0.0-beta", "1.0.0+build", "-1.0.0", "1.2.3\n"} {
|
|
if _, err := compareUpdateVersions(s, "1.0.0"); err == nil {
|
|
t.Fatalf("accepted %q", s)
|
|
}
|
|
}
|
|
}
|
|
func TestUpdateDeadline(t *testing.T) {
|
|
deadline := time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)
|
|
p := UpdatePolicy{Enabled: true, RecommendedVersion: "2.0.0", MinimumSupportedVersion: "1.5.0", EnforceAfter: deadline.Format(time.RFC3339)}
|
|
for _, tc := range []struct {
|
|
current string
|
|
now time.Time
|
|
available, mandatory, required bool
|
|
}{{"1.0.0", deadline.Add(-time.Second), true, true, false}, {"1.0.0", deadline, true, true, true}, {"1.5.0", deadline, true, false, false}, {"2.0.0", deadline, false, false, false}, {"2.1.0", deadline, false, false, false}} {
|
|
a, m, r := evaluateUpdate(p, tc.current, tc.now)
|
|
if a != tc.available || m != tc.mandatory || r != tc.required {
|
|
t.Fatalf("%+v: %v %v %v", tc, a, m, r)
|
|
}
|
|
}
|
|
p.Enabled = false
|
|
a, m, r := evaluateUpdate(p, "1.0.0", deadline)
|
|
if a || m || r {
|
|
t.Fatal("disabled policy enforced")
|
|
}
|
|
}
|
|
func TestUpdateInvalidRequests(t *testing.T) {
|
|
for _, body := range []string{`{"platform":"windows","arch":"x64","channel":"stable","release_id":1,"minimum_supported_version":"2.0.0"}`, `{"platform":"windows","arch":"x64","channel":"stable","release_id":1,"enforce_after":"2026-11-01T00:00:00Z"}`, `{"platform":"other","arch":"x64","channel":"stable","release_id":1}`} {
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Params = gin.Params{{Key: "id", Value: "1"}}
|
|
c.Request = httptest.NewRequest("PUT", "/", strings.NewReader(body))
|
|
(&BusinessAPI{}).saveUpdatePolicy(c)
|
|
if w.Code != 400 {
|
|
t.Fatalf("%s: %d %s", body, w.Code, w.Body)
|
|
}
|
|
}
|
|
for _, body := range []string{`{"current_version":"v1.0","platform":"windows","arch":"x64","channel":"stable"}`, `{"current_version":"1.0.0","platform":"windows","arch":"x64","channel":"stable","secret":"invalid"}`} {
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Params = gin.Params{{Key: "instance_id", Value: "11111111-2222-3333-4444-555555555555"}}
|
|
c.Request = httptest.NewRequest("POST", "/", strings.NewReader(body))
|
|
(&BusinessAPI{}).checkUpdate(c)
|
|
if w.Code != 400 && w.Code != 403 {
|
|
t.Fatalf("%s: %d", body, w.Code)
|
|
}
|
|
}
|
|
}
|