package webtest
import (
"bufio"
"context"
"database/sql"
"encoding/json"
"html"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"testing"
"time"
"amadan.net/oficina/base/internal/config"
"amadan.net/oficina/base/internal/equipo"
"amadan.net/oficina/base/internal/equipo/equipotest"
"amadan.net/oficina/base/internal/store"
"amadan.net/oficina/base/internal/web"
)
func strconv64(n int64) string { return strconv.FormatInt(n, 10) }
// newAskApp is Base with "Ask Base anything" on, answered by a fake Equipo,
// and the owner signed in.
func newAskApp(t *testing.T) (*testApp, *equipotest.Fake) {
t.Helper()
f := equipotest.New(t)
a := newAppConfigured(t, func(*sql.DB, config.Config) web.Installer { return noInstaller{} }, time.UTC,
func(c *config.Config) { c.EquipoOrigin = f.URL })
a.signedInOwner()
return a, f
}
var codeRE = regexp.MustCompile(`
]*>([A-Z0-9]{8})
`)
// ownerID is the bootstrapped owner's member id.
func (a *testApp) ownerID() string {
a.t.Helper()
m, ok, err := store.MemberByEmail(context.Background(), a.DB, ownerAddress)
if err != nil || !ok {
a.t.Fatalf("owner: %v %v", ok, err)
}
return m.ID
}
// pollable makes Base's last look at Equipo old enough to look again.
func (a *testApp) pollable() {
a.t.Helper()
if _, err := a.DB.Exec(`UPDATE equipo_links SET polled_at = ?`, time.Now().Add(-10*time.Second).UTC()); err != nil {
a.t.Fatal(err)
}
}
// connectEquipo connects the owner from Settings the way a person does:
// Connect Equipo, approve the code in Equipo, come back. It returns the
// bearer Equipo issued.
func (a *testApp) connectEquipo(f *equipotest.Fake) string {
a.t.Helper()
a.approveEquipo(f)
tokens := f.Tokens()
if len(tokens) != 1 {
a.t.Fatalf("tokens: %v", tokens)
}
return tokens[0]
}
// approveEquipo is connectEquipo without asking which bearer Equipo
// issued: it connects again just as well.
func (a *testApp) approveEquipo(f *equipotest.Fake) {
a.t.Helper()
r := a.post("/ask/connect", url.Values{"next": {"settings"}})
if r.Code != 303 || r.Location() != "/ask/connect?next=settings" {
a.t.Fatalf("connect: %d %q\n%s", r.Code, r.Location(), r.Body)
}
page := a.get(r.Location())
m := codeRE.FindStringSubmatch(page.Body)
if page.Code != 200 || m == nil {
a.t.Fatalf("pending page: %d\n%s", page.Code, page.Body)
}
if err := f.Approve(m[1]); err != nil {
a.t.Fatal(err)
}
a.pollable()
if r := a.check("settings"); r.Code != 303 || r.Location() != "/settings" {
a.t.Fatalf("after approval: %d %q\n%s", r.Code, r.Location(), r.Body)
}
}
// check presses "Check again" on the connect page.
func (a *testApp) check(next string) Resp {
a.t.Helper()
return a.post("/ask/connect/check", url.Values{"next": {next}})
}
// noLeak fails if any response so far carried token: body, header or cookie.
func (a *testApp) noLeak(token string) {
a.t.Helper()
for _, r := range a.seen {
if strings.Contains(r.Body, token) {
a.t.Fatalf("a response body carried the bearer:\n%s", r.Body)
}
for k, vs := range r.Header {
for _, v := range vs {
if strings.Contains(v, token) {
a.t.Fatalf("header %s carried the bearer", k)
}
}
}
}
}
func TestAskOffWithoutEquipo(t *testing.T) {
a := newApp(t)
a.signedInOwner()
// Home has no chat column, no Base button and no field.
home := a.get("/").Body
if strings.Contains(home, `class="ask`) || strings.Contains(home, "base-btn") || strings.Contains(home, `/ask`) ||
strings.Contains(home, "