feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
@@ -0,0 +1,294 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/adapter/primary/http/api"
|
||||
httpmiddleware "lincodex/backend/internal/adapter/primary/http/middleware"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/usecase/candidateview"
|
||||
)
|
||||
|
||||
func TestPrivacyGetOrganizationCandidateCardMapsClosedProjection(t *testing.T) {
|
||||
userID, organizationID := uuid.New(), uuid.New()
|
||||
view := domain.AnonymousCandidateViewDTO{
|
||||
Pseudonym: "cv1_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
Categories: []domain.CandidateViewCategory{{
|
||||
Specialization: domain.SpecializationBackend, Grade: domain.GradeJunior, Strength: domain.CandidateViewStrengthStrong,
|
||||
}},
|
||||
Skills: []domain.CandidateViewSkillCode{"go"},
|
||||
SelfAssessment: []domain.CandidateViewSelfAssessment{{
|
||||
Specialization: domain.SpecializationBackend, ConfirmationStatus: domain.CandidateViewConfirmationUnconfirmed,
|
||||
}},
|
||||
}
|
||||
views := &privacyViewStub{card: view}
|
||||
handler := NewPrivacyHandler(views, &privacySettingsStub{}, "https://app.example.test")
|
||||
ctx := privacyHTTPContext(userID, "")
|
||||
|
||||
response, err := handler.GetOrganizationCandidateCard(ctx, api.GetOrganizationCandidateCardParams{
|
||||
OrgID: organizationID, CandidateHandle: api.CandidateHandle(view.Pseudonym),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GetOrganizationCandidateCard() error = %v", err)
|
||||
}
|
||||
body, ok := response.(*api.AnonymousCardContentHeaders)
|
||||
if !ok {
|
||||
t.Fatalf("GetOrganizationCandidateCard() response = %T, want AnonymousCardContentHeaders", response)
|
||||
}
|
||||
if body.CacheControl != api.CacheControlNoStoreNoStore || body.Response.Pseudonym != view.Pseudonym ||
|
||||
len(body.Response.Categories) != 1 || body.Response.Categories[0].Strength != api.CardCategoryStrengthStrong ||
|
||||
len(body.Response.Stack) != 1 || body.Response.Stack[0] != "go" ||
|
||||
len(body.Response.SelfAssessment) != 1 || body.Response.SelfAssessment[0].ConfirmationStatus != api.CardSelfAssessmentConfirmationStatusUnconfirmed {
|
||||
t.Fatalf("anonymous content mapping = %+v", body)
|
||||
}
|
||||
if views.actorID != userID || views.organizationID != organizationID || views.handle != view.Pseudonym {
|
||||
t.Fatalf("read scope = actor %s organization %s handle %q", views.actorID, views.organizationID, views.handle)
|
||||
}
|
||||
if strings.Contains(view.String(), userID.String()) {
|
||||
t.Fatal("anonymous view string exposed the internal account ID")
|
||||
}
|
||||
|
||||
views.err = &candidateview.RateLimitedError{RetryAt: time.Now().Add(2 * time.Second)}
|
||||
limited, err := handler.GetOrganizationCandidateCard(ctx, api.GetOrganizationCandidateCardParams{
|
||||
OrgID: organizationID, CandidateHandle: api.CandidateHandle(view.Pseudonym),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("rate-limited GetOrganizationCandidateCard() error = %v", err)
|
||||
}
|
||||
limitedBody, ok := limited.(*api.TooManyRequestsHeaders)
|
||||
if !ok || !limitedBody.RetryAfter.IsSet() || limitedBody.RetryAfter.Value < 1 {
|
||||
t.Fatalf("rate-limit response = %#v, want 429 and Retry-After", limited)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivacyPublicationMapsConsentConflictAndVersionErrors(t *testing.T) {
|
||||
userID := uuid.New()
|
||||
settings := &privacySettingsStub{publicationErr: domain.ErrPublicationConsentRequired}
|
||||
handler := NewPrivacyHandler(&privacyViewStub{}, settings, "https://app.example.test")
|
||||
ctx := privacyHTTPContext(userID, "https://app.example.test")
|
||||
request := &api.PublicationUpdate{ExpectedVersion: 0, State: api.PublicationStatePublished, Fields: []api.CardField{api.CardFieldStack}}
|
||||
|
||||
response, err := handler.SetMyPublication(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatalf("SetMyPublication() error = %v", err)
|
||||
}
|
||||
conflict, ok := response.(*api.SetMyPublicationConflict)
|
||||
if !ok || conflict.Code != api.ProblemCodeStateConflict || len(conflict.Errors) != 1 ||
|
||||
conflict.Errors[0].Pointer != "/state" || conflict.Errors[0].Code != "consent_required" {
|
||||
t.Fatalf("publication consent response = %#v, want a scoped consent_required conflict", response)
|
||||
}
|
||||
if settings.userID != userID || settings.publicationExpectedVersion != 0 || settings.publicationState != domain.PublicationPublished ||
|
||||
len(settings.publicationFields) != 1 || settings.publicationFields[0] != domain.CardFieldStack {
|
||||
t.Fatalf("publication command = actor %s version %d state %q fields %v", settings.userID, settings.publicationExpectedVersion, settings.publicationState, settings.publicationFields)
|
||||
}
|
||||
|
||||
settings.publicationErr = domain.ErrVisibilityVersionConflict
|
||||
response, err = handler.SetMyPublication(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatalf("stale SetMyPublication() error = %v", err)
|
||||
}
|
||||
precondition, ok := response.(*api.SetMyPublicationPreconditionFailed)
|
||||
if !ok || precondition.Code != api.ProblemCodeVersionConflict {
|
||||
t.Fatalf("publication version response = %#v, want 412 version_conflict", response)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivacyListsRejectForeignAndMalformedCursorsBeforeRead(t *testing.T) {
|
||||
ownerID := uuid.New()
|
||||
views := &privacyViewStub{}
|
||||
handler := NewPrivacyHandler(views, &privacySettingsStub{}, "https://app.example.test")
|
||||
ctx := privacyHTTPContext(ownerID, "")
|
||||
|
||||
encoded, err := encodePrivacyCursor(ownerID, privacyCursorAccess, time.Date(2026, 10, 7, 12, 0, 0, 123000, time.UTC), uuid.New())
|
||||
if err != nil {
|
||||
t.Fatalf("encodePrivacyCursor() error = %v", err)
|
||||
}
|
||||
if _, err := decodePrivacyCursor(encoded, uuid.New(), privacyCursorAccess); err == nil {
|
||||
t.Fatal("decodePrivacyCursor() accepted a cursor belonging to another owner")
|
||||
}
|
||||
if _, err := decodePrivacyCursor(encoded, ownerID, privacyCursorEvents); err == nil {
|
||||
t.Fatal("decodePrivacyCursor() accepted a cursor from another listing")
|
||||
}
|
||||
|
||||
response, err := handler.ListMyCardAccessHistory(ctx, api.ListMyCardAccessHistoryParams{Cursor: api.NewOptString("malformed")})
|
||||
if err != nil {
|
||||
t.Fatalf("invalid-cursor list error = %v", err)
|
||||
}
|
||||
badCursor, ok := response.(*api.ListMyCardAccessHistoryBadRequest)
|
||||
if !ok || badCursor.Code != api.ProblemCodeValidationFailed || views.accessCalls != 0 {
|
||||
t.Fatalf("invalid cursor response = %#v, read calls = %d", response, views.accessCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivacyCursorRoundTripsSignedTimestampBoundaries(t *testing.T) {
|
||||
ownerID := uuid.MustParse("00112233-4455-6677-8899-aabbccddeeff")
|
||||
recordID := uuid.MustParse("ffeeddcc-bbaa-9988-7766-554433221100")
|
||||
times := map[string]time.Time{
|
||||
"current positive time": time.Date(2026, 10, 7, 12, 34, 56, 123456000, time.UTC),
|
||||
"before unix epoch": time.Unix(-1, 123456000).UTC(),
|
||||
"end of year 9999": time.Date(9999, 12, 31, 23, 59, 59, 999999000, time.UTC),
|
||||
}
|
||||
for name, want := range times {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
cursor, err := encodePrivacyCursor(ownerID, privacyCursorAccess, want, recordID)
|
||||
if err != nil {
|
||||
t.Fatalf("encodePrivacyCursor() error = %v", err)
|
||||
}
|
||||
got, err := decodePrivacyCursor(cursor, ownerID, privacyCursorAccess)
|
||||
if err != nil {
|
||||
t.Fatalf("decodePrivacyCursor() error = %v", err)
|
||||
}
|
||||
if !got.at.Equal(want) || got.id != recordID {
|
||||
t.Fatalf("decoded cursor = (%s, %s), want (%s, %s)", got.at, got.id, want, recordID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivacyCursorAcceptsExistingVersionOneToken(t *testing.T) {
|
||||
const legacyCursor = "AQMAESIzRFVmd4iZqrvM3e7___________8HW8oA_-7dzLuqmYh3ZlVEMyIRAA"
|
||||
ownerID := uuid.MustParse("00112233-4455-6677-8899-aabbccddeeff")
|
||||
wantID := uuid.MustParse("ffeeddcc-bbaa-9988-7766-554433221100")
|
||||
wantTime := time.Unix(-1, 123456000).UTC()
|
||||
|
||||
got, err := decodePrivacyCursor(legacyCursor, ownerID, privacyCursorAccess)
|
||||
if err != nil {
|
||||
t.Fatalf("decodePrivacyCursor(existing token) error = %v", err)
|
||||
}
|
||||
if !got.at.Equal(wantTime) || got.id != wantID {
|
||||
t.Fatalf("decoded legacy cursor = (%s, %s), want (%s, %s)", got.at, got.id, wantTime, wantID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivacyWriteRejectsOriginBeforeCallingSettings(t *testing.T) {
|
||||
userID := uuid.New()
|
||||
settings := &privacySettingsStub{}
|
||||
handler := NewPrivacyHandler(&privacyViewStub{}, settings, "https://app.example.test")
|
||||
request := &api.SearchStatusUpdate{ExpectedVersion: 0, Value: api.SearchStatusValueLooking}
|
||||
|
||||
response, err := handler.SetMySearchStatus(privacyHTTPContext(userID, "https://attacker.example.test"), request)
|
||||
if err != nil {
|
||||
t.Fatalf("SetMySearchStatus() error = %v", err)
|
||||
}
|
||||
forbidden, ok := response.(*api.SetMySearchStatusForbidden)
|
||||
if !ok || forbidden.Code != api.ProblemCodeCsrfCheckFailed || settings.searchCalls != 0 {
|
||||
t.Fatalf("cross-origin response = %#v, search calls = %d", response, settings.searchCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivacyMappingKeepsNullAndEmptyPublicationFieldsDistinct(t *testing.T) {
|
||||
userID := uuid.New()
|
||||
closed, err := domain.NewPublication(userID)
|
||||
if err != nil {
|
||||
t.Fatalf("NewPublication() error = %v", err)
|
||||
}
|
||||
response := publicationResponse(closed)
|
||||
if response.State != api.PublicationStateClosed || response.Version != 0 || response.ClosedReason.IsNull() ||
|
||||
response.ClosedReason.Value != api.PublicationClosedReasonNeverPublished || response.Fields == nil || len(response.Fields) != 0 ||
|
||||
!response.ConsentID.IsNull() || !response.PublishedAt.IsNull() {
|
||||
t.Fatalf("closed publication response = %+v", response)
|
||||
}
|
||||
|
||||
search, err := domain.NewSearchStatus(userID)
|
||||
if err != nil {
|
||||
t.Fatalf("NewSearchStatus() error = %v", err)
|
||||
}
|
||||
searchResponse := searchStatusResponse(search, time.Now())
|
||||
if searchResponse.Value != api.SearchStatusValueNotLooking || searchResponse.Lapsed || !searchResponse.ConfirmedAt.IsNull() || !searchResponse.LapsesAt.IsNull() {
|
||||
t.Fatalf("initial search status response = %+v", searchResponse)
|
||||
}
|
||||
}
|
||||
|
||||
func privacyHTTPContext(userID uuid.UUID, origin string) context.Context {
|
||||
request := httptest.NewRequest(http.MethodGet, "https://app.example.test/", nil)
|
||||
if origin != "" {
|
||||
request.Header.Set("Origin", origin)
|
||||
}
|
||||
capture := &privacyRequestContextCapture{}
|
||||
httpmiddleware.SessionRequestMetadata()(capture).ServeHTTP(httptest.NewRecorder(), request)
|
||||
return context.WithValue(capture.context, principalContextKey{}, Principal{UserID: userID, EmailVerified: true})
|
||||
}
|
||||
|
||||
type privacyRequestContextCapture struct{ context context.Context }
|
||||
|
||||
func (capture *privacyRequestContextCapture) ServeHTTP(_ http.ResponseWriter, request *http.Request) {
|
||||
// The middleware supplies the request context that carries origin metadata.
|
||||
capture.context = request.Context()
|
||||
}
|
||||
|
||||
type privacyViewStub struct {
|
||||
card domain.AnonymousCandidateViewDTO
|
||||
preview candidateview.PreviewResult
|
||||
access outbound.CandidateCardAccessListPage
|
||||
err error
|
||||
actorID uuid.UUID
|
||||
organizationID uuid.UUID
|
||||
handle string
|
||||
accessCalls int
|
||||
}
|
||||
|
||||
func (stub *privacyViewStub) GetOrganizationCard(_ context.Context, actorID, organizationID uuid.UUID, handle string) (domain.AnonymousCandidateViewDTO, error) {
|
||||
stub.actorID, stub.organizationID, stub.handle = actorID, organizationID, handle
|
||||
return stub.card, stub.err
|
||||
}
|
||||
|
||||
func (stub *privacyViewStub) GetMyCardPreview(context.Context, uuid.UUID) (candidateview.PreviewResult, error) {
|
||||
return stub.preview, stub.err
|
||||
}
|
||||
|
||||
func (stub *privacyViewStub) ListMyCardAccessHistory(context.Context, outbound.CandidateCardAccessListQuery) (outbound.CandidateCardAccessListPage, error) {
|
||||
stub.accessCalls++
|
||||
return stub.access, stub.err
|
||||
}
|
||||
|
||||
type privacySettingsStub struct {
|
||||
settings outbound.CandidatePrivacySettings
|
||||
publication domain.Publication
|
||||
publicationErr error
|
||||
userID uuid.UUID
|
||||
publicationExpectedVersion int64
|
||||
publicationState domain.PublicationState
|
||||
publicationFields []domain.CardField
|
||||
searchCalls int
|
||||
err error
|
||||
}
|
||||
|
||||
func (stub *privacySettingsStub) GetMyPrivacySettings(context.Context, uuid.UUID) (outbound.CandidatePrivacySettings, error) {
|
||||
return stub.settings, stub.err
|
||||
}
|
||||
|
||||
func (stub *privacySettingsStub) SetMyPublication(_ context.Context, userID uuid.UUID, expectedVersion int64, state domain.PublicationState, fields []domain.CardField) (domain.Publication, error) {
|
||||
stub.userID = userID
|
||||
stub.publicationExpectedVersion, stub.publicationState = expectedVersion, state
|
||||
stub.publicationFields = append([]domain.CardField(nil), fields...)
|
||||
return stub.publication, stub.publicationErr
|
||||
}
|
||||
|
||||
func (stub *privacySettingsStub) SetMySearchStatus(context.Context, uuid.UUID, int64, domain.SearchStatusValue) (domain.SearchStatus, time.Time, error) {
|
||||
stub.searchCalls++
|
||||
return domain.SearchStatus{}, time.Time{}, stub.err
|
||||
}
|
||||
|
||||
func (stub *privacySettingsStub) ListMyPrivacyHistory(context.Context, outbound.CandidatePrivacyEventListQuery) (outbound.CandidatePrivacyEventPage, error) {
|
||||
return outbound.CandidatePrivacyEventPage{}, stub.err
|
||||
}
|
||||
|
||||
func (stub *privacySettingsStub) ListMyHiddenOrganizations(context.Context, outbound.HiddenOrganizationListQuery) (outbound.HiddenOrganizationPage, error) {
|
||||
return outbound.HiddenOrganizationPage{}, stub.err
|
||||
}
|
||||
|
||||
func (stub *privacySettingsStub) HideOrganization(context.Context, uuid.UUID, uuid.UUID) (outbound.HiddenOrganizationRecord, error) {
|
||||
return outbound.HiddenOrganizationRecord{}, stub.err
|
||||
}
|
||||
|
||||
func (stub *privacySettingsStub) UnhideOrganization(context.Context, uuid.UUID, uuid.UUID) error {
|
||||
return stub.err
|
||||
}
|
||||
Reference in new issue
Block a user