diff --git a/connect/client.go b/connect/client.go index 32c9135..df36abe 100644 --- a/connect/client.go +++ b/connect/client.go @@ -236,7 +236,7 @@ func (rs *restClient) GetItemByTitle(title string, vaultUUID string) (*onepasswo return nil, fmt.Errorf("Found %d item(s) in vault %q with title %q", len(items), vaultUUID, title) } - return rs.GetItem(items[0].ID, items[0].Vault.ID) + return &items[0], nil } func (rs *restClient) GetItemsByTitle(title string, vaultUUID string) ([]onepassword.Item, error) { @@ -259,11 +259,20 @@ func (rs *restClient) GetItemsByTitle(title string, vaultUUID string) ([]onepass return nil, err } - var items []onepassword.Item - if err := parseResponse(response, http.StatusOK, &items); err != nil { + var itemSummaries []onepassword.Item + if err := parseResponse(response, http.StatusOK, &itemSummaries); err != nil { return nil, err } + items := make([]onepassword.Item, len(itemSummaries)) + for i, itemSummary := range itemSummaries { + tempItem, err := rs.GetItem(itemSummary.ID, itemSummary.Vault.ID) + if err != nil { + return nil, err + } + items[i] = *tempItem + } + return items, nil } diff --git a/connect/client_test.go b/connect/client_test.go index f9c17ca..4c62078 100644 --- a/connect/client_test.go +++ b/connect/client_test.go @@ -8,10 +8,10 @@ import ( "net/http" "os" "reflect" + "strings" "testing" "time" - "github.com/google/uuid" "github.com/opentracing/opentracing-go" "github.com/stretchr/testify/assert" @@ -31,6 +31,9 @@ var testUserAgent string var testServerDefaultVersion = version{1, 3, 0} +const testItemUUID = "2a47aa139ef74d7ca17918035e" +const testVaultUUID = "5b52aa139ef74d7ca17918nmf8" + type mockClient struct { Dofunc func(req *http.Request) (*http.Response, error) } @@ -277,8 +280,8 @@ func Test_restClient_GetItems(t *testing.T) { } func Test_restClient_GetItemsByTitle(t *testing.T) { - mockHTTPClient.Dofunc = listItems - items, err := testClient.GetItemsByTitle("test", testID) + mockHTTPClient.Dofunc = listItemsOrGetItem + items, err := testClient.GetItemsByTitle("test-item", testVaultUUID) if err != nil { t.Logf("Unable to get item: %s", err.Error()) @@ -289,6 +292,11 @@ func Test_restClient_GetItemsByTitle(t *testing.T) { t.Logf("Expected 1 item to exist in vault, found %d", len(items)) t.FailNow() } + + assert.Equal(t, items[0].Title, "test-item") + assert.NotEqual(t, len(items[0].Fields), 0) + assert.Equal(t, items[0].Fields[0].Value, "wendy") + assert.Equal(t, items[0].Fields[1].Value, "appleseed") } func Test_restClient_GetItemByTitle(t *testing.T) { @@ -306,6 +314,10 @@ func Test_restClient_GetItemByTitle(t *testing.T) { t.Log("Expected 1 item to exist") t.FailNow() } + + assert.Equal(t, item.Fields[0].Value, "wendy") + assert.Equal(t, item.Fields[1].Value, "appleseed") + } func Test_restClient_GetItemByNonUniqueTitle(t *testing.T) { @@ -526,9 +538,10 @@ func getVault(vault *onepassword.Vault) func(req *http.Request) (*http.Response, } } -func generateComplexItem(vaultUUID string, itemUUID string) *onepassword.Item { - return &onepassword.Item{ - ID: itemUUID, +func generateComplexItem(vaultUUID string) onepassword.Item { + return onepassword.Item{ + Title: "test-item", + ID: testItemUUID, Vault: onepassword.ItemVault{ ID: vaultUUID, }, @@ -536,29 +549,46 @@ func generateComplexItem(vaultUUID string, itemUUID string) *onepassword.Item { ID: "", Label: "section", }}, - Fields: []*onepassword.ItemField{{ - ID: testID, - Label: "username", - Value: "wendy", - }, { - ID: testID, - Label: "password", - Value: "appleseed", - Section: &onepassword.ItemSection{ - ID: "", - Label: "section", + Fields: []*onepassword.ItemField{ + { + ID: testID, + Label: "username", + Value: "wendy", + }, + { + Label: "password", + Value: "appleseed", + Section: &onepassword.ItemSection{ + ID: "", + Label: "section", + }, }, - }, }, } } func generateItem(vaultUUID string) *onepassword.Item { - return &onepassword.Item{ - ID: testID, - Vault: onepassword.ItemVault{ - ID: vaultUUID, - }, + item := generateComplexItem(vaultUUID) + return &item +} + +func listItemsOrGetItem(req *http.Request) (*http.Response, error) { + if strings.Contains(req.URL.RequestURI(), "test-item") { + json, _ := json.Marshal([]onepassword.Item{generateComplexItem(testVaultUUID)}) + return &http.Response{ + Status: http.StatusText(http.StatusOK), + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader(json)), + Header: req.Header, + }, nil + } else { + json, _ := json.Marshal(generateComplexItem(testVaultUUID)) + return &http.Response{ + Status: http.StatusText(http.StatusOK), + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader(json)), + Header: req.Header, + }, nil } } @@ -608,12 +638,11 @@ func getItemByID(req *http.Request) (*http.Response, error) { } func getComplexItem(req *http.Request) (*http.Response, error) { - vaultUUID := "" - itemUUID := "" + vaultUUID := testVaultUUID excessPath := "" fmt.Sscanf(req.URL.Path, "/v1/vaults/%s%s", vaultUUID, excessPath) - json, _ := json.Marshal(generateComplexItem(vaultUUID, itemUUID)) + json, _ := json.Marshal(generateComplexItem(vaultUUID)) return &http.Response{ Status: http.StatusText(http.StatusOK), StatusCode: http.StatusOK, @@ -647,8 +676,7 @@ func createItem(req *http.Request) (*http.Response, error) { return nil, err } - newUUID := uuid.New() - item.ID = newUUID.String() + item.ID = testItemUUID item.CreatedAt = time.Now() vaultUUID := "" @@ -691,8 +719,8 @@ func updateItem(req *http.Request) (*http.Response, error) { } func deleteItem(req *http.Request) (*http.Response, error) { - vaultUUID := "" - itemUUID := "" + vaultUUID := strings.ToLower(testVaultUUID) + itemUUID := strings.ToLower(testItemUUID) fmt.Sscanf(req.URL.Path, "/v1/vaults/%s/items/%s", vaultUUID, itemUUID) return &http.Response{