| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync/atomic" |
| 6 | "testing" |
| 7 | |
| 8 | access "amadan.net/oficina/agent-access.git" |
| 9 | ) |
| 10 | |
| 11 | func TestDelegatedMeetingRetryFinalCheckAndLiveMarker(t *testing.T) { |
| 12 | for _, mode := range []string{"retry-final", "marker-create", "marker-retry"} { |
| 13 | t.Run(mode, func(t *testing.T) { |
| 14 | f, p := linkedDelegated(t) |
| 15 | input := delegatedInputForTest() |
| 16 | if rec := delegatedCall(f.app, "calls_create_meeting", input); rec.Code != 200 { |
| 17 | t.Fatal(rec.Code) |
| 18 | } |
| 19 | want := 503 |
| 20 | if mode == "retry-final" { |
| 21 | want = 401 |
| 22 | var checks atomic.Int64 |
| 23 | if e := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 24 | if checks.Add(1) >= 3 { |
| 25 | return access.Principal{}, access.Fail("unauthenticated", "Revoked.") |
| 26 | } |
| 27 | return *p, nil |
| 28 | }), f.app.agentIssuer, "acme"); e != nil { |
| 29 | t.Fatal(e) |
| 30 | } |
| 31 | } else { |
| 32 | if _, e := f.app.db.Writer().Exec("UPDATE calls_agent_state SET keycheck='damaged'"); e != nil { |
| 33 | t.Fatal(e) |
| 34 | } |
| 35 | if mode == "marker-create" { |
| 36 | input["client_id"] = "15bb7913-1316-4b96-bc3a-4b4cded1ded4" |
| 37 | } |
| 38 | } |
| 39 | if rec := delegatedCall(f.app, "calls_create_meeting", input); rec.Code != want { |
| 40 | t.Fatal("boundary ignored", mode, rec.Code, rec.Body.String()) |
| 41 | } |
| 42 | for _, table := range []string{"meetings", "calls_agent_meetings"} { |
| 43 | var n int |
| 44 | if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); e != nil || n != 1 { |
| 45 | t.Fatal("retry wrote extra effect", n, e) |
| 46 | } |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | func TestDelegatedMeetingVerifierContinuity(t *testing.T) { |
| 52 | for _, field := range []string{"subject", "client", "grant", "team", "scope"} { |
| 53 | t.Run(field, func(t *testing.T) { |
| 54 | f, p := linkedDelegated(t) |
| 55 | var checks atomic.Int64 |
| 56 | if e := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 57 | current := *p |
| 58 | if checks.Add(1) > 1 { |
| 59 | switch field { |
| 60 | case "subject": |
| 61 | current.Subject = "different" |
| 62 | case "client": |
| 63 | current.Client = "different" |
| 64 | case "grant": |
| 65 | current.Grant = "different" |
| 66 | case "team": |
| 67 | current.Teams = []string{"green"} |
| 68 | case "scope": |
| 69 | current.Scopes = []string{"calls:read"} |
| 70 | } |
| 71 | } |
| 72 | return current, nil |
| 73 | }), f.app.agentIssuer, "acme"); e != nil { |
| 74 | t.Fatal(e) |
| 75 | } |
| 76 | if rec := delegatedCall(f.app, "calls_create_meeting", delegatedInputForTest()); rec.Code != 403 { |
| 77 | t.Fatal("changed principal accepted", field, rec.Code) |
| 78 | } |
| 79 | var n int |
| 80 | if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM meetings").Scan(&n); e != nil || n != 0 { |
| 81 | t.Fatal("changed principal wrote", n, e) |
| 82 | } |
| 83 | }) |
| 84 | } |
| 85 | } |
| 86 | |