| 1 | package webtest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "reflect" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/base/internal/config" |
| 13 | "amadan.net/oficina/base/internal/store" |
| 14 | ) |
| 15 | |
| 16 | // GET /api/v1/me and GET /api/v1/home: Base's reads for its native app |
| 17 | // (docs/api/base-native-v1.md), with the session the native exchange mints. |
| 18 | |
| 19 | func decode[T any](t *testing.T, r Resp) T { |
| 20 | t.Helper() |
| 21 | var v T |
| 22 | if err := json.Unmarshal([]byte(r.Body), &v); err != nil { |
| 23 | t.Fatalf("%d %q: %v", r.Code, r.Body, err) |
| 24 | } |
| 25 | return v |
| 26 | } |
| 27 | |
| 28 | func TestAPIMe(t *testing.T) { |
| 29 | dublin, _ := time.LoadLocation("Europe/Dublin") |
| 30 | a := newHomeAppWith(t, func(c *config.Config) { c.Timezone = dublin }) |
| 31 | a.link(ownerAddress, "subject-owner") |
| 32 | app := a.nativeSignIn("subject-owner") |
| 33 | r := app.nativeGet("/api/v1/me") |
| 34 | if r.Code != 200 || r.Header.Get("Cache-Control") != "no-store" || r.Header.Get("Content-Type") != "application/json" { |
| 35 | t.Fatalf("me: %d %q %q", r.Code, r.Header.Get("Cache-Control"), r.Header.Get("Content-Type")) |
| 36 | } |
| 37 | got := decode[map[string]any](t, r) |
| 38 | want := map[string]any{ |
| 39 | "member": map[string]any{"id": a.ownerID(), "name": "Owner", "email": ownerAddress, "initials": "O", "owner": true}, |
| 40 | "base": map[string]any{"name": "Acme", "slug": "acme", "origin": homeOrigin, "timezone": "Europe/Dublin"}, |
| 41 | "appearance": "system", |
| 42 | "equipo_origin": nil, |
| 43 | } |
| 44 | if !reflect.DeepEqual(got, want) { |
| 45 | t.Fatalf("me:\n got %v\nwant %v", got, want) |
| 46 | } |
| 47 | |
| 48 | // The member's Appearance, a member who is not the owner, and Ask on. |
| 49 | b := newHomeAppWith(t, func(c *config.Config) { c.EquipoOrigin = "https://equipo.acme.test" }) |
| 50 | ana, _, err := store.Invite(t.Context(), b.DB, "ana.ruiz@acme.test") |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if err := store.SetTheme(t.Context(), b.DB, ana.ID, "dark"); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | b.link("ana.ruiz@acme.test", "subject-ana") |
| 58 | me := decode[apiMeShape](t, b.nativeSignIn("subject-ana").nativeGet("/api/v1/me")) |
| 59 | if me.Member.Owner || me.Member.Email != "ana.ruiz@acme.test" || me.Member.Name != "Ana.ruiz" || me.Appearance != "dark" || |
| 60 | me.EquipoOrigin == nil || *me.EquipoOrigin != "https://equipo.acme.test" || me.Base.Timezone != "UTC" { |
| 61 | t.Fatalf("ana: %+v", me) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | type apiMeShape struct { |
| 66 | Member struct { |
| 67 | ID, Name, Email, Initials string |
| 68 | Owner bool |
| 69 | } |
| 70 | Base struct{ Name, Slug, Origin, Timezone string } |
| 71 | Appearance string |
| 72 | EquipoOrigin *string `json:"equipo_origin"` |
| 73 | } |
| 74 | |
| 75 | type apiAppShape struct { |
| 76 | ID string `json:"id"` |
| 77 | Kind string `json:"kind"` |
| 78 | Name string `json:"name"` |
| 79 | URL string `json:"url"` |
| 80 | IconURL string `json:"icon_url"` |
| 81 | Tint string `json:"tint"` |
| 82 | IconSVG string `json:"icon_svg"` |
| 83 | BundleID string `json:"bundle_id"` |
| 84 | Scheme string `json:"scheme"` |
| 85 | } |
| 86 | |
| 87 | type apiHomeShape struct { |
| 88 | Greeting string `json:"greeting"` |
| 89 | Lede string `json:"lede"` |
| 90 | Steps []struct { |
| 91 | Label string `json:"label"` |
| 92 | Done bool `json:"done"` |
| 93 | } `json:"steps"` |
| 94 | Apps []apiAppShape `json:"apps"` |
| 95 | } |
| 96 | |
| 97 | func TestAPIHome(t *testing.T) { |
| 98 | a := newHomeAppWith(t, func(c *config.Config) { |
| 99 | c.OficinaApps = []config.OficinaApp{ |
| 100 | {ID: "docs", Name: "Docs", Origin: "https://docs.oficina.works"}, |
| 101 | {ID: "calls", Name: "Calls", Origin: "https://calls.oficina.works"}, |
| 102 | } |
| 103 | }) |
| 104 | a.link(ownerAddress, "subject-owner") |
| 105 | a.setInstall("scratchpad", "installed", "") |
| 106 | app := a.nativeSignIn("subject-owner") |
| 107 | r := app.nativeGet("/api/v1/home") |
| 108 | if r.Code != 200 || r.Header.Get("Cache-Control") != "no-store" { |
| 109 | t.Fatalf("home: %d %q", r.Code, r.Header.Get("Cache-Control")) |
| 110 | } |
| 111 | var raw map[string]json.RawMessage |
| 112 | if err := json.Unmarshal([]byte(r.Body), &raw); err != nil || len(raw) != 4 { |
| 113 | t.Fatalf("home's keys: %v %s", err, r.Body) |
| 114 | } |
| 115 | h := decode[apiHomeShape](t, r) |
| 116 | if !regexp.MustCompile(`^(Hello|Good (morning|afternoon|evening)), Owner\.$`).MatchString(h.Greeting) { |
| 117 | t.Errorf("greeting %q", h.Greeting) |
| 118 | } |
| 119 | if h.Lede != "One step until Acme's Base is ready." { |
| 120 | t.Errorf("lede %q", h.Lede) |
| 121 | } |
| 122 | steps := []string{} |
| 123 | for _, s := range h.Steps { |
| 124 | steps = append(steps, s.Label+"="+map[bool]string{true: "done", false: "open"}[s.Done]) |
| 125 | } |
| 126 | if got := strings.Join(steps, ", "); got != "Signed in=done, Invite your team=open, Install your first app=done" { |
| 127 | t.Errorf("steps: %s", got) |
| 128 | } |
| 129 | var order []string |
| 130 | for _, x := range h.Apps { |
| 131 | order = append(order, x.Kind+":"+x.ID) |
| 132 | } |
| 133 | if got := strings.Join(order, " "); got != "oficina:docs oficina:calls installed:scratchpad own:store own:people own:settings" { |
| 134 | t.Fatalf("apps in order: %s", got) |
| 135 | } |
| 136 | icon := regexp.MustCompile(`^/static/oficina/docs\.[0-9a-f]+\.svg$`) |
| 137 | docs, calls, scratch, store := h.Apps[0], h.Apps[1], h.Apps[2], h.Apps[3] |
| 138 | if docs.Name != "Docs" || docs.URL != "https://docs.oficina.works/" || !icon.MatchString(docs.IconURL) || docs.BundleID != "works.oficina.docs" || |
| 139 | docs.Scheme != "" || docs.Tint != "" || docs.IconSVG != "" { |
| 140 | t.Errorf("docs: %+v", docs) |
| 141 | } |
| 142 | if calls.BundleID != "works.oficina.calls" || calls.Scheme != "oficina-calls" { |
| 143 | t.Errorf("calls: %+v", calls) |
| 144 | } |
| 145 | if scratch.Name != "Scratchpad" || scratch.URL != "https://acme-scratchpad.bab.oncarlos.com/" || scratch.Tint != "rose" || |
| 146 | !strings.HasPrefix(scratch.IconSVG, "<svg") || scratch.IconURL != "" || scratch.BundleID != "" { |
| 147 | t.Errorf("scratchpad: %+v", scratch) |
| 148 | } |
| 149 | if store.Name != "Store" || store.URL != homeOrigin+"/store" || store.Tint != "teal" || !strings.HasPrefix(store.IconSVG, "<svg") { |
| 150 | t.Errorf("store: %+v", store) |
| 151 | } |
| 152 | // The icon is served where it says. |
| 153 | if r := app.get(docs.IconURL); r.Code != 200 || !strings.Contains(r.Body, "<svg") { |
| 154 | t.Errorf("GET %s: %d", docs.IconURL, r.Code) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // The native app and scripts hear a refusal as JSON, never as a redirect. |
| 159 | func TestAPIRefusesWithoutSession(t *testing.T) { |
| 160 | a := newHomeAppWith(t, func(c *config.Config) { c.EquipoOrigin = "https://equipo.acme.test" }) |
| 161 | a.link(ownerAddress, "subject-owner") |
| 162 | refused := func(where string, r Resp) { |
| 163 | t.Helper() |
| 164 | if r.Code != http.StatusUnauthorized || strings.TrimSpace(r.Body) != `{"code":"unauthenticated"}` || |
| 165 | r.Header.Get("Cache-Control") != "no-store" || r.Location() != "" { |
| 166 | t.Errorf("%s: %d %q %q %q", where, r.Code, r.Body, r.Header.Get("Cache-Control"), r.Location()) |
| 167 | } |
| 168 | } |
| 169 | none := a.newBrowser() |
| 170 | refused("no session, /api/v1/me", none.get("/api/v1/me")) |
| 171 | refused("no session, /api/v1/home", none.nativeGet("/api/v1/home")) |
| 172 | refused("no session, /ask/state as JSON", none.nativeGet("/ask/state")) |
| 173 | refused("no session, POST /ask/messages as JSON", none.nativePost("/ask/messages", "")) |
| 174 | // A session that names nobody: unlinked, or a native one past seven days. |
| 175 | refused("unlinked", a.storedSession("subject-stranger", "home-assertion-native", time.Now()).nativeGet("/api/v1/me")) |
| 176 | refused("expired", a.storedSession("subject-owner", "home-assertion-native", time.Now().Add(-8*24*time.Hour)).nativeGet("/api/v1/me")) |
| 177 | // A signed-out page still goes to /signin. |
| 178 | if r := none.get("/"); r.Code != http.StatusSeeOther || !strings.HasPrefix(r.Location(), "/signin") { |
| 179 | t.Errorf("signed-out page: %d %q", r.Code, r.Location()) |
| 180 | } |
| 181 | // Without Oficina ID, the same. |
| 182 | b := newApp(t) |
| 183 | if r := b.get("/api/v1/me"); r.Code != http.StatusUnauthorized || strings.TrimSpace(r.Body) != `{"code":"unauthenticated"}` { |
| 184 | t.Errorf("Oficina off, /api/v1/me: %d %q", r.Code, r.Body) |
| 185 | } |
| 186 | b.signedInOwner() |
| 187 | if r := b.get("/api/v1/me"); r.Code != 200 { |
| 188 | t.Errorf("Oficina off, signed in: %d", r.Code) |
| 189 | } |
| 190 | } |
| 191 | |