Skip to content

Commit

Permalink
test for invalid username
Browse files Browse the repository at this point in the history
  • Loading branch information
carrala committed May 17, 2021
1 parent fc962ba commit 1f34457
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,9 @@ func (c *ToznySDKV3) ListSecrets(ctx context.Context, options ListSecretsOptions
if err != nil {
return nil, err
}
if len(identities.SearchedIdentitiesInformation) < 1 {
return nil, fmt.Errorf("ListSecrets: no identity found with clientID %s", record.Metadata.WriterID)
}
username := identities.SearchedIdentitiesInformation[0].RealmUsername
record.Metadata.Plain[SecretWriterUsernameMetadataKey] = username
record.Metadata.Plain[SecretSharedMetadataKey] = shared
Expand Down Expand Up @@ -2104,6 +2107,9 @@ func (c *ToznySDKV3) ShareSecretWithUsername(ctx context.Context, params ShareSe
if err != nil {
return err
}
if len(identities.SearchedIdentitiesInformation) < 1 {
return fmt.Errorf("ShareSecretWithUser: no identity found with username %s", params.UsernameToAdd)
}
// find or create the group for sharing with UsernameToAdd
namespaceOptions := NamespaceOptions{
RealmName: c.CurrentIdentity.Realm,
Expand Down
32 changes: 32 additions & 0 deletions secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,38 @@ func TestShareSecretByUsernameSucceeds(t *testing.T) {
}
}

func TestShareSecretInvalidUsernameFails(t *testing.T) {
request := TozIDLoginRequest{
Username: username,
Password: password,
RealmName: realmName,
APIBaseURL: baseURL,
LoginHandler: mfaHandler,
}
sdk, err := GetSDKV3ForTozIDUser(request)
if err != nil {
t.Fatalf("Could not log in %+v", err)
}
viewOptions := ViewSecretOptions{
SecretID: uuid.MustParse(secret1ID),
MaxSecrets: 1000,
}
secret, err := sdk.ViewSecret(testCtx, viewOptions)
if err != nil {
t.Fatalf("Error viewing shared secret: Err: %+v", err)
}
// share secret with a username that doesn't exist
shareOptions := ShareSecretInfo{
SecretName: secret.SecretName,
SecretType: secret.SecretType,
UsernameToAdd: "invalid-user",
}
err = sdk.ShareSecretWithUsername(testCtx, shareOptions)
if err == nil {
t.Fatal("Should error since username doesn't exist\n")
}
}

func mfaHandler(sessionResponse *IdentitySessionIntermediateResponse) (LoginActionData, error) {
if sessionResponse.LoginActionType == "login-totp" {
totpValue := make(map[string]string)
Expand Down

0 comments on commit 1f34457

Please sign in to comment.