| 1 | //go:build browser |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/chromedp/chromedp" |
| 11 | |
| 12 | "amadan.net/rastrillo/rastrillo" |
| 13 | "amadan.net/rastrillo/rastrillo/harness" |
| 14 | |
| 15 | "amadan.net/rastrillo/abono" |
| 16 | ) |
| 17 | |
| 18 | // TestBrowserSubscribeAndCancel drives the example in a real Chromium, |
| 19 | // through rastrillo's harness: sign in by emailed link, the plans, Checkout on the |
| 20 | // sandbox's page, Checkout's return, the account page, the sandbox's |
| 21 | // billing page, cancelling, and the account page saying so. Any console |
| 22 | // error, failed request or 4xx/5xx fails it at the screen it happened |
| 23 | // on. Without a browser it fails (harness.ChromePath); it never skips. |
| 24 | func TestBrowserSubscribeAndCancel(t *testing.T) { |
| 25 | box := &inbox{} |
| 26 | rig := harness.New(t, func(origin string) http.Handler { |
| 27 | opts, err := Options(testConfig(t, origin, box)) |
| 28 | if err != nil { |
| 29 | t.Fatalf("Options: %v", err) |
| 30 | } |
| 31 | h, closeApp, err := rastrillo.Handler(opts) |
| 32 | if err != nil { |
| 33 | t.Fatalf("rastrillo.Handler: %v", err) |
| 34 | } |
| 35 | t.Cleanup(func() { closeApp() }) |
| 36 | return h |
| 37 | }) |
| 38 | |
| 39 | // Ask for a sign-in link, open it from the inbox, and press Sign in. |
| 40 | rig.Run(chromedp.Navigate(rig.Origin + "/signin")) |
| 41 | rig.Screen(`#rst-signin-email`, "sign in") |
| 42 | rig.Run( |
| 43 | chromedp.SendKeys(`#rst-signin-email`, "reader@example.com", chromedp.ByQuery), |
| 44 | chromedp.Click(`form[action="/signin"] button[type=submit]`, chromedp.ByQuery), |
| 45 | ) |
| 46 | rig.Screen(`form[action="/signin/forget"]`, "the link was sent") |
| 47 | rig.Run(chromedp.Navigate(box.link(t, "reader@example.com"))) |
| 48 | rig.Screen(`form[action="/auth/verify"] button[type=submit]`, "the link's confirm page") |
| 49 | rig.Run(chromedp.Click(`form[action="/auth/verify"] button[type=submit]`, chromedp.ByQuery)) |
| 50 | rig.Screen(`form[action="/subscribe/annual"]`, "the plans, signed in") |
| 51 | |
| 52 | // Buy the annual plan on the sandbox's Checkout page. |
| 53 | rig.Run(chromedp.Click(`form[action="/subscribe/annual"] button[type=submit]`, chromedp.ByQuery)) |
| 54 | rig.Screen(`form[action$="/pay"]`, "the sandbox's checkout") |
| 55 | rig.Run(chromedp.Click(`form[action$="/pay"] button[type=submit]`, chromedp.ByQuery)) |
| 56 | rig.Screen(`main a[href="/account"]`, "Checkout's return") |
| 57 | mustSay(t, rig, "main", say("abono.pages.done_paid")) |
| 58 | |
| 59 | // The account page shows it renewing. |
| 60 | rig.Run(chromedp.Click(`main a[href="/account"]`, chromedp.ByQuery)) |
| 61 | rig.Screen(`form[action="/account/billing"]`, "the account, paid") |
| 62 | mustSay(t, rig, "main", say(abono.StatusRenews)) |
| 63 | |
| 64 | // Cancel on the sandbox's billing page, and come back. |
| 65 | rig.Run(chromedp.Click(`form[action="/account/billing"] button[type=submit]`, chromedp.ByQuery)) |
| 66 | rig.Screen(`form[action$="/cancel"] button[type=submit]`, "the sandbox's billing page") |
| 67 | rig.Run( |
| 68 | chromedp.Click(`form[action$="/cancel"] button[type=submit]`, chromedp.ByQuery), |
| 69 | chromedp.WaitNotPresent(`form[action$="/cancel"] button[type=submit]`, chromedp.ByQuery), |
| 70 | ) |
| 71 | rig.Screen(`a[href$="/account"]`, "the sandbox's billing page, cancelled") |
| 72 | rig.Run(chromedp.Click(`a[href$="/account"]`, chromedp.ByQuery)) |
| 73 | rig.Screen(`form[action="/account/billing"]`, "the account, cancelling") |
| 74 | mustSay(t, rig, "main", say(abono.StatusCancelling)) |
| 75 | } |
| 76 | |
| 77 | // mustSay fails unless sel's text on the current screen contains text. |
| 78 | func mustSay(t *testing.T, rig *harness.Rig, sel, text string) { |
| 79 | t.Helper() |
| 80 | var got string |
| 81 | rig.Run(chromedp.Text(sel, &got, chromedp.ByQuery)) |
| 82 | if !strings.Contains(got, text) { |
| 83 | t.Fatalf("%s does not say %q:\n%s", sel, text, got) |
| 84 | } |
| 85 | } |
| 86 | |