| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/chromedp/chromedp" |
| 12 | |
| 13 | "amadan.net/rastrillo/rastrillo/harness" |
| 14 | |
| 15 | "amadan.net/oficina/base/internal/equipo" |
| 16 | ) |
| 17 | |
| 18 | // A card's Connect link (rel oficina_connect) to Base's own connect page |
| 19 | // opens in the same tab; the same path on another host gets no such |
| 20 | // treatment and keeps its new tab. |
| 21 | func TestAskOficinaConnectLinkIsSameTab(t *testing.T) { |
| 22 | rig, f := askBase(t) |
| 23 | openPanel(t, rig) |
| 24 | f.OficinaConsent(f.URL + "/consent") |
| 25 | id := f.AskOutcome("Connect Oficina to go on.", nil, []equipo.Link{ |
| 26 | {Label: "Connect", URL: rig.Origin + "/ask/oficina/connect", Rel: "oficina_connect"}, |
| 27 | {Label: "Lookalike", URL: "https://evil.example.test/ask/oficina/connect", Rel: "oficina_connect"}, |
| 28 | }) |
| 29 | card := `#ask-panel [data-card="` + id + `"]` |
| 30 | drive(t, rig, chromedp.Poll[chromedp.Void](`document.querySelectorAll(`+strconv.Quote(card+` a.card-link`)+`).length === 2`, |
| 31 | chromedp.WithPollingTimeout(10*time.Second))) |
| 32 | got := harness.Evaluate[string](rig, `[...document.querySelectorAll(`+strconv.Quote(card+` a.card-link`)+`)] |
| 33 | .map(a => a.textContent + ":" + (a.getAttribute("target") || "-")).join("|")`) |
| 34 | if got != "Connect:-|Lookalike:_blank" { |
| 35 | t.Fatalf("link targets %q, want Connect:-|Lookalike:_blank", got) |
| 36 | } |
| 37 | // Clicking it leaves this tab for the consent address. |
| 38 | drive(t, rig, chromedp.Click(chromedp.CSS(card+` a.card-link`)), chromedp.Sleep(500*time.Millisecond)) |
| 39 | drive(t, rig, chromedp.Poll[chromedp.Void](`location.href.startsWith(`+strconv.Quote(f.URL)+`)`, chromedp.WithPollingTimeout(10*time.Second))) |
| 40 | if n := strings.Count(harness.Evaluate[string](rig, `location.href`), f.URL); n != 1 { |
| 41 | t.Errorf("not on the consent address") |
| 42 | } |
| 43 | } |
| 44 | |