oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package github
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "net/http"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/runner/forge/forgetest"
13)
14
15func TestAppAPIContract(t *testing.T) {
16 g := forgetest.NewGitHub(t)
17 api := NewAPI(g.Transport(), func() time.Time { return g.Now })
18 ctx := context.Background()
19 app, err := api.Convert(ctx, g.Code)
20 if err != nil {
21 t.Fatal("conversion:", err)
22 }
23 if app.ID != g.AppID || app.Owner.ID != g.AccountID || app.pem != g.PEM {
24 t.Fatal("conversion lost identity or key")
25 }
26 verified, err := api.Verify(ctx, app)
27 if err != nil || verified.ID != app.ID {
28 t.Fatal("JWT verification:", err)
29 }
30 rows, err := api.Installations(ctx, app)
31 if err != nil || len(rows) != 1 {
32 t.Fatal("installation listing:", err)
33 }
34 installation, err := api.Installation(ctx, app, rows[0].ID)
35 if err != nil || installation.Account.ID != g.AccountID {
36 t.Fatal("installation read:", err)
37 }
38 for _, mode := range []string{"selected", "all"} {
39 g.Selection = mode
40 installation, err = api.Installation(ctx, app, g.InstallationID)
41 if err != nil || installation.Selection != mode {
42 t.Fatal("repository selection:", err)
43 }
44 }
45 inventory, err := api.Inventory(ctx, app, installation)
46 if err != nil || inventory.Count != 2 || len(inventory.Repositories) != 2 {
47 t.Fatal("metadata inventory:", err)
48 }
49 if _, err = api.Inventory(ctx, app, installation); err != nil {
50 t.Fatal(err)
51 }
52 if got := g.Calls("POST", fmt.Sprintf("/app/installations/%d/access_tokens", g.InstallationID)); got != 1 {
53 t.Fatalf("metadata token not cached in memory: %d", got)
54 }
55 api.Forget(ctx, app.ID)
56 if g.Calls("DELETE", "/installation/token") != 1 {
57 t.Fatal("metadata token not revoked")
58 }
59 if _, err = api.Convert(ctx, g.Code); err == nil {
60 t.Fatal("code accepted twice")
61 }
62 // Diagnostic formatting must not accidentally disclose a retained App key.
63 for _, value := range []string{fmt.Sprint(app), fmt.Sprintf("%#v", app)} {
64 if strings.Contains(value, g.PEM) || strings.Contains(value, "PRIVATE KEY") {
65 t.Fatal("App diagnostics disclose key")
66 }
67 }
68}
69
70func TestAppAPIRefusals(t *testing.T) {
71 for _, tc := range []struct {
72 name string
73 status int
74 header http.Header
75 body string
76 want error
77 }{
78 {"redirect", 302, http.Header{"Location": []string{"https://evil.example/"}}, "", ErrUnavailable},
79 {"access", 401, nil, "discarded body", ErrAccess},
80 {"rate403", 403, http.Header{"X-Ratelimit-Remaining": []string{"0"}}, "", ErrRateLimit},
81 {"secondary403", 403, http.Header{"Retry-After": []string{"20"}}, "", ErrRateLimit},
82 {"rate429", 429, nil, "", ErrRateLimit},
83 {"forbidden403", 403, nil, "", ErrAccess},
84 {"server", 503, nil, "", ErrUnavailable},
85 {"oversized", 200, nil, strings.Repeat("x", (2<<20)+1), ErrUnavailable},
86 } {
87 t.Run(tc.name, func(t *testing.T) {
88 g := forgetest.NewGitHub(t)
89 api := NewAPI(g.Transport(), func() time.Time { return g.Now })
90 app, err := api.Convert(context.Background(), g.Code)
91 if err != nil {
92 t.Fatal(err)
93 }
94 g.FailPath, g.FailStatus, g.FailHeaders, g.FailBody = "/app", tc.status, tc.header, tc.body+g.ClientSecret
95 _, err = api.Verify(context.Background(), app)
96 if !errors.Is(err, tc.want) {
97 t.Fatalf("wrong safe outcome: %v", err)
98 }
99 if strings.Contains(err.Error(), g.ClientSecret) || strings.Contains(err.Error(), g.Code) {
100 t.Fatal("API error contains a secret")
101 }
102 })
103 }
104}
105
106func TestAppInventoryBound(t *testing.T) {
107 for _, count := range []int{0, 100, 101, 1000, 1001} {
108 t.Run(fmt.Sprint(count), func(t *testing.T) {
109 g := forgetest.NewGitHub(t)
110 g.Count = count
111 api := NewAPI(g.Transport(), func() time.Time { return g.Now })
112 app, err := api.Convert(context.Background(), g.Code)
113 if err != nil {
114 t.Fatal(err)
115 }
116 inst, err := api.Installation(context.Background(), app, g.InstallationID)
117 if err != nil {
118 t.Fatal(err)
119 }
120 got, err := api.Inventory(context.Background(), app, inst)
121 if count > 1000 {
122 if !errors.Is(err, ErrTooMany) || got.Count != 0 || len(got.Repositories) != 0 {
123 t.Fatal("over-limit inventory was presented as complete")
124 }
125 return
126 }
127 if err != nil || got.Count != count || len(got.Repositories) != count {
128 t.Fatalf("incomplete inventory: count=%d err=%v", got.Count, err)
129 }
130 })
131 }
132}
133