| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/chromedp/chromedp" |
| 10 | |
| 11 | "amadan.net/rastrillo/rastrillo/harness" |
| 12 | |
| 13 | "amadan.net/oficina/base/internal/consumer" |
| 14 | ) |
| 15 | |
| 16 | // count is how many requests to path with method an app received. |
| 17 | func count(rec *recorder, method, path string) int { |
| 18 | n := 0 |
| 19 | for _, r := range rec.all() { |
| 20 | if r.method == method && r.path == path { |
| 21 | n++ |
| 22 | } |
| 23 | } |
| 24 | return n |
| 25 | } |
| 26 | |
| 27 | // Findings 7 and 8, in the browser: each installed app's wordmark goes back |
| 28 | // to Base's Home, and each app has its own Sign out. Signing out of |
| 29 | // Scratchpad lands on its signed-out page; signing in again from there is a |
| 30 | // fresh handoff, so the app then shows whoever Base vouches for now. |
| 31 | func TestInstalledAppsLinkHomeAndSignOut(t *testing.T) { |
| 32 | w := newWorld(t) |
| 33 | signInByEmail(t, w.rig, w.baseOrigin, w.base.mail) |
| 34 | |
| 35 | note := chromedp.CSS(`textarea[name="note"]`) |
| 36 | drive(t, w.rig, chromedp.Click(chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.padOrigin+"/"))), chromedp.WaitVisible(note)) |
| 37 | if got := harness.Evaluate[string](w.rig, `document.querySelector(".crumb a.wordmark").href`); got != w.baseOrigin+"/" { |
| 38 | t.Errorf("Scratchpad's wordmark links to %q, want %s/", got, w.baseOrigin) |
| 39 | } |
| 40 | signOut := chromedp.CSS(`form[action="/auth/signout"] button[type="submit"]`) |
| 41 | if got := harness.Evaluate[string](w.rig, `document.querySelector('form[action="/auth/signout"] button').textContent`); got != "Sign out of Scratchpad" { |
| 42 | t.Errorf("Scratchpad's sign-out button says %q", got) |
| 43 | } |
| 44 | // Signing out lands on the app's own signed-out page: the form's |
| 45 | // redirect chain stays on Scratchpad's origin, as its CSP requires. |
| 46 | again := chromedp.CSS(`a.btn[href="/"]`) |
| 47 | drive(t, w.rig, chromedp.Click(signOut), chromedp.WaitVisible(again)) |
| 48 | w.rig.Screen("main", "signed out of Scratchpad") |
| 49 | if here := harness.Read(w.rig, chromedp.Location()); here != w.padOrigin+consumer.SignedOutPath { |
| 50 | t.Fatalf("signing out ended at %s", here) |
| 51 | } |
| 52 | if n := count(w.pad, "POST", consumer.SignOutPath); n != 1 { |
| 53 | t.Errorf("Scratchpad got %d sign-out POSTs, want 1", n) |
| 54 | } |
| 55 | // Signing in again is a fresh handoff from Base. |
| 56 | drive(t, w.rig, chromedp.Click(again), chromedp.WaitVisible(note)) |
| 57 | if n := count(w.pad, "POST", consumer.AssertionPath); n != 2 { |
| 58 | t.Errorf("Scratchpad got %d assertion POSTs, want a second one after signing in again", n) |
| 59 | } |
| 60 | if here := harness.Read(w.rig, chromedp.Location()); here != w.padOrigin+"/" { |
| 61 | t.Errorf("signing in again ended at %s", here) |
| 62 | } |
| 63 | |
| 64 | welcomeTile := chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.welcomeOrigin+"/")) |
| 65 | drive(t, w.rig, chromedp.Click(chromedp.CSS(".crumb a.wordmark")), chromedp.WaitVisible(welcomeTile)) |
| 66 | if here := harness.Read(w.rig, chromedp.Location()); here != w.baseOrigin+"/" { |
| 67 | t.Fatalf("Scratchpad's wordmark went to %s, want Base's Home", here) |
| 68 | } |
| 69 | |
| 70 | drive(t, w.rig, chromedp.Click(welcomeTile), chromedp.WaitVisible(signOut)) |
| 71 | w.rig.Screen("main", "Welcome") |
| 72 | if got := harness.Evaluate[string](w.rig, `document.querySelector('form[action="/auth/signout"] button').textContent`); got != "Sign out of Welcome" { |
| 73 | t.Errorf("Welcome's sign-out button says %q", got) |
| 74 | } |
| 75 | if got := harness.Evaluate[string](w.rig, `document.querySelector(".crumb a.wordmark").href`); got != w.baseOrigin+"/" { |
| 76 | t.Errorf("Welcome's wordmark links to %q, want %s/", got, w.baseOrigin) |
| 77 | } |
| 78 | |
| 79 | // Base's own inner pages link Home too. |
| 80 | drive(t, w.rig, chromedp.Navigate(w.baseOrigin+"/people"), chromedp.Click(chromedp.CSS(".crumb a.wordmark")), chromedp.WaitVisible(welcomeTile)) |
| 81 | if here := harness.Read(w.rig, chromedp.Location()); here != w.baseOrigin+"/" { |
| 82 | t.Errorf("People's wordmark went to %s", here) |
| 83 | } |
| 84 | } |
| 85 | |