| 1 | package team_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 13 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 14 | "amadan.net/oficina/equipo/internal/testci" |
| 15 | ) |
| 16 | |
| 17 | type noDescription struct{ t *testing.T } |
| 18 | |
| 19 | func (noDescription) BeforePush() bool { return true } |
| 20 | func (n noDescription) Set(context.Context, forge.Credential, forge.Description) error { |
| 21 | n.t.Fatal("description reached") |
| 22 | return nil |
| 23 | } |
| 24 | func (n noDescription) Read(context.Context, forge.Credential, string, string) (forge.Description, error) { |
| 25 | n.t.Fatal("description reached") |
| 26 | return forge.Description{}, nil |
| 27 | } |
| 28 | |
| 29 | func TestPublishCoreAuthorizationContract(t *testing.T) { |
| 30 | testci.Tools(t, "git") |
| 31 | a, now := team.PublicationAuthorizationFixture(t) |
| 32 | raw, h, err := forge.Canonical(a.Ticket.Dispatch.Payload) |
| 33 | if err != nil || !bytes.Equal(raw, a.Ticket.Dispatch.Payload) || h != a.Ticket.Dispatch.Hash || forge.CanonicalHash(a.Candidate) != a.Publication.CandidateHash || forge.Digest([]byte(a.Publication.Description)) != a.Publication.DescriptionHash { |
| 34 | t.Fatal("core publication hashes drifted", err) |
| 35 | } |
| 36 | for _, agent := range []string{"equipo~operator@example.test", "bot_1-2~person+tag@example.test", "person@example.test", "bot~a@b@c", "bot~space here@example.test", "bot~a@example.test\n"} { |
| 37 | t.Run(agent, func(t *testing.T) { |
| 38 | exported := false |
| 39 | p, err := forge.New(forge.Config{Root: filepath.Join(t.TempDir(), "private"), GitPath: "/usr/bin/git", Runner: a.Ticket.Handle.Runner, Incarnation: a.Ticket.Incarnation, Clock: func() time.Time { return now }, Authorize: func(context.Context, team.Dispatch) (forge.Authorization, error) { return a, nil }, Revalidate: func(context.Context, forge.Authorization) error { return nil }, Credential: func(context.Context, string) (forge.Credential, error) { |
| 40 | return forge.Credential{Remote: "https://" + a.Target.Repository, Agent: agent, Bearer: "offline-fixture", GitPassword: "git-offline-fixture"}, nil |
| 41 | }, Export: func(context.Context, team.GitCandidate, string) error { exported = true; return team.ErrNotDispatched }, Description: noDescription{t}}) |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | _, err = p.Execute(context.Background(), a.Ticket.Dispatch) |
| 46 | if !errors.Is(err, team.ErrNotDispatched) { |
| 47 | t.Fatal("export boundary class", err) |
| 48 | } |
| 49 | receipt := team.Receipt{Outcome: team.OutcomeSuccess, Fields: map[string]string{"repository": a.Publication.Repository, "ref": a.Publication.Ref, "qualified_ref": "refs/heads/" + a.Publication.Ref, "head": a.Publication.Head, "candidate_hash": a.Publication.CandidateHash, "description_hash": a.Publication.DescriptionHash, "agent": agent}} |
| 50 | payload, _ := json.Marshal(a.Publication) |
| 51 | accepted := team.ValidatePublicationReceipt(payload, receipt) == nil |
| 52 | if accepted != exported { |
| 53 | t.Fatalf("core/forge identity mismatch for %q: receipt=%v export=%v", agent, accepted, exported) |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |