package webtest import ( "database/sql" "time" "amadan.net/oficina/base/internal/config" "amadan.net/oficina/base/internal/web" "net/url" "strings" "testing" "amadan.net/oficina/base/internal/equipo" "amadan.net/oficina/base/internal/equipo/equipotest" ) // Fake tokens are built at run time (R12), never written as literals. func fakeCode() string { return strings.Repeat("c", 3) + "-" + strings.Repeat("9", 8) } func fakeState() string { return strings.Repeat("s", 3) + "_" + strings.Repeat("7", 8) } func doneURL(state, code string) string { return "/ask/oficina/done?" + url.Values{"oficina_state": {state}, "oficina_code": {code}}.Encode() } func TestOficinaConnectStartsAndRedirects(t *testing.T) { a, f := newAskApp(t) token := a.connectEquipo(f) f.OficinaConsent("https://oficina.example/consent?x=1") r := a.get("/ask/oficina/connect") if r.Code != 303 || r.Location() != "https://oficina.example/consent?x=1" { t.Fatalf("connect: %d %q", r.Code, r.Location()) } if r.Header.Get("Cache-Control") != "no-store" || r.Header.Get("Referrer-Policy") != "no-referrer" { t.Errorf("headers: %v", r.Header) } calls := f.Calls("/api/v1/assistant/oficina/connect") if len(calls) != 1 || calls[0].Header.Get("Authorization") != "Bearer "+token || !strings.Contains(calls[0].Body, `"return_to":"`+testOrigin+`/ask/oficina/done"`) { t.Fatalf("calls: %+v", calls) } } func TestOficinaConnectNeverRedirectsAnywhereOdd(t *testing.T) { a, f := newAskApp(t) a.connectEquipo(f) for _, bad := range []string{"http://evil.example/x", "javascript:alert(1)", "//evil.example/x", "/relative", "https://user:pw@evil.example/", "https:///nohost"} { f.OficinaConsent(bad) r := a.get("/ask/oficina/connect") if r.Code == 303 && r.Location() == bad || r.Code != 303 || r.Location() != "/" { t.Errorf("%q: %d %q, want 303 to /", bad, r.Code, r.Location()) } if bad != "" && !strings.Contains(r.Header.Get("Set-Cookie"), "oficina_note=unavailable") { t.Errorf("%q: no unavailable note: %v", bad, r.Header) } } } func TestOficinaConnectAllowsEquipoOrigin(t *testing.T) { a, f := newAskApp(t) a.connectEquipo(f) f.OficinaConsent(f.URL + "/consent") if r := a.get("/ask/oficina/connect"); r.Code != 303 || r.Location() != f.URL+"/consent" { t.Fatalf("%d %q", r.Code, r.Location()) } } func TestOficinaConnectNeedsSignIn(t *testing.T) { 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 }) if r := a.get("/ask/oficina/connect"); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") { t.Fatalf("signed out: %d %q", r.Code, r.Location()) } if r := a.get(doneURL(fakeState(), fakeCode())); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") { t.Fatalf("done, signed out: %d %q", r.Code, r.Location()) } } func TestOficinaConnectNeedsTheChatConnected(t *testing.T) { a, f := newAskApp(t) // signed in, Equipo not connected if r := a.get("/ask/oficina/connect"); r.Code != 303 || r.Location() != "/ask" { t.Fatalf("connect: %d %q", r.Code, r.Location()) } if r := a.get(doneURL(fakeState(), fakeCode())); r.Code != 303 || r.Location() != "/ask" { t.Fatalf("done: %d %q", r.Code, r.Location()) } if n := len(f.Calls("/api/v1/assistant/oficina/connect")) + len(f.Calls("/api/v1/assistant/oficina/connect/finish")); n != 0 { t.Errorf("%d calls without a bearer", n) } } func TestOficinaConnectEquipoErrors(t *testing.T) { for _, c := range []struct { status int code string }{{422, "invalid_input"}, {503, "unavailable"}, {500, "internal"}} { a, f := newAskApp(t) a.connectEquipo(f) f.FailOficina(c.status, c.code) r := a.get("/ask/oficina/connect") if r.Code != 303 || r.Location() != "/" || !strings.Contains(r.Header.Get("Set-Cookie"), "oficina_note=unavailable") { t.Errorf("%d: %d %q %v", c.status, r.Code, r.Location(), r.Header) } } a, f := newAskApp(t) a.connectEquipo(f) f.FailOficina(401, "unauthenticated") if r := a.get("/ask/oficina/connect"); r.Code != 303 || r.Location() != "/ask" { t.Errorf("401: %d %q", r.Code, r.Location()) } } func TestOficinaDoneFinishesWithTheBearer(t *testing.T) { a, f := newAskApp(t) token := a.connectEquipo(f) st, code := fakeState(), fakeCode() r := a.get(doneURL(st, code)) if r.Code != 303 || r.Location() != "/" { t.Fatalf("done: %d %q", r.Code, r.Location()) } if r.Header.Get("Cache-Control") != "no-store" || r.Header.Get("Referrer-Policy") != "no-referrer" { t.Errorf("headers: %v", r.Header) } calls := f.Calls("/api/v1/assistant/oficina/connect/finish") if len(calls) != 1 || calls[0].Header.Get("Authorization") != "Bearer "+token || !strings.Contains(calls[0].Body, `"state":"`+st+`"`) || !strings.Contains(calls[0].Body, `"code":"`+code+`"`) { t.Fatalf("calls: %+v", calls) } // The outcome reaches the chat once, and nothing secret is logged. home := a.get("/") if !strings.Contains(home.Body, "Oficina connected.") { t.Fatalf("home lacks the note:\n%s", home.Body) } if again := a.get("/"); strings.Contains(again.Body, "Oficina connected.") { t.Errorf("the note showed twice") } for _, secret := range []string{st, code} { if strings.Contains(a.logs.String(), secret) { t.Errorf("a log carried the state or code") } } } func TestOficinaDoneOutcomes(t *testing.T) { a, f := newAskApp(t) a.connectEquipo(f) for _, c := range []struct{ finish, want string }{ {"connected", "Oficina connected."}, {"denied", "Oficina wasn't connected."}, {"expired", "That link expired. Try again."}, {"unavailable", "Couldn't reach Oficina. Try again later."}, {"surprise", "Couldn't reach Oficina. Try again later."}, } { f.FinishWith(c.finish) if r := a.get(doneURL(fakeState(), fakeCode())); r.Code != 303 || r.Location() != "/" { t.Fatalf("%s: %d %q", c.finish, r.Code, r.Location()) } if home := a.get("/"); !strings.Contains(strings.ReplaceAll(home.Body, "'", "'"), c.want) { t.Errorf("%s: home lacks %q", c.finish, c.want) } } } func TestOficinaDoneReturnedOutcomeWithoutCode(t *testing.T) { a, f := newAskApp(t) a.connectEquipo(f) for param, want := range map[string]string{"denied": "Oficina wasn't connected.", "expired": "That link expired. Try again.", "unavailable": "Couldn't reach Oficina. Try again later."} { if r := a.get("/ask/oficina/done?oficina=" + param); r.Code != 303 || r.Location() != "/" { t.Fatalf("%s: %d %q", param, r.Code, r.Location()) } if home := a.get("/"); !strings.Contains(strings.ReplaceAll(home.Body, "'", "'"), want) { t.Errorf("%s: home lacks %q", param, want) } } if n := len(f.Calls("/api/v1/assistant/oficina/connect/finish")); n != 0 { t.Errorf("finished with no code") } // Only finishing connects: a bare "connected" says nothing. a, f = newAskApp(t) a.connectEquipo(f) if r := a.get("/ask/oficina/done?oficina=connected"); r.Code != 303 || r.Header.Get("Set-Cookie") != "" { t.Errorf("bare connected: %d %v", r.Code, r.Header) } // Anything else says nothing. if r := a.get("/ask/oficina/done?oficina=x"); r.Code != 303 || r.Header.Get("Set-Cookie") != "" { t.Errorf("odd outcome: %d %v", r.Code, r.Header) } } func TestOficinaDoneAnotherPersonsState(t *testing.T) { a, f := newAskApp(t) a.connectEquipo(f) r := a.get(doneURL(equipotest.OtherPersonsState, fakeCode())) if r.Code != 303 || r.Location() != "/" { t.Fatalf("%d %q", r.Code, r.Location()) } if home := a.get("/"); strings.Contains(home.Body, "Oficina connected.") { t.Error("someone else's state read as connected") } } func TestOficinaDoneLostBearer(t *testing.T) { a, f := newAskApp(t) a.connectEquipo(f) f.FailOficina(401, "unauthenticated") if r := a.get(doneURL(fakeState(), fakeCode())); r.Code != 303 || r.Location() != "/ask" { t.Errorf("%d %q", r.Code, r.Location()) } } func TestOficinaConnectCardLinkKeepsItsRel(t *testing.T) { a, f := newAskApp(t) a.connectEquipo(f) good := equipo.Link{Label: "Connect", URL: testOrigin + "/ask/oficina/connect", Rel: "oficina_connect"} elsewhere := equipo.Link{Label: "Other", URL: "https://evil.example/x", Rel: "oficina_connect"} // The same path on another host gets no same-tab treatment either. lookalike := equipo.Link{Label: "Lookalike", URL: "https://evil.example/ask/oficina/connect", Rel: "oficina_connect"} f.AskOutcome("Connect Oficina to go on.", nil, []equipo.Link{good, elsewhere, lookalike}) body := a.get("/ask/state").Body if !strings.Contains(body, `"rel":"oficina_connect"`) || strings.Count(body, `"rel":"oficina_connect"`) != 1 { t.Fatalf("state: %s", body) } page := a.get("/ask").Body if !strings.Contains(page, `Connect`) { t.Errorf("same-tab link missing:\n%s", page) } if !strings.Contains(page, `href="https://evil.example/ask/oficina/connect" target="_blank" rel="noopener">Lookalike`) { t.Errorf("lookalike link got same-tab treatment:\n%s", page) } if !strings.Contains(page, `href="https://evil.example/x" target="_blank" rel="noopener"`) { t.Errorf("other link not opened in a new tab:\n%s", page) } } // sends are the person's messages Base sent to Equipo, oldest first. func sends(f *equipotest.Fake) []string { var out []string for _, c := range f.Calls("/api/v1/assistant/messages") { if c.Method == "POST" { out = append(out, c.Body) } } return out } // Base never sends anything for the person on returning from connecting // Oficina, however it went: Equipo goes on with the conversation itself // once the connect card resolves. func TestOficinaDoneSendsNothing(t *testing.T) { for _, finish := range []string{"connected", "denied", "expired", "unavailable", "surprise"} { a, f := newAskApp(t) a.connectEquipo(f) f.FinishWith(finish) a.get(doneURL(fakeState(), fakeCode())) a.get("/") a.get("/") if got := sends(f); len(got) != 0 { t.Errorf("%s: sent %v", finish, got) } } a, f := newAskApp(t) a.connectEquipo(f) for _, target := range []string{"/ask/oficina/done?oficina=denied", "/ask/oficina/done?oficina=connected", "/ask/oficina/done?oficina=expired", doneURL(equipotest.OtherPersonsState, fakeCode())} { a.get(target) a.get("/") } if got := sends(f); len(got) != 0 { t.Errorf("sent %v", got) } } // The note sits in the conversation, after the line it follows (the reply // that asked to connect), not under it: what comes later goes below it. func TestOficinaNoteSitsInTheConversation(t *testing.T) { a, f := newAskApp(t) a.connectEquipo(f) f.Say("Connect Oficina so I can add it to your calendar.") a.get("/ask/oficina/done?oficina=denied") home := strings.ReplaceAll(a.get("/").Body, "'", "'") thread := home[strings.Index(home, `
    ")] reply := strings.Index(thread, "Connect Oficina so I can add it to your calendar.") note := strings.Index(thread, `
  1. Oficina wasn't connected.
  2. ") { t.Fatalf("the note is not in the conversation after the reply:\n%s", home) } if strings.Contains(home, `

    ")] for _, want := range []string{`autocomplete="off"`, `data-1p-ignore`, `data-lpignore="true"`, `data-form-type="other"`} { if !strings.Contains(field, want) { t.Errorf("%s: composer lacks %s: %s", path, want, field) } } for _, off := range []string{"spellcheck", "autocorrect", "autocapitalize", "inputmode"} { if strings.Contains(field, off) { t.Errorf("%s: composer sets %s: %s", path, off, field) } } } }