Skip to content

Commit

Permalink
fix(auth): Fix Tenant iterator to return the correct sequence of poin…
Browse files Browse the repository at this point in the history
…ters (#323)

Previously, tenant_mgt.go would copy the tenant from the network
response, and return a pointer to it. Then it would re-use the same
memory for the next tenant, resulting in a list of pointers that all
pointed to the last tenant.

This change returns pointers to the network response instead (and as a
bonus, avoids the copy entirely).
  • Loading branch information
rsgowman authored Jan 14, 2020
1 parent 04ebfd5 commit 715b5a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions auth/tenant_mgt.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ func (it *TenantIterator) fetch(pageSize int, pageToken string) (string, error)
return "", err
}

for _, tenant := range result.Tenants {
tenant.ID = extractResourceID(tenant.ID)
it.tenants = append(it.tenants, &tenant)
for i := range result.Tenants {
result.Tenants[i].ID = extractResourceID(result.Tenants[i].ID)
it.tenants = append(it.tenants, &result.Tenants[i])
}

it.pageInfo.Token = result.NextPageToken
Expand Down
18 changes: 16 additions & 2 deletions auth/tenant_mgt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,13 @@ const tenantResponse = `{
"enableEmailLinkSignin": true
}`

const tenantResponse2 = `{
"name":"projects/mock-project-id/tenants/tenantID2",
"displayName": "Test Tenant 2",
"allowPasswordSignup": true,
"enableEmailLinkSignin": true
}`

const tenantNotFoundResponse = `{
"error": {
"message": "TENANT_NOT_FOUND"
Expand All @@ -1099,6 +1106,13 @@ var testTenant = &Tenant{
EnableEmailLinkSignIn: true,
}

var testTenant2 = &Tenant{
ID: "tenantID2",
DisplayName: "Test Tenant 2",
AllowPasswordSignUp: true,
EnableEmailLinkSignIn: true,
}

func TestTenant(t *testing.T) {
s := echoServer([]byte(tenantResponse), t)
defer s.Close()
Expand Down Expand Up @@ -1423,13 +1437,13 @@ func TestTenants(t *testing.T) {
],
"nextPageToken": ""
}`
response := fmt.Sprintf(template, tenantResponse, tenantResponse, tenantResponse)
response := fmt.Sprintf(template, tenantResponse, tenantResponse2, tenantResponse)
s := echoServer([]byte(response), t)
defer s.Close()

want := []*Tenant{
testTenant,
testTenant,
testTenant2,
testTenant,
}
wantPath := "/projects/mock-project-id/tenants"
Expand Down

0 comments on commit 715b5a6

Please sign in to comment.