oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "encoding/base64"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "net/http"
10 "os"
11 "path/filepath"
12 "reflect"
13 "testing"
14
15 "amadan.net/oficina/equipo/internal/runner/forge"
16 "amadan.net/oficina/equipo/internal/runner/workspace"
17 "amadan.net/oficina/equipo/internal/wire/forgekey"
18)
19
20func TestRouterRefusalsKeepOperationIdentity(t *testing.T) {
21 ctx := context.Background()
22 f, _ := NewForges(&routeForge{host: forgekey.Hub})
23 for _, repo := range []string{"github.com/a", "github.com/o/r", "bad"} {
24 _, remote := f.Remote(ctx, repo, Read)
25 _, exists := f.Exists(ctx, repo)
26 create := f.Create(ctx, repo)
27 _, merge := f.Merge(ctx, repo, "main", forge.MergeRequest{})
28 _, hosted := f.CIHosted(ctx, repo)
29 _, status := f.CIStatus(ctx, repo, "main", forge.CILogsNone)
30 for _, tc := range []struct{ got, want error }{{remote, workspace.ErrGitPassword}, {exists, ErrNoCreate}, {create, ErrNoCreate}, {merge, forge.ErrMergeRefused}, {hosted, forge.ErrCIRead}, {status, forge.ErrCIRead}} {
31 if !errors.Is(tc.got, tc.want) || !errors.Is(tc.got, forgekey.ErrKey) {
32 t.Errorf("%q: %v lost %v or key identity", repo, tc.got, tc.want)
33 }
34 }
35 }
36 _, use := f.Remote(ctx, "o/r", Use(99))
37 _, duplicate := NewForges(&routeForge{host: forgekey.Hub}, &routeForge{host: forgekey.Hub})
38 if !errors.Is(use, ErrForgeUse) || !errors.Is(duplicate, ErrForgeDuplicate) || errors.Is(use, forgekey.ErrKey) || errors.Is(duplicate, forgekey.ErrKey) || errors.Is(use, duplicate) || !errors.Is(use, workspace.ErrGitPassword) {
39 t.Fatalf("use and registration need distinct identities: %v / %v", use, duplicate)
40 }
41}
42
43func TestRouterWithProductionFileForge(t *testing.T) {
44 ctx := context.Background()
45 const repo = "amadan.net/carlos/platform"
46 const branch = "equipo/t1-fix"
47 const sha = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
48 var calls []string
49 ff := accessForge(t, func(w http.ResponseWriter, r *http.Request) {
50 calls = append(calls, r.Method+" "+r.URL.Path)
51 if r.Header.Get("Authorization") != "Bearer SECRET-ACCESS-TOKEN" {
52 t.Error("missing hub credential")
53 }
54 switch r.URL.Path {
55 case "/api/v1/repos":
56 fmt.Fprint(w, `{"repos":[{"ns":"carlos","name":"platform"}]}`)
57 case "/api/v1/repos/carlos/platform/branches/" + branch + "/merge":
58 var req forge.MergeRequest
59 if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.ExpectSHA != sha {
60 t.Errorf("merge request: %+v %v", req, err)
61 }
62 fmt.Fprintf(w, `{"merged":true,"sha":%q,"into":"main"}`, sha)
63 case "/api/v1/repos/carlos/platform/ci/hosted":
64 fmt.Fprint(w, `{"on":true}`)
65 case "/api/v1/repos/carlos/platform/ci/status":
66 if r.URL.Query().Get("branch") != branch || r.URL.Query().Get("logs") != forge.CILogsFailed {
67 t.Error("CI query lost")
68 }
69 fmt.Fprintf(w, `{"ns":"carlos","repo":"platform","branch":%q,"sha":%q,"state":"passed"}`, branch, sha)
70 case "/api/v1/agents/access-requests":
71 w.WriteHeader(201)
72 writeAccess(w, "ar_one", "pending")
73 case "/api/v1/agents/access-requests/ar_one":
74 writeAccess(w, "ar_one", "approved")
75 default:
76 t.Error("unexpected route", r.URL)
77 w.WriteHeader(404)
78 }
79 })
80 if ff.Host() != forgekey.Hub {
81 t.Fatalf("production forge host: %q", ff.Host())
82 }
83 f, err := NewForges(ff)
84 if err != nil {
85 t.Fatal(err)
86 }
87 for _, use := range []Use{Read, Push} {
88 remote, err := f.Remote(ctx, repo, use)
89 if err != nil || remote.URL != "https://"+repo || remote.GitPassword != "SECRET-GIT-TOKEN" {
90 t.Fatalf("remote %d: %+v %v", use, remote, err)
91 }
92 }
93 res, err := f.Merge(ctx, repo, branch, forge.MergeRequest{ExpectSHA: sha})
94 if err != nil || !res.Merged || res.SHA != sha {
95 t.Fatalf("merge: %+v %v", res, err)
96 }
97 if hosted, err := f.CIHosted(ctx, repo); err != nil || !hosted {
98 t.Fatal("hosted", hosted, err)
99 }
100 if status, err := f.CIStatus(ctx, repo, branch, forge.CILogsFailed); err != nil || status.SHA != sha || status.State != "passed" {
101 t.Fatal("CI", status, err)
102 }
103 c := accessCommand()
104 if rep, err := f.RequestAccess(ctx, c); err != nil || rep.State != "pending" {
105 t.Fatal("request", rep, err)
106 }
107 if rep, err := f.PollAccess(ctx, "ar_one", c); err != nil || rep.State != "approved" {
108 t.Fatal("poll", rep, err)
109 }
110 // The read lists once; the push and the merge, writes with the
111 // agent-wide credential, each ask the hub afresh (writable).
112 want := []string{"GET /api/v1/repos", "GET /api/v1/repos", "GET /api/v1/repos", "POST /api/v1/repos/carlos/platform/branches/" + branch + "/merge", "GET /api/v1/repos/carlos/platform/ci/hosted", "GET /api/v1/repos/carlos/platform/ci/status", "POST /api/v1/agents/access-requests", "GET /api/v1/agents/access-requests/ar_one"}
113 if !reflect.DeepEqual(calls, want) {
114 t.Fatalf("hub calls: %v", calls)
115 }
116}
117
118type usernameForge struct {
119 Forge
120 remote workspace.Remote
121}
122
123func (f usernameForge) Remote(context.Context, string, Use) (workspace.Remote, error) {
124 return f.remote, nil
125}
126func TestHeadsUsesRemoteUsername(t *testing.T) {
127 root := t.TempDir()
128 script := filepath.Join(root, "git")
129 capture := filepath.Join(root, "header")
130 if err := os.WriteFile(script, []byte("#!/bin/sh\nprintf '%s' \"$GIT_CONFIG_VALUE_9\" > '"+capture+"'\n"), 0700); err != nil {
131 t.Fatal(err)
132 }
133 remote := workspace.Remote{URL: "https://github.com/o/r", GitPassword: "fixture-password", GitUsername: "x-access-token"}
134 c := Clones{Root: root, Git: script, Forge: usernameForge{remote: remote}}
135 if _, _, _, err := c.Heads(context.Background(), "github.com/o/r", nil); err != nil && !errors.Is(err, ErrEmptyRepo) {
136 t.Fatal(err)
137 }
138 got, err := os.ReadFile(capture)
139 want := "Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte("x-access-token:fixture-password"))
140 if err != nil || string(got) != want {
141 t.Fatalf("heads header %q: %v", got, err)
142 }
143}
144