| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 12 | ) |
| 13 | |
| 14 | func TestBranchGitUsesRemoteUsername(t *testing.T) { |
| 15 | dir := t.TempDir() |
| 16 | script := filepath.Join(dir, "git") |
| 17 | if err := os.WriteFile(script, []byte("#!/bin/sh\nprintf '%s' \"$GIT_CONFIG_VALUE_9\"\n"), 0700); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | b := BranchPush{Git: script, GitDir: dir} |
| 21 | for _, user := range []string{"", "x-access-token"} { |
| 22 | remote := workspace.Remote{URL: "https://amadan.net/o/r", GitPassword: "fixture-password", GitUsername: user} |
| 23 | got, err := b.git(context.Background(), dir, &remote, "push") |
| 24 | wantUser := user |
| 25 | if wantUser == "" { |
| 26 | wantUser = "x" |
| 27 | } |
| 28 | want := "Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(wantUser+":fixture-password")) |
| 29 | if err != nil || got != want { |
| 30 | t.Fatalf("git authentication user %q: %q %v", user, got, err) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestPushDetailScrubsRemoteUsername(t *testing.T) { |
| 36 | remote := workspace.Remote{URL: "https://github.com/o/r", GitPassword: "abc12345", GitUsername: "x-access-token"} |
| 37 | encoded := base64.StdEncoding.EncodeToString([]byte(remote.Username() + ":" + remote.GitPassword)) |
| 38 | answer := pushAnswer("! rejected", "remote: diagnostic "+encoded+" "+remote.GitPassword, &remote) |
| 39 | tail := PushDetail(&PushError{Op: "push", Answer: answer, Err: ErrPushUncertain}) |
| 40 | if strings.Contains(tail, encoded) || strings.Contains(tail, remote.GitPassword) || !strings.Contains(tail, "diagnostic") { |
| 41 | t.Fatalf("credentials leaked or detail lost: %q", tail) |
| 42 | } |
| 43 | } |
| 44 | |