oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "amadan.net/rastrillo/rastrillo/budget"
5 "bufio"
6 "context"
7 "io"
8 "net/http"
9 "os"
10 "path/filepath"
11 "strings"
12 "testing"
13 "time"
14)
15
16func TestDriveRefusesUnsafeStartup(t *testing.T) {
17 t.Parallel()
18 for _, addr := range []string{":8824", "0.0.0.0:8824", "localhost:8824", "[::]:8824", "100.64.0.1:8824"} {
19 if err := run(context.Background(), addr, filepath.Join(t.TempDir(), "drive.db"), io.Discard); err == nil {
20 t.Fatalf("accepted %s", addr)
21 }
22 }
23 if err := run(context.Background(), "127.0.0.1:0", "", io.Discard); err == nil {
24 t.Fatal("accepted missing scratch path")
25 }
26 for _, suffix := range []string{"", "-wal", "-shm"} {
27 path := filepath.Join(t.TempDir(), "drive.db")
28 if err := os.WriteFile(path+suffix, []byte("keep"), 0600); err != nil {
29 t.Fatal(err)
30 }
31 if err := run(context.Background(), "127.0.0.1:0", path, io.Discard); err == nil {
32 t.Fatal("accepted existing file")
33 }
34 b, err := os.ReadFile(path + suffix)
35 if err != nil || string(b) != "keep" {
36 t.Fatal("changed existing file")
37 }
38 }
39}
40
41func TestDriveRealSigninAndCleanup(t *testing.T) {
42 t.Parallel()
43 ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
44 defer cancel()
45 path := filepath.Join(t.TempDir(), "drive.db")
46 r, w := io.Pipe()
47 defer r.Close()
48 done := make(chan error, 1)
49 go func() { err := run(ctx, "127.0.0.1:0", path, w); w.Close(); done <- err }()
50 finished := false
51 t.Cleanup(func() {
52 cancel()
53 r.Close()
54 if !finished {
55 <-done
56 }
57 })
58 scanner := bufio.NewScanner(r)
59 if !scanner.Scan() {
60 cancel()
61 err := <-done
62 finished = true
63 t.Fatalf("no sign-in link: run: %v; read: %v", err, scanner.Err())
64 }
65 line := scanner.Text()
66 link := strings.Split(strings.TrimPrefix(line, "["), "](")[0]
67 c := newClient()
68 if err := confirm(c, link); err != nil {
69 t.Fatal(err)
70 }
71 origin := strings.Split(link, "/auth/")[0]
72 response, err := c.Get(origin + "/projects/accounts")
73 if err != nil {
74 t.Fatal(err)
75 }
76 b, err := io.ReadAll(response.Body)
77 response.Body.Close()
78 if err != nil || response.StatusCode != http.StatusOK {
79 t.Fatalf("projects: %d %v", response.StatusCode, err)
80 }
81 for _, kind := range []string{"app", "website", "repository"} {
82 if !strings.Contains(string(b), `data-project-kind="`+kind+`"`) {
83 t.Fatalf("missing %s", kind)
84 }
85 }
86 cancel()
87 err = <-done
88 finished = true
89 if err != nil {
90 t.Fatal(err)
91 }
92 for _, suffix := range []string{"", "-wal", "-shm"} {
93 if _, err := os.Stat(path + suffix); !os.IsNotExist(err) {
94 t.Fatalf("scratch remains: %s: %v", suffix, err)
95 }
96 }
97}
98
99func TestMain(m *testing.M) { os.Exit(budget.Main(m)) }
100