Skip to content

Commit 7d27682

Browse files
committed
chore: add unit test to execercise the bug
1 parent e11299c commit 7d27682

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,13 +3325,21 @@ func (q *FakeQuerier) GetQuotaAllowanceForUser(_ context.Context, userID uuid.UU
33253325
}
33263326
}
33273327
}
3328-
// Grab the quota for the Everyone group.
3329-
for _, group := range q.groups {
3330-
if group.ID == group.OrganizationID {
3331-
sum += int64(group.QuotaAllowance)
3332-
break
3328+
3329+
// Grab the quota for the Everyone group iff the user is a member of
3330+
// said organization.
3331+
for _, mem := range q.organizationMembers {
3332+
if mem.UserID != userID {
3333+
continue
33333334
}
3335+
3336+
group, err := q.getGroupByIDNoLock(context.Background(), mem.OrganizationID)
3337+
if err != nil {
3338+
return -1, xerrors.Errorf("failed to get everyone group for org %q", mem.OrganizationID.String())
3339+
}
3340+
sum += int64(group.QuotaAllowance)
33343341
}
3342+
33353343
return sum, nil
33363344
}
33373345

enterprise/coderd/workspacequota_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,50 @@ func TestWorkspaceQuota(t *testing.T) {
233233
verifyQuota(ctx, t, client, 4, 4)
234234
require.Equal(t, codersdk.WorkspaceStatusRunning, build.Status)
235235
})
236+
237+
// Ensures allowance from everyone groups only counts if you are an org member.
238+
// This was a bug where the group "Everyone" was being counted for all users,
239+
// regardless of membership.
240+
t.Run("AllowanceEveryone", func(t *testing.T) {
241+
t.Parallel()
242+
243+
dv := coderdtest.DeploymentValues(t)
244+
dv.Experiments = []string{string(codersdk.ExperimentMultiOrganization)}
245+
owner, first := coderdenttest.New(t, &coderdenttest.Options{
246+
Options: &coderdtest.Options{
247+
DeploymentValues: dv,
248+
},
249+
LicenseOptions: &coderdenttest.LicenseOptions{
250+
Features: license.Features{
251+
codersdk.FeatureTemplateRBAC: 1,
252+
codersdk.FeatureMultipleOrganizations: 1,
253+
},
254+
},
255+
})
256+
member, _ := coderdtest.CreateAnotherUser(t, owner, first.OrganizationID)
257+
258+
// Create a second organization
259+
second := coderdenttest.CreateOrganization(t, owner, coderdenttest.CreateOrganizationOptions{})
260+
261+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
262+
defer cancel()
263+
264+
// update everyone quotas
265+
_, err := owner.PatchGroup(ctx, first.OrganizationID, codersdk.PatchGroupRequest{
266+
QuotaAllowance: ptr.Ref(30),
267+
})
268+
require.NoError(t, err)
269+
270+
_, err = owner.PatchGroup(ctx, second.ID, codersdk.PatchGroupRequest{
271+
QuotaAllowance: ptr.Ref(15),
272+
})
273+
require.NoError(t, err)
274+
275+
verifyQuota(ctx, t, member, 0, 30)
276+
// This currently reports the total site wide quotas. We might want to
277+
// org scope this api call in the future.
278+
verifyQuota(ctx, t, owner, 0, 45)
279+
})
236280
}
237281

238282
func planWithCost(cost int32) []*proto.Response {

0 commit comments

Comments
 (0)