feat(profile): persist candidate edits and enforce company access
This commit is contained in:
1 parent
3ab6bd91a3
commit
73c7caa225
67 files changed
+5286
-229
No files matched your search
@@ -22,6 +22,7 @@ type organizationTestState struct {
|
||||
memberships map[organizationTestMembershipKey]domain.Membership
|
||||
idempotency map[[outbound.IdempotencyDigestSize]byte]outbound.IdempotencyRecord
|
||||
results map[[outbound.IdempotencyDigestSize]byte]outbound.OrganizationCreationResult
|
||||
lastListQuery *outbound.OrganizationListQuery
|
||||
}
|
||||
|
||||
type organizationTestMembershipKey struct {
|
||||
@@ -56,6 +57,14 @@ func (state organizationTestState) clone() organizationTestState {
|
||||
for scope, result := range state.results {
|
||||
cloned.results[scope] = result
|
||||
}
|
||||
if state.lastListQuery != nil {
|
||||
query := *state.lastListQuery
|
||||
if query.After != nil {
|
||||
position := *query.After
|
||||
query.After = &position
|
||||
}
|
||||
cloned.lastListQuery = &query
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
@@ -227,9 +236,10 @@ func (repository organizationTestOrganizations) GetForMember(
|
||||
}
|
||||
|
||||
func (repository organizationTestOrganizations) ListForUser(
|
||||
context.Context,
|
||||
outbound.OrganizationListQuery,
|
||||
_ context.Context,
|
||||
query outbound.OrganizationListQuery,
|
||||
) (outbound.OrganizationPage, error) {
|
||||
repository.state.lastListQuery = &query
|
||||
return outbound.OrganizationPage{}, nil
|
||||
}
|
||||
|
||||
@@ -550,6 +560,93 @@ func TestUpdateRequiresOwnerAndPreservesVerification(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeCompanyIssuesContextCapabilityOnlyToActiveOwner(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
now := time.Date(2026, 8, 15, 10, 30, 0, 0, time.UTC)
|
||||
tx := newOrganizationTestTransactor()
|
||||
ownerID, recruiterID, candidateID, unverifiedID, foreignID := uuid.New(), uuid.New(), uuid.New(), uuid.New(), uuid.New()
|
||||
tx.state.users[ownerID] = verifiedEmployer(ownerID, now)
|
||||
tx.state.users[recruiterID] = verifiedEmployer(recruiterID, now)
|
||||
tx.state.users[candidateID] = domain.User{ID: candidateID, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}
|
||||
tx.state.users[unverifiedID] = domain.User{ID: unverifiedID, Scenario: domain.ScenarioEmployer}
|
||||
tx.state.users[foreignID] = verifiedEmployer(foreignID, now)
|
||||
service := newOrganizationTestService(t, tx, func() time.Time { return now })
|
||||
profile := organizationProfile("ООО Пример", "Данные")
|
||||
created, err := service.Create(ctx, CreateCommand{
|
||||
ActorID: ownerID, IdempotencyKey: "company-access-01", CanonicalBody: canonicalBodyForTest(t, profile), Profile: profile,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Create() error = %v", err)
|
||||
}
|
||||
organizationID := created.Organization.ID()
|
||||
for _, input := range []domain.MembershipInput{
|
||||
{OrganizationID: organizationID, UserID: recruiterID, Role: domain.OrganizationRoleRecruiter, At: now.Add(time.Second)},
|
||||
{OrganizationID: organizationID, UserID: candidateID, Role: domain.OrganizationRoleOwner, At: now.Add(time.Second)},
|
||||
{OrganizationID: organizationID, UserID: unverifiedID, Role: domain.OrganizationRoleOwner, At: now.Add(time.Second)},
|
||||
} {
|
||||
membership, err := domain.NewMembership(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewMembership() error = %v", err)
|
||||
}
|
||||
tx.state.memberships[organizationTestMembershipKey{organizationID, input.UserID}] = membership
|
||||
}
|
||||
|
||||
access, err := service.AuthorizeCompany(ctx, ownerID, organizationID)
|
||||
if err != nil || !access.Matches(ownerID, organizationID) {
|
||||
t.Fatalf("AuthorizeCompany(owner) = %+v, %v; want matching capability", access, err)
|
||||
}
|
||||
if access.Matches(foreignID, organizationID) || access.Matches(ownerID, uuid.New()) {
|
||||
t.Fatal("CompanyAccess matched a different actor or organization")
|
||||
}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
user uuid.UUID
|
||||
want error
|
||||
}{
|
||||
{name: "recruiter role", user: recruiterID, want: outbound.ErrOrganizationManagerRequired},
|
||||
{name: "candidate even with owner membership", user: candidateID, want: ErrForbidden},
|
||||
{name: "unverified employer", user: unverifiedID, want: ErrEmailNotVerified},
|
||||
{name: "no membership", user: foreignID, want: outbound.ErrOrganizationNotFound},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if _, err := service.AuthorizeCompany(ctx, test.user, organizationID); !errors.Is(err, test.want) {
|
||||
t.Fatalf("AuthorizeCompany() error = %v, want %v", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
if _, err := service.Get(ctx, recruiterID, organizationID); !errors.Is(err, outbound.ErrOrganizationManagerRequired) {
|
||||
t.Fatalf("Get(recruiter) error = %v, want ErrOrganizationManagerRequired", err)
|
||||
}
|
||||
if _, err := service.Get(WithCompanyAccess(ctx, access), ownerID, uuid.New()); !errors.Is(err, ErrForbidden) {
|
||||
t.Fatalf("Get(mismatched company context) error = %v, want ErrForbidden", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListMyOrganizationsUsesOnlyVerifiedEmployerActorAndBoundedPage(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
now := time.Date(2026, 8, 15, 10, 30, 0, 0, time.UTC)
|
||||
tx := newOrganizationTestTransactor()
|
||||
employerID, candidateID := uuid.New(), uuid.New()
|
||||
tx.state.users[employerID] = verifiedEmployer(employerID, now)
|
||||
tx.state.users[candidateID] = domain.User{ID: candidateID, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}
|
||||
service := newOrganizationTestService(t, tx, func() time.Time { return now })
|
||||
position := outbound.OrganizationListPosition{JoinedAt: now.Add(-time.Minute), OrganizationID: uuid.New()}
|
||||
page, err := service.ListMyOrganizations(ctx, OrganizationListCommand{ActorID: employerID, After: &position, Limit: 8})
|
||||
if err != nil || len(page.Items) != 0 || page.HasMore {
|
||||
t.Fatalf("ListMyOrganizations() = %+v, %v; want empty page", page, err)
|
||||
}
|
||||
query := tx.snapshot().lastListQuery
|
||||
if query == nil || query.UserID != employerID || query.Limit != 8 || query.After == nil || *query.After != position {
|
||||
t.Fatalf("ListForUser query = %+v, want principal-scoped cursor and limit", query)
|
||||
}
|
||||
if _, err := service.ListMyOrganizations(ctx, OrganizationListCommand{ActorID: candidateID, Limit: 8}); !errors.Is(err, ErrForbidden) {
|
||||
t.Fatalf("ListMyOrganizations(candidate) error = %v, want ErrForbidden", err)
|
||||
}
|
||||
if _, err := service.ListMyOrganizations(ctx, OrganizationListCommand{ActorID: employerID, Limit: outbound.MaxOrganizationPageSize + 1}); !errors.Is(err, domain.ErrInvalidMembership) {
|
||||
t.Fatalf("ListMyOrganizations(oversized limit) error = %v, want ErrInvalidMembership", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentCreateWithSameKeyReturnsOneCompany(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
now := time.Date(2026, 8, 15, 10, 30, 0, 0, time.UTC)
|
||||
|
||||
Reference in new issue
Block a user