package webtest import ( "encoding/json" "net/http" "reflect" "regexp" "strings" "testing" "time" "amadan.net/oficina/base/internal/config" "amadan.net/oficina/base/internal/store" ) // GET /api/v1/me and GET /api/v1/home: Base's reads for its native app // (docs/api/base-native-v1.md), with the session the native exchange mints. func decode[T any](t *testing.T, r Resp) T { t.Helper() var v T if err := json.Unmarshal([]byte(r.Body), &v); err != nil { t.Fatalf("%d %q: %v", r.Code, r.Body, err) } return v } func TestAPIMe(t *testing.T) { dublin, _ := time.LoadLocation("Europe/Dublin") a := newHomeAppWith(t, func(c *config.Config) { c.Timezone = dublin }) a.link(ownerAddress, "subject-owner") app := a.nativeSignIn("subject-owner") r := app.nativeGet("/api/v1/me") if r.Code != 200 || r.Header.Get("Cache-Control") != "no-store" || r.Header.Get("Content-Type") != "application/json" { t.Fatalf("me: %d %q %q", r.Code, r.Header.Get("Cache-Control"), r.Header.Get("Content-Type")) } got := decode[map[string]any](t, r) want := map[string]any{ "member": map[string]any{"id": a.ownerID(), "name": "Owner", "email": ownerAddress, "initials": "O", "owner": true}, "base": map[string]any{"name": "Acme", "slug": "acme", "origin": homeOrigin, "timezone": "Europe/Dublin"}, "appearance": "system", "equipo_origin": nil, } if !reflect.DeepEqual(got, want) { t.Fatalf("me:\n got %v\nwant %v", got, want) } // The member's Appearance, a member who is not the owner, and Ask on. b := newHomeAppWith(t, func(c *config.Config) { c.EquipoOrigin = "https://equipo.acme.test" }) ana, _, err := store.Invite(t.Context(), b.DB, "ana.ruiz@acme.test") if err != nil { t.Fatal(err) } if err := store.SetTheme(t.Context(), b.DB, ana.ID, "dark"); err != nil { t.Fatal(err) } b.link("ana.ruiz@acme.test", "subject-ana") me := decode[apiMeShape](t, b.nativeSignIn("subject-ana").nativeGet("/api/v1/me")) if me.Member.Owner || me.Member.Email != "ana.ruiz@acme.test" || me.Member.Name != "Ana.ruiz" || me.Appearance != "dark" || me.EquipoOrigin == nil || *me.EquipoOrigin != "https://equipo.acme.test" || me.Base.Timezone != "UTC" { t.Fatalf("ana: %+v", me) } } type apiMeShape struct { Member struct { ID, Name, Email, Initials string Owner bool } Base struct{ Name, Slug, Origin, Timezone string } Appearance string EquipoOrigin *string `json:"equipo_origin"` } type apiAppShape struct { ID string `json:"id"` Kind string `json:"kind"` Name string `json:"name"` URL string `json:"url"` IconURL string `json:"icon_url"` Tint string `json:"tint"` IconSVG string `json:"icon_svg"` BundleID string `json:"bundle_id"` Scheme string `json:"scheme"` } type apiHomeShape struct { Greeting string `json:"greeting"` Lede string `json:"lede"` Steps []struct { Label string `json:"label"` Done bool `json:"done"` } `json:"steps"` Apps []apiAppShape `json:"apps"` } func TestAPIHome(t *testing.T) { a := newHomeAppWith(t, func(c *config.Config) { c.OficinaApps = []config.OficinaApp{ {ID: "docs", Name: "Docs", Origin: "https://docs.oficina.works"}, {ID: "calls", Name: "Calls", Origin: "https://calls.oficina.works"}, } }) a.link(ownerAddress, "subject-owner") a.setInstall("scratchpad", "installed", "") app := a.nativeSignIn("subject-owner") r := app.nativeGet("/api/v1/home") if r.Code != 200 || r.Header.Get("Cache-Control") != "no-store" { t.Fatalf("home: %d %q", r.Code, r.Header.Get("Cache-Control")) } var raw map[string]json.RawMessage if err := json.Unmarshal([]byte(r.Body), &raw); err != nil || len(raw) != 4 { t.Fatalf("home's keys: %v %s", err, r.Body) } h := decode[apiHomeShape](t, r) if !regexp.MustCompile(`^(Hello|Good (morning|afternoon|evening)), Owner\.$`).MatchString(h.Greeting) { t.Errorf("greeting %q", h.Greeting) } if h.Lede != "One step until Acme's Base is ready." { t.Errorf("lede %q", h.Lede) } steps := []string{} for _, s := range h.Steps { steps = append(steps, s.Label+"="+map[bool]string{true: "done", false: "open"}[s.Done]) } if got := strings.Join(steps, ", "); got != "Signed in=done, Invite your team=open, Install your first app=done" { t.Errorf("steps: %s", got) } var order []string for _, x := range h.Apps { order = append(order, x.Kind+":"+x.ID) } if got := strings.Join(order, " "); got != "oficina:docs oficina:calls installed:scratchpad own:store own:people own:settings" { t.Fatalf("apps in order: %s", got) } icon := regexp.MustCompile(`^/static/oficina/docs\.[0-9a-f]+\.svg$`) docs, calls, scratch, store := h.Apps[0], h.Apps[1], h.Apps[2], h.Apps[3] if docs.Name != "Docs" || docs.URL != "https://docs.oficina.works/" || !icon.MatchString(docs.IconURL) || docs.BundleID != "works.oficina.docs" || docs.Scheme != "" || docs.Tint != "" || docs.IconSVG != "" { t.Errorf("docs: %+v", docs) } if calls.BundleID != "works.oficina.calls" || calls.Scheme != "oficina-calls" { t.Errorf("calls: %+v", calls) } if scratch.Name != "Scratchpad" || scratch.URL != "https://acme-scratchpad.bab.oncarlos.com/" || scratch.Tint != "rose" || !strings.HasPrefix(scratch.IconSVG, "