Skip to content

Commit

Permalink
Improve fetching a vault by either name or UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
edif2008 committed May 11, 2022
1 parent 549d476 commit 050b9c4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
30 changes: 28 additions & 2 deletions connect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,18 @@ func (rs *restClient) GetVaults() ([]onepassword.Vault, error) {
return vaults, nil
}

// GetVaults Get a list of all available vaults
func (rs *restClient) GetVault(uuid string) (*onepassword.Vault, error) {
// GetVault Get a vault based on its name or ID
func (rs *restClient) GetVault(vaultQuery string) (*onepassword.Vault, error) {
if vaultQuery == "" {
return nil, fmt.Errorf("Please provide either the vault name or its ID.")
}
if !isValidUUID(vaultQuery) {
return rs.GetVaultByTitle(vaultQuery)
}
return rs.GetVaultByUUID(vaultQuery)
}

func (rs *restClient) GetVaultByUUID(uuid string) (*onepassword.Vault, error) {
if !isValidUUID(uuid) {
return nil, vaultUUIDError
}
Expand All @@ -166,6 +176,22 @@ func (rs *restClient) GetVault(uuid string) (*onepassword.Vault, error) {
return &vault, nil
}

func (rs *restClient) GetVaultByTitle(vaultName string) (*onepassword.Vault, error) {
span := rs.tracer.StartSpan("GetVaultByTitle")
defer span.Finish()

vaults, err := rs.GetVaultsByTitle(vaultName)
if err != nil {
return nil, err
}

if len(vaults) != 1 {
return nil, fmt.Errorf("Found %d vaults with title %q", len(vaults), vaultName)
}

return &vaults[0], nil
}

func (rs *restClient) GetVaultsByTitle(title string) ([]onepassword.Vault, error) {
span := rs.tracer.StartSpan("GetVaultsByTitle")
defer span.Finish()
Expand Down
35 changes: 32 additions & 3 deletions connect/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,40 @@ func Test_restClient_GetVault(t *testing.T) {
assert.Equal(t, expectedVault, vault, "retrieved vault is not as expected")
}

func Test_restClient_GetVaultEmptyUUID(t *testing.T) {
func Test_restClient_GetVaultByID(t *testing.T) {
expectedVault := &onepassword.Vault{
Name: "Test vault",
Description: "Test Vault description",
ID: testID,
}

mockHTTPClient.Dofunc = getVault(expectedVault)
vault, err := testClient.GetVaultByUUID(expectedVault.ID)

assert.Nil(t, err)
assert.Equal(t, expectedVault, vault, "retrieved vault is not as expected")
}

func Test_restClient_GetVaultByTitle(t *testing.T) {
expectedVault := &onepassword.Vault{
Name: "Test vault",
Description: "Test Vault description",
ID: testID,
}

mockHTTPClient.Dofunc = listVaults
vault, err := testClient.GetVaultByTitle(expectedVault.Name)

assert.Nil(t, err)
assert.Equal(t, expectedVault, vault, "retrieved vault is not as expected")
}

func Test_restClient_GetVaultEmptyQuery(t *testing.T) {
errResult := apiError(http.StatusNotFound, "Vault not found")
mockHTTPClient.Dofunc = respondError(errResult)
_, err := testClient.GetVault("")

assert.EqualError(t, err, "malformed vault uuid provided")
assert.EqualError(t, err, "Please provide either the vault name or its ID.")
}

func Test_restClient_GetVaultError(t *testing.T) {
Expand Down Expand Up @@ -500,7 +528,8 @@ func respondError(apiErr *onepassword.Error) func(req *http.Request) (*http.Resp
func listVaults(req *http.Request) (*http.Response, error) {
vaults := []onepassword.Vault{
{
Description: "Test Vault",
Name: "Test vault",
Description: "Test Vault description",
ID: testID,
},
}
Expand Down

0 comments on commit 050b9c4

Please sign in to comment.