| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "log/slog" |
| 6 | "maps" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "reflect" |
| 11 | "regexp" |
| 12 | "slices" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | |
| 16 | "amadan.net/oficina/equipo/internal/equipo/github" |
| 17 | ) |
| 18 | |
| 19 | // githubBriefKeys are branch A's 31 keys from the reviewed copy brief. |
| 20 | var githubBriefKeys = []string{"github.title", "github.connect", "github.account", "github.account_type", "github.organization", "github.personal_account", "github.create_app", "github.create_hint", "github.confirm_account", "github.repository_count", "github.confirm", "github.connected", "github.already_connected", "github.disconnect", "github.disconnect_hint", "github.pending", "github.pending_hint", "github.pending_approval", "github.check_again", "github.redirect_expired", "github.pending_expired", "github.setup_failed", "github.app_remains", "github.app_settings", "github.reconnect", "github.reconnect_hint", "github.check_failed", "github.not_connected", "github.suspended", "github.too_many", "github.choose_repositories"} |
| 21 | |
| 22 | // githubStateFixtures has one explicit entry per typed state; nil means the |
| 23 | // state is never rendered. |
| 24 | var githubStateFixtures = map[github.State][]string{ |
| 25 | github.StateManifest: {"github.create_hint"}, github.StateConverting: {"github.create_hint"}, |
| 26 | github.StatePending: {"github.pending", "github.pending_hint", "github.pending_approval"}, |
| 27 | github.StateReady: {"github.confirm_account", "github.repository_count"}, |
| 28 | github.StateLive: {"github.connected", "github.disconnect_hint"}, |
| 29 | github.StateAlreadyConnected: {"github.already_connected", "github.connected", "github.disconnect_hint"}, |
| 30 | github.StateManifestExpired: {"github.setup_failed", "github.app_remains"}, |
| 31 | github.StateInstallationExpired: {"github.redirect_expired"}, |
| 32 | github.StateExpired: {"github.pending_expired", "github.app_remains"}, |
| 33 | github.StateFailed: {"github.setup_failed", "github.app_remains"}, |
| 34 | github.StateSuperseded: {"github.app_remains"}, |
| 35 | github.StateAbandoned: nil, |
| 36 | github.StateDisconnected: {"github.not_connected", "github.app_remains"}, |
| 37 | github.StateUninstalled: {"github.not_connected"}, |
| 38 | github.StateUninstalledExpired: {"github.not_connected", "github.app_remains"}, |
| 39 | github.StateReconnect: {"github.reconnect_hint", "github.app_remains"}, |
| 40 | github.StateCheckFailed: {"github.check_failed"}, github.StateSuspended: {"github.suspended"}, github.StateTooMany: {"github.too_many"}, |
| 41 | } |
| 42 | |
| 43 | func TestGitHubStateCopy(t *testing.T) { |
| 44 | if len(githubStateFixtures) != len(github.States) { |
| 45 | t.Fatalf("%d fixtures for %d typed states", len(githubStateFixtures), len(github.States)) |
| 46 | } |
| 47 | for _, state := range github.States { |
| 48 | t.Run(string(state), func(t *testing.T) { |
| 49 | want, ok := githubStateFixtures[state] |
| 50 | if !ok { |
| 51 | t.Fatal("typed state has no explicit fixture") |
| 52 | } |
| 53 | got, err := githubStateKeys(github.View{State: state, AccountType: "Organization"}) |
| 54 | if want == nil { |
| 55 | if err == nil { |
| 56 | t.Fatalf("hidden state rendered %v", got) |
| 57 | } |
| 58 | return |
| 59 | } |
| 60 | if err != nil || !reflect.DeepEqual(got, want) { |
| 61 | t.Fatalf("state keys %v (%v), want %v", got, err, want) |
| 62 | } |
| 63 | for _, key := range got { |
| 64 | if copyText(key) == "" || copyText(key) == key { |
| 65 | t.Fatalf("unrendered %s", key) |
| 66 | } |
| 67 | } |
| 68 | }) |
| 69 | } |
| 70 | if got, err := githubStateKeys(github.View{State: github.StatePending, AccountType: "User"}); err != nil || !reflect.DeepEqual(got, []string{"github.pending", "github.pending_hint"}) { |
| 71 | t.Fatalf("personal account: %v", got) |
| 72 | } |
| 73 | if got, err := githubStateKeys(github.View{State: "unknown"}); err == nil { |
| 74 | t.Fatalf("unknown state rendered %v", got) |
| 75 | } |
| 76 | if _, err := githubRow(httptest.NewRequest("GET", "/", nil), github.View{State: "unknown"}); err == nil { |
| 77 | t.Fatal("unknown state row rendered") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // The keys a page can render are exactly the brief's 31, with no fallback. |
| 82 | func TestGitHubRenderedKeys(t *testing.T) { |
| 83 | r := httptest.NewRequest("GET", "https://equipo.example/settings", nil) |
| 84 | rendered := map[string]bool{} |
| 85 | for _, state := range github.States { |
| 86 | for _, kind := range []string{"Organization", "User"} { |
| 87 | for _, v := range []github.View{{State: state, AccountType: kind, Login: "oficina"}, {State: state, AccountType: kind, Login: "oficina", CountKnown: true, Count: 3, InstallationID: 12}} { |
| 88 | row, err := githubRow(r, v) |
| 89 | if githubStateFixtures[state] == nil { |
| 90 | if err == nil { |
| 91 | t.Fatalf("hidden state %s rendered", state) |
| 92 | } |
| 93 | continue |
| 94 | } |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | for _, key := range row.Keys { |
| 99 | rendered[key] = true |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | for _, notice := range []string{"1", "check"} { |
| 105 | key, ok := githubNotice(notice) |
| 106 | if !ok { |
| 107 | t.Fatalf("notice %s unmapped", notice) |
| 108 | } |
| 109 | rendered[key] = true |
| 110 | } |
| 111 | if _, ok := githubNotice("other"); ok { |
| 112 | t.Fatal("unknown notice rendered") |
| 113 | } |
| 114 | raw, err := os.ReadFile("templates/github.html") |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | for _, m := range regexp.MustCompile(`copy "(github\.[a-z_]+)"`).FindAllStringSubmatch(string(raw), -1) { |
| 119 | rendered[m[1]] = true |
| 120 | } |
| 121 | got := slices.Sorted(maps.Keys(rendered)) |
| 122 | want := slices.Sorted(slices.Values(githubBriefKeys)) |
| 123 | if len(want) != 31 || !reflect.DeepEqual(got, want) { |
| 124 | t.Fatalf("rendered keys differ from the brief:\n got %v\nwant %v", got, want) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestGitHubSweepLog(t *testing.T) { |
| 129 | var out strings.Builder |
| 130 | logger := slog.New(slog.NewTextHandler(&out, nil)) |
| 131 | logGitHubSweep(logger, fmt.Errorf("ghs_secretvalue: %w", github.ErrUnavailable)) |
| 132 | line := out.String() |
| 133 | if !strings.Contains(line, copyGitHubSweep) || !strings.Contains(line, "kind=unavailable") || !strings.Contains(line, "err_type=") || strings.Contains(line, "ghs_") || strings.Contains(line, copyText("github.check_failed")) { |
| 134 | t.Fatalf("sweep log: %s", line) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestGitHubCSPIsolation(t *testing.T) { |
| 139 | policy := "default-src 'self'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'" |
| 140 | w := httptest.NewRecorder() |
| 141 | w.Header().Set("Content-Security-Policy", policy) |
| 142 | githubManifestCSP(w) |
| 143 | if got := w.Header().Get("Content-Security-Policy"); got != "default-src 'self'; base-uri 'self'; form-action 'self' https://github.com; frame-ancestors 'none'" { |
| 144 | t.Fatalf("manifest policy: %s", got) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestGitHubConfirmationAndCleanup(t *testing.T) { |
| 149 | r := httptest.NewRequest("GET", "https://equipo.example/settings", nil) |
| 150 | for _, v := range []github.View{{State: "too_many", InstallationID: 12, AccountType: "Organization", Login: "oficina"}, {State: "ready", CountKnown: false}, {State: "ready", CountKnown: true, Count: 1001}} { |
| 151 | if mustGitHubRow(t, r, v).Confirm { |
| 152 | t.Fatal("Connect shown without complete allowed count") |
| 153 | } |
| 154 | } |
| 155 | row := mustGitHubRow(t, r, github.View{State: "too_many", InstallationID: 12, AccountType: "Organization", Login: "oficina"}) |
| 156 | if !strings.Contains(row.Lines[0], "1,000") || row.RepositoriesURL != "https://github.com/organizations/oficina/settings/installations/12" || !row.Check { |
| 157 | t.Fatal("over-limit controls or localized bound missing") |
| 158 | } |
| 159 | for _, state := range []github.State{"expired", "uninstalled_expired", "disconnected", "reconnect", "failed", "manifest_expired", "superseded"} { |
| 160 | row := mustGitHubRow(t, r, github.View{State: state}) |
| 161 | if !row.Cleanup || row.Check { |
| 162 | t.Fatalf("cleanup state %s offers Check or omits cleanup", state) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestGitHubDisconnectHeldKey(t *testing.T) { |
| 168 | r := httptest.NewRequest("GET", "https://equipo.example/settings", nil) |
| 169 | for _, state := range []github.State{"pending", "ready", "installation_expired", "check_failed", "uninstalled", "suspended", "too_many", "live"} { |
| 170 | if !mustGitHubRow(t, r, github.View{State: state}).Disconnect { |
| 171 | t.Fatalf("owner cannot disconnect %s", state) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func mustGitHubRow(t *testing.T, r *http.Request, v github.View) githubRowView { |
| 177 | t.Helper() |
| 178 | row, err := githubRow(r, v) |
| 179 | if err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | return row |
| 183 | } |
| 184 | |