| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "sync/atomic" |
| 10 | "testing" |
| 11 | |
| 12 | access "amadan.net/oficina/agent-access.git" |
| 13 | "amadan.net/oficina/calls/internal/calls/seal" |
| 14 | "amadan.net/rastrillo/rastrillo/db" |
| 15 | "gorm.io/gorm" |
| 16 | ) |
| 17 | |
| 18 | func TestDelegatedPrecommitAuthority(t *testing.T) { |
| 19 | for _, mode := range []string{"reserved-revoke", "final-revoke", "binding", "unlink", "member"} { |
| 20 | t.Run(mode, func(t *testing.T) { |
| 21 | f, p := linkedDelegated(t) |
| 22 | var checks atomic.Int64 |
| 23 | if e := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 24 | n := checks.Add(1) |
| 25 | if mode == "reserved-revoke" && n >= 2 || mode == "final-revoke" && n >= 3 { |
| 26 | return access.Principal{}, access.Fail("unauthenticated", "Revoked.") |
| 27 | } |
| 28 | return *p, nil |
| 29 | }), f.app.agentIssuer, "acme"); e != nil { |
| 30 | t.Fatal(e) |
| 31 | } |
| 32 | trigger := "" |
| 33 | switch mode { |
| 34 | case "reserved-revoke": |
| 35 | trigger = "CREATE TRIGGER body_must_not_run BEFORE INSERT ON meetings BEGIN SELECT RAISE(ABORT,'body attempted'); END" |
| 36 | case "unlink": |
| 37 | trigger = "CREATE TRIGGER lose_authority AFTER UPDATE OF meeting_id ON calls_agent_meetings BEGIN DELETE FROM home_identity_links; END" |
| 38 | case "binding": |
| 39 | trigger = "CREATE TRIGGER lose_binding AFTER INSERT ON meetings BEGIN UPDATE home_identity_links SET person_binding='broken'; END" |
| 40 | case "member": |
| 41 | trigger = "CREATE TRIGGER lose_authority AFTER UPDATE OF meeting_id ON calls_agent_meetings BEGIN UPDATE persons SET anonymous=1; END" |
| 42 | } |
| 43 | if trigger != "" { |
| 44 | if _, e := f.app.db.Writer().Exec(trigger); e != nil { |
| 45 | t.Fatal(e) |
| 46 | } |
| 47 | } |
| 48 | rec := delegatedCall(f.app, "calls_create_meeting", delegatedInputForTest()) |
| 49 | if rec.Code != 401 && rec.Code != 403 { |
| 50 | t.Fatal("lost authority committed", mode, rec.Code, rec.Body.String()) |
| 51 | } |
| 52 | for _, table := range []string{"meetings", "meeting_links", "calls_connections", "calls_agent_meetings"} { |
| 53 | var n int |
| 54 | if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); e != nil || n != 0 { |
| 55 | t.Fatal("partial meeting", table, n, e) |
| 56 | } |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | } |
| 61 | func TestDelegatedAuthorityBindingRestart(t *testing.T) { |
| 62 | f, p := linkedDelegated(t) |
| 63 | input := delegatedInputForTest() |
| 64 | first := delegatedCall(f.app, "calls_create_meeting", input) |
| 65 | if first.Code != 200 { |
| 66 | t.Fatal(first.Code) |
| 67 | } |
| 68 | var path string |
| 69 | if e := f.app.db.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path); e != nil { |
| 70 | t.Fatal(e) |
| 71 | } |
| 72 | f.app.signinMail.close() |
| 73 | f.app.db.Close() |
| 74 | d, e := db.Open(path, f.app.logger) |
| 75 | if e != nil { |
| 76 | t.Fatal(e) |
| 77 | } |
| 78 | defer d.Close() |
| 79 | restarted := newHomeRouterFixtureWith(t, d, f.app.logger, f.key, f.app.cfg.Home) |
| 80 | if e = restarted.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return *p, nil }), f.app.agentIssuer, "acme"); e != nil { |
| 81 | t.Fatal(e) |
| 82 | } |
| 83 | rec := delegatedCall(restarted.app, "calls_create_meeting", input) |
| 84 | if rec.Code != 200 || rec.Body.String() != first.Body.String() { |
| 85 | t.Fatal("restart changed receipt", rec.Code, rec.Body.String()) |
| 86 | } |
| 87 | a := restarted.app |
| 88 | for _, kind := range []string{"issuer", "home", "team", "key"} { |
| 89 | t.Run(kind, func(t *testing.T) { |
| 90 | oldIssuer, oldHome, oldTeam, oldSeal := a.agentIssuer, a.cfg.Home.Issuer, a.agentTeam, a.sealer |
| 91 | defer func() { |
| 92 | a.agentIssuer = oldIssuer |
| 93 | a.cfg.Home.Issuer = oldHome |
| 94 | a.agentTeam = oldTeam |
| 95 | a.sealer = oldSeal |
| 96 | }() |
| 97 | switch kind { |
| 98 | case "issuer": |
| 99 | a.agentIssuer = "https://other-connect.test" |
| 100 | case "home": |
| 101 | a.cfg.Home.Issuer = "https://other-home.test" |
| 102 | case "team": |
| 103 | a.agentTeam = "green" |
| 104 | case "key": |
| 105 | a.sealer, _ = seal.New(bytes.Repeat([]byte{29}, 32)) |
| 106 | } |
| 107 | if e := a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindDelegated(tx) }); e == nil { |
| 108 | t.Fatal("authority change accepted") |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | if _, e = a.db.Writer().Exec("DELETE FROM calls_agent_state"); e != nil { |
| 113 | t.Fatal(e) |
| 114 | } |
| 115 | if e = a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindDelegated(tx) }); e == nil { |
| 116 | t.Fatal("existing receipts adopted without marker") |
| 117 | } |
| 118 | // Test each preexisting table independently, so either missing predicate fails. |
| 119 | if e = a.db.G.Transaction(func(tx *gorm.DB) error { |
| 120 | if e := tx.Exec("DELETE FROM calls_connections").Error; e != nil { |
| 121 | return e |
| 122 | } |
| 123 | if e := a.bindDelegated(tx); e == nil { |
| 124 | t.Fatal("receipt-only state adopted without marker") |
| 125 | } |
| 126 | return gorm.ErrInvalidTransaction // restore attribution for the next case |
| 127 | }); e != gorm.ErrInvalidTransaction { |
| 128 | t.Fatal(e) |
| 129 | } |
| 130 | // Attribution alone must also prevent marker adoption. |
| 131 | if _, e = a.db.Writer().Exec("DELETE FROM calls_agent_meetings"); e != nil { |
| 132 | t.Fatal(e) |
| 133 | } |
| 134 | if e = a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindDelegated(tx) }); e == nil { |
| 135 | t.Fatal("existing attribution adopted without marker") |
| 136 | } |
| 137 | } |
| 138 | func TestDelegatedRejectsMalformedUnicode(t *testing.T) { |
| 139 | f, _ := linkedDelegated(t) |
| 140 | input := delegatedInputForTest() |
| 141 | input["title"] = "REPLACEME" |
| 142 | raw, _ := json.Marshal(input) |
| 143 | for _, bad := range []string{`\ud800`, string([]byte{255})} { |
| 144 | r := httptest.NewRequest("POST", homeTestOrigin+"/api/v1/operations/calls_create_meeting", strings.NewReader(strings.Replace(string(raw), "REPLACEME", bad, 1))) |
| 145 | r.Header.Set("Content-Type", "application/json") |
| 146 | r.Header.Set("Authorization", "Bearer fixture") |
| 147 | w := httptest.NewRecorder() |
| 148 | f.app.delegated.ServeHTTP(responseRecorder{w}, r) |
| 149 | if w.Code != 422 { |
| 150 | t.Fatal("malformed meeting Unicode", w.Code) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |