Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
66 lines
2.3 KiB
Go
66 lines
2.3 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCityOrganizerAPIRequiresAdminAndVersion(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
|
for _, p := range []string{"/life/home/one/page", "/life/home/two/page"} {
|
|
if _, err := s.store.CreatePageAtPathInDomain(domain, p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
city, err := s.store.GetCityMap(domain)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
one, two := city.Quartiers[0].Streets[0].Spots[0], city.Quartiers[0].Streets[0].Spots[1]
|
|
user, err := s.store.CreatePasswordAccount(domain, "reader", "a long test password", "Reader", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
readerToken, err := s.store.CreateAuthSession(domain, user.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
call := func(token, url string, body any) *httptest.ResponseRecorder {
|
|
b, _ := json.Marshal(body)
|
|
r := httptest.NewRequest(http.MethodPost, "http://localhost"+url, strings.NewReader(string(b)))
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
r.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
rename := map[string]any{"title": "Changed", "base_version": city.Version}
|
|
merge := map[string]any{"action": "merge", "base_version": city.Version, "spots": []string{one.UUID}, "target_spot_uuid": two.UUID}
|
|
for url, body := range map[string]any{"/_/api/v1/city/spots/" + one.UUID + "/title": rename, "/_/api/v1/city/organize": merge} {
|
|
if w := call(readerToken, url, body); w.Code != http.StatusForbidden {
|
|
t.Fatalf("non-admin: %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
if w := call(token, "/_/api/v1/city/spots/"+one.UUID+"/title", rename); w.Code != 200 {
|
|
t.Fatalf("rename: %d %s", w.Code, w.Body.String())
|
|
}
|
|
if w := call(token, "/_/api/v1/city/organize", merge); w.Code != http.StatusConflict {
|
|
t.Fatalf("stale merge: %d %s", w.Code, w.Body.String())
|
|
}
|
|
merge["base_version"] = city.Version + 1
|
|
if w := call(token, "/_/api/v1/city/organize", merge); w.Code != 200 {
|
|
t.Fatalf("merge: %d %s", w.Code, w.Body.String())
|
|
}
|
|
plan, err := s.buildCityAutoPlan(domain)
|
|
if err != nil || len(plan) != 1 {
|
|
t.Fatalf("manual membership re-inferred: %+v %v", plan, err)
|
|
}
|
|
}
|