oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package forge
2
3import (
4 "context"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9)
10
11func TestForgeCredentialsSplitAndLegacy(t *testing.T) {
12 for _, tc := range []struct {
13 name, entry string
14 git bool
15 }{
16 {"split", `{"api":"api-canary","git":"git-canary"}`, true},
17 {"legacy", `"api-canary"`, false},
18 {"api only", `{"api":"api-canary"}`, false},
19 } {
20 t.Run(tc.name, func(t *testing.T) {
21 path := filepath.Join(t.TempDir(), "amadan.json")
22 raw := `{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/repo":` + tc.entry + `}}`
23 must(t, os.WriteFile(path, []byte(raw), 0600))
24 f := FileCredentials{Path: path}
25 c, err := f.Credential(context.Background(), "amadan.net/ns/repo")
26 if err != nil || c.Bearer != "api-canary" {
27 t.Fatal("API credential unavailable", err)
28 }
29 remote, err := f.Resolve(context.Background(), "amadan.net/ns/repo")
30 if tc.git && (err != nil || c.GitPassword != "git-canary" || remote.GitPassword != "git-canary") {
31 t.Fatal("git credential unavailable", err)
32 }
33 if !tc.git && (err == nil || !strings.Contains(err.Error(), "git password")) {
34 t.Fatal("missing PAT needs a clear configuration error", err)
35 }
36 if _, err := f.Credential(context.Background(), "amadan.net/ns/other"); err == nil {
37 t.Fatal("credential crossed repository boundary")
38 }
39 })
40 }
41}
42
43func TestForgeCredentialFileProtection(t *testing.T) {
44 path := filepath.Join(t.TempDir(), "amadan.json")
45 raw := `{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/repo":{"api":"api-canary","git":"git-canary"}}}`
46 must(t, os.WriteFile(path, []byte(raw), 0600))
47 must(t, os.Chmod(path, 0644))
48 if _, err := (FileCredentials{Path: path}).Credential(context.Background(), "amadan.net/ns/repo"); err == nil {
49 t.Fatal("public credential file accepted")
50 }
51 must(t, os.Chmod(path, 0600))
52 alias := filepath.Join(t.TempDir(), "alias")
53 must(t, os.Symlink(path, alias))
54 if _, err := (FileCredentials{Path: alias}).Credential(context.Background(), "amadan.net/ns/repo"); err == nil {
55 t.Fatal("symlink credential file accepted")
56 }
57}
58
59func TestForgeCredentialInvalidEntries(t *testing.T) {
60 for _, entry := range []string{`null`, `42`, `[]`, `{}`, `{"git":"git-canary"}`, `{"api":"api-canary","git":123}`, `{"api":"api-canary","git":"git-canary","extra":"secret-canary"}`, `{"api":"api-canary\r\nInjected: secret-canary","git":"git-canary"}`, `{"api":"api-canary","git":"git-canary\u0000"}`} {
61 t.Run(entry, func(t *testing.T) {
62 path := filepath.Join(t.TempDir(), "amadan.json")
63 raw := `{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/repo":` + entry + `}}`
64 must(t, os.WriteFile(path, []byte(raw), 0600))
65 _, err := (FileCredentials{Path: path}).Resolve(context.Background(), "amadan.net/ns/repo")
66 if err == nil {
67 t.Fatal("invalid credential accepted")
68 }
69 for _, secret := range []string{"api-canary", "git-canary", "secret-canary"} {
70 if strings.Contains(err.Error(), secret) {
71 t.Fatal("credential leaked in error")
72 }
73 }
74 })
75 }
76}
77
78// TestForgeCredentialAgentWide: one {agent, api, git} entry serves any
79// repository; a per-repository entry overrides it for that repository.
80func TestForgeCredentialAgentWide(t *testing.T) {
81 path := filepath.Join(t.TempDir(), "amadan.json")
82 raw := `{"agent":"equipo~owner@example.test","api":"api-agent","git":"git-agent","repositories":{"amadan.net/ns/special":{"api":"api-special","git":"git-special"}}}`
83 must(t, os.WriteFile(path, []byte(raw), 0600))
84 f := FileCredentials{Path: path}
85 ctx := context.Background()
86 c, err := f.Credential(ctx, "amadan.net/woodstar/woodstar")
87 if err != nil || c.Bearer != "api-agent" || c.GitPassword != "git-agent" || c.Remote != "https://amadan.net/woodstar/woodstar" || c.Agent != "equipo~owner@example.test" {
88 t.Fatalf("agent-wide %+v %v", c, err)
89 }
90 if c, err = f.Credential(ctx, "amadan.net/ns/special"); err != nil || c.GitPassword != "git-special" {
91 t.Fatalf("override %+v %v", c, err)
92 }
93 if c, err = f.Agent(ctx); err != nil || c.Bearer != "api-agent" || c.Remote != "" {
94 t.Fatalf("agent %+v %v", c, err)
95 }
96 for _, bad := range []string{"github.com/x/y", "amadan.net/../x", "amadan.net/ns"} {
97 if _, err := f.Credential(ctx, bad); err == nil {
98 t.Fatalf("agent credential for %q", bad)
99 }
100 }
101 names, err := f.RepositoryNames()
102 if err != nil || len(names) != 1 {
103 t.Fatalf("names %v %v", names, err)
104 }
105 // A file of per-repository entries only still has an agent credential.
106 legacy := filepath.Join(t.TempDir(), "legacy.json")
107 must(t, os.WriteFile(legacy, []byte(`{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/a":{"api":"api-a","git":"git-a"}}}`), 0600))
108 if c, err := (FileCredentials{Path: legacy}).Agent(ctx); err != nil || c.GitPassword != "git-a" {
109 t.Fatalf("legacy agent %+v %v", c, err)
110 }
111 if _, err := (FileCredentials{Path: legacy}).Credential(ctx, "amadan.net/ns/b"); err == nil {
112 t.Fatal("a per-repository file crossed repositories")
113 }
114}
115
116// TestForgeCredentialLegacyAgentRule: in a file without an agent-wide api
117// the agent credential is the one bearer all usable entries share, never
118// whichever entry sorts first; entries with different bearers give none.
119func TestForgeCredentialLegacyAgentRule(t *testing.T) {
120 ctx := context.Background()
121 write := func(raw string) FileCredentials {
122 path := filepath.Join(t.TempDir(), "amadan.json")
123 must(t, os.WriteFile(path, []byte(raw), 0600))
124 return FileCredentials{Path: path}
125 }
126 shared := write(`{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/a":{"git":"git-broken"},"amadan.net/ns/b":{"api":"api-one","git":"git-one"},"amadan.net/ns/c":"api-one"}}`)
127 if c, err := shared.Agent(ctx); err != nil || c.Bearer != "api-one" || c.GitPassword != "git-one" || c.Remote != "" {
128 t.Fatalf("shared legacy %+v %v", c.Remote, err)
129 }
130 if !shared.Legacy() {
131 t.Fatal("legacy file not reported")
132 }
133 mixed := write(`{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/a":{"api":"api-a","git":"git-a"},"amadan.net/ns/b":{"api":"api-b","git":"git-b"}}}`)
134 if c, err := mixed.Agent(ctx); err == nil {
135 t.Fatalf("ambiguous legacy file chose %q", c.Bearer)
136 }
137 wide := write(`{"agent":"equipo~owner@example.test","api":"api-agent","git":"git-agent","repositories":{"amadan.net/ns/a":{"api":"api-a","git":"git-a"}}}`)
138 if c, err := wide.Agent(ctx); err != nil || c.Bearer != "api-agent" || wide.Legacy() {
139 t.Fatalf("agent-wide %+v %v", c.Remote, err)
140 }
141}
142