| 1 | package workspace |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 12 | ) |
| 13 | |
| 14 | func TestWorkspaceFetchUsesRemoteUsername(t *testing.T) { |
| 15 | dir := t.TempDir() |
| 16 | script := filepath.Join(dir, "git") |
| 17 | capture := filepath.Join(dir, "header") |
| 18 | sha := strings.Repeat("a", 40) |
| 19 | body := "#!/bin/sh\nif [ -n \"$GIT_CONFIG_VALUE_9\" ]; then\nprintf '%s' \"$GIT_CONFIG_VALUE_9\" > '" + capture + "'\nelse\nprintf '%s' '" + sha + "'\nfi\n" |
| 20 | if err := os.WriteFile(script, []byte(body), 0700); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | const repo = "amadan.net/o/r" |
| 24 | if err := os.Mkdir(filepath.Join(dir, "project-"+digest(repo)), 0700); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | for _, user := range []string{"", "x-access-token"} { |
| 28 | m := &Manager{cfg: Config{PrivateRoot: dir, GitPath: script, Forge: &forgeFake{remote: Remote{URL: "https://" + repo, GitPassword: "fixture-password", GitUsername: user}}}} |
| 29 | if _, _, err := m.fetch(context.Background(), team.TargetBinding{Repository: repo, BaseRevision: sha}); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | got, err := os.ReadFile(capture) |
| 33 | wantUser := user |
| 34 | if wantUser == "" { |
| 35 | wantUser = "x" |
| 36 | } |
| 37 | want := "Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(wantUser+":fixture-password")) |
| 38 | if err != nil || string(got) != want { |
| 39 | t.Fatalf("fetch authentication user %q: %q %v", user, got, err) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |