feat(consent): add versioned append-only consent ledger C1-C5

This commit is contained in:
shira committed 2026-10-06 14:52:33 +03:00
1 parent 008254e44a
commit de49f93b0b
64 files changed
+9244 -132

No files matched your search

@@ -9,6 +9,7 @@ import (
"log/slog"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
@@ -1884,6 +1885,94 @@ func TestInvitationDTOsCarryNoContactOrMinimum(t *testing.T) {
}
}
// myConsentsPath is the consent ledger of the session account.
const myConsentsPath = "/api/v1/me/consents"
// The schema alone must accept the read of the own ledger with an optional
// state filter and reject an unknown state and malformed page parameters.
// Security accepts the session here, so the request reaches validation; a
// valid request then answers 501 until the ledger read is implemented.
func TestConsentSchemaValidationPrecedesImplementation(t *testing.T) {
session := map[string]string{"Cookie": testSessionCookie}
for _, test := range []struct {
name string
query string
pointer string
}{
{name: "the whole history", query: ""},
{name: "the active consents", query: "?state=active"},
{name: "the revoked consents of the next page", query: "?state=revoked&limit=50&cursor=b3BhcXVlLWN1cnNvcg"},
{name: "an unknown state", query: "?state=granted", pointer: "/state"},
{name: "a purpose instead of a state", query: "?state=c4", pointer: "/state"},
{name: "a limit over 100", query: "?limit=101", pointer: "/limit"},
{name: "a malformed cursor", query: "?cursor=not%20a%20cursor", pointer: "/cursor"},
} {
t.Run(test.name, func(t *testing.T) {
logger := slog.New(slog.DiscardHandler)
healthHandler := newTestHealthHandler(func(context.Context) error { return nil })
router, err := NewRouter(healthHandler, v1.NewHandler(healthHandler, problem.NewResponder(logger)), &recordingSecurity{}, problem.NewResponder(logger), metrics.New(), logger)
if err != nil {
t.Fatalf("NewRouter() error = %v", err)
}
response := serveIdentityRequest(t, router, identityRequest{method: http.MethodGet, path: myConsentsPath + test.query, headers: session})
if test.pointer == "" {
requireProblem(t, response, http.StatusNotImplemented, api.ProblemCodeNotImplemented)
return
}
body := requireProblem(t, response, http.StatusBadRequest, api.ProblemCodeValidationFailed)
want := api.ProblemFieldError{In: api.ProblemFieldLocationQuery, Pointer: test.pointer, Code: "invalid"}
for _, field := range body.Errors {
if field.In == want.In && field.Pointer == want.Pointer && field.Code == want.Code {
return
}
}
t.Fatalf("errors = %+v, want an entry %+v", body.Errors, want)
})
}
}
// The ledger read requires a session; DenyAllSecurity grants none, with or
// without a cookie, and a request cannot name another subject instead.
func TestConsentAccessBeforeImplementation(t *testing.T) {
for _, test := range []identityRequest{
{method: http.MethodGet, path: myConsentsPath},
{method: http.MethodGet, path: myConsentsPath + "?state=active", headers: map[string]string{"Cookie": testSessionCookie}},
{method: http.MethodGet, path: myConsentsPath + "?subject_id=01928f7a-0000-7000-8000-00000000c0de", headers: map[string]string{"Cookie": testSessionCookie}},
} {
t.Run(test.method+" "+test.path, func(t *testing.T) {
router := newTestRouter(t, func(context.Context) error { return nil })
requireProblem(t, serveIdentityRequest(t, router, test), http.StatusUnauthorized, api.ProblemCodeUnauthenticated)
})
}
}
// The read model never carries the stored text hash, a subject, or a contact
// value, and its parameters cannot select a subject.
func TestConsentDTOsCarryNoHashSubjectOrContact(t *testing.T) {
private := []string{"text_hash", "hash", "body_sha256", "subject_id", "user_id", "account_id", "email", "phone", "telegram", "full_name"}
for _, dto := range []any{api.ConsentRecord{}, api.ConsentRecordList{}, api.ConsentOrganization{}} {
assertNoJSONFields(t, dto, private)
}
params := reflect.TypeFor[api.ListMyConsentsParams]()
names := make([]string, 0, params.NumField())
for i := range params.NumField() {
names = append(names, params.Field(i).Name)
}
if got := strings.Join(names, ","); got != "State,Limit,Cursor" {
t.Fatalf("ListMyConsentsParams fields = %s, want only the state filter and the page", got)
}
// The documented example is a valid response of the generated server.
var page api.ConsentRecordList
if err := page.UnmarshalJSON(supportCaseExample(t, "ConsentRecordListPage")); err != nil {
t.Fatalf("decode the ConsentRecordListPage example: %v", err)
}
if err := page.Validate(); err != nil {
t.Fatalf("validate the ConsentRecordListPage example: %v", err)
}
}
func TestPublicOperationInternalErrorIsLoggedButNotExposed(t *testing.T) {
var logs bytes.Buffer
logger := slog.New(slog.NewJSONHandler(&logs, nil))