476 lines
25 KiB
Go
476 lines
25 KiB
Go
package domain
|
||
|
||
import (
|
||
"errors"
|
||
"log/slog"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/google/uuid"
|
||
)
|
||
|
||
var visibilityNow = time.Date(2026, 10, 6, 9, 0, 0, 0, time.UTC)
|
||
|
||
func mustCardFields(t *testing.T, fields ...CardField) CardFieldSet {
|
||
t.Helper()
|
||
set, err := NewCardFieldSet(fields)
|
||
if err != nil {
|
||
t.Fatalf("NewCardFieldSet(%v) error = %v", fields, err)
|
||
}
|
||
return set
|
||
}
|
||
|
||
func mustPublicationRequest(t *testing.T, state PublicationState, fields ...CardField) PublicationRequest {
|
||
t.Helper()
|
||
request, err := NewPublicationRequest(state, fields)
|
||
if err != nil {
|
||
t.Fatalf("NewPublicationRequest(%s, %v) error = %v", state, fields, err)
|
||
}
|
||
return request
|
||
}
|
||
|
||
func newClosedPublication(t *testing.T) Publication {
|
||
t.Helper()
|
||
publication, err := NewPublication(uuid.New())
|
||
if err != nil {
|
||
t.Fatalf("NewPublication() error = %v", err)
|
||
}
|
||
return publication
|
||
}
|
||
|
||
func publish(t *testing.T, publication Publication, expectedVersion int64, consent uuid.UUID, at time.Time, fields ...CardField) Publication {
|
||
t.Helper()
|
||
change, err := publication.Apply(mustPublicationRequest(t, PublicationPublished, fields...), expectedVersion, consent, at)
|
||
if err != nil || !change.Changed {
|
||
t.Fatalf("Apply(publish) = %+v, %v, want a change", change, err)
|
||
}
|
||
return change.Publication
|
||
}
|
||
|
||
func TestNewCardFieldSetIsCanonicalAndClosed(t *testing.T) {
|
||
set := mustCardFields(t, CardFieldStack, CardFieldCategories)
|
||
if got := strings.Join(set.Codes(), ","); got != "categories,stack" {
|
||
t.Fatalf("Codes() = %q, want ascending order whatever the input order", got)
|
||
}
|
||
if empty := mustCardFields(t); !empty.IsEmpty() || empty.Codes() == nil {
|
||
t.Fatal("an empty set must be empty and encode as an empty, non-nil array")
|
||
}
|
||
for _, code := range []string{"full_name", "email", "phone", "telegram", "salary_minimum", "contacts", ""} {
|
||
if _, err := NewCardFieldSet([]CardField{CardField(code)}); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Errorf("NewCardFieldSet(%q) error = %v, want a refusal: the card never carries it", code, err)
|
||
}
|
||
}
|
||
if _, err := NewCardFieldSet([]CardField{CardFieldStack, CardFieldStack}); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Errorf("NewCardFieldSet(repeated) error = %v, want a refusal", err)
|
||
}
|
||
var fieldError *VisibilityFieldError
|
||
if _, err := NewCardFieldSet([]CardField{CardFieldStack, "contacts"}); !errors.As(err, &fieldError) || fieldError.Pointer != "/fields/1" || strings.Contains(err.Error(), "contacts") {
|
||
t.Errorf("an unknown field error = %v, want the pointer without the submitted value", err)
|
||
}
|
||
for _, field := range CardFields() {
|
||
if !field.Valid() {
|
||
t.Errorf("CardFields() lists %q, which is not valid", field)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestNewPublicationRequestChecksTheBodyAlone(t *testing.T) {
|
||
for _, test := range []struct {
|
||
name string
|
||
state PublicationState
|
||
fields []CardField
|
||
ok bool
|
||
}{
|
||
{"published with a field", PublicationPublished, []CardField{CardFieldStack}, true},
|
||
{"closed with no field", PublicationClosed, nil, true},
|
||
{"published with no field", PublicationPublished, nil, false},
|
||
{"closed with a field", PublicationClosed, []CardField{CardFieldStack}, false},
|
||
{"unknown state", "hidden", nil, false},
|
||
} {
|
||
t.Run(test.name, func(t *testing.T) {
|
||
_, err := NewPublicationRequest(test.state, test.fields)
|
||
if (err == nil) != test.ok {
|
||
t.Fatalf("NewPublicationRequest() error = %v, want ok %v", err, test.ok)
|
||
}
|
||
if err != nil && !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Fatalf("error = %v, want ErrInvalidVisibility", err)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestAProfileIsClosedByDefault(t *testing.T) {
|
||
publication := newClosedPublication(t)
|
||
if publication.State() != PublicationClosed || publication.ClosedReason() != ClosedNeverPublished ||
|
||
publication.Version() != 0 || !publication.Fields().IsEmpty() || publication.ConsentRecordID() != uuid.Nil {
|
||
t.Fatalf("a new publication = %v %v v%d, want closed, never published, version 0, no consent", publication.State(), publication.ClosedReason(), publication.Version())
|
||
}
|
||
// Closing a profile that never opened changes nothing and stores no row.
|
||
change, err := publication.Apply(mustPublicationRequest(t, PublicationClosed), 0, uuid.Nil, visibilityNow)
|
||
if err != nil || change.Changed || change.Publication.Version() != 0 {
|
||
t.Fatalf("Apply(close of a never published profile) = %+v, %v, want no change", change, err)
|
||
}
|
||
}
|
||
|
||
func TestPublishingNeedsAnActiveC2AndNamesNoConsent(t *testing.T) {
|
||
publication := newClosedPublication(t)
|
||
request := mustPublicationRequest(t, PublicationPublished, CardFieldCategories)
|
||
if _, err := publication.Apply(request, 0, uuid.Nil, visibilityNow); !errors.Is(err, ErrPublicationConsentRequired) {
|
||
t.Fatalf("Apply(without C2) error = %v, want ErrPublicationConsentRequired", err)
|
||
}
|
||
consent := uuid.New()
|
||
change, err := publication.Apply(request, 0, consent, visibilityNow)
|
||
if err != nil || !change.Changed || change.Event != PrivacyEventPublicationOpened {
|
||
t.Fatalf("Apply(publish) = %+v, %v, want an opened publication", change, err)
|
||
}
|
||
published := change.Publication
|
||
if published.State() != PublicationPublished || published.ClosedReason() != "" || published.Version() != 1 ||
|
||
published.ConsentRecordID() != consent || !published.PublishedAt().Equal(visibilityNow) ||
|
||
!published.Fields().Equal(mustCardFields(t, CardFieldCategories)) {
|
||
t.Fatalf("published = %v v%d, want the new record bound to the C2 record", published.State(), published.Version())
|
||
}
|
||
// The input did not change.
|
||
if publication.State() != PublicationClosed || publication.Version() != 0 {
|
||
t.Fatal("Apply changed its receiver")
|
||
}
|
||
}
|
||
|
||
func TestPublicationOrderOfChecks(t *testing.T) {
|
||
consent := uuid.New()
|
||
published := publish(t, newClosedPublication(t), 0, consent, visibilityNow, CardFieldStack, CardFieldCategories)
|
||
same := mustPublicationRequest(t, PublicationPublished, CardFieldCategories, CardFieldStack)
|
||
other := mustPublicationRequest(t, PublicationPublished, CardFieldCategories)
|
||
|
||
// A version above the current one never existed, even for an unchanged body.
|
||
if _, err := published.Apply(same, 5, consent, visibilityNow); !errors.Is(err, ErrVisibilityVersionConflict) {
|
||
t.Fatalf("Apply(future version) error = %v, want a version conflict", err)
|
||
}
|
||
// An unchanged body is a success over a stale version, so a lost response can be retried.
|
||
published = publish(t, published, 1, consent, visibilityNow.Add(time.Hour), CardFieldCategories)
|
||
if change, err := published.Apply(other, 1, uuid.Nil, visibilityNow); err != nil || change.Changed || change.Publication.Version() != 2 {
|
||
t.Fatalf("Apply(unchanged, stale) = %+v, %v, want no change at version 2", change, err)
|
||
}
|
||
// Any other change over a stale version is a conflict, also a closing.
|
||
if _, err := published.Apply(same, 1, consent, visibilityNow); !errors.Is(err, ErrVisibilityVersionConflict) {
|
||
t.Fatalf("Apply(change, stale) error = %v, want a version conflict", err)
|
||
}
|
||
// A stale version is a conflict before the missing consent is reported.
|
||
if _, err := published.Apply(same, 1, uuid.Nil, visibilityNow); !errors.Is(err, ErrVisibilityVersionConflict) {
|
||
t.Fatalf("Apply(change, stale, no consent) error = %v, want the version conflict first", err)
|
||
}
|
||
change, err := published.Apply(mustPublicationRequest(t, PublicationClosed), 2, uuid.Nil, visibilityNow.Add(2*time.Hour))
|
||
if err != nil || !change.Changed || change.Event != PrivacyEventPublicationClosed {
|
||
t.Fatalf("Apply(close) = %+v, %v, want a closing that needs no consent", change, err)
|
||
}
|
||
closed := change.Publication
|
||
if closed.State() != PublicationClosed || closed.ClosedReason() != ClosedByCandidate || !closed.Fields().IsEmpty() || closed.Version() != 3 ||
|
||
!closed.PublishedAt().Equal(visibilityNow.Add(time.Hour)) || !closed.ClosedAt().Equal(visibilityNow.Add(2*time.Hour)) {
|
||
t.Fatalf("closed = %v %v v%d, want closed by the candidate with the last published time kept", closed.State(), closed.ClosedReason(), closed.Version())
|
||
}
|
||
}
|
||
|
||
func TestPublishedFieldsChangeWithTheirOwnEvent(t *testing.T) {
|
||
consent := uuid.New()
|
||
published := publish(t, newClosedPublication(t), 0, consent, visibilityNow, CardFieldStack)
|
||
change, err := published.Apply(mustPublicationRequest(t, PublicationPublished, CardFieldStack, CardFieldLocation), 1, consent, visibilityNow.Add(time.Minute))
|
||
if err != nil || !change.Changed || change.Event != PrivacyEventPublicationChanged || change.Publication.Version() != 2 {
|
||
t.Fatalf("Apply(more fields) = %+v, %v, want a changed publication at version 2", change, err)
|
||
}
|
||
event, err := NewPublicationEvent(change, visibilityNow.Add(time.Minute))
|
||
if err != nil || event.Kind() != PrivacyEventPublicationChanged || !event.HasFieldSet() || event.ConsentRecordID() != consent ||
|
||
!event.Fields().Equal(mustCardFields(t, CardFieldLocation, CardFieldStack)) {
|
||
t.Fatalf("NewPublicationEvent() = %v, %v, want the fields and the C2 record behind them", event, err)
|
||
}
|
||
}
|
||
|
||
// A revoked C2 closes the card without any write, and a later C2 grant never
|
||
// reopens it: only an explicit publication under the new record does.
|
||
func TestARevokedConsentClosesAndNeverReopensByItself(t *testing.T) {
|
||
consent := uuid.New()
|
||
published := publish(t, newClosedPublication(t), 0, consent, visibilityNow, CardFieldStack)
|
||
stored := StoredPublication{
|
||
UserID: published.UserID(), State: string(PublicationPublished), Fields: published.StoredFields(), ConsentRecordID: consent,
|
||
PublishedAt: visibilityNow, Version: 1, ConsentRevoked: true,
|
||
}
|
||
revoked, err := RestorePublication(stored)
|
||
if err != nil {
|
||
t.Fatalf("RestorePublication() error = %v", err)
|
||
}
|
||
if revoked.State() != PublicationClosed || revoked.ClosedReason() != ClosedConsentRevoked || !revoked.Fields().IsEmpty() {
|
||
t.Fatalf("a revoked C2 left the card %v (%v) with fields %v, want closed", revoked.State(), revoked.ClosedReason(), revoked.Fields().Codes())
|
||
}
|
||
// The same body is not "unchanged": the card is closed now, so it is a new publication.
|
||
newConsent := uuid.New()
|
||
change, err := revoked.Apply(mustPublicationRequest(t, PublicationPublished, CardFieldStack), 1, newConsent, visibilityNow.Add(time.Hour))
|
||
if err != nil || !change.Changed || change.Event != PrivacyEventPublicationOpened || change.Publication.ConsentRecordID() != newConsent ||
|
||
change.Publication.State() != PublicationPublished {
|
||
t.Fatalf("Apply(publish again) = %+v, %v, want an opened publication under the new record", change, err)
|
||
}
|
||
// Closing a card that the revocation closed in effect is a real change:
|
||
// the stored row is normalized to closed and the closing is recorded.
|
||
closing, err := revoked.Apply(mustPublicationRequest(t, PublicationClosed), 1, uuid.Nil, visibilityNow.Add(2*time.Hour))
|
||
if err != nil || !closing.Changed || closing.Event != PrivacyEventPublicationClosed || closing.Publication.StoredState() != PublicationClosed ||
|
||
closing.Publication.ClosedReason() != ClosedByCandidate || closing.Publication.Version() != 2 {
|
||
t.Fatalf("Apply(close over a revoked C2) = %+v, %v, want a recorded closing at version 2", closing, err)
|
||
}
|
||
// A second closing is the repeat of the stored closing.
|
||
if again, err := closing.Publication.Apply(mustPublicationRequest(t, PublicationClosed), 1, uuid.Nil, visibilityNow); err != nil || again.Changed {
|
||
t.Fatalf("Apply(close again) = %+v, %v, want no change", again, err)
|
||
}
|
||
}
|
||
|
||
func TestRestorePublicationRejectsRowsThatBreakTheInvariants(t *testing.T) {
|
||
user, consent := uuid.New(), uuid.New()
|
||
valid := StoredPublication{UserID: user, State: "published", Fields: []string{"categories", "stack"}, ConsentRecordID: consent, PublishedAt: visibilityNow, Version: 1}
|
||
if _, err := RestorePublication(valid); err != nil {
|
||
t.Fatalf("RestorePublication(valid) error = %v", err)
|
||
}
|
||
for name, mutate := range map[string]func(*StoredPublication){
|
||
"published without fields": func(s *StoredPublication) { s.Fields = nil },
|
||
"closed with fields": func(s *StoredPublication) { s.State = "closed"; s.ClosedAt = visibilityNow },
|
||
"closed without a close time": func(s *StoredPublication) { s.State = "closed"; s.Fields = nil },
|
||
"published with a close time": func(s *StoredPublication) { s.ClosedAt = visibilityNow },
|
||
"fields out of order": func(s *StoredPublication) { s.Fields = []string{"stack", "categories"} },
|
||
"a contact as a field": func(s *StoredPublication) { s.Fields = []string{"phone"} },
|
||
"unknown state": func(s *StoredPublication) { s.State = "draft" },
|
||
"no consent record": func(s *StoredPublication) { s.ConsentRecordID = uuid.Nil },
|
||
"version zero": func(s *StoredPublication) { s.Version = 0 },
|
||
} {
|
||
t.Run(name, func(t *testing.T) {
|
||
stored := valid
|
||
mutate(&stored)
|
||
if _, err := RestorePublication(stored); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Fatalf("RestorePublication() error = %v, want ErrInvalidVisibility", err)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func demoSearchConfirmation(t *testing.T) SearchStatusConfirmation {
|
||
t.Helper()
|
||
id, err := NewPolicyVersionID()
|
||
if err != nil {
|
||
t.Fatalf("NewPolicyVersionID() error = %v", err)
|
||
}
|
||
confirmation, err := NewSearchStatusConfirmation(id, DemoProductPolicyV1().SearchStatus)
|
||
if err != nil {
|
||
t.Fatalf("NewSearchStatusConfirmation() error = %v", err)
|
||
}
|
||
return confirmation
|
||
}
|
||
|
||
func newSearchStatus(t *testing.T) SearchStatus {
|
||
t.Helper()
|
||
status, err := NewSearchStatus(uuid.New())
|
||
if err != nil {
|
||
t.Fatalf("NewSearchStatus() error = %v", err)
|
||
}
|
||
return status
|
||
}
|
||
|
||
func TestSearchStatusLapsesAfterThePolicyIntervalToTheNonPublicState(t *testing.T) {
|
||
confirmation := demoSearchConfirmation(t)
|
||
initial := newSearchStatus(t)
|
||
if initial.Effective(visibilityNow) != SearchStatusNotLooking || initial.Version() != 0 || initial.Lapsed(visibilityNow) {
|
||
t.Fatal("a new account must not be looking at version 0")
|
||
}
|
||
change, err := initial.Apply(SearchStatusLooking, 0, confirmation, visibilityNow)
|
||
if err != nil || !change.Changed || change.Event != PrivacyEventSearchStatusConfirmed {
|
||
t.Fatalf("Apply(looking) = %+v, %v, want a confirmation", change, err)
|
||
}
|
||
looking := change.Status
|
||
// The interval is the 30 days of the demo policy, read from the versioned document.
|
||
if want := visibilityNow.Add(30 * 24 * time.Hour); !looking.LapsesAt().Equal(want) || !looking.ConfirmedAt().Equal(visibilityNow) ||
|
||
looking.PolicyVersionID() != confirmation.policyVersionID || looking.Version() != 1 {
|
||
t.Fatalf("looking = confirmed %v lapses %v v%d, want 30 days under the policy version", looking.ConfirmedAt(), looking.LapsesAt(), looking.Version())
|
||
}
|
||
last := looking.LapsesAt().Add(-time.Nanosecond)
|
||
if looking.Effective(last) != SearchStatusLooking || looking.Lapsed(last) {
|
||
t.Fatal("the status must hold until the lapse time")
|
||
}
|
||
for _, at := range []time.Time{looking.LapsesAt(), looking.LapsesAt().Add(365 * 24 * time.Hour)} {
|
||
if looking.Effective(at) != SearchStatusNotLooking || !looking.Lapsed(at) {
|
||
t.Fatalf("at %v the status = %v lapsed %v, want not_looking and lapsed: a lapse only narrows", at, looking.Effective(at), looking.Lapsed(at))
|
||
}
|
||
}
|
||
// A lapse is not a stored change: the stored status is still what the candidate said.
|
||
if looking.StoredValue() != SearchStatusLooking {
|
||
t.Fatal("the lapse rewrote the stored status")
|
||
}
|
||
// The candidate confirms again after the lapse with the current version.
|
||
later := looking.LapsesAt().Add(time.Hour)
|
||
again, err := looking.Apply(SearchStatusLooking, 1, confirmation, later)
|
||
if err != nil || !again.Changed || again.Status.Version() != 2 || again.Status.Effective(later) != SearchStatusLooking || !again.Status.ConfirmedAt().Equal(later) {
|
||
t.Fatalf("Apply(confirm after the lapse) = %+v, %v, want a new confirmation at version 2", again, err)
|
||
}
|
||
}
|
||
|
||
func TestSearchStatusVersionRules(t *testing.T) {
|
||
confirmation := demoSearchConfirmation(t)
|
||
initial := newSearchStatus(t)
|
||
// Not looking over a status that was never set stores nothing.
|
||
if change, err := initial.Apply(SearchStatusNotLooking, 0, SearchStatusConfirmation{}, visibilityNow); err != nil || change.Changed {
|
||
t.Fatalf("Apply(not_looking, never set) = %+v, %v, want no change", change, err)
|
||
}
|
||
if _, err := initial.Apply(SearchStatusLooking, 4, confirmation, visibilityNow); !errors.Is(err, ErrVisibilityVersionConflict) {
|
||
t.Fatalf("Apply(future version) error = %v, want a version conflict", err)
|
||
}
|
||
if _, err := initial.Apply("searching", 0, confirmation, visibilityNow); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Fatalf("Apply(unknown value) error = %v, want ErrInvalidVisibility", err)
|
||
}
|
||
if _, err := initial.Apply(SearchStatusLooking, 0, SearchStatusConfirmation{}, visibilityNow); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Fatalf("Apply(looking without a policy) error = %v, want ErrInvalidVisibility", err)
|
||
}
|
||
looking := mustApplySearch(t, initial, SearchStatusLooking, 0, confirmation, visibilityNow)
|
||
// Looking again over a stale version is a conflict: a repeat of a confirmation changes the stored time.
|
||
if _, err := looking.Apply(SearchStatusLooking, 0, confirmation, visibilityNow); !errors.Is(err, ErrVisibilityVersionConflict) {
|
||
t.Fatalf("Apply(looking, stale) error = %v, want a version conflict", err)
|
||
}
|
||
cleared := mustApplySearch(t, looking, SearchStatusNotLooking, 1, SearchStatusConfirmation{}, visibilityNow.Add(time.Hour))
|
||
if cleared.StoredValue() != SearchStatusNotLooking || !cleared.ConfirmedAt().IsZero() || !cleared.LapsesAt().IsZero() || !cleared.PolicyVersionID().IsZero() || cleared.Lapsed(visibilityNow.Add(100*24*time.Hour)) {
|
||
t.Fatal("not_looking must carry no confirmation, and a cleared status is never lapsed")
|
||
}
|
||
// Clearing twice is not a change, even with a stale version.
|
||
if change, err := cleared.Apply(SearchStatusNotLooking, 1, SearchStatusConfirmation{}, visibilityNow); err != nil || change.Changed {
|
||
t.Fatalf("Apply(not_looking, stale, unchanged) = %+v, %v, want no change", change, err)
|
||
}
|
||
}
|
||
|
||
func mustApplySearch(t *testing.T, status SearchStatus, value SearchStatusValue, expected int64, confirmation SearchStatusConfirmation, at time.Time) SearchStatus {
|
||
t.Helper()
|
||
change, err := status.Apply(value, expected, confirmation, at)
|
||
if err != nil || !change.Changed {
|
||
t.Fatalf("Apply(%s) = %+v, %v, want a change", value, change, err)
|
||
}
|
||
return change.Status
|
||
}
|
||
|
||
func TestSearchStatusConfirmationComesFromAValidVersionedPolicy(t *testing.T) {
|
||
id, err := NewPolicyVersionID()
|
||
if err != nil {
|
||
t.Fatalf("NewPolicyVersionID() error = %v", err)
|
||
}
|
||
if _, err := NewSearchStatusConfirmation(PolicyVersionID{}, SearchStatusPolicy{SearchingConfirmationInterval: 30}); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Errorf("a confirmation without a policy version error = %v, want ErrInvalidVisibility", err)
|
||
}
|
||
for _, days := range []CalendarDays{0, -1, maxSearchingConfirmationInterval + 1} {
|
||
if _, err := NewSearchStatusConfirmation(id, SearchStatusPolicy{SearchingConfirmationInterval: days}); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Errorf("a confirmation of %d days error = %v, want a refusal", days, err)
|
||
}
|
||
}
|
||
// An owner-published interval is used as is.
|
||
confirmation, err := NewSearchStatusConfirmation(id, SearchStatusPolicy{SearchingConfirmationInterval: 14})
|
||
if err != nil {
|
||
t.Fatalf("NewSearchStatusConfirmation(14) error = %v", err)
|
||
}
|
||
looking := mustApplySearch(t, newSearchStatus(t), SearchStatusLooking, 0, confirmation, visibilityNow)
|
||
if want := visibilityNow.Add(14 * 24 * time.Hour); !looking.LapsesAt().Equal(want) {
|
||
t.Fatalf("lapses at %v, want %v", looking.LapsesAt(), want)
|
||
}
|
||
}
|
||
|
||
func TestRestoreSearchStatusRejectsRowsThatBreakTheInvariants(t *testing.T) {
|
||
policy, err := NewPolicyVersionID()
|
||
if err != nil {
|
||
t.Fatalf("NewPolicyVersionID() error = %v", err)
|
||
}
|
||
user := uuid.New()
|
||
valid := StoredSearchStatus{UserID: user, Status: "looking", ConfirmedAt: visibilityNow, LapsesAt: visibilityNow.Add(time.Hour), PolicyVersionID: policy, Version: 1}
|
||
if _, err := RestoreSearchStatus(valid); err != nil {
|
||
t.Fatalf("RestoreSearchStatus(valid) error = %v", err)
|
||
}
|
||
if _, err := RestoreSearchStatus(StoredSearchStatus{UserID: user, Status: "not_looking", Version: 2}); err != nil {
|
||
t.Fatalf("RestoreSearchStatus(not_looking) error = %v", err)
|
||
}
|
||
for name, mutate := range map[string]func(*StoredSearchStatus){
|
||
"looking without a confirmation": func(s *StoredSearchStatus) { s.ConfirmedAt = time.Time{} },
|
||
"lapse not after the confirm": func(s *StoredSearchStatus) { s.LapsesAt = s.ConfirmedAt },
|
||
"looking without a policy": func(s *StoredSearchStatus) { s.PolicyVersionID = PolicyVersionID{} },
|
||
"not looking with a confirmation": func(s *StoredSearchStatus) { s.Status = "not_looking" },
|
||
"unknown status": func(s *StoredSearchStatus) { s.Status = "open" },
|
||
"version zero": func(s *StoredSearchStatus) { s.Version = 0 },
|
||
} {
|
||
t.Run(name, func(t *testing.T) {
|
||
stored := valid
|
||
mutate(&stored)
|
||
if _, err := RestoreSearchStatus(stored); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Fatalf("RestoreSearchStatus() error = %v, want ErrInvalidVisibility", err)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestHiddenOrganizationAndEventsNeedTheirIdentifiers(t *testing.T) {
|
||
user, organization := uuid.New(), uuid.New()
|
||
hidden, err := NewHiddenOrganization(user, organization, visibilityNow)
|
||
if err != nil || hidden.UserID() != user || hidden.OrganizationID() != organization || !hidden.HiddenAt().Equal(visibilityNow) {
|
||
t.Fatalf("NewHiddenOrganization() = %v, %v, want the record", hidden, err)
|
||
}
|
||
for _, args := range [][2]uuid.UUID{{uuid.Nil, organization}, {user, uuid.Nil}} {
|
||
if _, err := NewHiddenOrganization(args[0], args[1], visibilityNow); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Errorf("NewHiddenOrganization(%v) error = %v, want a refusal", args, err)
|
||
}
|
||
}
|
||
if _, err := NewHiddenOrganization(user, organization, time.Time{}); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Errorf("NewHiddenOrganization(no time) error = %v, want a refusal", err)
|
||
}
|
||
event, err := NewOrganizationEvent(PrivacyEventOrganizationHidden, user, organization, visibilityNow)
|
||
if err != nil || event.ID() == uuid.Nil || event.HasFieldSet() || event.OrganizationID() != organization {
|
||
t.Fatalf("NewOrganizationEvent() = %v, %v, want an event without a field set", event, err)
|
||
}
|
||
if _, err := NewOrganizationEvent(PrivacyEventPublicationClosed, user, organization, visibilityNow); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Errorf("NewOrganizationEvent(publication kind) error = %v, want a refusal", err)
|
||
}
|
||
if _, err := NewPublicationEvent(PublicationChange{}, visibilityNow); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Errorf("NewPublicationEvent(no change) error = %v, want a refusal", err)
|
||
}
|
||
if _, err := NewSearchStatusEvent(SearchStatusChange{}, visibilityNow); !errors.Is(err, ErrInvalidVisibility) {
|
||
t.Errorf("NewSearchStatusEvent(no change) error = %v, want a refusal", err)
|
||
}
|
||
}
|
||
|
||
func TestVisibilityTypesLogOnlyIdentifiers(t *testing.T) {
|
||
publication := publish(t, newClosedPublication(t), 0, uuid.New(), visibilityNow, CardFieldStack, CardFieldLocation)
|
||
event, err := NewOrganizationEvent(PrivacyEventOrganizationHidden, uuid.New(), uuid.New(), visibilityNow)
|
||
if err != nil {
|
||
t.Fatalf("NewOrganizationEvent() error = %v", err)
|
||
}
|
||
var out strings.Builder
|
||
logger := slog.New(slog.NewTextHandler(&out, nil))
|
||
logger.Info("settings", "publication", publication, "event", event)
|
||
for _, secret := range []string{"location", "stack"} {
|
||
if strings.Contains(out.String(), secret) {
|
||
t.Errorf("log output %q contains %q", out.String(), secret)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestCardTextLooksLikeContact(t *testing.T) {
|
||
for _, text := range []string{
|
||
"anna@example.com", "write to anna@mail.ru", "@anna_dev", "tg: @anna_dev", "t.me/anna", "https://github.com/anna",
|
||
"www.example.org", "anna.dev", "+7 (900) 000-00-00", "89000000000", "8 900 000 00 00", "example.com",
|
||
// Internationalized domains and wider endings.
|
||
"вася.com", "site.рф", "вася.рф", "anna.ua", "anna.by", "anna.kz", "anna.co", "anna.ai", "anna.de", "anna.tv", "anna.site",
|
||
"anna.online", "anna.tech", "bit.ly/x", "telegram.dog/anna", "tg://resolve?domain=anna", "wa.me/79000000000", "asp.net",
|
||
// Other digit scripts and compatibility forms.
|
||
"٧٩٠٠٠٠٠٠٠٠٠", "89000000000", "+7 900 000 00 00", "anna@example.com", "@anna_dev",
|
||
// Obfuscation.
|
||
"anna [at] example dot com", "vasya at gmail", "vasya собака gmail точка com", "@ vasya", "anna (dot) dev",
|
||
// Messenger and contact words without an @.
|
||
"tg: vasya_ivanov", "telegram vasya_ivanov", "whatsapp 1", "viber", "skype: vasya", "почта", "телефон", "email me",
|
||
} {
|
||
if !CardTextLooksLikeContact(text) {
|
||
t.Errorf("CardTextLooksLikeContact(%q) = false, want true", text)
|
||
}
|
||
}
|
||
for _, text := range []string{
|
||
"PostgreSQL", "Node.js", "Vue.js", "Next.js", "Python 3.12", "ASP.NET", "C++", "C#", "Kubernetes", "Казань", "Mentoring",
|
||
"Go 1.22", "Java 17", "Санкт-Петербург", "Python 3.12, Django 4.2, PostgreSQL 16", "pro.company", "Team leadership",
|
||
} {
|
||
if CardTextLooksLikeContact(text) {
|
||
t.Errorf("CardTextLooksLikeContact(%q) = true, want false", text)
|
||
}
|
||
}
|
||
}
|