package webtest import ( "context" "database/sql" "net/url" "regexp" "strings" "sync" "testing" "time" "amadan.net/oficina/base/internal/config" "amadan.net/oficina/base/internal/store" "amadan.net/oficina/base/internal/web" ) func (a *testApp) setInstall(app, state, errMsg string) { a.t.Helper() ctx := context.Background() if _, err := store.ClaimInstall(ctx, a.DB, app, "acme-"+app+".bab.oncarlos.com"); err != nil { a.t.Fatal(err) } if err := store.SetInstallState(ctx, a.DB, app, state, errMsg); err != nil { a.t.Fatal(err) } } // stepDone reports whether the Home step with this label is ticked. func stepDone(t *testing.T, body, label string) bool { t.Helper() m := regexp.MustCompile(`
  • ]*>(?:[^<]*)?` + regexp.QuoteMeta(label) + `
  • `).FindStringSubmatch(body) if m == nil { t.Fatalf("no step %q in:\n%s", label, body) } return m[1] != "" } func TestHomeShowsGreetingWithName(t *testing.T) { a := newApp(t) if _, err := a.DB.Exec(`UPDATE members SET name = 'Paul'`); err != nil { t.Fatal(err) } a.signedInOwner() if body := a.get("/").Body; !regexp.MustCompile(`(Good (morning|afternoon|evening)|Hello), Paul\.`).MatchString(body) { t.Fatalf("no greeting:\n%s", body) } } func TestHomeNameFallsBackToEmailLocalPart(t *testing.T) { a := newApp(t) a.signedInOwner() if body := a.get("/").Body; !strings.Contains(body, ", Owner.") { t.Fatalf("greeting lacks capitalised local part:\n%s", body) } } func TestHomeSteps(t *testing.T) { a := newApp(t) a.signedInOwner() body := a.get("/").Body if !stepDone(t, body, "Signed in") || stepDone(t, body, "Invite your team") || stepDone(t, body, "Install your first app") { t.Fatalf("fresh Base steps wrong:\n%s", body) } a.setInstall("welcome", "installed", "") // a default app does not count if body := a.get("/").Body; stepDone(t, body, "Install your first app") { t.Fatalf("default app ticked the install step:\n%s", body) } if r := a.post("/people", invite("maria@acme.co")); r.Code != 303 { t.Fatal(r.Code) } a.setInstall("scratchpad", "installed", "") body = a.get("/").Body for _, s := range []string{"Signed in", "Invite your team", "Install your first app"} { if !stepDone(t, body, s) { t.Errorf("step %q not done:\n%s", s, body) } } } func TestHomeTilesLinkToOrigins(t *testing.T) { a := newApp(t) a.signedInOwner() body := a.get("/").Body for _, h := range []string{`href="/store"`, `href="/people"`, `href="/settings"`} { if !strings.Contains(body, h) { t.Errorf("built-in tile %s missing", h) } } if strings.Contains(body, "acme-scratchpad") { t.Fatal("uninstalled app has a tile") } a.setInstall("scratchpad", "installing", "") if strings.Contains(a.get("/").Body, "acme-scratchpad") { t.Fatal("installing app has a tile") } a.setInstall("scratchpad", "failed", "x") if strings.Contains(a.get("/").Body, "acme-scratchpad") { t.Fatal("failed app has a tile") } a.setInstall("scratchpad", "installing", "") a.setInstall("scratchpad", "installed", "") body = a.get("/").Body re := regexp.MustCompile(``) if !re.MatchString(body) { t.Fatalf("no tile for scratchpad:\n%s", body) } } // Finding 1: a tile links to the host Base installed the app at, even after // the slug or apps domain changed. func TestHomeTileUsesRecordedHost(t *testing.T) { a := newApp(t) a.signedInOwner() ctx := context.Background() if _, err := store.ClaimInstall(ctx, a.DB, "scratchpad", "oldco-scratchpad.old.example"); err != nil { t.Fatal(err) } if err := store.SetInstallState(ctx, a.DB, "scratchpad", "installed", ""); err != nil { t.Fatal(err) } body := a.get("/").Body if !strings.Contains(body, ``) || strings.Contains(body, "acme-scratchpad") { t.Fatalf("tile does not use the recorded host:\n%s", body) } } var ( appsRE = regexp.MustCompile(`(?s)`) itemRE = regexp.MustCompile(`(.*?)([^<]*)`) appIconRE = regexp.MustCompile(`^$`) oficinaApps = []config.OficinaApp{ {ID: "fichas", Name: "Fichas", Origin: "https://fichas.oficina.works"}, {ID: "calendar", Name: "Calendar", Origin: "https://calendar.oficina.works"}, {ID: "equipo", Name: "Equipo", Origin: "https://equipo.bds.oncarlos.com"}, } ) type homeItem struct { Href, Icon, Name string NewTab, Own bool // Own: one of Base's own pages, which CSS starts on a row of their own } // homeApps is Home's apps panel: one grid of every app. func homeApps(t *testing.T, body string) []homeItem { t.Helper() m := appsRE.FindStringSubmatch(body) if m == nil { t.Fatalf("no apps panel:\n%s", body) } var out []homeItem for _, i := range itemRE.FindAllStringSubmatch(m[1], -1) { out = append(out, homeItem{Own: i[1] != "", Href: i[2], NewTab: i[3] != "", Icon: i[4], Name: i[5]}) } return out } // Home's apps panel holds every app in one grid, all alike: the team's // Oficina apps in the setting's order (each opening in a new tab, with // Estilo's icon served from Base), then the installed Store apps (by app // id, as the Store keeps them), then Base's own Store, People and // Settings. There is no dock. The same markup serves a phone. func TestHomeAppsPanel(t *testing.T) { a := newAppConfigured(t, func(*sql.DB, config.Config) web.Installer { return noInstaller{} }, time.UTC, func(c *config.Config) { c.OficinaApps = oficinaApps }) a.signedInOwner() a.setInstall("welcome", "installed", "") a.setInstall("scratchpad", "installed", "") body := a.get("/").Body grid := homeApps(t, body) var names []string for _, i := range grid { names = append(names, i.Name) } if want := "Fichas Calendar Equipo Scratchpad Welcome Store People Settings"; strings.Join(names, " ") != want { t.Fatalf("grid %v, want %s", names, want) } for i, want := range oficinaApps { g := grid[i] if g.Href != want.Origin+"/" || !g.NewTab { t.Errorf("%s: href %q, new tab %v", want.Name, g.Href, g.NewTab) } icon := appIconRE.FindStringSubmatch(g.Icon) if icon == nil || !strings.HasPrefix(icon[1], "/static/oficina/"+want.ID+".") { t.Errorf("%s: icon %q", want.Name, g.Icon) continue } if r := a.get(icon[1]); r.Code != 200 || r.Header.Get("Content-Type") != "image/svg+xml" { t.Errorf("%s: %d %q", icon[1], r.Code, r.Header.Get("Content-Type")) } } for _, g := range grid[:5] { if g.Own { t.Errorf("%s is marked as Base's own", g.Name) } } for _, g := range grid[len(oficinaApps):5] { if g.NewTab || !strings.HasPrefix(g.Href, "https://acme-") || !strings.HasPrefix(g.Icon, ". func deskClosed(t *testing.T, body string) bool { t.Helper() m := regexp.MustCompile(`]*>`).FindString(body) if m == "" { t.Fatalf("no in:\n%s", body) } return strings.Contains(m, `class="desk-closed"`) } // Home for a member with Ask Base draws the chat docked beside it, open // with the conversation, until they close it: POST /ask/dock (open=0, by // the X) draws on every later visit, and open=1 // (the Base button, the field) clears it. It is theirs alone, a cross-site // post changes nothing, and without Equipo there is no chat at all. func TestHomeDocksAskOpenByDefault(t *testing.T) { a, f := newAskApp(t) a.connectEquipo(f) f.Say("Your week is clear.") home := a.get("/") if home.Code != 200 || deskClosed(t, home.Body) { t.Fatalf("Home: %d, closed %v", home.Code, deskClosed(t, home.Body)) } for _, want := range []string{ `