//go:build browser package browsertest import ( "fmt" "testing" "github.com/chromedp/chromedp" "amadan.net/rastrillo/rastrillo/harness" "amadan.net/oficina/base/internal/consumer" ) // count is how many requests to path with method an app received. func count(rec *recorder, method, path string) int { n := 0 for _, r := range rec.all() { if r.method == method && r.path == path { n++ } } return n } // Findings 7 and 8, in the browser: each installed app's wordmark goes back // to Base's Home, and each app has its own Sign out. Signing out of // Scratchpad lands on its signed-out page; signing in again from there is a // fresh handoff, so the app then shows whoever Base vouches for now. func TestInstalledAppsLinkHomeAndSignOut(t *testing.T) { w := newWorld(t) signInByEmail(t, w.rig, w.baseOrigin, w.base.mail) note := chromedp.CSS(`textarea[name="note"]`) drive(t, w.rig, chromedp.Click(chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.padOrigin+"/"))), chromedp.WaitVisible(note)) if got := harness.Evaluate[string](w.rig, `document.querySelector(".crumb a.wordmark").href`); got != w.baseOrigin+"/" { t.Errorf("Scratchpad's wordmark links to %q, want %s/", got, w.baseOrigin) } signOut := chromedp.CSS(`form[action="/auth/signout"] button[type="submit"]`) if got := harness.Evaluate[string](w.rig, `document.querySelector('form[action="/auth/signout"] button').textContent`); got != "Sign out of Scratchpad" { t.Errorf("Scratchpad's sign-out button says %q", got) } // Signing out lands on the app's own signed-out page: the form's // redirect chain stays on Scratchpad's origin, as its CSP requires. again := chromedp.CSS(`a.btn[href="/"]`) drive(t, w.rig, chromedp.Click(signOut), chromedp.WaitVisible(again)) w.rig.Screen("main", "signed out of Scratchpad") if here := harness.Read(w.rig, chromedp.Location()); here != w.padOrigin+consumer.SignedOutPath { t.Fatalf("signing out ended at %s", here) } if n := count(w.pad, "POST", consumer.SignOutPath); n != 1 { t.Errorf("Scratchpad got %d sign-out POSTs, want 1", n) } // Signing in again is a fresh handoff from Base. drive(t, w.rig, chromedp.Click(again), chromedp.WaitVisible(note)) if n := count(w.pad, "POST", consumer.AssertionPath); n != 2 { t.Errorf("Scratchpad got %d assertion POSTs, want a second one after signing in again", n) } if here := harness.Read(w.rig, chromedp.Location()); here != w.padOrigin+"/" { t.Errorf("signing in again ended at %s", here) } welcomeTile := chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.welcomeOrigin+"/")) drive(t, w.rig, chromedp.Click(chromedp.CSS(".crumb a.wordmark")), chromedp.WaitVisible(welcomeTile)) if here := harness.Read(w.rig, chromedp.Location()); here != w.baseOrigin+"/" { t.Fatalf("Scratchpad's wordmark went to %s, want Base's Home", here) } drive(t, w.rig, chromedp.Click(welcomeTile), chromedp.WaitVisible(signOut)) w.rig.Screen("main", "Welcome") if got := harness.Evaluate[string](w.rig, `document.querySelector('form[action="/auth/signout"] button').textContent`); got != "Sign out of Welcome" { t.Errorf("Welcome's sign-out button says %q", got) } if got := harness.Evaluate[string](w.rig, `document.querySelector(".crumb a.wordmark").href`); got != w.baseOrigin+"/" { t.Errorf("Welcome's wordmark links to %q, want %s/", got, w.baseOrigin) } // Base's own inner pages link Home too. drive(t, w.rig, chromedp.Navigate(w.baseOrigin+"/people"), chromedp.Click(chromedp.CSS(".crumb a.wordmark")), chromedp.WaitVisible(welcomeTile)) if here := harness.Read(w.rig, chromedp.Location()); here != w.baseOrigin+"/" { t.Errorf("People's wordmark went to %s", here) } }