package coderd_test import ( "testing" "github.com/google/uuid" "github.com/stretchr/testify/require" "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/rbac" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/testutil" ) func TestListMembers(t *testing.T) { t.Parallel() t.Run("OK", func(t *testing.T) { t.Parallel() owner := coderdtest.New(t, nil) first := coderdtest.CreateFirstUser(t, owner) client, user := coderdtest.CreateAnotherUser(t, owner, first.OrganizationID, rbac.ScopedRoleOrgAdmin(first.OrganizationID)) ctx := testutil.Context(t, testutil.WaitShort) members, err := client.OrganizationMembers(ctx, first.OrganizationID) require.NoError(t, err) require.Len(t, members, 2) require.ElementsMatch(t, []uuid.UUID{first.UserID, user.ID}, db2sdk.List(members, onlyIDs)) }) // Calling it from a user without the org access. t.Run("NotInOrg", func(t *testing.T) { t.Parallel() owner := coderdtest.New(t, nil) first := coderdtest.CreateFirstUser(t, owner) client, _ := coderdtest.CreateAnotherUser(t, owner, first.OrganizationID, rbac.ScopedRoleOrgAdmin(first.OrganizationID)) ctx := testutil.Context(t, testutil.WaitShort) org, err := owner.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{ Name: "test", DisplayName: "", Description: "", }) require.NoError(t, err, "create organization") // 404 error is expected instead of a 403/401 to not leak existence of // an organization. _, err = client.OrganizationMembers(ctx, org.ID) require.ErrorContains(t, err, "404") }) } func onlyIDs(u codersdk.OrganizationMemberWithName) uuid.UUID { return u.UserID }