| 1 | package idtest |
| 2 | |
| 3 | // scripts/staging-thin.py signs the owner and the suite in by logged mail: |
| 4 | // it fetches the door's email way and posts its /auth/begin form. Its |
| 5 | // parser runs here over the door Equipo really draws, in both modes, and |
| 6 | // the fields it reads are posted as the script posts them. |
| 7 | |
| 8 | import ( |
| 9 | "encoding/json" |
| 10 | "net/url" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/equipo/internal/testci" |
| 18 | "amadan.net/rastrillo/rastrillo/pow" |
| 19 | ) |
| 20 | |
| 21 | const stagingParse = `import importlib.util, json, sys |
| 22 | spec = importlib.util.spec_from_file_location('staging', sys.argv[1]) |
| 23 | staging = importlib.util.module_from_spec(spec) |
| 24 | spec.loader.exec_module(staging) |
| 25 | try: |
| 26 | print(json.dumps(staging.FormParser(sys.stdin.read(), '/auth/begin').fields)) |
| 27 | except RuntimeError as e: |
| 28 | print(e, file=sys.stderr) |
| 29 | sys.exit(3) |
| 30 | ` |
| 31 | |
| 32 | // stagingForm runs the staging script's FormParser over page: the fields of |
| 33 | // its /auth/begin form, or ok false when it finds none. |
| 34 | func stagingForm(t *testing.T, page string) (url.Values, bool) { |
| 35 | t.Helper() |
| 36 | testci.Tools(t, "python3") |
| 37 | testci.Files(t, "../../scripts/staging-thin.py") |
| 38 | cmd := exec.Command("python3", "-I", "-c", stagingParse, "../../scripts/staging-thin.py") |
| 39 | cmd.Stdin = strings.NewReader(page) |
| 40 | cmd.Env = []string{"PYTHONDONTWRITEBYTECODE=1", "TMPDIR=" + t.TempDir()} |
| 41 | out, err := cmd.Output() |
| 42 | if err != nil { |
| 43 | if exit, ok := err.(*exec.ExitError); ok && exit.ExitCode() == 3 { |
| 44 | return nil, false |
| 45 | } |
| 46 | t.Fatalf("staging parser: %v %s", err, out) |
| 47 | } |
| 48 | var fields map[string]string |
| 49 | if err := json.Unmarshal(out, &fields); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | form := url.Values{} |
| 53 | for k, v := range fields { |
| 54 | form.Set(k, v) |
| 55 | } |
| 56 | return form, true |
| 57 | } |
| 58 | |
| 59 | func TestStagingSigninFindsTheEmailForm(t *testing.T) { |
| 60 | t.Parallel() |
| 61 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 62 | script, err := os.ReadFile("../../scripts/staging-thin.py") |
| 63 | if err != nil || strings.Count(string(script), `opener.open(ORIGIN + '/signin?way=email'`) != 1 { |
| 64 | t.Fatal("staging-thin.py no longer fetches /signin?way=email") |
| 65 | } |
| 66 | // The plain door with Oficina ID offers it first, and no email form: |
| 67 | // what broke staging. |
| 68 | if _, ok := stagingForm(t, f.request("GET", "/signin", "", nil).Body.String()); ok == home { |
| 69 | t.Fatalf("plain /signin has an email form: %v, want %v", ok, !home) |
| 70 | } |
| 71 | page := f.request("GET", "/signin?way=email", "", nil) |
| 72 | form, ok := stagingForm(t, page.Body.String()) |
| 73 | if !ok { |
| 74 | t.Fatal("the staging parser finds no /auth/begin form on /signin?way=email") |
| 75 | } |
| 76 | // Posted as the script posts it, it sends the link. |
| 77 | form.Set("address", address) |
| 78 | form.Set("force", "1") |
| 79 | time.Sleep(pow.DefaultMinAge + 20*time.Millisecond) |
| 80 | begin := f.request("POST", "/auth/begin", f.origin, form, page.Result().Cookies()...) |
| 81 | if begin.Code != 303 || !strings.Contains(begin.Header().Get("Location"), "sent=1") { |
| 82 | t.Fatalf("begin %d %s", begin.Code, begin.Header().Get("Location")) |
| 83 | } |
| 84 | f.mailedLink(t) |
| 85 | }) |
| 86 | } |
| 87 | |