chore: fixup quotas to only include groups you are a member of (#14271)

* chore: fixup quotas to only include groups you are a member of

Before all everyone groups were included in the allowance.

* chore: add unit test to execercise the bug
* add unit test to add rows into the everyone group
This commit is contained in:
Steven Masley
2024-08-15 13:27:50 -05:00
committed by GitHub
parent f619500833
commit 83ccdaa755
5 changed files with 159 additions and 21 deletions

View File

@ -3318,6 +3318,12 @@ func (q *FakeQuerier) GetQuotaAllowanceForUser(_ context.Context, userID uuid.UU
if member.UserID != userID {
continue
}
if _, err := q.getOrganizationByIDNoLock(member.GroupID); err == nil {
// This should never happen, but it has been reported in customer deployments.
// The SQL handles this case, and omits `group_members` rows in the
// Everyone group. It counts these distinctly via `organization_members` table.
continue
}
for _, group := range q.groups {
if group.ID == member.GroupID {
sum += int64(group.QuotaAllowance)
@ -3325,13 +3331,21 @@ func (q *FakeQuerier) GetQuotaAllowanceForUser(_ context.Context, userID uuid.UU
}
}
}
// Grab the quota for the Everyone group.
for _, group := range q.groups {
if group.ID == group.OrganizationID {
sum += int64(group.QuotaAllowance)
break
// Grab the quota for the Everyone group iff the user is a member of
// said organization.
for _, mem := range q.organizationMembers {
if mem.UserID != userID {
continue
}
group, err := q.getGroupByIDNoLock(context.Background(), mem.OrganizationID)
if err != nil {
return -1, xerrors.Errorf("failed to get everyone group for org %q", mem.OrganizationID.String())
}
sum += int64(group.QuotaAllowance)
}
return sum, nil
}