Skip to content

Commit

Permalink
Fix UpdateCredential func
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed May 19, 2023
1 parent e88b33f commit e08e00e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
17 changes: 15 additions & 2 deletions management/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,21 @@ func (m *ClientManager) CreateCredential(clientID string, credential *Credential

// UpdateCredential updates a client application's client credential expiry.
func (m *ClientManager) UpdateCredential(clientID, credentialID string, credential *Credential, opts ...RequestOption) error {
credential = &Credential{ExpiresAt: credential.ExpiresAt} // The API only accepts the expires_at property.
return m.Request("PATCH", m.URI("clients", clientID, "credentials", credentialID), credential, opts...)
credentialClone := &Credential{ExpiresAt: credential.ExpiresAt} // The API only accepts the expires_at property.

defer func() {
credential.ID = credentialClone.ID
credential.Name = credentialClone.Name
credential.CredentialType = credentialClone.CredentialType
credential.KeyID = credentialClone.KeyID
credential.Algorithm = credentialClone.Algorithm
credential.CreatedAt = credentialClone.CreatedAt
credential.UpdatedAt = credentialClone.UpdatedAt
credential.ExpiresAt = credentialClone.ExpiresAt
// PEM and ParseExpiryFromCert don't get returned.
}()

return m.Request("PATCH", m.URI("clients", clientID, "credentials", credentialID), credentialClone, opts...)
}

// ListCredentials lists all client credentials associated with the client application.
Expand Down
8 changes: 7 additions & 1 deletion management/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,16 @@ func TestClient_UpdateCredential(t *testing.T) {
expiresAt := time.Now().Add(time.Minute * 10)
expectedCredential.ExpiresAt = &expiresAt

err := api.Client.UpdateCredential(expectedClient.GetClientID(), expectedCredential.GetID(), expectedCredential)
pem := expectedCredential.GetPEM()
credentialID := expectedCredential.GetID()
expectedCredential.ID = nil

err := api.Client.UpdateCredential(expectedClient.GetClientID(), credentialID, expectedCredential)

assert.NoError(t, err)
assert.Equal(t, expectedCredential.GetExpiresAt(), expiresAt)
assert.Equal(t, expectedCredential.GetID(), credentialID) // Check that we unmarshall the result into this struct.
assert.Equal(t, expectedCredential.GetPEM(), pem)
}

func TestClient_DeleteCredential(t *testing.T) {
Expand Down

0 comments on commit e08e00e

Please sign in to comment.