Files
landwelle/app_api_test.go
T

162 lines
5.4 KiB
Go

package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestExternalV1AliasBootstrap(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://api.landwelle.de/v1/app/bootstrap", nil)
rec := httptest.NewRecorder()
apiTestMux().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
}
var body struct {
Data struct {
Endpoints map[string]string `json:"endpoints"`
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
if got := body.Data.Endpoints["now"]; got != "/v1/now" {
t.Fatalf("expected external alias endpoint /v1/now, got %q", got)
}
}
func TestUnknownExternalV1EndpointReturnsJSON404(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://api.landwelle.de/v1/does-not-exist", nil)
rec := httptest.NewRecorder()
apiTestMux().ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Fatalf("expected 404, got %d: %s", rec.Code, rec.Body.String())
}
if contentType := rec.Header().Get("Content-Type"); !strings.Contains(contentType, "application/json") {
t.Fatalf("expected JSON response, got %q", contentType)
}
}
func TestCompatibilityRejectsUnknownPlatform(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://api.landwelle.de/v1/app/compatibility?platform=windows", nil)
rec := httptest.NewRecorder()
apiTestMux().ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", rec.Code)
}
}
func TestVersionComparison(t *testing.T) {
cases := []struct {
a, b string
want int
}{
{"1.0.0", "1.0.1", -1},
{"1.2.0", "1.1.9", 1},
{"v2.0", "2.0.0", 0},
}
for _, tc := range cases {
if got := compareVersions(tc.a, tc.b); got != tc.want {
t.Fatalf("compareVersions(%q,%q)=%d, want %d", tc.a, tc.b, got, tc.want)
}
}
}
func TestGeneratedUUIDIsValid(t *testing.T) {
for i := 0; i < 10; i++ {
if id := newUUID(); !uuidPattern.MatchString(id) {
t.Fatalf("generated invalid UUID: %q", id)
}
}
}
func TestInstallationRegistrationRequiresDatabase(t *testing.T) {
body := `{"platform":"android","device_class":"phone","distribution_channel":"development","app_version":"1.0.0"}`
req := httptest.NewRequest(http.MethodPost, "https://api.landwelle.de/v1/installations", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
apiTestMux().ServeHTTP(rec, req)
if rec.Code != http.StatusServiceUnavailable {
t.Fatalf("expected 503, got %d: %s", rec.Code, rec.Body.String())
}
}
func TestExternalNewsFallback(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://api.landwelle.de/v1/news?limit=2", nil)
rec := httptest.NewRecorder()
apiTestMux().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
}
var body struct {
Data []map[string]any `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
if len(body.Data) != 2 {
t.Fatalf("expected 2 fallback news items, got %d", len(body.Data))
}
}
func TestExternalV1RootReportsContractVersion(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://api.landwelle.de/v1", nil)
rec := httptest.NewRecorder()
apiTestMux().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
}
var body struct {
Data map[string]any `json:"data"`
Meta map[string]any `json:"meta"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
if body.Data["contract"] != apiContractVersion || body.Meta["contract_version"] != apiContractVersion {
t.Fatalf("contract version missing: %#v %#v", body.Data, body.Meta)
}
}
func TestCompatibilityRejectsInvalidChannel(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://api.landwelle.de/v1/app/compatibility?platform=ios&channel=google_play", nil)
rec := httptest.NewRecorder()
apiTestMux().ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d: %s", rec.Code, rec.Body.String())
}
}
func TestAppUpdateAvailableDoesNotDowngradeNewerVersion(t *testing.T) {
if appUpdateAvailable("2.0.0", "1", "1.9.9", "999") {
t.Fatal("newer installed version must not be marked as updateable because of a lower build number")
}
if !appUpdateAvailable("1.2.0", "10", "1.2.0", "11") {
t.Fatal("same version with lower build should be marked as updateable")
}
}
func TestUsagePropertiesWhitelist(t *testing.T) {
if _, err := validateUsageProperties("stream_stop", map[string]any{"duration_seconds": float64(125), "reason": "user"}); err != nil {
t.Fatalf("expected valid properties, got %v", err)
}
if _, err := validateUsageProperties("stream_stop", map[string]any{"url": "https://example.invalid"}); err == nil {
t.Fatal("unexpected arbitrary property accepted")
}
if _, err := validateUsageProperties("news_open", map[string]any{"content_id": strings.Repeat("x", 201)}); err == nil {
t.Fatal("oversized content id accepted")
}
}
func TestMobileEventWithoutImageDoesNotReturnHostRoot(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://api.landwelle.de/v1/events/1", nil)
item := CalendarEvent{ID: 1, Title: "Test"}
data := mobileEventJSON(req, item)
if got := data["image_url"]; got != "" {
t.Fatalf("expected empty image URL, got %#v", got)
}
}