1Password / onepassword-sdk-go

https://developer.1password.com/docs/sdks/
MIT License
53 stars 6 forks source link

Add client mock to use in unit testing #98

Open twpayne opened 4 months ago

twpayne commented 4 months ago

Current Behavior

I'm trying to integrate the 1Password SDK into chezmoi following the request in https://github.com/twpayne/chezmoi/pull/3852. The initial code is in https://github.com/twpayne/chezmoi/pull/3855.

Currently the SDK only provides code for a client to connect to a running 1Password instance. This means that it is not possible to test the SDK integration in a unit test.

Desired Behavior

I would like a way to create a test client so I can test my 1Password SDK integration in a unit test. One way to do this would be with a new ClientOption, something like:

client, err := onepassword.NewClient(ctx,
    onepassword.WithTestVault(vaultID, map[string]onepassword.Item{
        itemID: {
            // ...
        }
    }),
)
// ...
actual, err := client.Items.Get(ctx, vaultId, itemId)
if !reflect.DeepEqual(actual, onepassword.Item{ /* ... */ }) {
    t.Errorf("error getting item %s from %s", itemId, vaultId)
}

Benefits & Value

This would make it possible to test code using the 1Password SDK, with all the benefits that testing provides.

Additional information

No response