127 lines
4.1 KiB
Go
127 lines
4.1 KiB
Go
package consent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
func TestQueryServiceReadsCurrentTextAndOwnLedgerPosition(t *testing.T) {
|
|
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
|
body := "Current c2 text."
|
|
hash := domain.HashConsentText(body)
|
|
text, err := domain.RestoreConsentText(domain.StoredConsentText{
|
|
Purpose: string(domain.ConsentPurposePublication), Version: "c2-current", Body: body,
|
|
Hash: hash[:], Kind: string(domain.ConsentTextKindDemo), PublishedAt: now.Add(-time.Hour),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
accountID := uuid.New()
|
|
position := outbound.ConsentListPosition{GrantedAt: now.Add(-time.Minute), ID: uuid.New()}
|
|
ledger := &queryTestLedger{current: domain.ConsentRecord{}, page: outbound.ConsentPage{HasMore: true}}
|
|
reader := NewQueryService(&queryTestRepositories{
|
|
texts: queryTestTexts{text: text},
|
|
ledger: ledger,
|
|
}, func() time.Time { return now })
|
|
|
|
gotText, err := reader.CurrentText(context.Background(), domain.ConsentPurposePublication)
|
|
if err != nil || gotText.Version() != text.Version() || gotText.Body() != body {
|
|
t.Fatalf("CurrentText() = %#v, %v", gotText, err)
|
|
}
|
|
query := outbound.ConsentListQuery{
|
|
SubjectID: accountID, State: domain.ConsentStateActive,
|
|
After: &position, Limit: 25,
|
|
}
|
|
page, err := reader.List(context.Background(), query)
|
|
if err != nil || !page.HasMore {
|
|
t.Fatalf("List() = %#v, %v", page, err)
|
|
}
|
|
if ledger.listQuery.SubjectID != accountID || ledger.listQuery.State != query.State ||
|
|
ledger.listQuery.After == nil || *ledger.listQuery.After != position || ledger.listQuery.Limit != 25 {
|
|
t.Fatalf("List query = %#v", ledger.listQuery)
|
|
}
|
|
}
|
|
|
|
func TestQueryServiceHasCurrentConsentFailsClosed(t *testing.T) {
|
|
accountID := uuid.New()
|
|
for _, test := range []struct {
|
|
name string
|
|
current error
|
|
want bool
|
|
wantErr bool
|
|
}{
|
|
{name: "missing grant denies", current: outbound.ErrConsentNotFound},
|
|
{name: "current grant allows", want: true},
|
|
{name: "storage failure denies", current: errors.New("storage unavailable"), wantErr: true},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
ledger := &queryTestLedger{currentErr: test.current}
|
|
reader := NewQueryService(&queryTestRepositories{ledger: ledger}, time.Now)
|
|
allowed, err := reader.HasCurrentConsent(context.Background(), accountID, domain.ConsentPurposeFSPImport)
|
|
if allowed != test.want || (err != nil) != test.wantErr {
|
|
t.Fatalf("HasCurrentConsent() = %v, %v", allowed, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryServiceRejectsC4TextWithoutOrganizationSnapshot(t *testing.T) {
|
|
reader := NewQueryService(&queryTestRepositories{}, time.Now)
|
|
if _, err := reader.CurrentText(context.Background(), domain.ConsentPurposeContactDisclosure); !errors.Is(err, ErrScopedConsentRequired) {
|
|
t.Fatalf("CurrentText(c4) error = %v, want ErrScopedConsentRequired", err)
|
|
}
|
|
}
|
|
|
|
type queryTestRepositories struct {
|
|
outbound.ConsentRepositories
|
|
|
|
texts outbound.ConsentTextRepository
|
|
ledger outbound.ConsentLedger
|
|
}
|
|
|
|
func (repositories *queryTestRepositories) ConsentTexts() outbound.ConsentTextRepository {
|
|
return repositories.texts
|
|
}
|
|
|
|
func (repositories *queryTestRepositories) ConsentLedger() outbound.ConsentLedger {
|
|
return repositories.ledger
|
|
}
|
|
|
|
type queryTestTexts struct {
|
|
outbound.ConsentTextRepository
|
|
|
|
text domain.ConsentText
|
|
}
|
|
|
|
func (texts queryTestTexts) Current(context.Context, domain.ConsentPurpose, time.Time) (domain.ConsentText, error) {
|
|
return texts.text, nil
|
|
}
|
|
|
|
type queryTestLedger struct {
|
|
outbound.ConsentLedger
|
|
|
|
currentErr error
|
|
current domain.ConsentRecord
|
|
listQuery outbound.ConsentListQuery
|
|
page outbound.ConsentPage
|
|
}
|
|
|
|
func (ledger *queryTestLedger) Current(context.Context, uuid.UUID, domain.ConsentPurpose) (domain.ConsentRecord, error) {
|
|
if ledger.currentErr != nil {
|
|
return domain.ConsentRecord{}, ledger.currentErr
|
|
}
|
|
return ledger.current, nil
|
|
}
|
|
|
|
func (ledger *queryTestLedger) List(_ context.Context, query outbound.ConsentListQuery) (outbound.ConsentPage, error) {
|
|
ledger.listQuery = query
|
|
return ledger.page, nil
|
|
}
|