| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strconv" |
| 7 | "sync/atomic" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | access "amadan.net/oficina/agent-access.git" |
| 12 | ) |
| 13 | |
| 14 | func TestDelegatedActorNamespaceAndSingleRateCharge(t *testing.T) { |
| 15 | f, p := linkedDelegated(t) |
| 16 | var checks atomic.Int64 |
| 17 | if e := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { checks.Add(1); return *p, nil }), f.app.agentIssuer, "acme"); e != nil { |
| 18 | t.Fatal(e) |
| 19 | } |
| 20 | now := time.Now() |
| 21 | f.app.store.clk.set(func() time.Time { return now }) |
| 22 | if rec := delegatedCall(f.app, "calls_create_meeting", delegatedInputForTest()); rec.Code != 200 { |
| 23 | t.Fatal(rec.Code, rec.Body.String()) |
| 24 | } |
| 25 | if checks.Load() < 3 { |
| 26 | t.Fatal("write did not reverify delegation") |
| 27 | } |
| 28 | person, e := f.app.delegatedPerson(t.Context(), *p, "acme") |
| 29 | if e != nil { |
| 30 | t.Fatal(e) |
| 31 | } |
| 32 | raw, _ := json.Marshal([]string{f.app.cfg.Home.Issuer, f.app.agentIssuer, p.Subject, p.Client, "acme", strconv.FormatInt(person.ID, 10)}) |
| 33 | want, e := f.app.sealer.Fingerprint("connection.actor.v1", string(raw)) |
| 34 | if e != nil { |
| 35 | t.Fatal(e) |
| 36 | } |
| 37 | var got string |
| 38 | if e = f.app.db.Writer().QueryRow("SELECT coordinate FROM calls_connections").Scan(&got); e != nil || got != want { |
| 39 | t.Fatal("actor namespace mismatch", e) |
| 40 | } |
| 41 | // One creation plus 59 reads exhaust exactly the 60-request allowance. |
| 42 | for i := 0; i < 59; i++ { |
| 43 | if rec := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme"}); rec.Code != 200 { |
| 44 | t.Fatal("one request charged repeatedly", i, rec.Code) |
| 45 | } |
| 46 | } |
| 47 | if rec := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme"}); rec.Code != 429 { |
| 48 | t.Fatal("request not charged", rec.Code) |
| 49 | } |
| 50 | } |
| 51 | |